t_strtod.c revision 1.12 1 /* $NetBSD: t_strtod.c,v 1.12 2011/05/31 20:17:36 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 (at) drijf.net>, 2006. */
33
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_strtod.c,v 1.12 2011/05/31 20:17:36 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 ATF_TC(strtod_basic);
51 ATF_TC_HEAD(strtod_basic, tc)
52 {
53 atf_tc_set_md_var(tc, "descr", "A basic test of strtod(3)");
54 }
55
56 ATF_TC_BODY(strtod_basic, tc)
57 {
58 char buf[512];
59 size_t i, n;
60 double d;
61
62 n = 1024 * 1000;
63
64 for (i = 1; i < n; i = i + 1024) {
65
66 (void)snprintf(buf, sizeof(buf), "%zu.%zu", i, i + 1);
67
68 errno = 0;
69 d = strtod(buf, NULL);
70
71 ATF_REQUIRE(d > 0.0);
72 ATF_REQUIRE(errno == 0);
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 double d;
87
88 str = "-0x0";
89 d = strtod(str, &end); /* -0.0 */
90
91 ATF_REQUIRE(end == str + 4);
92 ATF_REQUIRE(signbit(d) != 0);
93 ATF_REQUIRE(fabs(d) < 1.0e-40);
94
95 str = "-0x";
96 d = strtod(str, &end); /* -0.0 */
97
98 ATF_REQUIRE(end == str + 2);
99 ATF_REQUIRE(signbit(d) != 0);
100 ATF_REQUIRE(fabs(d) < 1.0e-40);
101 }
102
103 ATF_TC(strtod_inf);
104 ATF_TC_HEAD(strtod_inf, tc)
105 {
106 atf_tc_set_md_var(tc, "descr", "A strtod(3) with INF");
107 }
108
109 ATF_TC_BODY(strtod_inf, tc)
110 {
111 #ifndef __vax__
112
113 long double ld;
114 double d;
115 float f;
116
117 /*
118 * See the closed PR lib/33262.
119 *
120 * This may also fail under QEMU; cf. PR misc/44767.
121 */
122 if (system("cpuctl identify 0 | grep -q QEMU") == 0)
123 atf_tc_expect_fail("PR misc/44767");
124
125 d = strtod("INF", NULL);
126 ATF_REQUIRE(isinf(d) != 0);
127
128 f = strtof("INF", NULL);
129 ATF_REQUIRE(isinf(f) != 0);
130
131 ld = strtold("INF", NULL);
132 ATF_REQUIRE(isinf(ld) != 0);
133 #endif
134 }
135
136 ATF_TC(strtod_round);
137 ATF_TC_HEAD(strtod_round, tc)
138 {
139 atf_tc_set_md_var(tc, "descr", "Test rouding in strtod(3)");
140 }
141
142 ATF_TC_BODY(strtod_round, tc)
143 {
144 #if defined(__i386__) || defined(__amd64__) || defined(__sparc__)
145
146 const char *val;
147 double d1, d2;
148
149 /*
150 * Test that strtod(3) honors the current rounding mode.
151 * The used value is somewhere near 1 + DBL_EPSILON + FLT_EPSILON.
152 *
153 * May fail under QEMU; cf. PR misc/44767.
154 */
155 if (system("cpuctl identify 0 | grep -q QEMU") == 0)
156 atf_tc_expect_fail("PR misc/44767");
157
158 val = "1.00000011920928977282585492503130808472633361816406";
159
160 (void)fesetround(FE_UPWARD);
161
162 d1 = strtod(val, NULL);
163
164 (void)fesetround(FE_DOWNWARD);
165
166 d2 = strtod(val, NULL);
167
168 ATF_REQUIRE(fabs(d1 - d2) > 0.0);
169 #endif
170 }
171
172 ATF_TC(strtod_underflow);
173 ATF_TC_HEAD(strtod_underflow, tc)
174 {
175 atf_tc_set_md_var(tc, "descr", "Test underflow in strtod(3)");
176 }
177
178 ATF_TC_BODY(strtod_underflow, tc)
179 {
180
181 const char *tmp =
182 "0.0000000000000000000000000000000000000000000000000000"
183 "000000000000000000000000000000000000000000000000000000"
184 "000000000000000000000000000000000000000000000000000000"
185 "000000000000000000000000000000000000000000000000000000"
186 "000000000000000000000000000000000000000000000000000000"
187 "000000000000000000000000000000000000000000000000000000"
188 "000000000000000000000000000000000000000000000000000000"
189 "000000000000000002";
190
191 double d;
192
193 errno = 0;
194 d = strtod(tmp, NULL);
195
196 if (errno != ERANGE)
197 atf_tc_fail("strtod(3) did not detect underflow");
198 }
199
200 ATF_TP_ADD_TCS(tp)
201 {
202
203 ATF_TP_ADD_TC(tp, strtod_basic);
204 ATF_TP_ADD_TC(tp, strtod_hex);
205 ATF_TP_ADD_TC(tp, strtod_inf);
206 ATF_TP_ADD_TC(tp, strtod_round);
207 ATF_TP_ADD_TC(tp, strtod_underflow);
208
209 return atf_no_error();
210 }
211