]> Savannah Git Hosting - gnulib.git/commitdiff
u8-asnprintf tests: Add test of %U, %s directives with large arguments.
authorBruno Haible <bruno@clisp.org>
Wed, 19 Jun 2024 18:56:17 +0000 (20:56 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 19 Jun 2024 18:56:17 +0000 (20:56 +0200)
* tests/unistdio/test-u8-asnprintf-big.c: New file, based on
tests/test-vasnprintf-big.c.
* modules/unistdio/u8-asnprintf-extra-tests: New file.
* modules/unistdio/u8-asnprintf-tests (Depends-on): Depend on it.

ChangeLog
modules/unistdio/u8-asnprintf-extra-tests [new file with mode: 0644]
modules/unistdio/u8-asnprintf-tests
tests/unistdio/test-u8-asnprintf-big.c [new file with mode: 0644]

index cc1671b2b5811ac5bbda5e429a190da9fa003338..cb05d354c2ff3a11fef041b7859d145497370bf5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2024-06-19  Bruno Haible  <bruno@clisp.org>
 
+       u8-asnprintf tests: Add test of %U, %s directives with large arguments.
+       * tests/unistdio/test-u8-asnprintf-big.c: New file, based on
+       tests/test-vasnprintf-big.c.
+       * modules/unistdio/u8-asnprintf-extra-tests: New file.
+       * modules/unistdio/u8-asnprintf-tests (Depends-on): Depend on it.
+
        vasnprintf tests: Add test of %s directive with large arguments.
        * tests/test-vasnprintf-big.c: New file.
        * modules/vasnprintf-extra-tests: New file.
diff --git a/modules/unistdio/u8-asnprintf-extra-tests b/modules/unistdio/u8-asnprintf-extra-tests
new file mode 100644 (file)
index 0000000..c4f7a18
--- /dev/null
@@ -0,0 +1,20 @@
+Status:
+longrunning-test
+
+Files:
+tests/unistdio/test-u8-asnprintf-big.c
+tests/macros.h
+
+Depends-on:
+stdbool
+stdint
+physmem
+
+configure.ac:
+AC_CHECK_FUNCS_ONCE([setrlimit])
+
+Makefile.am:
+TESTS += test-u8-asnprintf-big
+check_PROGRAMS += test-u8-asnprintf-big
+test_u8_asnprintf_big_SOURCES = unistdio/test-u8-asnprintf-big.c
+test_u8_asnprintf_big_LDADD = $(LDADD) $(LIBUNISTRING) @LIBICONV@
index d39689feaa6278d06766d0b2e92d948ad693b436..6c46c112c5c169fabb211a87206181fcc562f3ed 100644 (file)
@@ -5,6 +5,7 @@ tests/unistdio/test-u8-printf1.h
 tests/macros.h
 
 Depends-on:
+unistdio/u8-asnprintf-extra-tests
 
 configure.ac:
 
diff --git a/tests/unistdio/test-u8-asnprintf-big.c b/tests/unistdio/test-u8-asnprintf-big.c
new file mode 100644 (file)
index 0000000..25a5880
--- /dev/null
@@ -0,0 +1,200 @@
+/* Test of u8_asnprintf() with big results.
+   Copyright (C) 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.  */
+
+#include <config.h>
+
+#include <unistdio.h>
+
+#include "physmem.h"
+
+/* Get INT_MAX.  */
+#include <limits.h>
+
+/* Get PTRDIFF_MAX.  */
+#include <stdint.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#if HAVE_SETRLIMIT
+# include <sys/types.h>
+# include <sys/time.h>
+# include <sys/resource.h>
+#endif
+
+#include "macros.h"
+
+int
+main ()
+{
+#if PTRDIFF_MAX == INT_MAX
+  fputs ("Skipping test: ptrdiff_t is not 64-bits wide\n", stderr);
+  return 77;
+#else
+  bool skipped = false;
+  /* Disable core files that would be huge.  */
+# if HAVE_SETRLIMIT && defined RLIMIT_CORE
+  struct rlimit rl;
+  rl.rlim_cur = rl.rlim_max = 0;
+  setrlimit (RLIMIT_CORE, &rl);
+# endif
+  /* The test below needs about 12 GiB of memory:
+     3 GiB for the inputs and up to 9 GiB for temporary output buffers.  */
+  double needed = 12.0 * 1024 * 1024 * 1024;
+  double avail = physmem_claimable (1.0);
+  printf ("memory needed = %g MiB, available = %g MiB\n",
+          needed / 1024 / 1024, avail / 1024 / 1024);
+  if (avail >= needed)
+    {
+      /* Note: The malloc() calls can fail, due to ulimit of RLIMIT_DATA.
+         For example, on OpenBSD 7.5, the soft limit is 1.0 GiB or 1.5 GiB,
+         and you need "ulimit -d 15728640".  */
+
+      /* Verify that u8_asnprintf() can return a string of size > 4 GiB.  */
+      {
+        size_t n1 = 3 * (INT_MAX / 4) + 10;
+        size_t n2 = 3 * (INT_MAX / 4) + 20;
+        char *s1;
+        char *s2;
+
+        s1 = (char *) malloc (n1 + 1);
+        if (s1 != NULL)
+          {
+            memset (s1, 'a', n1);
+            s1[n1] = '\0';
+
+            s2 = (char *) malloc (n2 + 1);
+            if (s2 != NULL)
+              {
+                memset (s2, 'b', n2);
+                s1[n1] = '\0';
+
+                size_t len;
+                uint8_t *s = u8_asnprintf (NULL, &len, "x%sy%sz", s1, s2);
+                if (s == NULL)
+                  {
+                    ASSERT (errno == ENOMEM);
+                    skipped = true;
+                  }
+                else
+                  {
+                    ASSERT (strlen (s) == len);
+                    ASSERT (len == n1 + n2 + 3);
+                    size_t i;
+                    for (i = 0; i <= len; i++)
+                      s[i] = (i == 0 ? 'x' :
+                              i <= n1 ? 'a' :
+                              i == n1 + 1 ? 'y' :
+                              i <= n1 + n2 + 1 ? 'b' :
+                              i == n1 + n2 + 2 ? 'z' :
+                              '\0');
+                    free (s);
+                  }
+                free (s2);
+              }
+            free (s1);
+          }
+      }
+
+      /* Verify that u8_asnprintf() can take a string of size > 2 GiB, < 4 GiB
+         as argument.  */
+      {
+        size_t n1 = 3 * (size_t) (INT_MAX / 2) + 10;
+        char *s1;
+
+        s1 = (char *) malloc (n1 + 1);
+        if (s1 != NULL)
+          {
+            memset (s1, 'a', n1);
+            s1[n1] = '\0';
+
+            size_t len;
+            uint8_t *s = u8_asnprintf (NULL, &len, "x%sy", s1);
+            if (s == NULL)
+              {
+                ASSERT (errno == ENOMEM);
+                skipped = true;
+              }
+            else
+              {
+                ASSERT (strlen (s) == len);
+                ASSERT (len == n1 + 2);
+                size_t i;
+                for (i = 0; i <= len; i++)
+                  s[i] = (i == 0 ? 'x' :
+                          i <= n1 ? 'a' :
+                          i == n1 + 1 ? 'y' :
+                          '\0');
+                free (s);
+              }
+            free (s1);
+          }
+      }
+
+      /* Verify that u8_asnprintf() can take a string of size > 4 GiB
+         as argument.  */
+      {
+        size_t n1 = 5 * (size_t) (INT_MAX / 2) + 10;
+        if (n1 > (size_t) INT_MAX)
+          {
+            char *s1;
+
+            s1 = (char *) malloc (n1 + 1);
+            if (s1 != NULL)
+              {
+                memset (s1, 'a', n1);
+                s1[n1] = '\0';
+
+                size_t len;
+                uint8_t *s = u8_asnprintf (NULL, &len, "x%sy", s1);
+                if (s == NULL)
+                  {
+                    ASSERT (errno == ENOMEM);
+                    skipped = true;
+                  }
+                else
+                  {
+                    ASSERT (strlen (s) == len);
+                    ASSERT (len == n1 + 2);
+                    size_t i;
+                    for (i = 0; i <= len; i++)
+                      s[i] = (i == 0 ? 'x' :
+                              i <= n1 ? 'a' :
+                              i == n1 + 1 ? 'y' :
+                              '\0');
+                    free (s);
+                  }
+                free (s1);
+              }
+          }
+      }
+    }
+  else
+    skipped = true;
+
+  if (test_exit_status != EXIT_SUCCESS)
+    return test_exit_status;
+  if (skipped)
+    {
+      fputs ("Skipping test: not enough memory available\n", stderr);
+      return 77;
+    }
+  return 0;
+#endif
+}