From 56f77044cfa5591c4c8b5bace4d01e0755a58f86 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 25 Jun 2024 16:42:30 +0200 Subject: [PATCH 10/12] vazsprintf-gnu: Add tests. * tests/test-vazsprintf-gnu.c: New file, based on tests/test-zsnprintf-gnu.h. * modules/vazsprintf-gnu-tests: New file. --- ChangeLog | 5 +++ modules/vazsprintf-gnu-tests | 11 +++++ tests/test-vazsprintf-gnu.c | 83 ++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 modules/vazsprintf-gnu-tests create mode 100644 tests/test-vazsprintf-gnu.c diff --git a/ChangeLog b/ChangeLog index e1b41b8e08..60f37baf05 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2024-06-25 Bruno Haible + vazsprintf-gnu: Add tests. + * tests/test-vazsprintf-gnu.c: New file, based on + tests/test-zsnprintf-gnu.h. + * modules/vazsprintf-gnu-tests: New file. + vazsprintf-gnu: New module. * modules/vazsprintf-gnu: New file. diff --git a/modules/vazsprintf-gnu-tests b/modules/vazsprintf-gnu-tests new file mode 100644 index 0000000000..33cb15cd56 --- /dev/null +++ b/modules/vazsprintf-gnu-tests @@ -0,0 +1,11 @@ +Files: +tests/test-vazsprintf-gnu.c +tests/macros.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-vazsprintf-gnu +check_PROGRAMS += test-vazsprintf-gnu diff --git a/tests/test-vazsprintf-gnu.c b/tests/test-vazsprintf-gnu.c new file mode 100644 index 0000000000..cd31b378ce --- /dev/null +++ b/tests/test-vazsprintf-gnu.c @@ -0,0 +1,83 @@ +/* Test of POSIX and GNU compatible vazsprintf() and azsprintf() functions. + Copyright (C) 2007-2024 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 , 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 + +#include + +#include +#include +#include +#include + +#include "macros.h" + +static void +test_function (ptrdiff_t (*my_azsprintf) (char **, const char *, ...)) +{ + char result[5000]; + + /* Test the support of the 'B' conversion specifier for binary output of + integers. */ + + { /* This test would fail on all platforms other than glibc ≥ 2.35. */ + char *result; + ptrdiff_t retval = + my_azsprintf (&result, "%#B %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0B11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } +} + +static ptrdiff_t +my_azsprintf (char **result, const char *format, ...) +{ + va_list args; + ptrdiff_t ret; + + va_start (args, format); + ret = vazsprintf (result, format, args); + va_end (args); + return ret; +} + +static void +test_vazsprintf () +{ + test_function (my_azsprintf); +} + +static void +test_azsprintf () +{ + test_function (azsprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vazsprintf (); + test_azsprintf (); + return test_exit_status; +} -- 2.34.1