Update.
[glibc.git] / stdlib / tst-strtol.c
blobc072891ddbc4547313ac48069bcbbb23df60e1ef
1 /* My bet is this was written by Chris Torek.
2 I reformatted and ansidecl-ized it, and tweaked it a little. */
4 #include <ctype.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <limits.h>
11 struct ltest
13 const char *str; /* Convert this. */
14 unsigned long int expect; /* To get this. */
15 int base; /* Use this base. */
16 char left; /* With this left over. */
17 int err; /* And this in errno. */
19 static const struct ltest tests[] =
21 /* First, signed numbers: */
22 /* simple... */
23 {"123", 123, 0, 0, 0},
24 {"+123", 123, 0, 0, 0},
25 {" 123", 123, 0, 0, 0},
26 {" 123 ", 123, 0, ' ', 0},
27 {" -17", -17, 0, 0, 0},
29 /* implicit base... */
30 {"0123", 0123, 0, 0, 0},
31 {"0123a", 0123, 0, 'a', 0},
32 {"01239", 0123, 0, '9', 0},
33 {"0x123", 0x123, 0, 0, 0},
34 {"-0x123", -0x123, 0, 0, 0},
35 {"0x0xc", 0, 0, 'x', 0},
36 {" +0x123fg", 0x123f, 0, 'g', 0},
38 /* explicit base... */
39 {"123", 0x123, 16, 0, 0},
40 {"0x123", 0x123, 16, 0, 0},
41 {"123", 0123, 8, 0, 0},
42 {"0123", 0123, 8, 0, 0},
43 {"0123", 123, 10, 0, 0},
44 {"0x123", 0, 10, 'x', 0},
46 /* case insensitivity... */
47 {"abcd", 0xabcd, 16, 0, 0},
48 {"AbCd", 0xabcd, 16, 0, 0},
49 {"0xABCD", 0xabcd, 16, 0, 0},
50 {"0Xabcd", 0xabcd, 16, 0, 0},
52 /* odd bases... */
53 {"0xyz", 33 * 35 + 34, 35, 'z', 0},
54 {"yz!", 34 * 36 + 35, 36, '!', 0},
55 {"-yz", -(34*36 + 35), 36, 0, 0},
56 {"GhI4", ((16*20 + 17)*20 + 18)*20 + 4, 20, 0, 0},
58 /* extremes... */
59 #if LONG_MAX == 0x7fffffff
60 {"2147483647", 2147483647, 0, 0, 0},
61 {"2147483648", 2147483647, 0, 0, ERANGE},
62 {"214748364888", 2147483647, 0, 0, ERANGE},
63 {"2147483650", 2147483647, 0, 0, ERANGE},
64 {"-2147483648", 0x80000000, 0, 0, 0},
65 {"-2147483649", 0x80000000, 0, 0, ERANGE},
66 {"0x1122334455z", 2147483647, 16, 'z', ERANGE},
67 #else
68 {"9223372036854775807", 9223372036854775807, 0, 0, 0},
69 {"9223372036854775808", 9223372036854775807, 0, 0, ERANGE},
70 {"922337203685477580777", 9223372036854775807, 0, 0, ERANGE},
71 {"9223372036854775810", 9223372036854775807, 0, 0, ERANGE},
72 {"-2147483648", -2147483648, 0, 0, 0},
73 {"-9223372036854775808", -9223372036854775808, 0, 0, 0},
74 {"-9223372036854775809", -9223372036854775808, 0, 0, ERANGE},
75 {"0x112233445566778899z", 9223372036854775807, 16, 'z', ERANGE},
76 #endif
77 {NULL, 0, 0, 0, 0},
79 /* Then unsigned. */
80 {" 0", 0, 0, 0, 0},
81 {"0xffffffffg", 0xffffffff, 0, 'g', 0},
82 #if LONG_MAX == 0x7fffffff
83 {"-0xfedcba98", 0x01234568, 0, 0, 0},
84 {"0xf1f2f3f4f5", 0xffffffff, 0, 0, ERANGE},
85 {"-0x123456789", 0xffffffff, 0, 0, ERANGE},
86 #else
87 {"0xffffffffffffffffg", 0xffffffffffffffff, 0, 'g', 0},
88 {"-0xfedcba987654321", 0xf0123456789abcdf, 0, 0, 0},
89 {"0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffff, 0, 0, ERANGE},
90 {"-0x123456789abcdef01", 0xffffffffffffffff, 0, 0, ERANGE},
91 #endif
92 {NULL, 0, 0, 0, 0},
95 static void expand __P ((char *dst, int c));
97 int
98 main (int argc, char ** argv)
100 register const struct ltest *lt;
101 char *ep;
102 int status = 0;
103 int save_errno;
105 for (lt = tests; lt->str != NULL; ++lt)
107 register long int l;
109 errno = 0;
110 l = strtol (lt->str, &ep, lt->base);
111 save_errno = errno;
112 printf ("strtol(\"%s\", , %d) test %u",
113 lt->str, lt->base, (unsigned int) (lt - tests));
114 if (l == (long int) lt->expect && *ep == lt->left
115 && save_errno == lt->err)
116 puts("\tOK");
117 else
119 puts("\tBAD");
120 if (l != (long int) lt->expect)
121 printf(" returns %ld, expected %ld\n",
122 l, (long int) lt->expect);
123 if (lt->left != *ep)
125 char exp1[5], exp2[5];
126 expand (exp1, *ep);
127 expand (exp2, lt->left);
128 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
130 if (save_errno != lt->err)
131 printf (" errno %d (%s) instead of %d (%s)\n",
132 save_errno, strerror (save_errno),
133 lt->err, strerror (lt->err));
134 status = 1;
138 for (++lt; lt->str != NULL; lt++)
140 register unsigned long int ul;
142 errno = 0;
143 ul = strtoul (lt->str, &ep, lt->base);
144 save_errno = errno;
145 printf ("strtoul(\"%s\", , %d) test %u",
146 lt->str, lt->base, (unsigned int) (lt - tests));
147 if (ul == lt->expect && *ep == lt->left && save_errno == lt->err)
148 puts("\tOK");
149 else
151 puts ("\tBAD");
152 if (ul != lt->expect)
153 printf (" returns %lu, expected %lu\n",
154 ul, lt->expect);
155 if (lt->left != *ep)
157 char exp1[5], exp2[5];
158 expand (exp1, *ep);
159 expand (exp2, lt->left);
160 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
162 if (save_errno != lt->err)
163 printf (" errno %d (%s) instead of %d (%s)\n",
164 save_errno, strerror (save_errno),
165 lt->err, strerror (lt->err));
166 status = 1;
170 exit (status ? EXIT_FAILURE : EXIT_SUCCESS);
173 static void
174 expand (dst, c)
175 char *dst;
176 int c;
178 if (isprint (c))
180 dst[0] = c;
181 dst[1] = '\0';
183 else
184 (void) sprintf (dst, "%#.3o", (unsigned int) c);