Deleted Added
full compact
t_strtoi.c (1.3) t_strtoi.c (1.4)
1/* $NetBSD: t_strtoi.c,v 1.3 2024/01/20 16:52:41 christos Exp $ */
1/* $NetBSD: t_strtoi.c,v 1.4 2024/07/24 08:59:11 kre Exp $ */
2
3/*-
4 * Copyright (c) 2015 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 *

--- 20 unchanged lines hidden (view full) ---

30 */
31
32/*
33 * Created by Kamil Rytarowski, based on ID:
34 * NetBSD: t_strtol.c,v 1.5 2011/06/14 02:45:58 jruoho Exp
35 */
36
37#include <sys/cdefs.h>
2
3/*-
4 * Copyright (c) 2015 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 *

--- 20 unchanged lines hidden (view full) ---

30 */
31
32/*
33 * Created by Kamil Rytarowski, based on ID:
34 * NetBSD: t_strtol.c,v 1.5 2011/06/14 02:45:58 jruoho Exp
35 */
36
37#include <sys/cdefs.h>
38__RCSID("$NetBSD: t_strtoi.c,v 1.3 2024/01/20 16:52:41 christos Exp $");
38__RCSID("$NetBSD: t_strtoi.c,v 1.4 2024/07/24 08:59:11 kre Exp $");
39
40#include <atf-c.h>
41#include <errno.h>
42#include <inttypes.h>
43#include <stdlib.h>
44#include <string.h>
45#include <limits.h>
46

--- 9 unchanged lines hidden (view full) ---

56
57static void check(struct test *, intmax_t, char *, int);
58
59static void
60check(struct test *t, intmax_t rv, char *end, int rstatus)
61{
62
63 if (rv != t->res)
39
40#include <atf-c.h>
41#include <errno.h>
42#include <inttypes.h>
43#include <stdlib.h>
44#include <string.h>
45#include <limits.h>
46

--- 9 unchanged lines hidden (view full) ---

56
57static void check(struct test *, intmax_t, char *, int);
58
59static void
60check(struct test *t, intmax_t rv, char *end, int rstatus)
61{
62
63 if (rv != t->res)
64 atf_tc_fail_nonfatal("strtoi(%s, &end, %d, %jd, %jd, &rstatus)"
65 " failed (rv = %jd)", t->str, t->base, t->lo, t->hi, rv);
64 atf_tc_fail_nonfatal("strtoi(\"%s\", &end, %d, %jd, %jd, "
65 "&rstatus) failed (rv = %jd)", t->str, t->base,
66 t->lo, t->hi, rv);
66
67
67 if (rstatus != t->rstatus)
68 atf_tc_fail_nonfatal("strtoi(%s, &end, %d, %jd, %jd, &rstatus)"
69 " failed (rstatus: %d ('%s'))",
70 t->str, t->base, t->lo, t->hi, rstatus, strerror(rstatus));
68 if (rstatus != t->rstatus) {
69 char *emsg;
71
70
71 if (rstatus != 0) {
72 emsg = strerror(rstatus);
73 if (emsg != NULL) {
74 emsg = strdup(emsg);
75 if (emsg == NULL) {
76 atf_tc_fail("Out of Memory");
77 return;
78 }
79 }
80 } else
81 emsg = NULL;
82
83 atf_tc_fail_nonfatal("strtoi(\"%s\", &end, %d, %jd, %jd, &rstatus)"
84 " failed (rstatus: %d %s%s%sexpected %d%s%s%s)",
85 t->str, t->base, t->lo, t->hi, rstatus, rstatus ? "('" : "",
86 emsg != NULL ? emsg : "", rstatus ? "') " : "", t->rstatus,
87 t->rstatus ? " ('" : "", t->rstatus ? strerror(t->rstatus)
88 : "", t->rstatus ? "')" : "");
89
90 free(emsg);
91 }
92
72 if ((t->end != NULL && strcmp(t->end, end) != 0) ||
73 (t->end == NULL && *end != '\0'))
74 atf_tc_fail_nonfatal("invalid end pointer ('%s') from "
93 if ((t->end != NULL && strcmp(t->end, end) != 0) ||
94 (t->end == NULL && *end != '\0'))
95 atf_tc_fail_nonfatal("invalid end pointer ('%s') from "
75 "strtoi(%s, &end, %d, %jd, %jd, &rstatus)",
76 end, t->str, t->base, t->lo, t->hi);
96 "strtoi(\"%s\", &end, %d, %jd, %jd, &rstatus), "
97 "expected '%s'", end, t->str, t->base, t->lo, t->hi,
98 t->end != NULL ? t->end : "\\0");
77}
78
99}
100
101static void
102check_errno(int e)
103{
104 if (e != 0)
105 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
106 e, strerror(e));
107}
108
79ATF_TC(strtoi_base);
80ATF_TC_HEAD(strtoi_base, tc)
81{
82 atf_tc_set_md_var(tc, "descr", "Test strtoi(3) with different bases");
83}
84
85ATF_TC_BODY(strtoi_base, tc)
86{

--- 34 unchanged lines hidden (view full) ---

121 INTMAX_MIN, INTMAX_MAX, 0 },
122 { "01234567", 342391, 0, NULL,
123 INTMAX_MIN, INTMAX_MAX, 0 },
124 { "0123456789", 123456789, 10, NULL,
125 INTMAX_MIN, INTMAX_MAX, 0 },
126 { "0x75bcd15", 123456789, 0, NULL,
127 INTMAX_MIN, INTMAX_MAX, 0 },
128 };
109ATF_TC(strtoi_base);
110ATF_TC_HEAD(strtoi_base, tc)
111{
112 atf_tc_set_md_var(tc, "descr", "Test strtoi(3) with different bases");
113}
114
115ATF_TC_BODY(strtoi_base, tc)
116{

--- 34 unchanged lines hidden (view full) ---

151 INTMAX_MIN, INTMAX_MAX, 0 },
152 { "01234567", 342391, 0, NULL,
153 INTMAX_MIN, INTMAX_MAX, 0 },
154 { "0123456789", 123456789, 10, NULL,
155 INTMAX_MIN, INTMAX_MAX, 0 },
156 { "0x75bcd15", 123456789, 0, NULL,
157 INTMAX_MIN, INTMAX_MAX, 0 },
158 };
159 struct test f[] = {
160 { "1", 0, 1, "1",
161 INTMAX_MIN, INTMAX_MAX, EINVAL },
162 { "2", 0, -1, "2",
163 INTMAX_MIN, INTMAX_MAX, EINVAL },
164 { "3", 0, 37, "3",
165 INTMAX_MIN, INTMAX_MAX, EINVAL },
166 { "4", 0, -1, "4",
167 INTMAX_MIN, INTMAX_MAX, EINVAL },
168 { "0x", 0, 0, "x",
169 INTMAX_MIN, INTMAX_MAX, ENOTSUP },
170 };
129
130 intmax_t rv;
131 char *end;
132 int e;
133 size_t i;
134
135 for (i = 0; i < __arraycount(t); i++) {
136
137 errno = 0;
138 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
139
171
172 intmax_t rv;
173 char *end;
174 int e;
175 size_t i;
176
177 for (i = 0; i < __arraycount(t); i++) {
178
179 errno = 0;
180 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
181
140 if (errno != 0)
141 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
142 e, strerror(e));
182 check_errno(errno);
143
144 check(&t[i], rv, end, e);
145 }
183
184 check(&t[i], rv, end, e);
185 }
186
187 for (i = 0; i < __arraycount(f); i++) {
188
189 end = NULL;
190 errno = 0;
191 e = -99;
192
193 rv = strtoi(f[i].str, &end, f[i].base, f[i].lo, f[i].hi, &e);
194
195 check_errno(errno);
196
197 check(&f[i], rv, end, e);
198 }
146}
147
148ATF_TC(strtoi_case);
149ATF_TC_HEAD(strtoi_case, tc)
150{
151 atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtoi(3)");
152}
153

