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