xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/t_atoi.c (revision 313376588638950ba1e93c403dd8c97bc52fd3a2)
1 /* $NetBSD: t_atoi.c,v 1.2 2012/03/29 05:56:36 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2012 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 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_atoi.c,v 1.2 2012/03/29 05:56:36 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <float.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 
40 ATF_TC(atof_strtod);
41 ATF_TC_HEAD(atof_strtod, tc)
42 {
43 	atf_tc_set_md_var(tc, "descr",
44 	    "Test that atof(3) matches the corresponding strtod(3) call");
45 }
46 
47 ATF_TC_BODY(atof_strtod, tc)
48 {
49 	char buf[128];
50 
51 	(void)snprintf(buf, sizeof(buf), "%f\n", DBL_MAX);
52 
53 	ATF_REQUIRE(atof("0")  == strtod("0", NULL));
54 	ATF_REQUIRE(atof("-1") == strtod("-1", NULL));
55 	ATF_REQUIRE(atof(buf)  == strtod(buf, NULL));
56 }
57 
58 ATF_TC(atoi_strtol);
59 ATF_TC_HEAD(atoi_strtol, tc)
60 {
61 	atf_tc_set_md_var(tc, "descr",
62 	    "Test that atoi(3) matches the corresponding strtol(3) call");
63 }
64 
65 ATF_TC_BODY(atoi_strtol, tc)
66 {
67 	char buf[64];
68 
69 	(void)snprintf(buf, sizeof(buf), "%d\n", INT_MAX);
70 
71 	ATF_REQUIRE(atoi("0")  == strtol("0", NULL, 10));
72 	ATF_REQUIRE(atoi("-1") == strtol("-1", NULL, 10));
73 	ATF_REQUIRE(atoi(buf)  == strtol(buf, NULL, 10));
74 }
75 
76 ATF_TC(atol_strtol);
77 ATF_TC_HEAD(atol_strtol, tc)
78 {
79 	atf_tc_set_md_var(tc, "descr",
80 	    "Test that atol(3) matches the corresponding strtol(3) call");
81 }
82 
83 ATF_TC_BODY(atol_strtol, tc)
84 {
85 	char buf[64];
86 
87 	(void)snprintf(buf, sizeof(buf), "%ld\n", LONG_MAX);
88 
89 	ATF_REQUIRE(atol("0")  == strtol("0", NULL, 10));
90 	ATF_REQUIRE(atol("-1") == strtol("-1", NULL, 10));
91 	ATF_REQUIRE(atol(buf)  == strtol(buf, NULL, 10));
92 }
93 
94 ATF_TC(atoll_strtoll);
95 ATF_TC_HEAD(atoll_strtoll, tc)
96 {
97 	atf_tc_set_md_var(tc, "descr",
98 	    "Test that atoll(3) matches the corresponding strtoll(3) call");
99 }
100 
101 ATF_TC_BODY(atoll_strtoll, tc)
102 {
103 	char buf[128];
104 
105 	(void)snprintf(buf, sizeof(buf), "%lld\n", LLONG_MAX);
106 
107 	ATF_REQUIRE(atoll("0")  == strtoll("0", NULL, 10));
108 	ATF_REQUIRE(atoll("-1") == strtoll("-1", NULL, 10));
109 	ATF_REQUIRE(atoll(buf)  == strtoll(buf, NULL, 10));
110 }
111 
112 ATF_TP_ADD_TCS(tp)
113 {
114 
115 	ATF_TP_ADD_TC(tp, atof_strtod);
116 	ATF_TP_ADD_TC(tp, atoi_strtol);
117 	ATF_TP_ADD_TC(tp, atol_strtol);
118 	ATF_TP_ADD_TC(tp, atoll_strtoll);
119 
120 	return atf_no_error();
121 }
122