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