xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/t_strtod.c (revision 3fe8969a749c0e4a62ffdbf4f6883898027a9e19)
1 /*	$NetBSD: t_strtod.c,v 1.31 2012/09/26 07:24:38 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.31 2012/09/26 07:24:38 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 #if !defined(__vax__)
51 static const char * const inf_strings[] =
52     { "Inf", "INF", "-Inf", "-INF", "Infinity", "+Infinity",
53       "INFINITY", "-INFINITY", "InFiNiTy", "+InFiNiTy" };
54 const char *nan_string = "NaN(x)y";
55 #endif
56 
57 ATF_TC(strtod_basic);
58 ATF_TC_HEAD(strtod_basic, tc)
59 {
60 	atf_tc_set_md_var(tc, "descr", "A basic test of strtod(3)");
61 }
62 
63 ATF_TC_BODY(strtod_basic, tc)
64 {
65 	static const size_t n = 1024 * 1000;
66 
67 	for (size_t i = 1; i < n; i = i + 1024) {
68 		char buf[512];
69 		(void)snprintf(buf, sizeof(buf), "%zu.%zu", i, i + 1);
70 
71 		errno = 0;
72 		double d = strtod(buf, NULL);
73 
74 		ATF_REQUIRE(d > 0.0);
75 		ATF_REQUIRE(errno == 0);
76 	}
77 }
78 
79 ATF_TC(strtod_hex);
80 ATF_TC_HEAD(strtod_hex, tc)
81 {
82 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with hexadecimals");
83 }
84 
85 #ifdef __vax__
86 #define SMALL_NUM       1.0e-38
87 #else
88 #define SMALL_NUM       1.0e-40
89 #endif
90 
91 ATF_TC_BODY(strtod_hex, tc)
92 {
93 	const char *str;
94 	char *end;
95 	volatile double d;
96 
97 	str = "-0x0";
98 	d = strtod(str, &end);	/* -0.0 */
99 
100 	ATF_REQUIRE(end == str + 4);
101 	ATF_REQUIRE(signbit(d) != 0);
102 	ATF_REQUIRE(fabs(d) < SMALL_NUM);
103 
104 	str = "-0x";
105 	d = strtod(str, &end);	/* -0.0 */
106 
107 	ATF_REQUIRE(end == str + 2);
108 	ATF_REQUIRE(signbit(d) != 0);
109 	ATF_REQUIRE(fabs(d) < SMALL_NUM);
110 }
111 
112 ATF_TC(strtod_inf);
113 ATF_TC_HEAD(strtod_inf, tc)
114 {
115 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with INF (PR lib/33262)");
116 }
117 
118 ATF_TC_BODY(strtod_inf, tc)
119 {
120 #ifndef __vax__
121 	for (size_t i = 0; i < __arraycount(inf_strings); i++) {
122 		volatile double d = strtod(inf_strings[i], NULL);
123 		ATF_REQUIRE(isinf(d) != 0);
124 	}
125 #else
126 	atf_tc_skip("vax not supported");
127 #endif
128 }
129 
130 ATF_TC(strtof_inf);
131 ATF_TC_HEAD(strtof_inf, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr", "A strtof(3) with INF (PR lib/33262)");
134 }
135 
136 ATF_TC_BODY(strtof_inf, tc)
137 {
138 #ifndef __vax__
139 	for (size_t i = 0; i < __arraycount(inf_strings); i++) {
140 		volatile float f = strtof(inf_strings[i], NULL);
141 		ATF_REQUIRE(isinf(f) != 0);
142 	}
143 #else
144 	atf_tc_skip("vax not supported");
145 #endif
146 }
147 
148 ATF_TC(strtold_inf);
149 ATF_TC_HEAD(strtold_inf, tc)
150 {
151 	atf_tc_set_md_var(tc, "descr", "A strtold(3) with INF (PR lib/33262)");
152 }
153 
154 ATF_TC_BODY(strtold_inf, tc)
155 {
156 #ifndef __vax__
157 #   ifdef __HAVE_LONG_DOUBLE
158 
159 	for (size_t i = 0; i < __arraycount(inf_strings); i++) {
160 		volatile long double ld = strtold(inf_strings[i], NULL);
161 		ATF_REQUIRE(isinf(ld) != 0);
162 	}
163 #   else
164 	atf_tc_skip("Requires long double support");
165 #   endif
166 #else
167 	atf_tc_skip("vax not supported");
168 #endif
169 }
170 
171 ATF_TC(strtod_nan);
172 ATF_TC_HEAD(strtod_nan, tc)
173 {
174 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with NaN");
175 }
176 
177 ATF_TC_BODY(strtod_nan, tc)
178 {
179 #ifndef __vax__
180 	char *end;
181 
182 	volatile double d = strtod(nan_string, &end);
183 	ATF_REQUIRE(isnan(d) != 0);
184 	ATF_REQUIRE(strcmp(end, "y") == 0);
185 #else
186 	atf_tc_skip("vax not supported");
187 #endif
188 }
189 
190 ATF_TC(strtof_nan);
191 ATF_TC_HEAD(strtof_nan, tc)
192 {
193 	atf_tc_set_md_var(tc, "descr", "A strtof(3) with NaN");
194 }
195 
196 ATF_TC_BODY(strtof_nan, tc)
197 {
198 #ifndef __vax__
199 	char *end;
200 
201 	volatile float f = strtof(nan_string, &end);
202 	ATF_REQUIRE(isnanf(f) != 0);
203 	ATF_REQUIRE(strcmp(end, "y") == 0);
204 #else
205 	atf_tc_skip("vax not supported");
206 #endif
207 }
208 
209 ATF_TC(strtold_nan);
210 ATF_TC_HEAD(strtold_nan, tc)
211 {
212 	atf_tc_set_md_var(tc, "descr", "A strtold(3) with NaN (PR lib/45020)");
213 }
214 
215 ATF_TC_BODY(strtold_nan, tc)
216 {
217 #ifndef __vax__
218 #   ifdef __HAVE_LONG_DOUBLE
219 
220 	char *end;
221 
222 	volatile long double ld = strtold(nan_string, &end);
223 	ATF_REQUIRE(isnan(ld) != 0);
224 #if !defined(__FreeBSD__)
225 	ATF_REQUIRE(__isnanl(ld) != 0);
226 #endif
227 	ATF_REQUIRE(strcmp(end, "y") == 0);
228 #   else
229 	atf_tc_skip("Requires long double support");
230 #   endif
231 #else
232 	atf_tc_skip("vax not supported");
233 #endif
234 }
235 
236 ATF_TC(strtod_round);
237 ATF_TC_HEAD(strtod_round, tc)
238 {
239 	atf_tc_set_md_var(tc, "descr", "Test rouding in strtod(3)");
240 }
241 
242 ATF_TC_BODY(strtod_round, tc)
243 {
244 #if defined(__i386__) || defined(__amd64__) || defined(__sparc__)
245 
246 	/*
247 	 * Test that strtod(3) honors the current rounding mode.
248 	 * The used value is somewhere near 1 + DBL_EPSILON + FLT_EPSILON.
249 	 */
250 	const char *val =
251 	    "1.00000011920928977282585492503130808472633361816406";
252 
253 	(void)fesetround(FE_UPWARD);
254 
255 	volatile double d1 = strtod(val, NULL);
256 
257 	(void)fesetround(FE_DOWNWARD);
258 
259 	volatile double d2 = strtod(val, NULL);
260 
261 	if (fabs(d1 - d2) > 0.0)
262 		return;
263 	else {
264 		atf_tc_expect_fail("PR misc/44767");
265 		atf_tc_fail("strtod(3) did not honor fesetround(3)");
266 	}
267 #else
268 	atf_tc_skip("Requires one of i386, amd64 or sparc");
269 #endif
270 }
271 
272 ATF_TC(strtod_underflow);
273 ATF_TC_HEAD(strtod_underflow, tc)
274 {
275 	atf_tc_set_md_var(tc, "descr", "Test underflow in strtod(3)");
276 }
277 
278 ATF_TC_BODY(strtod_underflow, tc)
279 {
280 
281 	const char *tmp =
282 	    "0.0000000000000000000000000000000000000000000000000000"
283 	    "000000000000000000000000000000000000000000000000000000"
284 	    "000000000000000000000000000000000000000000000000000000"
285 	    "000000000000000000000000000000000000000000000000000000"
286 	    "000000000000000000000000000000000000000000000000000000"
287 	    "000000000000000000000000000000000000000000000000000000"
288 	    "000000000000000000000000000000000000000000000000000000"
289 	    "000000000000000002";
290 
291 	errno = 0;
292 	volatile double d = strtod(tmp, NULL);
293 
294 	if (d != 0 || errno != ERANGE)
295 		atf_tc_fail("strtod(3) did not detect underflow");
296 }
297 
298 /*
299  * Bug found by Geza Herman.
300  * See
301  * http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/
302  */
303 ATF_TC(strtod_gherman_bug);
304 ATF_TC_HEAD(strtod_gherman_bug, tc)
305 {
306 	atf_tc_set_md_var(tc, "descr", "Test a bug found by Geza Herman");
307 }
308 
309 ATF_TC_BODY(strtod_gherman_bug, tc)
310 {
311 
312 	const char *str =
313 	    "1.8254370818746402660437411213933955878019332885742187";
314 
315 	errno = 0;
316 	volatile double d = strtod(str, NULL);
317 
318 	ATF_CHECK(d == 0x1.d34fd8378ea83p+0);
319 }
320 
321 ATF_TP_ADD_TCS(tp)
322 {
323 
324 	ATF_TP_ADD_TC(tp, strtod_basic);
325 	ATF_TP_ADD_TC(tp, strtod_hex);
326 	ATF_TP_ADD_TC(tp, strtod_inf);
327 	ATF_TP_ADD_TC(tp, strtof_inf);
328 	ATF_TP_ADD_TC(tp, strtold_inf);
329 	ATF_TP_ADD_TC(tp, strtod_nan);
330 	ATF_TP_ADD_TC(tp, strtof_nan);
331 	ATF_TP_ADD_TC(tp, strtold_nan);
332 	ATF_TP_ADD_TC(tp, strtod_round);
333 	ATF_TP_ADD_TC(tp, strtod_underflow);
334 	ATF_TP_ADD_TC(tp, strtod_gherman_bug);
335 
336 	return atf_no_error();
337 }
338