xref: /netbsd-src/tests/lib/libc/stdlib/t_strtod.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: t_strtod.c,v 1.9 2011/05/10 19:18:19 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 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 
32 /* Public domain, Otto Moerbeek <otto@drijf.net>, 2006. */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_strtod.c,v 1.9 2011/05/10 19:18:19 jruoho Exp $");
36 
37 #include <errno.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <atf-c.h>
44 #include <atf-c/config.h>
45 
46 #if defined(__i386__) || defined(__amd64__) || defined(__sparc__)
47 #include <fenv.h>
48 #endif
49 
50 ATF_TC(strtod_basic);
51 ATF_TC_HEAD(strtod_basic, tc)
52 {
53 	atf_tc_set_md_var(tc, "descr", "A basic test of strtod(3)");
54 }
55 
56 ATF_TC_BODY(strtod_basic, tc)
57 {
58 	char buf[512];
59 	size_t i, n;
60 	double d;
61 
62 	n = 1024 * 1000;
63 
64 	for (i = 1; i < n; i = i + 1024) {
65 
66 		(void)snprintf(buf, sizeof(buf), "%zu.%zu", i, i + 1);
67 
68 		errno = 0;
69 		d = strtod(buf, NULL);
70 
71 		ATF_REQUIRE(d > 0.0);
72 		ATF_REQUIRE(errno == 0);
73 	}
74 }
75 
76 ATF_TC(strtod_hex);
77 ATF_TC_HEAD(strtod_hex, tc)
78 {
79 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with hexadecimals");
80 }
81 
82 ATF_TC_BODY(strtod_hex, tc)
83 {
84 	const char *str;
85 	char *end;
86 	double d;
87 
88 	str = "-0x0";
89 	d = strtod(str, &end);	/* -0.0 */
90 
91 	ATF_REQUIRE(end == str + 4);
92 	ATF_REQUIRE(signbit(d) != 0);
93 	ATF_REQUIRE(fabs(d) < 1.0e-40);
94 
95 	str = "-0x";
96 	d = strtod(str, &end);	/* -0.0 */
97 
98 	ATF_REQUIRE(end == str + 2);
99 	ATF_REQUIRE(signbit(d) != 0);
100 	ATF_REQUIRE(fabs(d) < 1.0e-40);
101 }
102 
103 ATF_TC(strtod_inf);
104 ATF_TC_HEAD(strtod_inf, tc)
105 {
106 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with INF");
107 }
108 
109 ATF_TC_BODY(strtod_inf, tc)
110 {
111 #ifndef __vax__
112 
113 	long double ld;
114 	double d;
115 	float f;
116 
117 	/*
118 	 * See old PR lib/33262.
119 	 */
120 	d = strtod("INF", NULL);
121 	ATF_REQUIRE(isinf(d) != 0);
122 
123 	f = strtof("INF", NULL);
124 	ATF_REQUIRE(isinf(f) != 0);
125 
126 	ld = strtold("INF", NULL);
127 	ATF_REQUIRE(isinf(ld) != 0);
128 #endif
129 }
130 
131 ATF_TC(strtod_round);
132 ATF_TC_HEAD(strtod_round, tc)
133 {
134 	atf_tc_set_md_var(tc, "descr", "Test rouding in strtod(3)");
135 }
136 
137 ATF_TC_BODY(strtod_round, tc)
138 {
139 #if defined(__i386__) || defined(__amd64__) || defined(__sparc__)
140 
141 	const char *val;
142 	double d1, d2;
143 
144 	/*
145 	 * Test that strtod(3) honors the current rounding mode.
146 	 *
147 	 * The used value is somewhere near 1 + DBL_EPSILON + FLT_EPSILON.
148 	 */
149 	val = "1.00000011920928977282585492503130808472633361816406";
150 
151 	(void)fesetround(FE_UPWARD);
152 
153 	d1 = strtod(val, NULL);
154 
155 	(void)fesetround(FE_DOWNWARD);
156 
157 	d2 = strtod(val, NULL);
158 
159 	ATF_REQUIRE(fabs(d1 - d2) > 0.0);
160 #endif
161 }
162 
163 ATF_TC(strtod_underflow);
164 ATF_TC_HEAD(strtod_underflow, tc)
165 {
166 	atf_tc_set_md_var(tc, "descr", "Test underflow in strtod(3)");
167 }
168 
169 ATF_TC_BODY(strtod_underflow, tc)
170 {
171 
172 	const char *tmp =
173 	    "0.0000000000000000000000000000000000000000000000000000"
174 	    "000000000000000000000000000000000000000000000000000000"
175 	    "000000000000000000000000000000000000000000000000000000"
176 	    "000000000000000000000000000000000000000000000000000000"
177 	    "000000000000000000000000000000000000000000000000000000"
178 	    "000000000000000000000000000000000000000000000000000000"
179 	    "000000000000000000000000000000000000000000000000000000"
180 	    "000000000000000002";
181 
182 	double d;
183 
184 	errno = 0;
185 	d = strtod(tmp, NULL);
186 
187 	if (errno != ERANGE)
188 		atf_tc_fail("strtod(3) did not detect underflow");
189 }
190 
191 ATF_TP_ADD_TCS(tp)
192 {
193 
194 	ATF_TP_ADD_TC(tp, strtod_basic);
195 	ATF_TP_ADD_TC(tp, strtod_hex);
196 	ATF_TP_ADD_TC(tp, strtod_inf);
197 	ATF_TP_ADD_TC(tp, strtod_round);
198 	ATF_TP_ADD_TC(tp, strtod_underflow);
199 
200 	return atf_no_error();
201 }
202