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