xref: /netbsd-src/tests/lib/libc/stdlib/t_strtod.c (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1 /*	$NetBSD: t_strtod.c,v 1.27 2011/09/30 14:50:20 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.27 2011/09/30 14:50:20 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 	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");
116 }
117 
118 ATF_TC_BODY(strtod_inf, tc)
119 {
120 #ifndef __vax__
121 	/*
122 	 * See the closed PR lib/33262.
123 	 */
124 	for (size_t i = 0; i < __arraycount(inf_strings); i++) {
125 		double d = strtod(inf_strings[i], NULL);
126 		ATF_REQUIRE(isinf(d) != 0);
127 	}
128 #else
129 	atf_tc_skip("vax not supported");
130 #endif
131 }
132 
133 ATF_TC(strtof_inf);
134 ATF_TC_HEAD(strtof_inf, tc)
135 {
136 	atf_tc_set_md_var(tc, "descr", "A strtof(3) with INF");
137 }
138 
139 ATF_TC_BODY(strtof_inf, tc)
140 {
141 #ifndef __vax__
142 	/*
143 	 * See the closed PR lib/33262.
144 	 */
145 	for (size_t i = 0; i < __arraycount(inf_strings); i++) {
146 		float f = strtof(inf_strings[i], NULL);
147 		ATF_REQUIRE(isinf(f) != 0);
148 	}
149 #else
150 	atf_tc_skip("vax not supported");
151 #endif
152 }
153 
154 ATF_TC(strtold_inf);
155 ATF_TC_HEAD(strtold_inf, tc)
156 {
157 	atf_tc_set_md_var(tc, "descr", "A strtold(3) with INF");
158 }
159 
160 ATF_TC_BODY(strtold_inf, tc)
161 {
162 #ifndef __vax__
163 #   ifdef __HAVE_LONG_DOUBLE
164 
165 	/*
166 	 * See the closed PR lib/33262.
167 	 *
168 	 * This may also fail under QEMU; cf. PR misc/44767.
169 	 */
170 	if (system("cpuctl identify 0 | grep -q QEMU") == 0)
171 		atf_tc_expect_fail("PR misc/44767");
172 
173 	for (size_t i = 0; i < __arraycount(inf_strings); i++) {
174 		long double ld = strtold(inf_strings[i], NULL);
175 		ATF_REQUIRE(isinf(ld) != 0);
176 	}
177 #   else
178 	atf_tc_skip("Requires long double support");
179 #   endif
180 #else
181 	atf_tc_skip("vax not supported");
182 #endif
183 }
184 
185 ATF_TC(strtod_nan);
186 ATF_TC_HEAD(strtod_nan, tc)
187 {
188 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with NaN");
189 }
190 
191 ATF_TC_BODY(strtod_nan, tc)
192 {
193 #ifndef __vax__
194 	char *end;
195 
196 	double d = strtod(nan_string, &end);
197 	ATF_REQUIRE(isnan(d) != 0);
198 	ATF_REQUIRE(strcmp(end, "y") == 0);
199 #else
200 	atf_tc_skip("vax not supported");
201 #endif
202 }
203 
204 ATF_TC(strtof_nan);
205 ATF_TC_HEAD(strtof_nan, tc)
206 {
207 	atf_tc_set_md_var(tc, "descr", "A strtof(3) with NaN");
208 }
209 
210 ATF_TC_BODY(strtof_nan, tc)
211 {
212 #ifndef __vax__
213 	char *end;
214 
215 	float f = strtof(nan_string, &end);
216 	ATF_REQUIRE(isnanf(f) != 0);
217 	ATF_REQUIRE(strcmp(end, "y") == 0);
218 #else
219 	atf_tc_skip("vax not supported");
220 #endif
221 }
222 
223 ATF_TC(strtold_nan);
224 ATF_TC_HEAD(strtold_nan, tc)
225 {
226 	atf_tc_set_md_var(tc, "descr", "A strtold(3) with NaN");
227 }
228 
229 ATF_TC_BODY(strtold_nan, tc)
230 {
231 #ifndef __vax__
232 #   ifdef __HAVE_LONG_DOUBLE
233 
234 	char *end;
235 
236 	/*
237 	 * See PR lib/45020.
238 	 *
239 	 * This may also fail under QEMU; cf. PR misc/44767.
240 	 */
241 	if (system("cpuctl identify 0 | grep -q QEMU") == 0)
242 		atf_tc_expect_fail("PR misc/44767");
243 
244 	long double ld = strtold(nan_string, &end);
245 	ATF_REQUIRE(isnan(ld) != 0);
246 	ATF_REQUIRE(__isnanl(ld) != 0);
247 	ATF_REQUIRE(strcmp(end, "y") == 0);
248 #   else
249 	atf_tc_skip("Requires long double support");
250 #   endif
251 #else
252 	atf_tc_skip("vax not supported");
253 #endif
254 }
255 
256 ATF_TC(strtod_round);
257 ATF_TC_HEAD(strtod_round, tc)
258 {
259 	atf_tc_set_md_var(tc, "descr", "Test rouding in strtod(3)");
260 }
261 
262 ATF_TC_BODY(strtod_round, tc)
263 {
264 #if defined(__i386__) || defined(__amd64__) || defined(__sparc__)
265 
266 	/*
267 	 * Test that strtod(3) honors the current rounding mode.
268 	 * The used value is somewhere near 1 + DBL_EPSILON + FLT_EPSILON.
269 	 *
270 	 * May fail under QEMU; cf. PR misc/44767.
271 	 */
272 	const char *val =
273 	    "1.00000011920928977282585492503130808472633361816406";
274 
275 	(void)fesetround(FE_UPWARD);
276 
277 	double d1 = strtod(val, NULL);
278 
279 	(void)fesetround(FE_DOWNWARD);
280 
281 	double d2 = strtod(val, NULL);
282 
283 	if (fabs(d1 - d2) > 0.0)
284 		return;
285 	else {
286 		atf_tc_expect_fail("PR misc/44767");
287 		atf_tc_fail("strtod(3) did not honor fesetround(3)");
288 	}
289 #else
290 	atf_tc_skip("Requires one of i386, amd64 or sparc");
291 #endif
292 }
293 
294 ATF_TC(strtod_underflow);
295 ATF_TC_HEAD(strtod_underflow, tc)
296 {
297 	atf_tc_set_md_var(tc, "descr", "Test underflow in strtod(3)");
298 }
299 
300 ATF_TC_BODY(strtod_underflow, tc)
301 {
302 
303 	const char *tmp =
304 	    "0.0000000000000000000000000000000000000000000000000000"
305 	    "000000000000000000000000000000000000000000000000000000"
306 	    "000000000000000000000000000000000000000000000000000000"
307 	    "000000000000000000000000000000000000000000000000000000"
308 	    "000000000000000000000000000000000000000000000000000000"
309 	    "000000000000000000000000000000000000000000000000000000"
310 	    "000000000000000000000000000000000000000000000000000000"
311 	    "000000000000000002";
312 
313 	errno = 0;
314 	double d = strtod(tmp, NULL);
315 
316 	if (d != 0 || errno != ERANGE)
317 		atf_tc_fail("strtod(3) did not detect underflow");
318 }
319 
320 ATF_TP_ADD_TCS(tp)
321 {
322 
323 	ATF_TP_ADD_TC(tp, strtod_basic);
324 	ATF_TP_ADD_TC(tp, strtod_hex);
325 	ATF_TP_ADD_TC(tp, strtod_inf);
326 	ATF_TP_ADD_TC(tp, strtof_inf);
327 	ATF_TP_ADD_TC(tp, strtold_inf);
328 	ATF_TP_ADD_TC(tp, strtod_nan);
329 	ATF_TP_ADD_TC(tp, strtof_nan);
330 	ATF_TP_ADD_TC(tp, strtold_nan);
331 	ATF_TP_ADD_TC(tp, strtod_round);
332 	ATF_TP_ADD_TC(tp, strtod_underflow);
333 
334 	return atf_no_error();
335 }
336