|
Packit |
aea12f |
/* Test of vasnprintf() and asnprintf() functions.
|
|
Packit |
aea12f |
Copyright (C) 2007-2019 Free Software Foundation, Inc.
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
aea12f |
it under the terms of the GNU General Public License as published by
|
|
Packit |
aea12f |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
aea12f |
(at your option) any later version.
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
This program is distributed in the hope that it will be useful,
|
|
Packit |
aea12f |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
aea12f |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
aea12f |
GNU General Public License for more details.
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
You should have received a copy of the GNU General Public License
|
|
Packit |
aea12f |
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <config.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "vasnprintf.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <stdarg.h>
|
|
Packit |
aea12f |
#include <stdlib.h>
|
|
Packit |
aea12f |
#include <string.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "macros.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
char buf[8];
|
|
Packit |
aea12f |
int size;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (size = 0; size <= 8; size++)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
size_t length = size;
|
|
Packit |
aea12f |
char *result = my_asnprintf (NULL, &length, "%d", 12345);
|
|
Packit |
aea12f |
ASSERT (result != NULL);
|
|
Packit |
aea12f |
ASSERT (strcmp (result, "12345") == 0);
|
|
Packit |
aea12f |
ASSERT (length == 5);
|
|
Packit |
aea12f |
free (result);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (size = 0; size <= 8; size++)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
size_t length;
|
|
Packit |
aea12f |
char *result;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
memcpy (buf, "DEADBEEF", 8);
|
|
Packit |
aea12f |
length = size;
|
|
Packit |
aea12f |
result = my_asnprintf (buf, &length, "%d", 12345);
|
|
Packit |
aea12f |
ASSERT (result != NULL);
|
|
Packit |
aea12f |
ASSERT (strcmp (result, "12345") == 0);
|
|
Packit |
aea12f |
ASSERT (length == 5);
|
|
Packit |
aea12f |
if (size < 5 + 1)
|
|
Packit |
aea12f |
ASSERT (result != buf);
|
|
Packit |
aea12f |
ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0);
|
|
Packit |
aea12f |
if (result != buf)
|
|
Packit |
aea12f |
free (result);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Note: This test assumes IEEE 754 representation of 'double' floats. */
|
|
Packit |
aea12f |
for (size = 0; size <= 8; size++)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
size_t length;
|
|
Packit |
aea12f |
char *result;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
memcpy (buf, "DEADBEEF", 8);
|
|
Packit |
aea12f |
length = size;
|
|
Packit |
aea12f |
result = my_asnprintf (buf, &length, "%2.0f", 1.6314159265358979e+125);
|
|
Packit |
aea12f |
ASSERT (result != NULL);
|
|
Packit |
aea12f |
/* The exact result and the result on glibc systems is
|
|
Packit |
aea12f |
163141592653589790215729350939528493057529598899734151772468186268423257777068536614838678161083520756952076273094236944990208
|
|
Packit |
aea12f |
On Cygwin, the result is
|
|
Packit |
aea12f |
163141592653589790215729350939528493057529600000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
|
Packit |
aea12f |
On HP-UX 11.31 / hppa and IRIX 6.5, the result is
|
|
Packit |
aea12f |
163141592653589790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
ASSERT (strlen (result) == 126);
|
|
Packit |
aea12f |
ASSERT (memcmp (result, "163141592653589790", 18) == 0);
|
|
Packit |
aea12f |
ASSERT (length == 126);
|
|
Packit |
aea12f |
if (size < 126 + 1)
|
|
Packit |
aea12f |
ASSERT (result != buf);
|
|
Packit |
aea12f |
ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0);
|
|
Packit |
aea12f |
if (result != buf)
|
|
Packit |
aea12f |
free (result);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static char *
|
|
Packit |
aea12f |
my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
va_list args;
|
|
Packit |
aea12f |
char *ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
va_start (args, format);
|
|
Packit |
aea12f |
ret = vasnprintf (resultbuf, lengthp, format, args);
|
|
Packit |
aea12f |
va_end (args);
|
|
Packit |
aea12f |
return ret;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
test_vasnprintf ()
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
test_function (my_asnprintf);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
test_asnprintf ()
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
test_function (asnprintf);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
int
|
|
Packit |
aea12f |
main (int argc, char *argv[])
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
test_vasnprintf ();
|
|
Packit |
aea12f |
test_asnprintf ();
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|