t_strtol.c revision 1.8 1 /* $NetBSD: t_strtol.c,v 1.8 2024/07/22 16:41:05 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: t_strtol.c,v 1.8 2024/07/22 16:41:05 christos Exp $");
34
35 #include <atf-c.h>
36 #include <errno.h>
37 #include <inttypes.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <limits.h>
41
42 struct test {
43 const char *str;
44 int64_t res;
45 int base;
46 const char *end;
47 };
48
49 static void
50 check(struct test *t, long int li, long long int lli, intmax_t ji, char *end)
51 {
52
53 if (li != -1 && li != t->res)
54 atf_tc_fail_nonfatal("strtol(%s, &end, %d) failed "
55 "(rv = %ld)", t->str, t->base, li);
56
57 if (lli != -1 && lli != t->res)
58 atf_tc_fail_nonfatal("strtoll(%s, NULL, %d) failed "
59 "(rv = %lld)", t->str, t->base, lli);
60
61 if (ji != -1 && ji != t->res)
62 atf_tc_fail_nonfatal("strtoll(%s, NULL, %d) failed "
63 "(rv = %jd)", t->str, t->base, ji);
64
65 if ((t->end != NULL && strcmp(t->end, end) != 0) ||
66 (t->end == NULL && *end != '\0'))
67 atf_tc_fail_nonfatal("invalid end pointer (%p) from "
68 "strtol(%p, &end, %d)", end, t->str, t->base);
69 }
70
71 ATF_TC(strtol_base);
72 ATF_TC_HEAD(strtol_base, tc)
73 {
74 atf_tc_set_md_var(tc, "descr", "Test strtol(3) with different bases");
75 }
76
77 ATF_TC_BODY(strtol_base, tc)
78 {
79 struct test t[] = {
80 { "123456789", 123456789, 0, NULL },
81 { "111010110111100110100010101", 123456789, 2, NULL },
82 { "22121022020212200", 123456789, 3, NULL },
83 { "13112330310111", 123456789, 4, NULL },
84 { "223101104124", 123456789, 5, NULL },
85 { "20130035113", 123456789, 6, NULL },
86 { "3026236221", 123456789, 7, NULL },
87 { "726746425", 123456789, 8, NULL },
88 { "277266780", 123456789, 9, NULL },
89 { "123456789", 123456789, 10, NULL },
90 { "63762A05", 123456789, 11, NULL },
91 { "35418A99", 123456789, 12, NULL },
92 { "1C767471", 123456789, 13, NULL },
93 { "12579781", 123456789, 14, NULL },
94 { "AC89BC9", 123456789, 15, NULL },
95 { "75BCD15", 123456789, 16, NULL },
96 { "1234567", 342391, 8, NULL },
97 { "01234567", 342391, 0, NULL },
98 { "0123456789", 123456789, 10, NULL },
99 { "0x75bcd15", 123456789, 0, NULL },
100 { " 0xX", 0, 0, "xX" },
101 { " 0xX", 0, 16, "xX" },
102 { " 0XX", 0, 0, "XX" },
103 { " 0XX", 0, 16, "XX" },
104 };
105
106 long long int lli;
107 long int li;
108 intmax_t ji;
109 long long int ulli;
110 long int uli;
111 uintmax_t uji;
112 char *end, *end2;
113 size_t i;
114
115 for (i = 0; i < __arraycount(t); i++) {
116
117 li = strtol(t[i].str, &end, t[i].base);
118 lli = strtoll(t[i].str, NULL, t[i].base);
119 ji = strtoimax(t[i].str, NULL, t[i].base);
120
121 uli = strtoul(t[i].str, &end2, t[i].base);
122 ulli = strtoull(t[i].str, NULL, t[i].base);
123 uji = strtoumax(t[i].str, NULL, t[i].base);
124
125 check(&t[i], li, lli, ji, end);
126
127 if (li != uli)
128 atf_tc_fail_nonfatal("strtoul(%s, NULL, %d) failed "
129 "(rv = %lu)", t[i].str, t[i].base, uli);
130 if (lli != ulli)
131 atf_tc_fail_nonfatal("strtoull(%s, NULL, %d) failed "
132 "(rv = %llu)", t[i].str, t[i].base, ulli);
133 if ((uintmax_t)ji != uji)
134 atf_tc_fail_nonfatal("strtoumax(%s, NULL, %d) failed "
135 "(rv = %ju)", t[i].str, t[i].base, uji);
136 if (end != end2)
137 atf_tc_fail_nonfatal("invalid end pointer ('%p') from "
138 "strtoul(%s, &end, %d)", end2, t[i].str, t[i].base);
139 }
140 }
141
142 ATF_TC(strtol_invbase);
143 ATF_TC_HEAD(strtol_invbase, tc)
144 {
145 atf_tc_set_md_var(tc, "descr", "Test strtol(3) with an invalid base");
146 }
147
148 ATF_TC_BODY(strtol_invbase, tc)
149 {
150 long long int lli;
151 long int li;
152 intmax_t ji;
153 long long int ulli;
154 long int uli;
155 uintmax_t uji;
156 char *end;
157 char boo[] = "boo";
158 const char str[] = "1";
159
160 #define CHECK(r, f, fun) \
161 end = boo; \
162 r = fun(str, &end, -2); \
163 if (strcmp(str, end) != 0) \
164 atf_tc_fail_nonfatal( \
165 "%s(1, boo, -2) failed endptr=%s ", # fun, end); \
166 if (r) \
167 atf_tc_fail_nonfatal( \
168 "%s(1, boo, -2) failed rv=" f, # fun, r); \
169 if (errno != EINVAL) \
170 atf_tc_fail_nonfatal( \
171 "%s(1, boo, -2) failed errno=%d", # fun, errno)
172
173 CHECK(li, "%ld", strtol);
174 CHECK(lli, "%lld", strtoll);
175 CHECK(ji, "%jd", strtoimax);
176 CHECK(uli, "%lu", strtoul);
177 CHECK(ulli, "%llu", strtoull);
178 CHECK(uji, "%ju", strtoumax);
179 }
180
181 ATF_TC(strtol_case);
182 ATF_TC_HEAD(strtol_case, tc)
183 {
184 atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtol(3)");
185 }
186
187 ATF_TC_BODY(strtol_case, tc)
188 {
189 struct test t[] = {
190 { "abcd", 0xabcd, 16, NULL },
191 { " dcba", 0xdcba, 16, NULL },
192 { "abcd dcba", 0xabcd, 16, " dcba" },
193 { "abc0x123", 0xabc0, 16, "x123" },
194 { "abcd\0x123", 0xabcd, 16, "\0x123" },
195 { "ABCD", 0xabcd, 16, NULL },
196 { "aBcD", 0xabcd, 16, NULL },
197 { "0xABCD", 0xabcd, 16, NULL },
198 { "0xABCDX", 0xabcd, 16, "X" },
199 };
200
201 long long int lli;
202 long int li;
203 intmax_t ji;
204 char *end;
205 size_t i;
206
207 for (i = 0; i < __arraycount(t); i++) {
208
209 li = strtol(t[i].str, &end, t[i].base);
210 lli = strtoll(t[i].str, NULL, t[i].base);
211 ji = strtoimax(t[i].str, NULL, t[i].base);
212
213 check(&t[i], li, lli, ji, end);
214 }
215 }
216
217 ATF_TC(strtol_range);
218 ATF_TC_HEAD(strtol_range, tc)
219 {
220 atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtol(3)");
221 }
222
223 ATF_TC_BODY(strtol_range, tc)
224 {
225
226 #if LONG_MAX == 0x7fffffff /* XXX: Is this portable? */
227
228 struct test t[] = {
229 { "20000000000", 2147483647, 8, NULL },
230 { "2147483648", 2147483647, 10, NULL },
231 { "80000000", 2147483647, 16, NULL },
232 };
233 #else
234 struct test t[] = {
235 { "1000000000000000000000", 9223372036854775807, 8, NULL },
236 { "9223372036854775808", 9223372036854775807, 10, NULL },
237 { "8000000000000000", 9223372036854775807, 16, NULL },
238 };
239 #endif
240
241 long int li;
242 char *end;
243 size_t i;
244
245 for (i = 0; i < __arraycount(t); i++) {
246
247 errno = 0;
248 li = strtol(t[i].str, &end, t[i].base);
249
250 if (errno != ERANGE)
251 atf_tc_fail("strtol(3) did not catch ERANGE");
252
253 check(&t[i], li, -1, -1, end);
254 }
255 }
256
257 ATF_TC(strtol_signed);
258 ATF_TC_HEAD(strtol_signed, tc)
259 {
260 atf_tc_set_md_var(tc, "descr", "A basic test of strtol(3)");
261 }
262
263 ATF_TC_BODY(strtol_signed, tc)
264 {
265 struct test t[] = {
266 { "1", 1, 0, NULL },
267 { " 2", 2, 0, NULL },
268 { " 3", 3, 0, NULL },
269 { " -3", -3, 0, NULL },
270 { "--1", 0, 0, "--1" },
271 { "+-2", 0, 0, "+-2" },
272 { "++3", 0, 0, "++3" },
273 { "+9", 9, 0, NULL },
274 { "+123", 123, 0, NULL },
275 { "-1 3", -1, 0, " 3" },
276 { "-1.3", -1, 0, ".3" },
277 { "- 3", 0, 0, "- 3" },
278 { "+33.", 33, 0, "." },
279 { "30x0", 30, 0, "x0" },
280 };
281
282 long long int lli;
283 long int li;
284 intmax_t ji;
285 char *end;
286 size_t i;
287
288 for (i = 0; i < __arraycount(t); i++) {
289
290 li = strtol(t[i].str, &end, t[i].base);
291 lli = strtoll(t[i].str, NULL, t[i].base);
292 ji = strtoimax(t[i].str, NULL, t[i].base);
293
294 check(&t[i], li, lli, ji, end);
295 }
296 }
297
298 ATF_TP_ADD_TCS(tp)
299 {
300
301 ATF_TP_ADD_TC(tp, strtol_invbase);
302 ATF_TP_ADD_TC(tp, strtol_base);
303 ATF_TP_ADD_TC(tp, strtol_case);
304 ATF_TP_ADD_TC(tp, strtol_range);
305 ATF_TP_ADD_TC(tp, strtol_signed);
306
307 return atf_no_error();
308 }
309