Debian Codegraph @

gnulib-tests/test-xstrtol.c

raw
1 /* Test of xstrtol module.
2 Copyright (C) 1995-1996, 1998-2001, 2003-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include <error.h>
24 #include "macros.h"
25 #include "xstrtol-error.h"
26
27 #ifndef __xstrtol
28 # define __xstrtol xstrtol
29 # define __strtol_t long int
30 # define __spec "ld"
31 # define is_GNULIB_strtol GNULIB_defined_strtol_function
32 #endif
33
34 /* Don't show the program name in error messages. */
35 static void
36 print_no_progname (void)
37 {
38 }
39
40 int
41 main (int argc, char **argv)
42 {
43 if (argc > 1)
44 {
45 /* Test cases given on the command line. */
46 int i;
47
48 error_print_progname = print_no_progname;
49
50 for (i = 1; i < argc; i++)
51 {
52 char *p;
53 __strtol_t val;
54 strtol_error s_err = __xstrtol (argv[i], &p, 0, &val, "bckMw0");
55 if (s_err == LONGINT_OK)
56 {
57 printf ("%s->%" __spec " (%s)\n", argv[i], val, p);
58 }
59 else
60 {
61 xstrtol_fatal (s_err, -2, 'X', NULL, argv[i]);
62 }
63 }
64
65 return 0;
66 }
67 else
68 {
69 /* Miscellaneous test cases. */
70
71 /* Test an invalid base (undefined behaviour, as documented in xstrtol.h).
72 Reported by Alejandro Colomar. */
73 #if !(defined __CYGWIN__ || defined _MSC_VER)
74 {
75 const char input[] = "k";
76 char *endp = NULL;
77 __strtol_t val = -17;
78 strtol_error s_err = __xstrtol (input, &endp, -1, &val, "k");
79 # if !(defined __GLIBC__ || is_GNULIB_strtol)
80 ASSERT (s_err == LONGINT_OK);
81 ASSERT (endp == input + 1);
82 ASSERT (val == 1024);
83 # else
84 ASSERT (s_err == LONGINT_INVALID);
85 ASSERT (val == -17);
86 # endif
87 }
88 #endif
89
90 return test_exit_status;
91 }
92 }