]> Savannah Git Hosting - gnulib.git/commitdiff
vazsprintf-gnu: Add tests.
authorBruno Haible <bruno@clisp.org>
Tue, 25 Jun 2024 14:42:30 +0000 (16:42 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 25 Jun 2024 14:42:30 +0000 (16:42 +0200)
* tests/test-vazsprintf-gnu.c: New file, based on
tests/test-zsnprintf-gnu.h.
* modules/vazsprintf-gnu-tests: New file.

ChangeLog
modules/vazsprintf-gnu-tests [new file with mode: 0644]
tests/test-vazsprintf-gnu.c [new file with mode: 0644]

index e1b41b8e087066f317f9865efa86c4dd4d2f02f6..60f37baf0557c81778899d28c73075e7cb47c699 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2024-06-25  Bruno Haible  <bruno@clisp.org>
 
+       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 (file)
index 0000000..33cb15c
--- /dev/null
@@ -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 (file)
index 0000000..cd31b37
--- /dev/null
@@ -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 <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"
+
+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;
+}