New file.
[glibc.git] / stdlib / tst-strtoll.c
blob9007488d80b88f5fb348a629bb1071ca9a56633e
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 {"0xFFFFFFFFFFFF00FF" , 9223372036854775807ll, 0, 0, ERANGE},
72 {NULL, 0, 0, 0, 0},
74 /* Then unsigned. */
75 {" 0", 0, 0, 0, 0},
76 {"0xffffffffg", 0xffffffff, 0, 'g', 0},
77 {"0xffffffffffffffffg", 0xffffffffffffffffull, 0, 'g', 0},
78 {"-0xfedcba987654321", 0xf0123456789abcdfull, 0, 0, 0},
79 {"0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffffull, 0, 0, ERANGE},
80 {"-0x123456789abcdef01", 0xffffffffffffffffull, 0, 0, ERANGE},
81 {NULL, 0, 0, 0, 0},
84 /* Prototypes for local functions. */
85 static void expand (char *dst, int c);
87 int
88 main (void)
90 register const struct ltest *lt;
91 char *ep;
92 int status = 0;
93 int save_errno;
95 for (lt = tests; lt->str != NULL; ++lt)
97 register long long int l;
99 errno = 0;
100 l = strtoll (lt->str, &ep, lt->base);
101 save_errno = errno;
102 printf ("strtoll(\"%s\", , %d) test %u",
103 lt->str, lt->base, (unsigned int) (lt - tests));
104 if (l == (long long int) lt->expect && *ep == lt->left
105 && save_errno == lt->err)
106 puts("\tOK");
107 else
109 puts("\tBAD");
110 if (l != (long long int) lt->expect)
111 printf(" returns %lld, expected %lld\n",
112 l, (long long int) lt->expect);
113 if (lt->left != *ep)
115 char exp1[5], exp2[5];
116 expand (exp1, *ep);
117 expand (exp2, lt->left);
118 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
120 if (save_errno != lt->err)
121 printf (" errno %d (%s) instead of %d (%s)\n",
122 save_errno, strerror (save_errno),
123 lt->err, strerror (lt->err));
124 status = 1;
128 for (++lt; lt->str != NULL; lt++)
130 register unsigned long long int ul;
132 errno = 0;
133 ul = strtoull (lt->str, &ep, lt->base);
134 save_errno = errno;
135 printf ("strtoull(\"%s\", , %d) test %u",
136 lt->str, lt->base, (unsigned int) (lt - tests));
137 if (ul == lt->expect && *ep == lt->left && save_errno == lt->err)
138 puts("\tOK");
139 else
141 puts ("\tBAD");
142 if (ul != lt->expect)
143 printf (" returns %llu, expected %llu\n",
144 ul, lt->expect);
145 if (lt->left != *ep)
147 char exp1[5], exp2[5];
148 expand (exp1, *ep);
149 expand (exp2, lt->left);
150 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
152 if (save_errno != lt->err)
153 printf (" errno %d (%s) instead of %d (%s)\n",
154 save_errno, strerror (save_errno),
155 lt->err, strerror (lt->err));
156 status = 1;
160 return status ? EXIT_FAILURE : EXIT_SUCCESS;
163 static void
164 expand (dst, c)
165 char *dst;
166 int c;
168 if (isprint (c))
170 dst[0] = c;
171 dst[1] = '\0';
173 else
174 (void) sprintf (dst, "%#.3o", (unsigned int) c);