From b6fa7b47c363f799bcb4a65b34c1f63e8ee74f9c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 25 Jun 2024 16:38:11 +0200 Subject: [PATCH 02/12] vzsnprintf-gnu: Add tests. * tests/test-vzsnprintf-gnu.c: New file. * tests/test-zsnprintf-gnu.h: New file, based on tests/test-snprintf-gnu.h. * modules/vzsnprintf-gnu-tests: New file. --- ChangeLog | 6 +++++ modules/vzsnprintf-gnu-tests | 12 +++++++++ tests/test-vzsnprintf-gnu.c | 48 ++++++++++++++++++++++++++++++++++++ tests/test-zsnprintf-gnu.h | 37 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 modules/vzsnprintf-gnu-tests create mode 100644 tests/test-vzsnprintf-gnu.c create mode 100644 tests/test-zsnprintf-gnu.h diff --git a/ChangeLog b/ChangeLog index 2272ada314..2aafab3277 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2024-06-25 Bruno Haible + vzsnprintf-gnu: Add tests. + * tests/test-vzsnprintf-gnu.c: New file. + * tests/test-zsnprintf-gnu.h: New file, based on + tests/test-snprintf-gnu.h. + * modules/vzsnprintf-gnu-tests: New file. + vzsnprintf-gnu: New module. * modules/vzsnprintf-gnu: New file. diff --git a/modules/vzsnprintf-gnu-tests b/modules/vzsnprintf-gnu-tests new file mode 100644 index 0000000000..caf3f4ee8d --- /dev/null +++ b/modules/vzsnprintf-gnu-tests @@ -0,0 +1,12 @@ +Files: +tests/test-vzsnprintf-gnu.c +tests/test-zsnprintf-gnu.h +tests/macros.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-vzsnprintf-gnu +check_PROGRAMS += test-vzsnprintf-gnu diff --git a/tests/test-vzsnprintf-gnu.c b/tests/test-vzsnprintf-gnu.c new file mode 100644 index 0000000000..65e893c050 --- /dev/null +++ b/tests/test-vzsnprintf-gnu.c @@ -0,0 +1,48 @@ +/* Test of POSIX and GNU compatible vzsnprintf() function. + 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 , 2007. */ + +#include + +#include + +#include +#include +#include + +#include "macros.h" + +#include "test-zsnprintf-gnu.h" + +static ptrdiff_t +my_zsnprintf (char *str, size_t size, const char *format, ...) +{ + va_list args; + ptrdiff_t ret; + + va_start (args, format); + ret = vzsnprintf (str, size, format, args); + va_end (args); + return ret; +} + +int +main (int argc, char *argv[]) +{ + test_function (my_zsnprintf); + return test_exit_status; +} diff --git a/tests/test-zsnprintf-gnu.h b/tests/test-zsnprintf-gnu.h new file mode 100644 index 0000000000..0e28c588ab --- /dev/null +++ b/tests/test-zsnprintf-gnu.h @@ -0,0 +1,37 @@ +/* Test of POSIX and GNU compatible vzsnprintf() and zsnprintf() 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-snprintf-posix.h. */ + +static void +test_function (ptrdiff_t (*my_zsnprintf) (char *, size_t, 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. */ + ptrdiff_t retval = + my_zsnprintf (result, sizeof (result), "%#B %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0B11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } +} -- 2.34.1