Blame gnulib-tests/test-vasnprintf.c

Packit 709fb3
/* Test of vasnprintf() and asnprintf() functions.
Packit 709fb3
   Copyright (C) 2007-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include "vasnprintf.h"
Packit 709fb3
Packit 709fb3
#include <stdarg.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
Packit 709fb3
#include "macros.h"
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
Packit 709fb3
{
Packit 709fb3
  char buf[8];
Packit 709fb3
  int size;
Packit 709fb3
Packit 709fb3
  for (size = 0; size <= 8; size++)
Packit 709fb3
    {
Packit 709fb3
      size_t length = size;
Packit 709fb3
      char *result = my_asnprintf (NULL, &length, "%d", 12345);
Packit 709fb3
      ASSERT (result != NULL);
Packit 709fb3
      ASSERT (strcmp (result, "12345") == 0);
Packit 709fb3
      ASSERT (length == 5);
Packit 709fb3
      free (result);
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  for (size = 0; size <= 8; size++)
Packit 709fb3
    {
Packit 709fb3
      size_t length;
Packit 709fb3
      char *result;
Packit 709fb3
Packit 709fb3
      memcpy (buf, "DEADBEEF", 8);
Packit 709fb3
      length = size;
Packit 709fb3
      result = my_asnprintf (buf, &length, "%d", 12345);
Packit 709fb3
      ASSERT (result != NULL);
Packit 709fb3
      ASSERT (strcmp (result, "12345") == 0);
Packit 709fb3
      ASSERT (length == 5);
Packit 709fb3
      if (size < 6)
Packit 709fb3
        ASSERT (result != buf);
Packit 709fb3
      ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0);
Packit 709fb3
      if (result != buf)
Packit 709fb3
        free (result);
Packit 709fb3
    }
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
static char *
Packit 709fb3
my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
Packit 709fb3
{
Packit 709fb3
  va_list args;
Packit 709fb3
  char *ret;
Packit 709fb3
Packit 709fb3
  va_start (args, format);
Packit 709fb3
  ret = vasnprintf (resultbuf, lengthp, format, args);
Packit 709fb3
  va_end (args);
Packit 709fb3
  return ret;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
test_vasnprintf ()
Packit 709fb3
{
Packit 709fb3
  test_function (my_asnprintf);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
test_asnprintf ()
Packit 709fb3
{
Packit 709fb3
  test_function (asnprintf);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (int argc, char *argv[])
Packit 709fb3
{
Packit 709fb3
  test_vasnprintf ();
Packit 709fb3
  test_asnprintf ();
Packit 709fb3
  return 0;
Packit 709fb3
}