Update.
[glibc.git] / stdlib / tst-strtol.c
blob483e71373927532f3a553f6c9acdd97b3c763bda
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 #if LONG_MAX == 0x7fffffff
22 /* First, signed numbers. */
23 { " -17", -17, 0, 0, 0 },
24 { " +0x123fg", 0x123f, 0, 'g', 0 },
25 { "2147483647", 2147483647, 0, 0, 0 },
26 { "2147483648", 2147483647, 0, 0, ERANGE },
27 { "214748364888", 2147483647, 0, 0, ERANGE },
28 { "2147483650", 2147483647, 0, 0, ERANGE },
29 { "-2147483649", 0x80000000, 0, 0, ERANGE },
30 { "-2147483648", 0x80000000, 0, 0, 0 },
31 { "0123", 0123, 0, 0, 0 },
32 { "0x1122334455z", 2147483647, 16, 'z', ERANGE },
33 { "0x0xc", 0, 0, 'x', 0 },
34 { "yz!", 34*36+35, 36, '!', 0 },
35 { NULL, 0, 0, 0, 0 },
37 /* Then unsigned. */
38 { " 0", 0, 0, 0, 0 },
39 { "0xffffffffg", 0xffffffff, 0, 'g', 0 },
40 { "0xf1f2f3f4f5", 0xffffffff, 0, 0, ERANGE },
41 { "-0x123456789", 0xffffffff, 0, 0, ERANGE },
42 { "-0xfedcba98", 0x01234568, 0, 0, 0 },
43 { NULL, 0, 0, 0, 0 },
44 #else
45 /* assume 64 bit long... */
47 /* First, signed numbers. */
48 { " -17", -17, 0, 0, 0 },
49 { " +0x123fg", 0x123f, 0, 'g', 0 },
50 { "2147483647", 2147483647, 0, 0, 0 },
51 { "9223372036854775807", 9223372036854775807, 0, 0, 0 },
52 { "9223372036854775808", 9223372036854775807, 0, 0, ERANGE },
53 { "922337203685477580777", 9223372036854775807, 0, 0, ERANGE },
54 { "9223372036854775810", 9223372036854775807, 0, 0, ERANGE },
55 { "-2147483648", -2147483648, 0, 0, 0 },
56 { "-9223372036854775808", -9223372036854775808, 0, 0, 0 },
57 { "-9223372036854775809", -9223372036854775808, 0, 0, ERANGE },
58 { "0123", 0123, 0, 0, 0 },
59 { "0x112233445566778899z", 9223372036854775807, 16, 'z', ERANGE },
60 { "0x0xc", 0, 0, 'x', 0 },
61 { "yz!", 34*36+35, 36, '!', 0 },
62 { NULL, 0, 0, 0, 0 },
64 /* Then unsigned. */
65 { " 0", 0, 0, 0, 0 },
66 { "0xffffffffg", 0xffffffff, 0, 'g', 0 },
67 { "0xffffffffffffffffg", 0xffffffffffffffff, 0, 'g', 0 },
68 { "0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffff, 0, 0, ERANGE },
69 { "-0x123456789abcdef01", 0xffffffffffffffff, 0, 0, ERANGE },
70 { "-0xfedcba987654321", 0xf0123456789abcdf, 0, 0, 0 },
71 { NULL, 0, 0, 0, 0 },
72 #endif
75 static void expand __P ((char *dst, int c));
77 int
78 main (int argc, char ** argv)
80 register const struct ltest *lt;
81 char *ep;
82 int status = 0;
83 int save_errno;
85 for (lt = tests; lt->str != NULL; ++lt)
87 register long int l;
89 errno = 0;
90 l = strtol (lt->str, &ep, lt->base);
91 save_errno = errno;
92 printf ("strtol(\"%s\", , %d) test %u",
93 lt->str, lt->base, (unsigned int) (lt - tests));
94 if (l == (long int) lt->expect && *ep == lt->left
95 && save_errno == lt->err)
96 puts("\tOK");
97 else
99 puts("\tBAD");
100 if (l != (long int) lt->expect)
101 printf(" returns %ld, expected %ld\n",
102 l, (long int) lt->expect);
103 if (lt->left != *ep)
105 char exp1[5], exp2[5];
106 expand (exp1, *ep);
107 expand (exp2, lt->left);
108 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
110 if (save_errno != lt->err)
111 printf (" errno %d (%s) instead of %d (%s)\n",
112 save_errno, strerror (save_errno),
113 lt->err, strerror (lt->err));
114 status = 1;
118 for (++lt; lt->str != NULL; lt++)
120 register unsigned long int ul;
122 errno = 0;
123 ul = strtoul (lt->str, &ep, lt->base);
124 save_errno = errno;
125 printf ("strtoul(\"%s\", , %d) test %u",
126 lt->str, lt->base, (unsigned int) (lt - tests));
127 if (ul == lt->expect && *ep == lt->left && save_errno == lt->err)
128 puts("\tOK");
129 else
131 puts ("\tBAD");
132 if (ul != lt->expect)
133 printf (" returns %lu, expected %lu\n",
134 ul, lt->expect);
135 if (lt->left != *ep)
137 char exp1[5], exp2[5];
138 expand (exp1, *ep);
139 expand (exp2, lt->left);
140 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
142 if (save_errno != lt->err)
143 printf (" errno %d (%s) instead of %d (%s)\n",
144 save_errno, strerror (save_errno),
145 lt->err, strerror (lt->err));
146 status = 1;
150 exit (status ? EXIT_FAILURE : EXIT_SUCCESS);
153 static void
154 expand (dst, c)
155 char *dst;
156 int c;
158 if (isprint (c))
160 dst[0] = c;
161 dst[1] = '\0';
163 else
164 (void) sprintf (dst, "%#.3o", (unsigned int) c);