t_strtoi.c revision 1.5 1 1.5 kre /* $NetBSD: t_strtoi.c,v 1.5 2024/07/24 09:26:06 kre Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Jukka Ruohonen.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos /*
33 1.2 kamil * Created by Kamil Rytarowski, based on ID:
34 1.1 christos * NetBSD: t_strtol.c,v 1.5 2011/06/14 02:45:58 jruoho Exp
35 1.1 christos */
36 1.1 christos
37 1.1 christos #include <sys/cdefs.h>
38 1.5 kre __RCSID("$NetBSD: t_strtoi.c,v 1.5 2024/07/24 09:26:06 kre Exp $");
39 1.1 christos
40 1.1 christos #include <atf-c.h>
41 1.1 christos #include <errno.h>
42 1.1 christos #include <inttypes.h>
43 1.1 christos #include <stdlib.h>
44 1.1 christos #include <string.h>
45 1.1 christos #include <limits.h>
46 1.1 christos
47 1.1 christos struct test {
48 1.1 christos const char *str;
49 1.1 christos intmax_t res;
50 1.1 christos int base;
51 1.1 christos const char *end;
52 1.1 christos intmax_t lo;
53 1.1 christos intmax_t hi;
54 1.1 christos int rstatus;
55 1.1 christos };
56 1.1 christos
57 1.1 christos static void check(struct test *, intmax_t, char *, int);
58 1.1 christos
59 1.1 christos static void
60 1.1 christos check(struct test *t, intmax_t rv, char *end, int rstatus)
61 1.1 christos {
62 1.1 christos
63 1.1 christos if (rv != t->res)
64 1.4 kre atf_tc_fail_nonfatal("strtoi(\"%s\", &end, %d, %jd, %jd, "
65 1.4 kre "&rstatus) failed (rv = %jd)", t->str, t->base,
66 1.4 kre t->lo, t->hi, rv);
67 1.4 kre
68 1.4 kre if (rstatus != t->rstatus) {
69 1.4 kre char *emsg;
70 1.4 kre
71 1.4 kre if (rstatus != 0) {
72 1.4 kre emsg = strerror(rstatus);
73 1.4 kre if (emsg != NULL) {
74 1.4 kre emsg = strdup(emsg);
75 1.4 kre if (emsg == NULL) {
76 1.4 kre atf_tc_fail("Out of Memory");
77 1.4 kre return;
78 1.4 kre }
79 1.4 kre }
80 1.4 kre } else
81 1.4 kre emsg = NULL;
82 1.4 kre
83 1.4 kre atf_tc_fail_nonfatal("strtoi(\"%s\", &end, %d, %jd, %jd, &rstatus)"
84 1.4 kre " failed (rstatus: %d %s%s%sexpected %d%s%s%s)",
85 1.4 kre t->str, t->base, t->lo, t->hi, rstatus, rstatus ? "('" : "",
86 1.4 kre emsg != NULL ? emsg : "", rstatus ? "') " : "", t->rstatus,
87 1.4 kre t->rstatus ? " ('" : "", t->rstatus ? strerror(t->rstatus)
88 1.4 kre : "", t->rstatus ? "')" : "");
89 1.1 christos
90 1.4 kre free(emsg);
91 1.4 kre }
92 1.1 christos
93 1.1 christos if ((t->end != NULL && strcmp(t->end, end) != 0) ||
94 1.1 christos (t->end == NULL && *end != '\0'))
95 1.1 christos atf_tc_fail_nonfatal("invalid end pointer ('%s') from "
96 1.4 kre "strtoi(\"%s\", &end, %d, %jd, %jd, &rstatus), "
97 1.4 kre "expected '%s'", end, t->str, t->base, t->lo, t->hi,
98 1.4 kre t->end != NULL ? t->end : "\\0");
99 1.4 kre }
100 1.4 kre
101 1.4 kre static void
102 1.4 kre check_errno(int e)
103 1.4 kre {
104 1.4 kre if (e != 0)
105 1.4 kre atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
106 1.4 kre e, strerror(e));
107 1.1 christos }
108 1.1 christos
109 1.1 christos ATF_TC(strtoi_base);
110 1.1 christos ATF_TC_HEAD(strtoi_base, tc)
111 1.1 christos {
112 1.1 christos atf_tc_set_md_var(tc, "descr", "Test strtoi(3) with different bases");
113 1.1 christos }
114 1.1 christos
115 1.1 christos ATF_TC_BODY(strtoi_base, tc)
116 1.1 christos {
117 1.1 christos struct test t[] = {
118 1.1 christos { "123456789", 123456789, 0, NULL,
119 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
120 1.1 christos { "111010110111100110100010101",123456789, 2, NULL,
121 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
122 1.1 christos { "22121022020212200", 123456789, 3, NULL,
123 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
124 1.1 christos { "13112330310111", 123456789, 4, NULL,
125 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
126 1.1 christos { "223101104124", 123456789, 5, NULL,
127 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
128 1.1 christos { "20130035113", 123456789, 6, NULL,
129 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
130 1.1 christos { "3026236221", 123456789, 7, NULL,
131 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
132 1.1 christos { "726746425", 123456789, 8, NULL,
133 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
134 1.1 christos { "277266780", 123456789, 9, NULL,
135 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
136 1.1 christos { "123456789", 123456789, 10, NULL,
137 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
138 1.1 christos { "63762A05", 123456789, 11, NULL,
139 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
140 1.1 christos { "35418A99", 123456789, 12, NULL,
141 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
142 1.1 christos { "1C767471", 123456789, 13, NULL,
143 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
144 1.1 christos { "12579781", 123456789, 14, NULL,
145 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
146 1.1 christos { "AC89BC9", 123456789, 15, NULL,
147 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
148 1.1 christos { "75BCD15", 123456789, 16, NULL,
149 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
150 1.1 christos { "1234567", 342391, 8, NULL,
151 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
152 1.1 christos { "01234567", 342391, 0, NULL,
153 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
154 1.1 christos { "0123456789", 123456789, 10, NULL,
155 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
156 1.1 christos { "0x75bcd15", 123456789, 0, NULL,
157 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
158 1.1 christos };
159 1.4 kre struct test f[] = {
160 1.4 kre { "1", 0, 1, "1",
161 1.4 kre INTMAX_MIN, INTMAX_MAX, EINVAL },
162 1.4 kre { "2", 0, -1, "2",
163 1.4 kre INTMAX_MIN, INTMAX_MAX, EINVAL },
164 1.4 kre { "3", 0, 37, "3",
165 1.4 kre INTMAX_MIN, INTMAX_MAX, EINVAL },
166 1.4 kre { "4", 0, -1, "4",
167 1.4 kre INTMAX_MIN, INTMAX_MAX, EINVAL },
168 1.4 kre { "0x", 0, 0, "x",
169 1.4 kre INTMAX_MIN, INTMAX_MAX, ENOTSUP },
170 1.4 kre };
171 1.1 christos
172 1.1 christos intmax_t rv;
173 1.1 christos char *end;
174 1.1 christos int e;
175 1.1 christos size_t i;
176 1.1 christos
177 1.1 christos for (i = 0; i < __arraycount(t); i++) {
178 1.1 christos
179 1.1 christos errno = 0;
180 1.1 christos rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
181 1.1 christos
182 1.4 kre check_errno(errno);
183 1.1 christos
184 1.1 christos check(&t[i], rv, end, e);
185 1.1 christos }
186 1.4 kre
187 1.4 kre for (i = 0; i < __arraycount(f); i++) {
188 1.4 kre
189 1.4 kre end = NULL;
190 1.4 kre errno = 0;
191 1.4 kre e = -99;
192 1.4 kre
193 1.4 kre rv = strtoi(f[i].str, &end, f[i].base, f[i].lo, f[i].hi, &e);
194 1.4 kre
195 1.4 kre check_errno(errno);
196 1.4 kre
197 1.4 kre check(&f[i], rv, end, e);
198 1.4 kre }
199 1.1 christos }
200 1.1 christos
201 1.1 christos ATF_TC(strtoi_case);
202 1.1 christos ATF_TC_HEAD(strtoi_case, tc)
203 1.1 christos {
204 1.1 christos atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtoi(3)");
205 1.1 christos }
206 1.1 christos
207 1.1 christos ATF_TC_BODY(strtoi_case, tc)
208 1.1 christos {
209 1.1 christos struct test t[] = {
210 1.1 christos { "abcd", 0xabcd, 16, NULL,
211 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
212 1.1 christos { " dcba", 0xdcba, 16, NULL,
213 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
214 1.1 christos { "abcd dcba", 0xabcd, 16, " dcba",
215 1.1 christos INTMAX_MIN, INTMAX_MAX, ENOTSUP },
216 1.1 christos { "abc0x123", 0xabc0, 16, "x123",
217 1.1 christos INTMAX_MIN, INTMAX_MAX, ENOTSUP },
218 1.1 christos { "abcd\0x123", 0xabcd, 16, "\0x123",
219 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
220 1.1 christos { "ABCD", 0xabcd, 16, NULL,
221 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
222 1.1 christos { "aBcD", 0xabcd, 16, NULL,
223 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
224 1.1 christos { "0xABCD", 0xabcd, 16, NULL,
225 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
226 1.1 christos { "0xABCDX", 0xabcd, 16, "X",
227 1.1 christos INTMAX_MIN, INTMAX_MAX, ENOTSUP},
228 1.1 christos };
229 1.1 christos
230 1.1 christos intmax_t rv;
231 1.1 christos char *end;
232 1.1 christos int e;
233 1.1 christos size_t i;
234 1.1 christos
235 1.1 christos for (i = 0; i < __arraycount(t); i++) {
236 1.1 christos
237 1.1 christos errno = 0;
238 1.1 christos rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
239 1.1 christos
240 1.4 kre check_errno(errno);
241 1.1 christos
242 1.1 christos check(&t[i], rv, end, e);
243 1.1 christos }
244 1.1 christos }
245 1.1 christos
246 1.1 christos ATF_TC(strtoi_range);
247 1.1 christos ATF_TC_HEAD(strtoi_range, tc)
248 1.1 christos {
249 1.1 christos atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtoi(3)");
250 1.1 christos }
251 1.1 christos
252 1.1 christos ATF_TC_BODY(strtoi_range, tc)
253 1.1 christos {
254 1.1 christos struct test t[] = {
255 1.1 christos #if INTMAX_MAX == 0x7fffffffffffffff
256 1.1 christos { "1000000000000000000000", INTMAX_MAX, 8, NULL,
257 1.1 christos INTMAX_MIN, INTMAX_MAX, ERANGE },
258 1.1 christos { "9223372036854775808", INTMAX_MAX, 10, NULL,
259 1.1 christos INTMAX_MIN, INTMAX_MAX, ERANGE },
260 1.1 christos { "8000000000000000", INTMAX_MAX, 16, NULL,
261 1.1 christos INTMAX_MIN, INTMAX_MAX, ERANGE },
262 1.1 christos #else
263 1.1 christos #error extend this test to your platform!
264 1.1 christos #endif
265 1.4 kre { "10", 1, 10, NULL,
266 1.4 kre -1, 1, ERANGE },
267 1.4 kre { "10", 11, 10, NULL,
268 1.4 kre 11, 20, ERANGE },
269 1.4 kre { "7", 7, 0, NULL,
270 1.4 kre 7, 7, 0 },
271 1.4 kre { "6", 7, 0, NULL,
272 1.4 kre 7, 7, ERANGE },
273 1.4 kre { "8", 7, 0, NULL,
274 1.4 kre 7, 7, ERANGE },
275 1.4 kre { "7x", 7, 0, "x",
276 1.4 kre 7, 7, ENOTSUP },
277 1.4 kre { "8x", 7, 0, "x",
278 1.4 kre 7, 7, ERANGE },
279 1.4 kre { "Z", 11, 10, "Z",
280 1.4 kre 11, 20, ECANCELED },
281 1.1 christos };
282 1.1 christos
283 1.1 christos intmax_t rv;
284 1.1 christos char *end;
285 1.1 christos int e;
286 1.1 christos size_t i;
287 1.1 christos
288 1.1 christos for (i = 0; i < __arraycount(t); i++) {
289 1.1 christos
290 1.1 christos errno = 0;
291 1.1 christos rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
292 1.1 christos
293 1.1 christos if (errno != 0)
294 1.4 kre atf_tc_fail("Range test %zd set errno=%d", i, errno);
295 1.4 kre check_errno(errno);
296 1.1 christos
297 1.1 christos check(&t[i], rv, end, e);
298 1.1 christos }
299 1.1 christos }
300 1.1 christos
301 1.3 christos ATF_TC(strtoi_range_trail);
302 1.3 christos ATF_TC_HEAD(strtoi_range_trail, tc)
303 1.3 christos {
304 1.3 christos atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtoi(3) "
305 1.3 christos "with trailing characters");
306 1.3 christos }
307 1.3 christos
308 1.3 christos ATF_TC_BODY(strtoi_range_trail, tc)
309 1.3 christos {
310 1.3 christos struct test t[] = {
311 1.4 kre { "11x", 9, 10, "x", 0, 9, ERANGE },
312 1.4 kre { " -3y", -2, 10, "y", -2, 1, ERANGE },
313 1.4 kre { "11111z", 9, 10, "z", 0, 9, ERANGE },
314 1.4 kre { "+0xAq", 9, 16, "q", 0, 9, ERANGE },
315 1.4 kre { "-0xBAr", 0, 16, "r", 0, 9, ERANGE },
316 1.3 christos };
317 1.3 christos
318 1.3 christos intmax_t rv;
319 1.3 christos char *end;
320 1.3 christos int e;
321 1.3 christos size_t i;
322 1.3 christos
323 1.3 christos for (i = 0; i < __arraycount(t); i++) {
324 1.3 christos
325 1.3 christos errno = 0;
326 1.3 christos rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
327 1.3 christos
328 1.4 kre check_errno(errno);
329 1.3 christos
330 1.3 christos check(&t[i], rv, end, e);
331 1.3 christos }
332 1.3 christos }
333 1.3 christos
334 1.1 christos ATF_TC(strtoi_signed);
335 1.1 christos ATF_TC_HEAD(strtoi_signed, tc)
336 1.1 christos {
337 1.1 christos atf_tc_set_md_var(tc, "descr", "A basic test of strtoi(3)");
338 1.1 christos }
339 1.1 christos
340 1.1 christos ATF_TC_BODY(strtoi_signed, tc)
341 1.1 christos {
342 1.1 christos struct test t[] = {
343 1.1 christos { "1", 1, 0, NULL,
344 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
345 1.1 christos { " 2", 2, 0, NULL,
346 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
347 1.1 christos { " 3", 3, 0, NULL,
348 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
349 1.1 christos { " -3", -3, 0, NULL,
350 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
351 1.1 christos { "--1", 0, 0, "--1",
352 1.1 christos INTMAX_MIN, INTMAX_MAX, ECANCELED },
353 1.1 christos { "+-2", 0, 0, "+-2",
354 1.1 christos INTMAX_MIN, INTMAX_MAX, ECANCELED },
355 1.1 christos { "++3", 0, 0, "++3",
356 1.1 christos INTMAX_MIN, INTMAX_MAX, ECANCELED },
357 1.1 christos { "+9", 9, 0, NULL,
358 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
359 1.1 christos { "+123", 123, 0, NULL,
360 1.1 christos INTMAX_MIN, INTMAX_MAX, 0 },
361 1.1 christos { "-1 3", -1, 0, " 3",
362 1.1 christos INTMAX_MIN, INTMAX_MAX, ENOTSUP },
363 1.1 christos { "-1.3", -1, 0, ".3",
364 1.1 christos INTMAX_MIN, INTMAX_MAX, ENOTSUP },
365 1.1 christos { "- 3", 0, 0, "- 3",
366 1.1 christos INTMAX_MIN, INTMAX_MAX, ECANCELED },
367 1.1 christos { "+33.", 33, 0, ".",
368 1.1 christos INTMAX_MIN, INTMAX_MAX, ENOTSUP },
369 1.1 christos { "30x0", 30, 0, "x0",
370 1.1 christos INTMAX_MIN, INTMAX_MAX, ENOTSUP },
371 1.1 christos };
372 1.1 christos
373 1.1 christos intmax_t rv;
374 1.1 christos char *end;
375 1.1 christos int e;
376 1.1 christos size_t i;
377 1.1 christos
378 1.1 christos for (i = 0; i < __arraycount(t); i++) {
379 1.1 christos
380 1.1 christos errno = 0;
381 1.1 christos rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
382 1.1 christos
383 1.4 kre check_errno(errno);
384 1.1 christos
385 1.1 christos check(&t[i], rv, end, e);
386 1.1 christos }
387 1.1 christos }
388 1.1 christos
389 1.1 christos ATF_TP_ADD_TCS(tp)
390 1.1 christos {
391 1.1 christos
392 1.1 christos ATF_TP_ADD_TC(tp, strtoi_base);
393 1.1 christos ATF_TP_ADD_TC(tp, strtoi_case);
394 1.1 christos ATF_TP_ADD_TC(tp, strtoi_range);
395 1.3 christos ATF_TP_ADD_TC(tp, strtoi_range_trail);
396 1.1 christos ATF_TP_ADD_TC(tp, strtoi_signed);
397 1.1 christos
398 1.1 christos return atf_no_error();
399 1.1 christos }
400