Update.
[glibc.git] / stdlib / tst-strtoll.c
blob38ceaeb0bf1ac8c45bf35ac1c22e9ec87a3652d7
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 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 /* special case for the 32-bit version of strtoll,
59 from a ncftp configure test */
60 {"99000000001", 1000000000ll * 99ll + 1ll, 0, 0},
62 /* extremes... */
63 {"9223372036854775807", 9223372036854775807ll, 0, 0, 0},
64 {"9223372036854775808", 9223372036854775807ll, 0, 0, ERANGE},
65 {"922337203685477580777", 9223372036854775807ll, 0, 0, ERANGE},
66 {"9223372036854775810", 9223372036854775807ll, 0, 0, ERANGE},
67 {"-2147483648", -2147483648ll, 0, 0, 0},
68 {"-9223372036854775808", -9223372036854775807ll - 1, 0, 0, 0},
69 {"-9223372036854775809", -9223372036854775807ll - 1, 0, 0, ERANGE},
70 {"0x112233445566778899z", 9223372036854775807ll, 16, 'z', ERANGE},
71 {NULL, 0, 0, 0, 0},
73 /* Then unsigned. */
74 {" 0", 0, 0, 0, 0},
75 {"0xffffffffg", 0xffffffff, 0, 'g', 0},
76 {"0xffffffffffffffffg", 0xffffffffffffffffull, 0, 'g', 0},
77 {"-0xfedcba987654321", 0xf0123456789abcdfull, 0, 0, 0},
78 {"0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffffull, 0, 0, ERANGE},
79 {"-0x123456789abcdef01", 0xffffffffffffffffull, 0, 0, ERANGE},
80 {NULL, 0, 0, 0, 0},
83 static void expand __P ((char *dst, int c));
85 int
86 main (int argc, char ** argv)
88 register const struct ltest *lt;
89 char *ep;
90 int status = 0;
91 int save_errno;
93 for (lt = tests; lt->str != NULL; ++lt)
95 register long long int l;
97 errno = 0;
98 l = strtoll (lt->str, &ep, lt->base);
99 save_errno = errno;
100 printf ("strtoll(\"%s\", , %d) test %u",
101 lt->str, lt->base, (unsigned int) (lt - tests));
102 if (l == (long long int) lt->expect && *ep == lt->left
103 && save_errno == lt->err)
104 puts("\tOK");
105 else
107 puts("\tBAD");
108 if (l != (long long int) lt->expect)
109 printf(" returns %lld, expected %lld\n",
110 l, (long long int) lt->expect);
111 if (lt->left != *ep)
113 char exp1[5], exp2[5];
114 expand (exp1, *ep);
115 expand (exp2, lt->left);
116 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
118 if (save_errno != lt->err)
119 printf (" errno %d (%s) instead of %d (%s)\n",
120 save_errno, strerror (save_errno),
121 lt->err, strerror (lt->err));
122 status = 1;
126 for (++lt; lt->str != NULL; lt++)
128 register unsigned long long int ul;
130 errno = 0;
131 ul = strtoull (lt->str, &ep, lt->base);
132 save_errno = errno;
133 printf ("strtoull(\"%s\", , %d) test %u",
134 lt->str, lt->base, (unsigned int) (lt - tests));
135 if (ul == lt->expect && *ep == lt->left && save_errno == lt->err)
136 puts("\tOK");
137 else
139 puts ("\tBAD");
140 if (ul != lt->expect)
141 printf (" returns %llu, expected %llu\n",
142 ul, lt->expect);
143 if (lt->left != *ep)
145 char exp1[5], exp2[5];
146 expand (exp1, *ep);
147 expand (exp2, lt->left);
148 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
150 if (save_errno != lt->err)
151 printf (" errno %d (%s) instead of %d (%s)\n",
152 save_errno, strerror (save_errno),
153 lt->err, strerror (lt->err));
154 status = 1;
158 exit (status ? EXIT_FAILURE : EXIT_SUCCESS);
161 static void
162 expand (dst, c)
163 char *dst;
164 int c;
166 if (isprint (c))
168 dst[0] = c;
169 dst[1] = '\0';
171 else
172 (void) sprintf (dst, "%#.3o", (unsigned int) c);