--- 25 unchanged lines hidden (view full) ---

179 int e;
180 size_t i;
181
182 for (i = 0; i < __arraycount(t); i++) {
183
184 errno = 0;
185 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
186
199}
200
201ATF_TC(strtoi_case);
202ATF_TC_HEAD(strtoi_case, tc)
203{
204 atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtoi(3)");
205}
206

--- 25 unchanged lines hidden (view full) ---

232 int e;
233 size_t i;
234
235 for (i = 0; i < __arraycount(t); i++) {
236
237 errno = 0;
238 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
239
187 if (errno != 0)
188 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
189 e, strerror(e));
240 check_errno(errno);
190
191 check(&t[i], rv, end, e);
192 }
193}
194
195ATF_TC(strtoi_range);
196ATF_TC_HEAD(strtoi_range, tc)
197{

--- 8 unchanged lines hidden (view full) ---

206 INTMAX_MIN, INTMAX_MAX, ERANGE },
207 { "9223372036854775808", INTMAX_MAX, 10, NULL,
208 INTMAX_MIN, INTMAX_MAX, ERANGE },
209 { "8000000000000000", INTMAX_MAX, 16, NULL,
210 INTMAX_MIN, INTMAX_MAX, ERANGE },
211#else
212#error extend this test to your platform!
213#endif
241
242 check(&t[i], rv, end, e);
243 }
244}
245
246ATF_TC(strtoi_range);
247ATF_TC_HEAD(strtoi_range, tc)
248{

--- 8 unchanged lines hidden (view full) ---

257 INTMAX_MIN, INTMAX_MAX, ERANGE },
258 { "9223372036854775808", INTMAX_MAX, 10, NULL,
259 INTMAX_MIN, INTMAX_MAX, ERANGE },
260 { "8000000000000000", INTMAX_MAX, 16, NULL,
261 INTMAX_MIN, INTMAX_MAX, ERANGE },
262#else
263#error extend this test to your platform!
264#endif
214 { "10", 1, 10, NULL,
215 -1, 1, ERANGE },
216 { "10", 11, 10, NULL,
217 11, 20, ERANGE },
265 { "10", 1, 10, NULL,
266 -1, 1, ERANGE },
267 { "10", 11, 10, NULL,
268 11, 20, ERANGE },
269 { "7", 7, 0, NULL,
270 7, 7, 0 },
271 { "6", 7, 0, NULL,
272 7, 7, ERANGE },
273 { "8", 7, 0, NULL,
274 7, 7, ERANGE },
275 { "7x", 7, 0, "x",
276 7, 7, ENOTSUP },
277 { "8x", 7, 0, "x",
278 7, 7, ERANGE },
279 { "Z", 11, 10, "Z",
280 11, 20, ECANCELED },
218 };
219
220 intmax_t rv;
221 char *end;
222 int e;
223 size_t i;
224
225 for (i = 0; i < __arraycount(t); i++) {
226
227 errno = 0;
228 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
229
230 if (errno != 0)
281 };
282
283 intmax_t rv;
284 char *end;
285 int e;
286 size_t i;
287
288 for (i = 0; i < __arraycount(t); i++) {
289
290 errno = 0;
291 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
292
293 if (errno != 0)
231 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
232 e, strerror(e));
294 atf_tc_fail("Range test %zd set errno=%d", i, errno);
295 check_errno(errno);
233
234 check(&t[i], rv, end, e);
235 }
236}
237
238ATF_TC(strtoi_range_trail);
239ATF_TC_HEAD(strtoi_range_trail, tc)
240{
241 atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtoi(3) "
242 "with trailing characters");
243}
244
245ATF_TC_BODY(strtoi_range_trail, tc)
246{
247 struct test t[] = {
296
297 check(&t[i], rv, end, e);
298 }
299}
300
301ATF_TC(strtoi_range_trail);
302ATF_TC_HEAD(strtoi_range_trail, tc)
303{
304 atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtoi(3) "
305 "with trailing characters");
306}
307
308ATF_TC_BODY(strtoi_range_trail, tc)
309{
310 struct test t[] = {
248 { "11x", 9, 10, "x", 0, 9, ERANGE },
249 { " -3y", -2, 10, "y", -2, 1, ERANGE },
311 { "11x", 9, 10, "x", 0, 9, ERANGE },
312 { " -3y", -2, 10, "y", -2, 1, ERANGE },
313 { "11111z", 9, 10, "z", 0, 9, ERANGE },
314 { "+0xAq", 9, 16, "q", 0, 9, ERANGE },
315 { "-0xBAr", 0, 16, "r", 0, 9, ERANGE },
250 };
251
252 intmax_t rv;
253 char *end;
254 int e;
255 size_t i;
256
257 for (i = 0; i < __arraycount(t); i++) {
258
259 errno = 0;
260 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
261
316 };
317
318 intmax_t rv;
319 char *end;
320 int e;
321 size_t i;
322
323 for (i = 0; i < __arraycount(t); i++) {
324
325 errno = 0;
326 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
327
262 if (errno != 0)
263 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
264 e, strerror(e));
328 check_errno(errno);
265
266 check(&t[i], rv, end, e);
267 }
268}
269
270ATF_TC(strtoi_signed);
271ATF_TC_HEAD(strtoi_signed, tc)
272{

--- 38 unchanged lines hidden (view full) ---

311 int e;
312 size_t i;
313
314 for (i = 0; i < __arraycount(t); i++) {
315
316 errno = 0;
317 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
318
329
330 check(&t[i], rv, end, e);
331 }
332}
333
334ATF_TC(strtoi_signed);
335ATF_TC_HEAD(strtoi_signed, tc)
336{

--- 38 unchanged lines hidden (view full) ---

375 int e;
376 size_t i;
377
378 for (i = 0; i < __arraycount(t); i++) {
379
380 errno = 0;
381 rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
382
319 if (errno != 0)
320 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
321 e, strerror(e));
383 check_errno(errno);
322
323 check(&t[i], rv, end, e);
324 }
325}
326
327ATF_TP_ADD_TCS(tp)
328{
329
330 ATF_TP_ADD_TC(tp, strtoi_base);
331 ATF_TP_ADD_TC(tp, strtoi_case);
332 ATF_TP_ADD_TC(tp, strtoi_range);
333 ATF_TP_ADD_TC(tp, strtoi_range_trail);
334 ATF_TP_ADD_TC(tp, strtoi_signed);
335
336 return atf_no_error();
337}
384
385 check(&t[i], rv, end, e);
386 }
387}
388
389ATF_TP_ADD_TCS(tp)
390{
391
392 ATF_TP_ADD_TC(tp, strtoi_base);
393 ATF_TP_ADD_TC(tp, strtoi_case);
394 ATF_TP_ADD_TC(tp, strtoi_range);
395 ATF_TP_ADD_TC(tp, strtoi_range_trail);
396 ATF_TP_ADD_TC(tp, strtoi_signed);
397
398 return atf_no_error();
399}