xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/t_atoi.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /* $NetBSD: t_atoi.c,v 1.2 2012/03/29 05:56:36 jruoho Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
8*57718be8SEnji Cooper  * by Jukka Ruohonen.
9*57718be8SEnji Cooper  *
10*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
11*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
12*57718be8SEnji Cooper  * are met:
13*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
14*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
15*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
16*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
17*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
18*57718be8SEnji Cooper  *
19*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
30*57718be8SEnji Cooper  */
31*57718be8SEnji Cooper #include <sys/cdefs.h>
32*57718be8SEnji Cooper __RCSID("$NetBSD: t_atoi.c,v 1.2 2012/03/29 05:56:36 jruoho Exp $");
33*57718be8SEnji Cooper 
34*57718be8SEnji Cooper #include <atf-c.h>
35*57718be8SEnji Cooper #include <float.h>
36*57718be8SEnji Cooper #include <limits.h>
37*57718be8SEnji Cooper #include <stdio.h>
38*57718be8SEnji Cooper #include <stdlib.h>
39*57718be8SEnji Cooper 
40*57718be8SEnji Cooper ATF_TC(atof_strtod);
ATF_TC_HEAD(atof_strtod,tc)41*57718be8SEnji Cooper ATF_TC_HEAD(atof_strtod, tc)
42*57718be8SEnji Cooper {
43*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
44*57718be8SEnji Cooper 	    "Test that atof(3) matches the corresponding strtod(3) call");
45*57718be8SEnji Cooper }
46*57718be8SEnji Cooper 
ATF_TC_BODY(atof_strtod,tc)47*57718be8SEnji Cooper ATF_TC_BODY(atof_strtod, tc)
48*57718be8SEnji Cooper {
49*57718be8SEnji Cooper 	char buf[128];
50*57718be8SEnji Cooper 
51*57718be8SEnji Cooper 	(void)snprintf(buf, sizeof(buf), "%f\n", DBL_MAX);
52*57718be8SEnji Cooper 
53*57718be8SEnji Cooper 	ATF_REQUIRE(atof("0")  == strtod("0", NULL));
54*57718be8SEnji Cooper 	ATF_REQUIRE(atof("-1") == strtod("-1", NULL));
55*57718be8SEnji Cooper 	ATF_REQUIRE(atof(buf)  == strtod(buf, NULL));
56*57718be8SEnji Cooper }
57*57718be8SEnji Cooper 
58*57718be8SEnji Cooper ATF_TC(atoi_strtol);
ATF_TC_HEAD(atoi_strtol,tc)59*57718be8SEnji Cooper ATF_TC_HEAD(atoi_strtol, tc)
60*57718be8SEnji Cooper {
61*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
62*57718be8SEnji Cooper 	    "Test that atoi(3) matches the corresponding strtol(3) call");
63*57718be8SEnji Cooper }
64*57718be8SEnji Cooper 
ATF_TC_BODY(atoi_strtol,tc)65*57718be8SEnji Cooper ATF_TC_BODY(atoi_strtol, tc)
66*57718be8SEnji Cooper {
67*57718be8SEnji Cooper 	char buf[64];
68*57718be8SEnji Cooper 
69*57718be8SEnji Cooper 	(void)snprintf(buf, sizeof(buf), "%d\n", INT_MAX);
70*57718be8SEnji Cooper 
71*57718be8SEnji Cooper 	ATF_REQUIRE(atoi("0")  == strtol("0", NULL, 10));
72*57718be8SEnji Cooper 	ATF_REQUIRE(atoi("-1") == strtol("-1", NULL, 10));
73*57718be8SEnji Cooper 	ATF_REQUIRE(atoi(buf)  == strtol(buf, NULL, 10));
74*57718be8SEnji Cooper }
75*57718be8SEnji Cooper 
76*57718be8SEnji Cooper ATF_TC(atol_strtol);
ATF_TC_HEAD(atol_strtol,tc)77*57718be8SEnji Cooper ATF_TC_HEAD(atol_strtol, tc)
78*57718be8SEnji Cooper {
79*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
80*57718be8SEnji Cooper 	    "Test that atol(3) matches the corresponding strtol(3) call");
81*57718be8SEnji Cooper }
82*57718be8SEnji Cooper 
ATF_TC_BODY(atol_strtol,tc)83*57718be8SEnji Cooper ATF_TC_BODY(atol_strtol, tc)
84*57718be8SEnji Cooper {
85*57718be8SEnji Cooper 	char buf[64];
86*57718be8SEnji Cooper 
87*57718be8SEnji Cooper 	(void)snprintf(buf, sizeof(buf), "%ld\n", LONG_MAX);
88*57718be8SEnji Cooper 
89*57718be8SEnji Cooper 	ATF_REQUIRE(atol("0")  == strtol("0", NULL, 10));
90*57718be8SEnji Cooper 	ATF_REQUIRE(atol("-1") == strtol("-1", NULL, 10));
91*57718be8SEnji Cooper 	ATF_REQUIRE(atol(buf)  == strtol(buf, NULL, 10));
92*57718be8SEnji Cooper }
93*57718be8SEnji Cooper 
94*57718be8SEnji Cooper ATF_TC(atoll_strtoll);
ATF_TC_HEAD(atoll_strtoll,tc)95*57718be8SEnji Cooper ATF_TC_HEAD(atoll_strtoll, tc)
96*57718be8SEnji Cooper {
97*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
98*57718be8SEnji Cooper 	    "Test that atoll(3) matches the corresponding strtoll(3) call");
99*57718be8SEnji Cooper }
100*57718be8SEnji Cooper 
ATF_TC_BODY(atoll_strtoll,tc)101*57718be8SEnji Cooper ATF_TC_BODY(atoll_strtoll, tc)
102*57718be8SEnji Cooper {
103*57718be8SEnji Cooper 	char buf[128];
104*57718be8SEnji Cooper 
105*57718be8SEnji Cooper 	(void)snprintf(buf, sizeof(buf), "%lld\n", LLONG_MAX);
106*57718be8SEnji Cooper 
107*57718be8SEnji Cooper 	ATF_REQUIRE(atoll("0")  == strtoll("0", NULL, 10));
108*57718be8SEnji Cooper 	ATF_REQUIRE(atoll("-1") == strtoll("-1", NULL, 10));
109*57718be8SEnji Cooper 	ATF_REQUIRE(atoll(buf)  == strtoll(buf, NULL, 10));
110*57718be8SEnji Cooper }
111*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)112*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
113*57718be8SEnji Cooper {
114*57718be8SEnji Cooper 
115*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, atof_strtod);
116*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, atoi_strtol);
117*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, atol_strtol);
118*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, atoll_strtoll);
119*57718be8SEnji Cooper 
120*57718be8SEnji Cooper 	return atf_no_error();
121*57718be8SEnji Cooper }
122