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