update from main archive 961211
[glibc/history.git] / stdlib / tst-strtol.c
blob1566364b32d807a689f4da5b1e729a32655c9a8c
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 <strings.h>
10 struct ltest
12 const char *str; /* Convert this. */
13 unsigned long int expect; /* To get this. */
14 int base; /* Use this base. */
15 char left; /* With this left over. */
16 int err; /* And this in errno. */
18 static const struct ltest tests[] =
20 #if ~0UL == 0xffffffff
21 /* First, signed numbers. */
22 { " -17", -17, 0, 0, 0 },
23 { " +0x123fg", 0x123f, 0, 'g', 0 },
24 { "2147483647", 2147483647, 0, 0, 0 },
25 { "2147483648", 2147483647, 0, 0, ERANGE },
26 { "214748364888", 2147483647, 0, 0, ERANGE },
27 { "2147483650", 2147483647, 0, 0, ERANGE },
28 { "-2147483649", -2147483648, 0, 0, ERANGE },
29 { "-2147483648", -2147483648, 0, 0, 0 },
30 { "0123", 0123, 0, 0, 0 },
31 { "0x1122334455z", 2147483647, 16, 'z', ERANGE },
32 { "0x0xc", 0, 0, 'x', 0 },
33 { "yz!", 34*36+35, 36, '!', 0 },
34 { NULL, 0, 0, 0, 0 },
36 /* Then unsigned. */
37 { " 0", 0, 0, 0, 0 },
38 { "0xffffffffg", 0xffffffff, 0, 'g', 0 },
39 { "0xf1f2f3f4f5", 0xffffffff, 0, 0, ERANGE },
40 { "-0x123456789", 0, 0, 0, EINVAL },
41 { "-0xfedcba98", 0, 0, 0, EINVAL },
42 { NULL, 0, 0, 0, 0 },
43 #else
44 /* assume 64 bit long... */
46 /* First, signed numbers. */
47 { " -17", -17, 0, 0, 0 },
48 { " +0x123fg", 0x123f, 0, 'g', 0 },
49 { "2147483647", 2147483647, 0, 0, 0 },
50 { "9223372036854775807", 9223372036854775807, 0, 0, 0 },
51 { "9223372036854775808", 9223372036854775807, 0, 0, ERANGE },
52 { "922337203685477580777", 9223372036854775807, 0, 0, ERANGE },
53 { "9223372036854775810", 9223372036854775807, 0, 0, ERANGE },
54 { "-2147483648", -2147483648, 0, 0, 0 },
55 { "-9223372036854775808", -9223372036854775808, 0, 0, 0 },
56 { "-9223372036854775809", -9223372036854775808, 0, 0, ERANGE },
57 { "0123", 0123, 0, 0, 0 },
58 { "0x112233445566778899z", 9223372036854775807, 16, 'z', ERANGE },
59 { "0x0xc", 0, 0, 'x', 0 },
60 { "yz!", 34*36+35, 36, '!', 0 },
61 { NULL, 0, 0, 0, 0 },
63 /* Then unsigned. */
64 { " 0", 0, 0, 0, 0 },
65 { "0xffffffffg", 0xffffffff, 0, 'g', 0 },
66 { "0xffffffffffffffffg", 0xffffffffffffffff, 0, 'g', 0 },
67 { "0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffff, 0, 0, ERANGE },
68 { "-0x123456789abcdef01", 0, 0, 0, EINVAL },
69 { "-0xfedcba987654321", 0, 0, 0, EINVAL },
70 { NULL, 0, 0, 0, 0 },
71 #endif
74 static void expand __P ((char *dst, int c));
76 int
77 main (int argc, char ** argv)
79 register const struct ltest *lt;
80 char *ep;
81 int status = 0;
83 for (lt = tests; lt->str != NULL; ++lt)
85 register long int l;
87 errno = 0;
88 l = strtol(lt->str, &ep, lt->base);
89 printf("strtol(\"%s\", , %d) test %u",
90 lt->str, lt->base, (unsigned int) (lt - tests));
91 if (l == (long int) lt->expect && *ep == lt->left && errno == lt->err)
92 puts("\tOK");
93 else
95 puts("\tBAD");
96 if (l != (long int) lt->expect)
97 printf(" returns %ld, expected %ld\n",
98 l, (long int) lt->expect);
99 if (lt->left != *ep)
101 char exp1[5], exp2[5];
102 expand(exp1, *ep);
103 expand(exp2, lt->left);
104 printf(" leaves '%s', expected '%s'\n", exp1, exp2);
106 if (errno != lt->err)
107 printf(" errno %d (%s) instead of %d (%s)\n",
108 errno, strerror(errno), lt->err, strerror(lt->err));
109 status = 1;
113 for (++lt; lt->str != NULL; lt++)
115 register unsigned long int ul;
117 errno = 0;
118 ul = strtoul(lt->str, &ep, lt->base);
119 printf("strtoul(\"%s\", , %d) test %u",
120 lt->str, lt->base, (unsigned int) (lt - tests));
121 if (ul == lt->expect && *ep == lt->left && errno == lt->err)
122 puts("\tOK");
123 else
125 puts("\tBAD");
126 if (ul != lt->expect)
127 printf(" returns %lu, expected %lu\n",
128 ul, lt->expect);
129 if (lt->left != *ep)
131 char exp1[5], exp2[5];
132 expand(exp1, *ep);
133 expand(exp2, lt->left);
134 printf(" leaves '%s', expected '%s'\n", exp1, exp2);
136 if (errno != lt->err)
137 printf(" errno %d (%s) instead of %d (%s)\n",
138 errno, strerror(errno), lt->err, strerror(lt->err));
139 status = 1;
143 exit(status ? EXIT_FAILURE : EXIT_SUCCESS);
146 static void
147 expand (dst, c)
148 char *dst;
149 int c;
151 if (isprint(c))
153 dst[0] = c;
154 dst[1] = '\0';
156 else
157 (void) sprintf(dst, "%#.3o", (unsigned int) c);