1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2026-10-24 00:48:56 +03:00
Files
gnulib/tests/test-vaszprintf-posix.c
Bruno Haible 2d74c2f3a1 vaszprintf-extra-tests: New module.
* tests/test-vaszprintf-big.c: New file, extracted from
tests/test-vaszprintf-posix.c.
* tests/test-vaszprintf-posix.c (test_function): Revert last change.
* modules/vaszprintf-extra-tests: New file.
* modules/vaszprintf-tests: New file.
2026-07-13 23:22:03 +02:00

262 lines
7.7 KiB
C

/* Test of POSIX compatible vaszprintf() and aszprintf() functions.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2024. */
/* This test exercises only a few POSIX compliance problems that are still
visible on platforms relevant in 2024. For a much more complete test suite,
see test-vasprintf-posix.c. */
#include <config.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "macros.h"
#include "infinity.h"
#include "nan.h"
/* Test whether string[start_index..end_index-1] is a valid textual
representation of NaN. */
static int
strisnan (const char *string, size_t start_index, size_t end_index, int uppercase)
{
if (start_index < end_index)
{
if (string[start_index] == '-')
start_index++;
if (start_index + 3 <= end_index
&& memeq (string + start_index, uppercase ? "NAN" : "nan", 3))
{
start_index += 3;
if (start_index == end_index
|| (string[start_index] == '(' && string[end_index - 1] == ')'))
return 1;
}
}
return 0;
}
static void
test_function (ptrdiff_t (*my_aszprintf) (char **, const char *, ...))
{
/* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
output of floating-point numbers. */
{ /* This test would fail on Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%a %d", 3.1416015625, 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "0x1.922p+1 33")
|| streq (result, "0x3.244p+0 33")
|| streq (result, "0x6.488p-1 33")
|| streq (result, "0xc.91p-2 33"));
ASSERT (retval == strlen (result));
free (result);
}
{ /* This test would fail on Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%A %d", -3.1416015625, 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "-0X1.922P+1 33")
|| streq (result, "-0X3.244P+0 33")
|| streq (result, "-0X6.488P-1 33")
|| streq (result, "-0XC.91P-2 33"));
ASSERT (retval == strlen (result));
free (result);
}
{ /* This test would fail on FreeBSD 6.1, NetBSD 10.0. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%.2a %d", 1.51, 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "0x1.83p+0 33")
|| streq (result, "0x3.05p-1 33")
|| streq (result, "0x6.0ap-2 33")
|| streq (result, "0xc.14p-3 33"));
ASSERT (retval == strlen (result));
free (result);
}
{ /* This test would fail on macOS 14, FreeBSD 14.0, OpenBSD 7.5, AIX 7.3,
Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%.0a %d", 1.51, 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "0x2p+0 33")
|| streq (result, "0x3p-1 33")
|| streq (result, "0x6p-2 33")
|| streq (result, "0xcp-3 33"));
ASSERT (retval == strlen (result));
free (result);
}
/* Test the support of the %f format directive. */
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%f %d", Infinityd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "inf 33")
|| streq (result, "infinity 33"));
ASSERT (retval == strlen (result));
free (result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%f %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
&& streq (result + strlen (result) - 3, " 33"));
ASSERT (retval == strlen (result));
free (result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%010f %d", Infinityd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, " inf 33")
|| streq (result, " infinity 33"));
ASSERT (retval == strlen (result));
free (result);
}
/* Test the support of the %e format directive. */
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%e %d", Infinityd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "inf 33")
|| streq (result, "infinity 33"));
ASSERT (retval == strlen (result));
free (result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%e %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
&& streq (result + strlen (result) - 3, " 33"));
ASSERT (retval == strlen (result));
free (result);
}
/* Test the support of the %g format directive. */
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%g %d", Infinityd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "inf 33")
|| streq (result, "infinity 33"));
ASSERT (retval == strlen (result));
free (result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%g %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
&& streq (result + strlen (result) - 3, " 33"));
ASSERT (retval == strlen (result));
free (result);
}
/* Test the support of large precision. */
{ /* This test would fail on AIX 7.1. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%.4000d %d", 1234567, 99);
ASSERT (result != NULL);
for (size_t i = 0; i < 4000 - 7; i++)
ASSERT (result[i] == '0');
ASSERT (streq (result + 4000 - 7, "1234567 99"));
ASSERT (retval == strlen (result));
free (result);
}
/* Test the support of the 'b' conversion specifier for binary output of
integers. */
{ /* This test would fail on glibc 2.34, musl libc, macOS 14,
FreeBSD 13.2, NetBSD 10.0, OpenBSD 7.5, AIX 7.3, Solaris 11.4. */
char *result;
ptrdiff_t retval =
my_aszprintf (&result, "%b %d", 12345, 33, 44, 55);
ASSERT (result != NULL);
ASSERT (streq (result, "11000000111001 33"));
ASSERT (retval == strlen (result));
free (result);
}
}
static ptrdiff_t
my_aszprintf (char **result, const char *format, ...)
{
va_list args;
ptrdiff_t ret;
va_start (args, format);
ret = vaszprintf (result, format, args);
va_end (args);
return ret;
}
static void
test_vaszprintf ()
{
test_function (my_aszprintf);
}
static void
test_aszprintf ()
{
test_function (aszprintf);
}
int
main (int argc, char *argv[])
{
test_vaszprintf ();
test_aszprintf ();
return test_exit_status;
}