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