xref: /netbsd-src/tests/lib/libc/stdlib/t_strtod.c (revision 288bb96063654ec504ca8732afc683d3ebc514b5)
1 /*	$NetBSD: t_strtod.c,v 1.22 2011/07/04 22:33:29 mrg 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.22 2011/07/04 22:33:29 mrg 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 	 * See the closed PR lib/33262.
166 	 *
167 	 * This may also fail under QEMU; cf. PR misc/44767.
168 	 */
169 	if (system("cpuctl identify 0 | grep -q QEMU") == 0)
170 		atf_tc_expect_fail("PR misc/44767");
171 
172 	for (size_t i = 0; i < __arraycount(inf_strings); i++) {
173 		long double ld = strtold(inf_strings[i], NULL);
174 		ATF_REQUIRE(isinf(ld) != 0);
175 	}
176 #   else
177 	atf_tc_skip("Requires long double support");
178 #   endif
179 #else
180 	atf_tc_skip("vax not supported");
181 #endif
182 }
183 
184 ATF_TC(strtod_nan);
185 ATF_TC_HEAD(strtod_nan, tc)
186 {
187 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with NaN");
188 }
189 
190 ATF_TC_BODY(strtod_nan, tc)
191 {
192 #ifndef __vax__
193 	char *end;
194 
195 	double d = strtod(nan_string, &end);
196 	ATF_REQUIRE(isnan(d) != 0);
197 	ATF_REQUIRE(strcmp(end, "y") == 0);
198 #else
199 	atf_tc_skip("vax not supported");
200 #endif
201 }
202 
203 ATF_TC(strtof_nan);
204 ATF_TC_HEAD(strtof_nan, tc)
205 {
206 	atf_tc_set_md_var(tc, "descr", "A strtof(3) with NaN");
207 }
208 
209 ATF_TC_BODY(strtof_nan, tc)
210 {
211 #ifndef __vax__
212 	char *end;
213 
214 	float f = strtof(nan_string, &end);
215 	ATF_REQUIRE(isnanf(f) != 0);
216 	ATF_REQUIRE(strcmp(end, "y") == 0);
217 #else
218 	atf_tc_skip("vax not supported");
219 #endif
220 }
221 
222 ATF_TC(strtold_nan);
223 ATF_TC_HEAD(strtold_nan, tc)
224 {
225 	atf_tc_set_md_var(tc, "descr", "A strtold(3) with NaN");
226 }
227 
228 ATF_TC_BODY(strtold_nan, tc)
229 {
230 #ifndef __vax__
231 #   ifdef __HAVE_LONG_DOUBLE
232 	char *end;
233 
234 	/*
235 	 * See PR lib/45020.
236 	 *
237 	 * This may also fail under QEMU; cf. PR misc/44767.
238 	 */
239 	if (system("cpuctl identify 0 | grep -q QEMU") == 0)
240 		atf_tc_expect_fail("PR misc/44767");
241 
242 	long double ld = strtold(nan_string, &end);
243 	ATF_REQUIRE(isnan(ld) != 0);
244 	ATF_REQUIRE(__isnanl(ld) != 0);
245 	ATF_REQUIRE(strcmp(end, "y") == 0);
246 #   else
247 	atf_tc_skip("Requires long double support");
248 #   endif
249 #else
250 	atf_tc_skip("vax not supported");
251 #endif
252 }
253 
254 ATF_TC(strtod_round);
255 ATF_TC_HEAD(strtod_round, tc)
256 {
257 	atf_tc_set_md_var(tc, "descr", "Test rouding in strtod(3)");
258 }
259 
260 ATF_TC_BODY(strtod_round, tc)
261 {
262 #if defined(__i386__) || defined(__amd64__) || defined(__sparc__)
263 	/*
264 	 * Test that strtod(3) honors the current rounding mode.
265 	 * The used value is somewhere near 1 + DBL_EPSILON + FLT_EPSILON.
266 	 *
267 	 * May fail under QEMU; cf. PR misc/44767.
268 	 */
269 	if (system("cpuctl identify 0 | grep -q QEMU") == 0)
270 		atf_tc_expect_fail("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 	ATF_REQUIRE(fabs(d1 - d2) > 0.0);
284 #else
285 	atf_tc_skip("Requires one of i386, amd64 or sparc");
286 #endif
287 }
288 
289 ATF_TC(strtod_underflow);
290 ATF_TC_HEAD(strtod_underflow, tc)
291 {
292 	atf_tc_set_md_var(tc, "descr", "Test underflow in strtod(3)");
293 }
294 
295 ATF_TC_BODY(strtod_underflow, tc)
296 {
297 
298 	const char *tmp =
299 	    "0.0000000000000000000000000000000000000000000000000000"
300 	    "000000000000000000000000000000000000000000000000000000"
301 	    "000000000000000000000000000000000000000000000000000000"
302 	    "000000000000000000000000000000000000000000000000000000"
303 	    "000000000000000000000000000000000000000000000000000000"
304 	    "000000000000000000000000000000000000000000000000000000"
305 	    "000000000000000000000000000000000000000000000000000000"
306 	    "000000000000000002";
307 
308 	errno = 0;
309 	double d = strtod(tmp, NULL);
310 
311 	if (d != 0 || errno != ERANGE)
312 		atf_tc_fail("strtod(3) did not detect underflow");
313 }
314 
315 ATF_TP_ADD_TCS(tp)
316 {
317 
318 	ATF_TP_ADD_TC(tp, strtod_basic);
319 	ATF_TP_ADD_TC(tp, strtod_hex);
320 	ATF_TP_ADD_TC(tp, strtod_inf);
321 	ATF_TP_ADD_TC(tp, strtof_inf);
322 	ATF_TP_ADD_TC(tp, strtold_inf);
323 	ATF_TP_ADD_TC(tp, strtod_nan);
324 	ATF_TP_ADD_TC(tp, strtof_nan);
325 	ATF_TP_ADD_TC(tp, strtold_nan);
326 	ATF_TP_ADD_TC(tp, strtod_round);
327 	ATF_TP_ADD_TC(tp, strtod_underflow);
328 
329 	return atf_no_error();
330 }
331