1 /* My bet is this was written by Chris Torek.
2 I reformatted and ansidecl-ized it, and tweaked it a little. */
3
4 #include <ctype.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <limits.h>
10
11 struct ltest
12 {
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. */
18 };
19 static const struct ltest tests[] =
20 {
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},
28
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},
37
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},
45
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},
51
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},
57
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
68 {"1111111111111111111111111111111", 2147483647, 2, 0, 0},
69 {"10000000000000000000000000000000", 2147483647, 2, 0, ERANGE},
70 {"12112122212110202101", 2147483647, 3, 0, 0},
71 {"12112122212110202102", 2147483647, 3, 0, ERANGE},
72 {"1333333333333333", 2147483647, 4, 0, 0},
73 {"2000000000000000", 2147483647, 4, 0, ERANGE},
74 {"13344223434042", 2147483647, 5, 0, 0},
75 {"13344223434043", 2147483647, 5, 0, ERANGE},
76 {"553032005531", 2147483647, 6, 0, 0},
77 {"553032005532", 2147483647, 6, 0, ERANGE},
78 {"104134211161", 2147483647, 7, 0, 0},
79 {"104134211162", 2147483647, 7, 0, ERANGE},
80 {"17777777777", 2147483647, 8, 0, 0},
81 {"20000000000", 2147483647, 8, 0, ERANGE},
82 {"5478773671", 2147483647, 9, 0, 0},
83 {"5478773672", 2147483647, 9, 0, ERANGE},
84 {"2147483647", 2147483647, 10, 0, 0},
85 {"2147483648", 2147483647, 10, 0, ERANGE},
86 {"a02220281", 2147483647, 11, 0, 0},
87 {"a02220282", 2147483647, 11, 0, ERANGE},
88 {"4bb2308a7", 2147483647, 12, 0, 0},
89 {"4bb2308a8", 2147483647, 12, 0, ERANGE},
90 {"282ba4aaa", 2147483647, 13, 0, 0},
91 {"282ba4aab", 2147483647, 13, 0, ERANGE},
92 {"1652ca931", 2147483647, 14, 0, 0},
93 {"1652ca932", 2147483647, 14, 0, ERANGE},
94 {"c87e66b7", 2147483647, 15, 0, 0},
95 {"c87e66b8", 2147483647, 15, 0, ERANGE},
96 {"7fffffff", 2147483647, 16, 0, 0},
97 {"80000000", 2147483647, 16, 0, ERANGE},
98 {"53g7f548", 2147483647, 17, 0, 0},
99 {"53g7f549", 2147483647, 17, 0, ERANGE},
100 {"3928g3h1", 2147483647, 18, 0, 0},
101 {"3928g3h2", 2147483647, 18, 0, ERANGE},
102 {"27c57h32", 2147483647, 19, 0, 0},
103 {"27c57h33", 2147483647, 19, 0, ERANGE},
104 {"1db1f927", 2147483647, 20, 0, 0},
105 {"1db1f928", 2147483647, 20, 0, ERANGE},
106 {"140h2d91", 2147483647, 21, 0, 0},
107 {"140h2d92", 2147483647, 21, 0, ERANGE},
108 {"ikf5bf1", 2147483647, 22, 0, 0},
109 {"ikf5bf2", 2147483647, 22, 0, ERANGE},
110 {"ebelf95", 2147483647, 23, 0, 0},
111 {"ebelf96", 2147483647, 23, 0, ERANGE},
112 {"b5gge57", 2147483647, 24, 0, 0},
113 {"b5gge58", 2147483647, 24, 0, ERANGE},
114 {"8jmdnkm", 2147483647, 25, 0, 0},
115 {"8jmdnkn", 2147483647, 25, 0, ERANGE},
116 {"6oj8ion", 2147483647, 26, 0, 0},
117 {"6oj8ioo", 2147483647, 26, 0, ERANGE},
118 {"5ehncka", 2147483647, 27, 0, 0},
119 {"5ehnckb", 2147483647, 27, 0, ERANGE},
120 {"4clm98f", 2147483647, 28, 0, 0},
121 {"4clm98g", 2147483647, 28, 0, ERANGE},
122 {"3hk7987", 2147483647, 29, 0, 0},
123 {"3hk7988", 2147483647, 29, 0, ERANGE},
124 {"2sb6cs7", 2147483647, 30, 0, 0},
125 {"2sb6cs8", 2147483647, 30, 0, ERANGE},
126 {"2d09uc1", 2147483647, 31, 0, 0},
127 {"2d09uc2", 2147483647, 31, 0, ERANGE},
128 {"1vvvvvv", 2147483647, 32, 0, 0},
129 {"2000000", 2147483647, 32, 0, ERANGE},
130 {"1lsqtl1", 2147483647, 33, 0, 0},
131 {"1lsqtl2", 2147483647, 33, 0, ERANGE},
132 {"1d8xqrp", 2147483647, 34, 0, 0},
133 {"1d8xqrq", 2147483647, 34, 0, ERANGE},
134 {"15v22um", 2147483647, 35, 0, 0},
135 {"15v22un", 2147483647, 35, 0, ERANGE},
136 {"zik0zj", 2147483647, 36, 0, 0},
137 {"zik0zk", 2147483647, 36, 0, ERANGE},
138
139 {"-10000000000000000000000000000000", -2147483648, 2, 0, 0},
140 {"-10000000000000000000000000000001", -2147483648, 2, 0, ERANGE},
141 {"-12112122212110202102", -2147483648, 3, 0, 0},
142 {"-12112122212110202110", -2147483648, 3, 0, ERANGE},
143 {"-2000000000000000", -2147483648, 4, 0, 0},
144 {"-2000000000000001", -2147483648, 4, 0, ERANGE},
145 {"-13344223434043", -2147483648, 5, 0, 0},
146 {"-13344223434044", -2147483648, 5, 0, ERANGE},
147 {"-553032005532", -2147483648, 6, 0, 0},
148 {"-553032005533", -2147483648, 6, 0, ERANGE},
149 {"-104134211162", -2147483648, 7, 0, 0},
150 {"-104134211163", -2147483648, 7, 0, ERANGE},
151 {"-20000000000", -2147483648, 8, 0, },
152 {"-20000000001", -2147483648, 8, 0, ERANGE},
153 {"-5478773672", -2147483648, 9, 0, 0},
154 {"-5478773673", -2147483648, 9, 0, ERANGE},
155 {"-2147483648", -2147483648, 10, 0, 0},
156 {"-2147483649", -2147483648, 10, 0, ERANGE},
157 {"-a02220282", -2147483648, 11, 0, 0},
158 {"-a02220283", -2147483648, 11, 0, ERANGE},
159 {"-4bb2308a8", -2147483648, 12, 0, 0},
160 {"-4bb2308a9", -2147483648, 12, 0, ERANGE},
161 {"-282ba4aab", -2147483648, 13, 0, 0},
162 {"-282ba4aac", -2147483648, 13, 0, ERANGE},
163 {"-1652ca932", -2147483648, 14, 0, 0},
164 {"-1652ca933", -2147483648, 14, 0, ERANGE},
165 {"-c87e66b8", -2147483648, 15, 0, 0},
166 {"-c87e66b9", -2147483648, 15, 0, ERANGE},
167 {"-80000000", -2147483648, 16, 0, 0},
168 {"-80000001", -2147483648, 16, 0, ERANGE},
169 {"-53g7f549", -2147483648, 17, 0, 0},
170 {"-53g7f54a", -2147483648, 17, 0, ERANGE},
171 {"-3928g3h2", -2147483648, 18, 0, 0},
172 {"-3928g3h3", -2147483648, 18, 0, ERANGE},
173 {"-27c57h33", -2147483648, 19, 0, 0},
174 {"-27c57h34", -2147483648, 19, 0, ERANGE},
175 {"-1db1f928", -2147483648, 20, 0, 0},
176 {"-1db1f929", -2147483648, 20, 0, ERANGE},
177 {"-140h2d92", -2147483648, 21, 0, 0},
178 {"-140h2d93", -2147483648, 21, 0, ERANGE},
179 {"-ikf5bf2", -2147483648, 22, 0, 0},
180 {"-ikf5bf3", -2147483648, 22, 0, ERANGE},
181 {"-ebelf96", -2147483648, 23, 0, 0},
182 {"-ebelf97", -2147483648, 23, 0, ERANGE},
183 {"-b5gge58", -2147483648, 24, 0, 0},
184 {"-b5gge59", -2147483648, 24, 0, ERANGE},
185 {"-8jmdnkn", -2147483648, 25, 0, 0},
186 {"-8jmdnko", -2147483648, 25, 0, ERANGE},
187 {"-6oj8ioo", -2147483648, 26, 0, 0},
188 {"-6oj8iop", -2147483648, 26, 0, ERANGE},
189 {"-5ehnckb", -2147483648, 27, 0, 0},
190 {"-5ehnckc", -2147483648, 27, 0, ERANGE},
191 {"-4clm98g", -2147483648, 28, 0, 0},
192 {"-4clm98h", -2147483648, 28, 0, ERANGE},
193 {"-3hk7988", -2147483648, 29, 0, 0},
194 {"-3hk7989", -2147483648, 29, 0, ERANGE},
195 {"-2sb6cs8", -2147483648, 30, 0, 0},
196 {"-2sb6cs9", -2147483648, 30, 0, ERANGE},
197 {"-2d09uc2", -2147483648, 31, 0, 0},
198 {"-2d09uc3", -2147483648, 31, 0, ERANGE},
199 {"-2000000", -2147483648, 32, 0, 0},
200 {"-2000001", -2147483648, 32, 0, ERANGE},
201 {"-1lsqtl2", -2147483648, 33, 0, 0},
202 {"-1lsqtl3", -2147483648, 33, 0, ERANGE},
203 {"-1d8xqrq", -2147483648, 34, 0, 0},
204 {"-1d8xqrr", -2147483648, 34, 0, ERANGE},
205 {"-15v22un", -2147483648, 35, 0, 0},
206 {"-15v22uo", -2147483648, 35, 0, ERANGE},
207 {"-zik0zk", -2147483648, 36, 0, 0},
208 {"-zik0zl", -2147483648, 36, 0, ERANGE},
209 #else
210 {"9223372036854775807", 9223372036854775807, 0, 0, 0},
211 {"9223372036854775808", 9223372036854775807, 0, 0, ERANGE},
212 {"922337203685477580777", 9223372036854775807, 0, 0, ERANGE},
213 {"9223372036854775810", 9223372036854775807, 0, 0, ERANGE},
214 {"-2147483648", -2147483648, 0, 0, 0},
215 {"-9223372036854775808", 0x8000000000000000, 0, 0, 0},
216 {"-9223372036854775809", 0x8000000000000000, 0, 0, ERANGE},
217 {"0x112233445566778899z", 9223372036854775807, 16, 'z', ERANGE},
218 {"0xFFFFFFFFFFFF00FF" , 9223372036854775807, 0, 0, ERANGE},
219
220 {"111111111111111111111111111111111111111111111111111111111111111",
221 9223372036854775807, 2, 0, 0},
222 {"1000000000000000000000000000000000000000000000000000000000000000",
223 9223372036854775807, 2, 0, ERANGE},
224 {"2021110011022210012102010021220101220221",
225 9223372036854775807, 3, 0, 0},
226 {"2021110011022210012102010021220101220222",
227 9223372036854775807, 3, 0, ERANGE},
228 {"13333333333333333333333333333333", 9223372036854775807, 4, 0, 0},
229 {"20000000000000000000000000000000", 9223372036854775807, 4, 0, ERANGE},
230 {"1104332401304422434310311212", 9223372036854775807, 5, 0, 0},
231 {"1104332401304422434310311213", 9223372036854775807, 5, 0, ERANGE},
232 {"1540241003031030222122211", 9223372036854775807, 6, 0, 0},
233 {"1540241003031030222122212", 9223372036854775807, 6, 0, ERANGE},
234 {"22341010611245052052300", 9223372036854775807, 7, 0, 0},
235 {"22341010611245052052301", 9223372036854775807, 7, 0, ERANGE},
236 {"777777777777777777777", 9223372036854775807, 8, 0, 0},
237 {"1000000000000000000000", 9223372036854775807, 8, 0, ERANGE},
238 {"67404283172107811827", 9223372036854775807, 9, 0, 0},
239 {"67404283172107811828", 9223372036854775807, 9, 0, ERANGE},
240 {"9223372036854775807", 9223372036854775807, 10, 0, 0},
241 {"9223372036854775808", 9223372036854775807, 10, 0, ERANGE},
242 {"1728002635214590697", 9223372036854775807, 11, 0, 0},
243 {"1728002635214590698", 9223372036854775807, 11, 0, ERANGE},
244 {"41a792678515120367", 9223372036854775807, 12, 0, 0},
245 {"41a792678515120368", 9223372036854775807, 12, 0, ERANGE},
246 {"10b269549075433c37", 9223372036854775807, 13, 0, 0},
247 {"10b269549075433c38", 9223372036854775807, 13, 0, ERANGE},
248 {"4340724c6c71dc7a7", 9223372036854775807, 14, 0, 0},
249 {"4340724c6c71dc7a8", 9223372036854775807, 14, 0, ERANGE},
250 {"160e2ad3246366807", 9223372036854775807, 15, 0, 0},
251 {"160e2ad3246366808", 9223372036854775807, 15, 0, ERANGE},
252 {"7fffffffffffffff", 9223372036854775807, 16, 0, 0},
253 {"8000000000000000", 9223372036854775807, 16, 0, ERANGE},
254 {"33d3d8307b214008", 9223372036854775807, 17, 0, 0},
255 {"33d3d8307b214009", 9223372036854775807, 17, 0, ERANGE},
256 {"16agh595df825fa7", 9223372036854775807, 18, 0, 0},
257 {"16agh595df825fa8", 9223372036854775807, 18, 0, ERANGE},
258 {"ba643dci0ffeehh", 9223372036854775807, 19, 0, 0},
259 {"ba643dci0ffeehi", 9223372036854775807, 19, 0, ERANGE},
260 {"5cbfjia3fh26ja7", 9223372036854775807, 20, 0, 0},
261 {"5cbfjia3fh26ja8", 9223372036854775807, 20, 0, ERANGE},
262 {"2heiciiie82dh97", 9223372036854775807, 21, 0, 0},
263 {"2heiciiie82dh98", 9223372036854775807, 21, 0, ERANGE},
264 {"1adaibb21dckfa7", 9223372036854775807, 22, 0, 0},
265 {"1adaibb21dckfa8", 9223372036854775807, 22, 0, ERANGE},
266 {"i6k448cf4192c2", 9223372036854775807, 23, 0, 0},
267 {"i6k448cf4192c3", 9223372036854775807, 23, 0, ERANGE},
268 {"acd772jnc9l0l7", 9223372036854775807, 24, 0, 0},
269 {"acd772jnc9l0l8", 9223372036854775807, 24, 0, ERANGE},
270 {"64ie1focnn5g77", 9223372036854775807, 25, 0, 0},
271 {"64ie1focnn5g78", 9223372036854775807, 25, 0, ERANGE},
272 {"3igoecjbmca687", 9223372036854775807, 26, 0, 0},
273 {"3igoecjbmca688", 9223372036854775807, 26, 0, ERANGE},
274 {"27c48l5b37oaop", 9223372036854775807, 27, 0, 0},
275 {"27c48l5b37oaoq", 9223372036854775807, 27, 0, ERANGE},
276 {"1bk39f3ah3dmq7", 9223372036854775807, 28, 0, 0},
277 {"1bk39f3ah3dmq8", 9223372036854775807, 28, 0, ERANGE},
278 {"q1se8f0m04isb", 9223372036854775807, 29, 0, 0},
279 {"q1se8f0m04isc", 9223372036854775807, 29, 0, ERANGE},
280 {"hajppbc1fc207", 9223372036854775807, 30, 0, 0},
281 {"hajppbc1fc208", 9223372036854775807, 30, 0, ERANGE},
282 {"bm03i95hia437", 9223372036854775807, 31, 0, 0},
283 {"bm03i95hia438", 9223372036854775807, 31, 0, ERANGE},
284 {"7vvvvvvvvvvvv", 9223372036854775807, 32, 0, 0},
285 {"8000000000000", 9223372036854775807, 32, 0, ERANGE},
286 {"5hg4ck9jd4u37", 9223372036854775807, 33, 0, 0},
287 {"5hg4ck9jd4u38", 9223372036854775807, 33, 0, ERANGE},
288 {"3tdtk1v8j6tpp", 9223372036854775807, 34, 0, 0},
289 {"3tdtk1v8j6tpq", 9223372036854775807, 34, 0, ERANGE},
290 {"2pijmikexrxp7", 9223372036854775807, 35, 0, 0},
291 {"2pijmikexrxp8", 9223372036854775807, 35, 0, ERANGE},
292 {"1y2p0ij32e8e7", 9223372036854775807, 36, 0, 0},
293 {"1y2p0ij32e8e8", 9223372036854775807, 36, 0, ERANGE},
294
295 {"-1000000000000000000000000000000000000000000000000000000000000000",
296 -9223372036854775808ull, 2, 0, 0},
297 {"-1000000000000000000000000000000000000000000000000000000000000001",
298 -9223372036854775808ull, 2, 0, ERANGE},
299 {"-2021110011022210012102010021220101220222",
300 -9223372036854775808ull, 3, 0, 0},
301 {"-2021110011022210012102010021220101221000",
302 -9223372036854775808ull, 3, 0, ERANGE},
303 {"-20000000000000000000000000000000", -9223372036854775808ull, 4, 0, 0},
304 {"-20000000000000000000000000000001", -9223372036854775808ull, 4, 0, ERANGE},
305 {"-1104332401304422434310311213", -9223372036854775808ull, 5, 0, 0},
306 {"-1104332401304422434310311214", -9223372036854775808ull, 5, 0, ERANGE},
307 {"-1540241003031030222122212", -9223372036854775808ull, 6, 0, 0},
308 {"-1540241003031030222122213", -9223372036854775808ull, 6, 0, ERANGE},
309 {"-22341010611245052052301", -9223372036854775808ull, 7, 0, 0},
310 {"-22341010611245052052302", -9223372036854775808ull, 7, 0, ERANGE},
311 {"-1000000000000000000000", -9223372036854775808ull, 8, 0, 0},
312 {"-1000000000000000000001", -9223372036854775808ull, 8, 0, ERANGE},
313 {"-67404283172107811828", -9223372036854775808ull, 9, 0, 0},
314 {"-67404283172107811830", -9223372036854775808ull, 9, 0, ERANGE},
315 {"-9223372036854775808", -9223372036854775808ull, 10, 0, 0},
316 {"-9223372036854775809", -9223372036854775808ull, 10, 0, ERANGE},
317 {"-1728002635214590698", -9223372036854775808ull, 11, 0, 0},
318 {"-1728002635214590699", -9223372036854775808ull, 11, 0, ERANGE},
319 {"-41a792678515120368", -9223372036854775808ull, 12, 0, 0},
320 {"-41a792678515120369", -9223372036854775808ull, 12, 0, ERANGE},
321 {"-10b269549075433c38", -9223372036854775808ull, 13, 0, 0},
322 {"-10b269549075433c39", -9223372036854775808ull, 13, 0, ERANGE},
323 {"-4340724c6c71dc7a8", -9223372036854775808ull, 14, 0, 0},
324 {"-4340724c6c71dc7a9", -9223372036854775808ull, 14, 0, ERANGE},
325 {"-160e2ad3246366808", -9223372036854775808ull, 15, 0, 0},
326 {"-160e2ad3246366809", -9223372036854775808ull, 15, 0, ERANGE},
327 {"-8000000000000000", -9223372036854775808ull, 16, 0, 0},
328 {"-8000000000000001", -9223372036854775808ull, 16, 0, ERANGE},
329 {"-33d3d8307b214009", -9223372036854775808ull, 17, 0, 0},
330 {"-33d3d8307b21400a", -9223372036854775808ull, 17, 0, ERANGE},
331 {"-16agh595df825fa8", -9223372036854775808ull, 18, 0, 0},
332 {"-16agh595df825fa9", -9223372036854775808ull, 18, 0, ERANGE},
333 {"-ba643dci0ffeehi", -9223372036854775808ull, 19, 0, 0},
334 {"-ba643dci0ffeei0", -9223372036854775808ull, 19, 0, ERANGE},
335 {"-5cbfjia3fh26ja8", -9223372036854775808ull, 20, 0, 0},
336 {"-5cbfjia3fh26ja9", -9223372036854775808ull, 20, 0, ERANGE},
337 {"-2heiciiie82dh98", -9223372036854775808ull, 21, 0, 0},
338 {"-2heiciiie82dh99", -9223372036854775808ull, 21, 0, ERANGE},
339 {"-1adaibb21dckfa8", -9223372036854775808ull, 22, 0, 0},
340 {"-1adaibb21dckfa9", -9223372036854775808ull, 22, 0, ERANGE},
341 {"-i6k448cf4192c3", -9223372036854775808ull, 23, 0, 0},
342 {"-i6k448cf4192c4", -9223372036854775808ull, 23, 0, ERANGE},
343 {"-acd772jnc9l0l8", -9223372036854775808ull, 24, 0, 0},
344 {"-acd772jnc9l0l9", -9223372036854775808ull, 24, 0, ERANGE},
345 {"-64ie1focnn5g78", -9223372036854775808ull, 25, 0, 0},
346 {"-64ie1focnn5g79", -9223372036854775808ull, 25, 0, ERANGE},
347 {"-3igoecjbmca688", -9223372036854775808ull, 26, 0, 0},
348 {"-3igoecjbmca689", -9223372036854775808ull, 26, 0, ERANGE},
349 {"-27c48l5b37oaoq", -9223372036854775808ull, 27, 0, 0},
350 {"-27c48l5b37oap0", -9223372036854775808ull, 27, 0, ERANGE},
351 {"-1bk39f3ah3dmq8", -9223372036854775808ull, 28, 0, 0},
352 {"-1bk39f3ah3dmq9", -9223372036854775808ull, 28, 0, ERANGE},
353 {"-q1se8f0m04isc", -9223372036854775808ull, 29, 0, 0},
354 {"-q1se8f0m04isd", -9223372036854775808ull, 29, 0, ERANGE},
355 {"-hajppbc1fc208", -9223372036854775808ull, 30, 0, 0},
356 {"-hajppbc1fc209", -9223372036854775808ull, 30, 0, ERANGE},
357 {"-bm03i95hia438", -9223372036854775808ull, 31, 0, 0},
358 {"-bm03i95hia439", -9223372036854775808ull, 31, 0, ERANGE},
359 {"-8000000000000", -9223372036854775808ull, 32, 0, 0},
360 {"-8000000000001", -9223372036854775808ull, 32, 0, ERANGE},
361 {"-5hg4ck9jd4u38", -9223372036854775808ull, 33, 0, 0},
362 {"-5hg4ck9jd4u39", -9223372036854775808ull, 33, 0, ERANGE},
363 {"-3tdtk1v8j6tpq", -9223372036854775808ull, 34, 0, 0},
364 {"-3tdtk1v8j6tpr", -9223372036854775808ull, 34, 0, ERANGE},
365 {"-2pijmikexrxp8", -9223372036854775808ull, 35, 0, 0},
366 {"-2pijmikexrxp9", -9223372036854775808ull, 35, 0, ERANGE},
367 {"-1y2p0ij32e8e8", -9223372036854775808ull, 36, 0, 0},
368 {"-1y2p0ij32e8e9", -9223372036854775808ull, 36, 0, ERANGE},
369 #endif
370 {NULL, 0, 0, 0, 0},
371
372 /* Then unsigned. */
373 {" 0", 0, 0, 0, 0},
374 {"0xffffffffg", 0xffffffff, 0, 'g', 0},
375 #if LONG_MAX == 0x7fffffff
376 {"-0xfedcba98", 0x01234568, 0, 0, 0},
377 {"0xf1f2f3f4f5", 0xffffffff, 0, 0, ERANGE},
378 {"-0x123456789", 0xffffffff, 0, 0, ERANGE},
379
380 {"11111111111111111111111111111111", 0xffffffff, 2, 0, 0},
381 {"100000000000000000000000000000000", 0xffffffff, 2, 0, ERANGE},
382 {"102002022201221111210", 0xffffffff, 3, 0, 0},
383 {"102002022201221111211", 0xffffffff, 3, 0, ERANGE},
384 {"3333333333333333", 0xffffffff, 4, 0, 0},
385 {"10000000000000000", 0xffffffff, 4, 0, ERANGE},
386 {"32244002423140", 0xffffffff, 5, 0, 0},
387 {"32244002423141", 0xffffffff, 5, 0, ERANGE},
388 {"1550104015503", 0xffffffff, 6, 0, 0},
389 {"1550104015504", 0xffffffff, 6, 0, ERANGE},
390 {"211301422353", 0xffffffff, 7, 0, 0},
391 {"211301422354", 0xffffffff, 7, 0, ERANGE},
392 {"37777777777", 0xffffffff, 8, 0, 0},
393 {"40000000000", 0xffffffff, 8, 0, ERANGE},
394 {"12068657453", 0xffffffff, 9, 0, 0},
395 {"12068657454", 0xffffffff, 9, 0, ERANGE},
396 {"4294967295", 0xffffffff, 10, 0, 0},
397 {"4294967296", 0xffffffff, 10, 0, ERANGE},
398 {"1904440553", 0xffffffff, 11, 0, 0},
399 {"1904440554", 0xffffffff, 11, 0, ERANGE},
400 {"9ba461593", 0xffffffff, 12, 0, 0},
401 {"9ba461594", 0xffffffff, 12, 0, ERANGE},
402 {"535a79888", 0xffffffff, 13, 0, 0},
403 {"535a79889", 0xffffffff, 13, 0, ERANGE},
404 {"2ca5b7463", 0xffffffff, 14, 0, 0},
405 {"2ca5b7464", 0xffffffff, 14, 0, ERANGE},
406 {"1a20dcd80", 0xffffffff, 15, 0, 0},
407 {"1a20dcd81", 0xffffffff, 15, 0, ERANGE},
408 {"ffffffff", 0xffffffff, 16, 0, 0},
409 {"100000000", 0xffffffff, 16, 0, ERANGE},
410 {"a7ffda90", 0xffffffff, 17, 0, 0},
411 {"a7ffda91", 0xffffffff, 17, 0, ERANGE},
412 {"704he7g3", 0xffffffff, 18, 0, 0},
413 {"704he7g4", 0xffffffff, 18, 0, ERANGE},
414 {"4f5aff65", 0xffffffff, 19, 0, 0},
415 {"4f5aff66", 0xffffffff, 19, 0, ERANGE},
416 {"3723ai4f", 0xffffffff, 20, 0, 0},
417 {"3723ai4g", 0xffffffff, 20, 0, ERANGE},
418 {"281d55i3", 0xffffffff, 21, 0, 0},
419 {"281d55i4", 0xffffffff, 21, 0, ERANGE},
420 {"1fj8b183", 0xffffffff, 22, 0, 0},
421 {"1fj8b184", 0xffffffff, 22, 0, ERANGE},
422 {"1606k7ib", 0xffffffff, 23, 0, 0},
423 {"1606k7ic", 0xffffffff, 23, 0, ERANGE},
424 {"mb994af", 0xffffffff, 24, 0, 0},
425 {"mb994ag", 0xffffffff, 24, 0, ERANGE},
426 {"hek2mgk", 0xffffffff, 25, 0, 0},
427 {"hek2mgl", 0xffffffff, 25, 0, ERANGE},
428 {"dnchbnl", 0xffffffff, 26, 0, 0},
429 {"dnchbnm", 0xffffffff, 26, 0, ERANGE},
430 {"b28jpdl", 0xffffffff, 27, 0, 0},
431 {"b28jpdm", 0xffffffff, 27, 0, ERANGE},
432 {"8pfgih3", 0xffffffff, 28, 0, 0},
433 {"8pfgih4", 0xffffffff, 28, 0, ERANGE},
434 {"76beigf", 0xffffffff, 29, 0, 0},
435 {"76beigg", 0xffffffff, 29, 0, ERANGE},
436 {"5qmcpqf", 0xffffffff, 30, 0, 0},
437 {"5qmcpqg", 0xffffffff, 30, 0, ERANGE},
438 {"4q0jto3", 0xffffffff, 31, 0, 0},
439 {"4q0jto4", 0xffffffff, 31, 0, ERANGE},
440 {"3vvvvvv", 0xffffffff, 32, 0, 0},
441 {"4000000", 0xffffffff, 32, 0, ERANGE},
442 {"3aokq93", 0xffffffff, 33, 0, 0},
443 {"3aokq94", 0xffffffff, 33, 0, ERANGE},
444 {"2qhxjlh", 0xffffffff, 34, 0, 0},
445 {"2qhxjli", 0xffffffff, 34, 0, ERANGE},
446 {"2br45qa", 0xffffffff, 35, 0, 0},
447 {"2br45qb", 0xffffffff, 35, 0, ERANGE},
448 {"1z141z3", 0xffffffff, 36, 0, 0},
449 {"1z141z4", 0xffffffff, 36, 0, ERANGE},
450 #else
451 {"0xffffffffffffffffg", 0xffffffffffffffff, 0, 'g', 0},
452 {"-0xfedcba987654321", 0xf0123456789abcdf, 0, 0, 0},
453 {"0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffff, 0, 0, ERANGE},
454 {"-0x123456789abcdef01", 0xffffffffffffffff, 0, 0, ERANGE},
455
456 {"1111111111111111111111111111111111111111111111111111111111111111",
457 0xffffffffffffffff, 2, 0, 0},
458 {"10000000000000000000000000000000000000000000000000000000000000000",
459 0xffffffffffffffff, 2, 0, ERANGE},
460 {"11112220022122120101211020120210210211220",
461 0xffffffffffffffff, 3, 0, 0},
462 {"11112220022122120101211020120210210211221",
463 0xffffffffffffffff, 3, 0, ERANGE},
464 {"33333333333333333333333333333333", 0xffffffffffffffff, 4, 0, 0},
465 {"100000000000000000000000000000000", 0xffffffffffffffff, 4, 0, ERANGE},
466 {"2214220303114400424121122430", 0xffffffffffffffff, 5, 0, 0},
467 {"2214220303114400424121122431", 0xffffffffffffffff, 5, 0, ERANGE},
468 {"3520522010102100444244423", 0xffffffffffffffff, 6, 0, 0},
469 {"3520522010102100444244424", 0xffffffffffffffff, 6, 0, ERANGE},
470 {"45012021522523134134601", 0xffffffffffffffff, 7, 0, 0},
471 {"45012021522523134134602", 0xffffffffffffffff, 7, 0, ERANGE},
472 {"1777777777777777777777", 0xffffffffffffffff, 8, 0, 0},
473 {"2000000000000000000000", 0xffffffffffffffff, 8, 0, ERANGE},
474 {"145808576354216723756", 0xffffffffffffffff, 9, 0, 0},
475 {"145808576354216723757", 0xffffffffffffffff, 9, 0, ERANGE},
476 {"18446744073709551615", 0xffffffffffffffff, 10, 0, 0},
477 {"18446744073709551616", 0xffffffffffffffff, 10, 0, ERANGE},
478 {"335500516a429071284", 0xffffffffffffffff, 11, 0, 0},
479 {"335500516a429071285", 0xffffffffffffffff, 11, 0, ERANGE},
480 {"839365134a2a240713", 0xffffffffffffffff, 12, 0, 0},
481 {"839365134a2a240714", 0xffffffffffffffff, 12, 0, ERANGE},
482 {"219505a9511a867b72", 0xffffffffffffffff, 13, 0, 0},
483 {"219505a9511a867b73", 0xffffffffffffffff, 13, 0, ERANGE},
484 {"8681049adb03db171", 0xffffffffffffffff, 14, 0, 0},
485 {"8681049adb03db172", 0xffffffffffffffff, 14, 0, ERANGE},
486 {"2c1d56b648c6cd110", 0xffffffffffffffff, 15, 0, 0},
487 {"2c1d56b648c6cd111", 0xffffffffffffffff, 15, 0, ERANGE},
488 {"ffffffffffffffff", 0xffffffffffffffff, 16, 0, 0},
489 {"10000000000000000", 0xffffffffffffffff, 16, 0, ERANGE},
490 {"67979g60f5428010", 0xffffffffffffffff, 17, 0, 0},
491 {"67979g60f5428011", 0xffffffffffffffff, 17, 0, ERANGE},
492 {"2d3fgb0b9cg4bd2f", 0xffffffffffffffff, 18, 0, 0},
493 {"2d3fgb0b9cg4bd2g", 0xffffffffffffffff, 18, 0, ERANGE},
494 {"141c8786h1ccaagg", 0xffffffffffffffff, 19, 0, 0},
495 {"141c8786h1ccaagh", 0xffffffffffffffff, 19, 0, ERANGE},
496 {"b53bjh07be4dj0f", 0xffffffffffffffff, 20, 0, 0},
497 {"b53bjh07be4dj0g", 0xffffffffffffffff, 20, 0, ERANGE},
498 {"5e8g4ggg7g56dif", 0xffffffffffffffff, 21, 0, 0},
499 {"5e8g4ggg7g56dig", 0xffffffffffffffff, 21, 0, ERANGE},
500 {"2l4lf104353j8kf", 0xffffffffffffffff, 22, 0, 0},
501 {"2l4lf104353j8kg", 0xffffffffffffffff, 22, 0, ERANGE},
502 {"1ddh88h2782i515", 0xffffffffffffffff, 23, 0, 0},
503 {"1ddh88h2782i516", 0xffffffffffffffff, 23, 0, ERANGE},
504 {"l12ee5fn0ji1if", 0xffffffffffffffff, 24, 0, 0},
505 {"l12ee5fn0ji1ig", 0xffffffffffffffff, 24, 0, ERANGE},
506 {"c9c336o0mlb7ef", 0xffffffffffffffff, 25, 0, 0},
507 {"c9c336o0mlb7eg", 0xffffffffffffffff, 25, 0, ERANGE},
508 {"7b7n2pcniokcgf", 0xffffffffffffffff, 26, 0, 0},
509 {"7b7n2pcniokcgg", 0xffffffffffffffff, 26, 0, ERANGE},
510 {"4eo8hfam6fllmo", 0xffffffffffffffff, 27, 0, 0},
511 {"4eo8hfam6fllmp", 0xffffffffffffffff, 27, 0, ERANGE},
512 {"2nc6j26l66rhof", 0xffffffffffffffff, 28, 0, 0},
513 {"2nc6j26l66rhog", 0xffffffffffffffff, 28, 0, ERANGE},
514 {"1n3rsh11f098rn", 0xffffffffffffffff, 29, 0, 0},
515 {"1n3rsh11f098ro", 0xffffffffffffffff, 29, 0, ERANGE},
516 {"14l9lkmo30o40f", 0xffffffffffffffff, 30, 0, 0},
517 {"14l9lkmo30o40g", 0xffffffffffffffff, 30, 0, ERANGE},
518 {"nd075ib45k86f", 0xffffffffffffffff, 31, 0, 0},
519 {"nd075ib45k86g", 0xffffffffffffffff, 31, 0, ERANGE},
520 {"fvvvvvvvvvvvv", 0xffffffffffffffff, 32, 0, 0},
521 {"g000000000000", 0xffffffffffffffff, 32, 0, ERANGE},
522 {"b1w8p7j5q9r6f", 0xffffffffffffffff, 33, 0, 0},
523 {"b1w8p7j5q9r6g", 0xffffffffffffffff, 33, 0, ERANGE},
524 {"7orp63sh4dphh", 0xffffffffffffffff, 34, 0, 0},
525 {"7orp63sh4dphi", 0xffffffffffffffff, 34, 0, ERANGE},
526 {"5g24a25twkwff", 0xffffffffffffffff, 35, 0, 0},
527 {"5g24a25twkwfg", 0xffffffffffffffff, 35, 0, ERANGE},
528 {"3w5e11264sgsf", 0xffffffffffffffff, 36, 0, 0},
529 {"3w5e11264sgsg", 0xffffffffffffffff, 36, 0, ERANGE},
530 #endif
531 {NULL, 0, 0, 0, 0},
532 };
533
534 /* Prototypes for local functions. */
535 static void expand (char *dst, int c);
536
537 static int
do_test(void)538 do_test (void)
539 {
540 const struct ltest *lt;
541 char *ep;
542 int status = 0;
543 int save_errno;
544
545 for (lt = tests; lt->str != NULL; ++lt)
546 {
547 long int l;
548
549 errno = 0;
550 l = strtol (lt->str, &ep, lt->base);
551 save_errno = errno;
552 printf ("strtol(\"%s\", , %d) test %u",
553 lt->str, lt->base, (unsigned int) (lt - tests));
554 if (l == (long int) lt->expect && *ep == lt->left
555 && save_errno == lt->err)
556 puts("\tOK");
557 else
558 {
559 puts("\tBAD");
560 if (l != (long int) lt->expect)
561 printf(" returns %ld, expected %ld\n",
562 l, (long int) lt->expect);
563 if (lt->left != *ep)
564 {
565 char exp1[5], exp2[5];
566 expand (exp1, *ep);
567 expand (exp2, lt->left);
568 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
569 }
570 if (save_errno != lt->err)
571 printf (" errno %d (%s) instead of %d (%s)\n",
572 save_errno, strerror (save_errno),
573 lt->err, strerror (lt->err));
574 status = 1;
575 }
576 }
577
578 for (++lt; lt->str != NULL; lt++)
579 {
580 unsigned long int ul;
581
582 errno = 0;
583 ul = strtoul (lt->str, &ep, lt->base);
584 save_errno = errno;
585 printf ("strtoul(\"%s\", , %d) test %u",
586 lt->str, lt->base, (unsigned int) (lt - tests));
587 if (ul == lt->expect && *ep == lt->left && save_errno == lt->err)
588 puts("\tOK");
589 else
590 {
591 puts ("\tBAD");
592 if (ul != lt->expect)
593 printf (" returns %lu, expected %lu\n",
594 ul, lt->expect);
595 if (lt->left != *ep)
596 {
597 char exp1[5], exp2[5];
598 expand (exp1, *ep);
599 expand (exp2, lt->left);
600 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
601 }
602 if (save_errno != lt->err)
603 printf (" errno %d (%s) instead of %d (%s)\n",
604 save_errno, strerror (save_errno),
605 lt->err, strerror (lt->err));
606 status = 1;
607 }
608 }
609
610 return status ? EXIT_FAILURE : EXIT_SUCCESS;
611 }
612
613 static void
expand(char * dst,int c)614 expand (char *dst, int c)
615 {
616 if (isprint (c))
617 {
618 dst[0] = c;
619 dst[1] = '\0';
620 }
621 else
622 (void) sprintf (dst, "%#.3o", (unsigned int) c);
623 }
624
625 #define TEST_FUNCTION do_test ()
626 #include "../test-skeleton.c"
627