]> Savannah Git Hosting - gnulib.git/commitdiff
xstrtol, xstrtoll tests: Test behaviour for an invalid base.
authorBruno Haible <bruno@clisp.org>
Thu, 25 Jul 2024 20:05:18 +0000 (22:05 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 25 Jul 2024 20:05:59 +0000 (22:05 +0200)
Reported by Alejandro Colomar <alx@kernel.org> in
<https://lists.gnu.org/archive/html/bug-gnulib/2024-07/msg00249.html>.

* modules/xstrtol-tests (Files): Add tests/macros.h.
* modules/xstrtoll-tests (Files): Likewise.
* tests/test-xstrtol.c: Include macros.h.
(main): If no arguments are given on the command line, perform
miscellaneous tests.
* tests/test-xstrtol.sh: Also invoke test-xstrtol without arguments.
* tests/test-xstrtoll.sh: Also invoke test-xstrtoll without arguments.

ChangeLog
modules/xstrtol-tests
modules/xstrtoll-tests
tests/test-xstrtol.c
tests/test-xstrtol.sh
tests/test-xstrtoll.sh

index ad0e632c0307931a8f145e90a9d5e03a68ab2417..e1f849e82cf8374d06f8518bc7fc335bf76c07c7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-07-25  Bruno Haible  <bruno@clisp.org>
+
+       xstrtol, xstrtoll tests: Test behaviour for an invalid base.
+       Reported by Alejandro Colomar <alx@kernel.org> in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2024-07/msg00249.html>.
+       * modules/xstrtol-tests (Files): Add tests/macros.h.
+       * modules/xstrtoll-tests (Files): Likewise.
+       * tests/test-xstrtol.c: Include macros.h.
+       (main): If no arguments are given on the command line, perform
+       miscellaneous tests.
+       * tests/test-xstrtol.sh: Also invoke test-xstrtol without arguments.
+       * tests/test-xstrtoll.sh: Also invoke test-xstrtoll without arguments.
+
 2024-07-24  Collin Funk  <collin.funk1@gmail.com>
 
        sys_un: Add C++ tests.
index 1786873d07732c3cca1cdd9be7aeb0cf6cfa405d..c52e1611d8745d63df4c3c8c3fd78ac4522b56ac 100644 (file)
@@ -2,6 +2,7 @@ Files:
 tests/test-xstrtol.c
 tests/test-xstrtoul.c
 tests/test-xstrtol.sh
+tests/macros.h
 
 Depends-on:
 inttypes
index d4e24d9d9a72a2efeebb638cbf2f52586279ebab..d28ebca73f183d0a447c815ec428eabd0b0d8822 100644 (file)
@@ -3,6 +3,7 @@ tests/test-xstrtol.c
 tests/test-xstrtoll.c
 tests/test-xstrtoull.c
 tests/test-xstrtoll.sh
+tests/macros.h
 
 Depends-on:
 inttypes
index c9568379aea92877251855fcfc9f28d585e7f9b5..f83d41905ff188cf99296d78fa2b72c6efd1d753 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 
 #include <error.h>
+#include "macros.h"
 #include "xstrtol-error.h"
 
 #ifndef __xstrtol
@@ -38,25 +39,45 @@ print_no_progname (void)
 int
 main (int argc, char **argv)
 {
-  strtol_error s_err;
-  int i;
-
-  error_print_progname = print_no_progname;
-
-  for (i = 1; i < argc; i++)
+  if (argc > 1)
     {
-      char *p;
-      __strtol_t val;
+      /* Test cases given on the command line.  */
+      int i;
 
-      s_err = __xstrtol (argv[i], &p, 0, &val, "bckMw0");
-      if (s_err == LONGINT_OK)
-        {
-          printf ("%s->%" __spec " (%s)\n", argv[i], val, p);
-        }
-      else
+      error_print_progname = print_no_progname;
+
+      for (i = 1; i < argc; i++)
         {
-          xstrtol_fatal (s_err, -2, 'X', NULL, argv[i]);
+          char *p;
+          __strtol_t val;
+          strtol_error s_err = __xstrtol (argv[i], &p, 0, &val, "bckMw0");
+          if (s_err == LONGINT_OK)
+            {
+              printf ("%s->%" __spec " (%s)\n", argv[i], val, p);
+            }
+          else
+            {
+              xstrtol_fatal (s_err, -2, 'X', NULL, argv[i]);
+            }
         }
+
+      return 0;
+    }
+  else
+    {
+      /* Miscellaneous test cases.  */
+
+      /* Test an invalid base.  Reported by Alejandro Colomar.  */
+      {
+        const char input[] = "k";
+        char *endp = NULL;
+        __strtol_t val = -17;
+        strtol_error s_err = __xstrtol (input, &endp, -1, &val, "k");
+        ASSERT (s_err == LONGINT_INVALID);
+        ASSERT (endp == NULL);
+        ASSERT (val == -17);
+      }
+
+      return test_exit_status;
     }
-  exit (0);
 }
index e82b585bf4077b8b6c08d0d17467e288a0e8377e..cec0a307644a24ad8e5a0decad062288cb8017a4 100755 (executable)
@@ -68,4 +68,7 @@ EOF
 
 compare expected out || result=1
 
+# Other misc tests.
+${CHECKER} test-xstrtol || result=1
+
 Exit $result
index d738acc38177b65eb108d7d145b6897ab23dd202..b8d235adced4d11adedd5b8447404f07d39fd2e0 100755 (executable)
@@ -64,4 +64,7 @@ EOF
 
 compare expected out || result=1
 
+# Other misc tests.
+${CHECKER} test-xstrtoll || result=1
+
 Exit $result