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