1 /* $NetBSD: t_strtoi.c,v 1.1 2015/05/01 14:17:56 christos Exp $ */
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Created by Kamil Rytarowski, vesed on ID:
34 * NetBSD: t_strtol.c,v 1.5 2011/06/14 02:45:58 jruoho Exp
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: t_strtoi.c,v 1.1 2015/05/01 14:17:56 christos Exp $");
57 static void check(struct test
*, intmax_t, char *, int);
60 check(struct test
*t
, intmax_t rv
, char *end
, int rstatus
)
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
);
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
));
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 "
75 "strtoi(%s, &end, %d, %jd, %jd, &rstatus)",
76 end
, t
->str
, t
->base
, t
->lo
, t
->hi
);
80 ATF_TC_HEAD(strtoi_base
, tc
)
82 atf_tc_set_md_var(tc
, "descr", "Test strtoi(3) with different bases");
85 ATF_TC_BODY(strtoi_base
, tc
)
88 { "123456789", 123456789, 0, NULL
,
89 INTMAX_MIN
, INTMAX_MAX
, 0 },
90 { "111010110111100110100010101",123456789, 2, NULL
,
91 INTMAX_MIN
, INTMAX_MAX
, 0 },
92 { "22121022020212200", 123456789, 3, NULL
,
93 INTMAX_MIN
, INTMAX_MAX
, 0 },
94 { "13112330310111", 123456789, 4, NULL
,
95 INTMAX_MIN
, INTMAX_MAX
, 0 },
96 { "223101104124", 123456789, 5, NULL
,
97 INTMAX_MIN
, INTMAX_MAX
, 0 },
98 { "20130035113", 123456789, 6, NULL
,
99 INTMAX_MIN
, INTMAX_MAX
, 0 },
100 { "3026236221", 123456789, 7, NULL
,
101 INTMAX_MIN
, INTMAX_MAX
, 0 },
102 { "726746425", 123456789, 8, NULL
,
103 INTMAX_MIN
, INTMAX_MAX
, 0 },
104 { "277266780", 123456789, 9, NULL
,
105 INTMAX_MIN
, INTMAX_MAX
, 0 },
106 { "123456789", 123456789, 10, NULL
,
107 INTMAX_MIN
, INTMAX_MAX
, 0 },
108 { "63762A05", 123456789, 11, NULL
,
109 INTMAX_MIN
, INTMAX_MAX
, 0 },
110 { "35418A99", 123456789, 12, NULL
,
111 INTMAX_MIN
, INTMAX_MAX
, 0 },
112 { "1C767471", 123456789, 13, NULL
,
113 INTMAX_MIN
, INTMAX_MAX
, 0 },
114 { "12579781", 123456789, 14, NULL
,
115 INTMAX_MIN
, INTMAX_MAX
, 0 },
116 { "AC89BC9", 123456789, 15, NULL
,
117 INTMAX_MIN
, INTMAX_MAX
, 0 },
118 { "75BCD15", 123456789, 16, NULL
,
119 INTMAX_MIN
, INTMAX_MAX
, 0 },
120 { "1234567", 342391, 8, NULL
,
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 },
135 for (i
= 0; i
< __arraycount(t
); i
++) {
138 rv
= strtoi(t
[i
].str
, &end
, t
[i
].base
, t
[i
].lo
, t
[i
].hi
, &e
);
141 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
144 check(&t
[i
], rv
, end
, e
);
149 ATF_TC_HEAD(strtoi_case
, tc
)
151 atf_tc_set_md_var(tc
, "descr", "Case insensitivity with strtoi(3)");
154 ATF_TC_BODY(strtoi_case
, tc
)
157 { "abcd", 0xabcd, 16, NULL
,
158 INTMAX_MIN
, INTMAX_MAX
, 0 },
159 { " dcba", 0xdcba, 16, NULL
,
160 INTMAX_MIN
, INTMAX_MAX
, 0 },
161 { "abcd dcba", 0xabcd, 16, " dcba",
162 INTMAX_MIN
, INTMAX_MAX
, ENOTSUP
},
163 { "abc0x123", 0xabc0, 16, "x123",
164 INTMAX_MIN
, INTMAX_MAX
, ENOTSUP
},
165 { "abcd\0x123", 0xabcd, 16, "\0x123",
166 INTMAX_MIN
, INTMAX_MAX
, 0 },
167 { "ABCD", 0xabcd, 16, NULL
,
168 INTMAX_MIN
, INTMAX_MAX
, 0 },
169 { "aBcD", 0xabcd, 16, NULL
,
170 INTMAX_MIN
, INTMAX_MAX
, 0 },
171 { "0xABCD", 0xabcd, 16, NULL
,
172 INTMAX_MIN
, INTMAX_MAX
, 0 },
173 { "0xABCDX", 0xabcd, 16, "X",
174 INTMAX_MIN
, INTMAX_MAX
, ENOTSUP
},
182 for (i
= 0; i
< __arraycount(t
); i
++) {
185 rv
= strtoi(t
[i
].str
, &end
, t
[i
].base
, t
[i
].lo
, t
[i
].hi
, &e
);
188 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
191 check(&t
[i
], rv
, end
, e
);
195 ATF_TC(strtoi_range
);
196 ATF_TC_HEAD(strtoi_range
, tc
)
198 atf_tc_set_md_var(tc
, "descr", "Test ERANGE from strtoi(3)");
201 ATF_TC_BODY(strtoi_range
, tc
)
204 #if INTMAX_MAX == 0x7fffffffffffffff
205 { "1000000000000000000000", INTMAX_MAX
, 8, NULL
,
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
},
212 #error extend this test to your platform!
216 { "10", 11, 10, NULL
,
225 for (i
= 0; i
< __arraycount(t
); i
++) {
228 rv
= strtoi(t
[i
].str
, &end
, t
[i
].base
, t
[i
].lo
, t
[i
].hi
, &e
);
231 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
234 check(&t
[i
], rv
, end
, e
);
238 ATF_TC(strtoi_signed
);
239 ATF_TC_HEAD(strtoi_signed
, tc
)
241 atf_tc_set_md_var(tc
, "descr", "A basic test of strtoi(3)");
244 ATF_TC_BODY(strtoi_signed
, tc
)
248 INTMAX_MIN
, INTMAX_MAX
, 0 },
250 INTMAX_MIN
, INTMAX_MAX
, 0 },
252 INTMAX_MIN
, INTMAX_MAX
, 0 },
253 { " -3", -3, 0, NULL
,
254 INTMAX_MIN
, INTMAX_MAX
, 0 },
255 { "--1", 0, 0, "--1",
256 INTMAX_MIN
, INTMAX_MAX
, ECANCELED
},
257 { "+-2", 0, 0, "+-2",
258 INTMAX_MIN
, INTMAX_MAX
, ECANCELED
},
259 { "++3", 0, 0, "++3",
260 INTMAX_MIN
, INTMAX_MAX
, ECANCELED
},
262 INTMAX_MIN
, INTMAX_MAX
, 0 },
263 { "+123", 123, 0, NULL
,
264 INTMAX_MIN
, INTMAX_MAX
, 0 },
265 { "-1 3", -1, 0, " 3",
266 INTMAX_MIN
, INTMAX_MAX
, ENOTSUP
},
267 { "-1.3", -1, 0, ".3",
268 INTMAX_MIN
, INTMAX_MAX
, ENOTSUP
},
269 { "- 3", 0, 0, "- 3",
270 INTMAX_MIN
, INTMAX_MAX
, ECANCELED
},
271 { "+33.", 33, 0, ".",
272 INTMAX_MIN
, INTMAX_MAX
, ENOTSUP
},
273 { "30x0", 30, 0, "x0",
274 INTMAX_MIN
, INTMAX_MAX
, ENOTSUP
},
282 for (i
= 0; i
< __arraycount(t
); i
++) {
285 rv
= strtoi(t
[i
].str
, &end
, t
[i
].base
, t
[i
].lo
, t
[i
].hi
, &e
);
288 atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
291 check(&t
[i
], rv
, end
, e
);
298 ATF_TP_ADD_TC(tp
, strtoi_base
);
299 ATF_TP_ADD_TC(tp
, strtoi_case
);
300 ATF_TP_ADD_TC(tp
, strtoi_range
);
301 ATF_TP_ADD_TC(tp
, strtoi_signed
);
303 return atf_no_error();