>From e396564a334c9467ea919068f58678ccdb821e72 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 24 Mar 2023 11:06:44 +0100 Subject: [PATCH 05/31] vsnprintf-gnu: Add tests. * tests/test-vsnprintf-gnu.c: New file, based on tests/test-vsnprintf-posix.c. * tests/test-snprintf-gnu.h: New file, based on tests/test-vasnprintf-gnu.c. * modules/vsnprintf-gnu-tests: New file, based on modules/vsnprintf-posix-tests. --- ChangeLog | 8 ++ modules/vsnprintf-gnu-tests | 13 +++ tests/test-snprintf-gnu.h | 175 ++++++++++++++++++++++++++++++++++++ tests/test-vsnprintf-gnu.c | 48 ++++++++++ 4 files changed, 244 insertions(+) create mode 100644 modules/vsnprintf-gnu-tests create mode 100644 tests/test-snprintf-gnu.h create mode 100644 tests/test-vsnprintf-gnu.c diff --git a/ChangeLog b/ChangeLog index e152b9cd55..21b11ddca4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2023-03-24 Bruno Haible + vsnprintf-gnu: Add tests. + * tests/test-vsnprintf-gnu.c: New file, based on + tests/test-vsnprintf-posix.c. + * tests/test-snprintf-gnu.h: New file, based on + tests/test-vasnprintf-gnu.c. + * modules/vsnprintf-gnu-tests: New file, based on + modules/vsnprintf-posix-tests. + vsnprintf-gnu: New module. * m4/vsnprintf-posix.m4 (gl_FUNC_VSNPRINTF_IS_POSIX): New macro, extracted from gl_FUNC_VSNPRINTF_POSIX. diff --git a/modules/vsnprintf-gnu-tests b/modules/vsnprintf-gnu-tests new file mode 100644 index 0000000000..6749904cc6 --- /dev/null +++ b/modules/vsnprintf-gnu-tests @@ -0,0 +1,13 @@ +Files: +tests/test-vsnprintf-gnu.c +tests/test-snprintf-gnu.h +tests/macros.h + +Depends-on: +vsnprintf-posix-tests + +configure.ac: + +Makefile.am: +TESTS += test-vsnprintf-gnu +check_PROGRAMS += test-vsnprintf-gnu diff --git a/tests/test-snprintf-gnu.h b/tests/test-snprintf-gnu.h new file mode 100644 index 0000000000..eda2c80334 --- /dev/null +++ b/tests/test-snprintf-gnu.h @@ -0,0 +1,175 @@ +/* Test of POSIX and GNU compatible vsnprintf() and snprintf() functions. + Copyright (C) 2007-2023 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 . */ + +/* Written by Bruno Haible , 2023. */ + +static void +test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) +{ + char result[5000]; + + /* Test the support of the 'B' conversion specifier for binary output of + integers. */ + + { /* Zero. */ + int retval = + my_snprintf (result, sizeof (result), "%B %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* A positive number. */ + int retval = + my_snprintf (result, sizeof (result), "%B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* A large positive number. */ + int retval = + my_snprintf (result, sizeof (result), "%B %d", 0xFFFFFFFEU, 33, 44, 55); + ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Width. */ + int retval = + my_snprintf (result, sizeof (result), "%20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Width given as argument. */ + int retval = + my_snprintf (result, sizeof (result), "%*B %d", 20, 12345, 33, 44, 55); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Negative width given as argument (cf. FLAG_LEFT below). */ + int retval = + my_snprintf (result, sizeof (result), "%*B %d", -20, 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision. */ + int retval = + my_snprintf (result, sizeof (result), "%.20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "00000011000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Zero precision and a positive number. */ + int retval = + my_snprintf (result, sizeof (result), "%.0B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Zero precision and a zero number. */ + int retval = + my_snprintf (result, sizeof (result), "%.0B %d", 0, 33, 44, 55); + /* ISO C and POSIX specify that "The result of converting a zero value + with a precision of zero is no characters." */ + ASSERT (strcmp (result, " 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Width and precision. */ + int retval = + my_snprintf (result, sizeof (result), "%25.20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, " 00000011000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Padding and precision. */ + int retval = + my_snprintf (result, sizeof (result), "%025.20B %d", 12345, 33, 44, 55); + /* Neither ISO C nor POSIX specify that the '0' flag is ignored when + a width and a precision are both present. But implementations do so. */ + ASSERT (strcmp (result, " 00000011000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_LEFT. */ + int retval = + my_snprintf (result, sizeof (result), "%-20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with zero. */ + int retval = + my_snprintf (result, sizeof (result), "%#B %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number. */ + int retval = + my_snprintf (result, sizeof (result), "%#B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0B11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number and width. */ + int retval = + my_snprintf (result, sizeof (result), "%#20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, " 0B11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number and padding. */ + int retval = + my_snprintf (result, sizeof (result), "%0#20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0B000011000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number and precision. */ + int retval = + my_snprintf (result, sizeof (result), "%0#.20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0B00000011000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number and width and precision. */ + int retval = + my_snprintf (result, sizeof (result), "%#25.20B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, " 0B00000011000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number and padding and precision. */ + int retval = + my_snprintf (result, sizeof (result), "%0#25.20B %d", 12345, 33, 44, 55); + /* Neither ISO C nor POSIX specify that the '0' flag is ignored when + a width and a precision are both present. But implementations do so. */ + ASSERT (strcmp (result, " 0B00000011000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a zero precision and a zero number. */ + int retval = + my_snprintf (result, sizeof (result), "%#.0B %d", 0, 33, 44, 55); + /* ISO C and POSIX specify that "The result of converting a zero value + with a precision of zero is no characters.", and the prefix is added + only for non-zero values. */ + ASSERT (strcmp (result, " 33") == 0); + ASSERT (retval == strlen (result)); + } +} diff --git a/tests/test-vsnprintf-gnu.c b/tests/test-vsnprintf-gnu.c new file mode 100644 index 0000000000..420930f7d0 --- /dev/null +++ b/tests/test-vsnprintf-gnu.c @@ -0,0 +1,48 @@ +/* Test of POSIX and GNU compatible vsnprintf() function. + Copyright (C) 2007-2023 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 . */ + +/* Written by Bruno Haible , 2007. */ + +#include + +#include + +#include +#include +#include + +#include "macros.h" + +#include "test-snprintf-gnu.h" + +static int +my_snprintf (char *str, size_t size, const char *format, ...) +{ + va_list args; + int ret; + + va_start (args, format); + ret = vsnprintf (str, size, format, args); + va_end (args); + return ret; +} + +int +main (int argc, char *argv[]) +{ + test_function (my_snprintf); + return 0; +} -- 2.34.1