From: Bruno Haible Date: Thu, 25 Jul 2024 20:05:18 +0000 (+0200) Subject: xstrtol, xstrtoll tests: Test behaviour for an invalid base. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=77dd627875935a4bfd39baf1e597a33ad4fad804;p=gnulib.git xstrtol, xstrtoll tests: Test behaviour for an invalid base. Reported by Alejandro Colomar in . * 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. --- diff --git a/ChangeLog b/ChangeLog index ad0e632c03..e1f849e82c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2024-07-25 Bruno Haible + + xstrtol, xstrtoll tests: Test behaviour for an invalid base. + Reported by Alejandro Colomar in + . + * 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 sys_un: Add C++ tests. diff --git a/modules/xstrtol-tests b/modules/xstrtol-tests index 1786873d07..c52e1611d8 100644 --- a/modules/xstrtol-tests +++ b/modules/xstrtol-tests @@ -2,6 +2,7 @@ Files: tests/test-xstrtol.c tests/test-xstrtoul.c tests/test-xstrtol.sh +tests/macros.h Depends-on: inttypes diff --git a/modules/xstrtoll-tests b/modules/xstrtoll-tests index d4e24d9d9a..d28ebca73f 100644 --- a/modules/xstrtoll-tests +++ b/modules/xstrtoll-tests @@ -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 diff --git a/tests/test-xstrtol.c b/tests/test-xstrtol.c index c9568379ae..f83d41905f 100644 --- a/tests/test-xstrtol.c +++ b/tests/test-xstrtol.c @@ -21,6 +21,7 @@ #include #include +#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); } diff --git a/tests/test-xstrtol.sh b/tests/test-xstrtol.sh index e82b585bf4..cec0a30764 100755 --- a/tests/test-xstrtol.sh +++ b/tests/test-xstrtol.sh @@ -68,4 +68,7 @@ EOF compare expected out || result=1 +# Other misc tests. +${CHECKER} test-xstrtol || result=1 + Exit $result diff --git a/tests/test-xstrtoll.sh b/tests/test-xstrtoll.sh index d738acc381..b8d235adce 100755 --- a/tests/test-xstrtoll.sh +++ b/tests/test-xstrtoll.sh @@ -64,4 +64,7 @@ EOF compare expected out || result=1 +# Other misc tests. +${CHECKER} test-xstrtoll || result=1 + Exit $result