1 /* $NetBSD: t_strtod.c,v 1.37 2024/06/15 12:20:22 rillig 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.37 2024/06/15 12:20:22 rillig Exp $"); 36 37 #include <errno.h> 38 #include <fenv.h> 39 #include <float.h> 40 #include <math.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include <atf-c.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(strtold_basic); 77 ATF_TC_HEAD(strtold_basic, tc) 78 { 79 atf_tc_set_md_var(tc, "descr", "Some examples of strtold(3)"); 80 } 81 82 ATF_TC_BODY(strtold_basic, tc) 83 { 84 static const struct { 85 const char *str; 86 long double val; 87 } testcases[] = { 88 { "0x0p0", 0.0L }, 89 { "0x1.234p0", 0x1234 / 4096.0L }, 90 { "0x1.234p0", 0x1234 / 4096.0L }, 91 #if FLT_RADIX == 2 && LDBL_MAX_EXP == 16384 && LDBL_MANT_DIG == 64 92 { "2.16", 0x8.a3d70a3d70a3d71p-2L }, 93 #endif 94 }; 95 96 for (size_t i = 0, n = __arraycount(testcases); i < n; i++) { 97 char *end; 98 errno = 0; 99 long double val = strtold(testcases[i].str, &end); 100 101 ATF_CHECK_MSG( 102 errno == 0 && *end == '\0' && val == testcases[i].val, 103 "'%s' want %La have %La errno %d end '%s'", 104 testcases[i].str, testcases[i].val, val, errno, end); 105 } 106 } 107 108 ATF_TC(strtod_hex); 109 ATF_TC_HEAD(strtod_hex, tc) 110 { 111 atf_tc_set_md_var(tc, "descr", "A strtod(3) with hexadecimals"); 112 } 113 114 ATF_TC_BODY(strtod_hex, tc) 115 { 116 const char *str; 117 char *end; 118 volatile double d; 119 120 str = "-0x0"; 121 d = strtod(str, &end); /* -0.0 */ 122 123 ATF_CHECK_EQ_MSG(end, str + 4, "str=%p end=%p", str, end); 124 ATF_CHECK_MSG(signbit(d) != 0, "d=%g=%a signbit=%d", d, d, signbit(d)); 125 ATF_CHECK_EQ_MSG(fabs(d), 0, "d=%g=%a, fabs(d)=%g=%a", 126 d, d, fabs(d), fabs(d)); 127 128 str = "-0x"; 129 d = strtod(str, &end); /* -0.0 */ 130 131 ATF_CHECK_EQ_MSG(end, str + 2, "str=%p end=%p", str, end); 132 ATF_CHECK_MSG(signbit(d) != 0, "d=%g=%a signbit=%d", d, d, signbit(d)); 133 ATF_CHECK_EQ_MSG(fabs(d), 0, "d=%g=%a fabs(d)=%g=%a", 134 d, d, fabs(d), fabs(d)); 135 } 136 137 ATF_TC(strtod_inf); 138 ATF_TC_HEAD(strtod_inf, tc) 139 { 140 atf_tc_set_md_var(tc, "descr", "A strtod(3) with INF (PR lib/33262)"); 141 } 142 143 ATF_TC_BODY(strtod_inf, tc) 144 { 145 146 if (!isinf(INFINITY)) 147 atf_tc_skip("no infinities on this architecture"); 148 149 for (size_t i = 0; i < __arraycount(inf_strings); i++) { 150 volatile double d = strtod(inf_strings[i], NULL); 151 ATF_CHECK_MSG(isinf(d), "inf_strings[%zu]=\"%s\" d=%g=%a", 152 i, inf_strings[i], d, d); 153 } 154 } 155 156 ATF_TC(strtof_inf); 157 ATF_TC_HEAD(strtof_inf, tc) 158 { 159 atf_tc_set_md_var(tc, "descr", "A strtof(3) with INF (PR lib/33262)"); 160 } 161 162 ATF_TC_BODY(strtof_inf, tc) 163 { 164 165 if (!isinf(INFINITY)) 166 atf_tc_skip("no infinities on this architecture"); 167 168 for (size_t i = 0; i < __arraycount(inf_strings); i++) { 169 volatile float f = strtof(inf_strings[i], NULL); 170 ATF_CHECK_MSG(isinf(f), "inf_strings[%zu]=\"%s\" f=%g=%a", 171 i, inf_strings[i], f, f); 172 ATF_CHECK_MSG(isinff(f), "inf_strings[%zu]=\"%s\" f=%g=%a", 173 i, inf_strings[i], f, f); 174 } 175 } 176 177 ATF_TC(strtold_inf); 178 ATF_TC_HEAD(strtold_inf, tc) 179 { 180 atf_tc_set_md_var(tc, "descr", "A strtold(3) with INF (PR lib/33262)"); 181 } 182 183 ATF_TC_BODY(strtold_inf, tc) 184 { 185 186 if (!isinf(INFINITY)) 187 atf_tc_skip("no infinities on this architecture"); 188 189 for (size_t i = 0; i < __arraycount(inf_strings); i++) { 190 volatile long double ld = strtold(inf_strings[i], NULL); 191 ATF_CHECK_MSG(isinf(ld), "inf_strings[%zu]=\"%s\" ld=%Lg=%La", 192 i, inf_strings[i], ld, ld); 193 } 194 } 195 196 ATF_TC(strtod_nan); 197 ATF_TC_HEAD(strtod_nan, tc) 198 { 199 atf_tc_set_md_var(tc, "descr", "A strtod(3) with NaN"); 200 } 201 202 ATF_TC_BODY(strtod_nan, tc) 203 { 204 char *end; 205 206 #ifndef NAN 207 atf_tc_skip("no NaNs on this architecture"); 208 #endif 209 210 volatile double d = strtod(nan_string, &end); 211 ATF_CHECK_MSG(isnan(d), "nan_string=\"%s\" d=%g=%a", nan_string, d, d); 212 ATF_CHECK_MSG(strcmp(end, "y") == 0, "nan_string=\"%s\"@%p end=%p", 213 nan_string, nan_string, end); 214 } 215 216 ATF_TC(strtof_nan); 217 ATF_TC_HEAD(strtof_nan, tc) 218 { 219 atf_tc_set_md_var(tc, "descr", "A strtof(3) with NaN"); 220 } 221 222 ATF_TC_BODY(strtof_nan, tc) 223 { 224 char *end; 225 226 #ifndef NAN 227 atf_tc_skip("no NaNs on this architecture"); 228 #endif 229 230 volatile float f = strtof(nan_string, &end); 231 ATF_CHECK_MSG(isnan(f), "nan_string=\"%s\" f=%g=%a", nan_string, f, f); 232 ATF_CHECK_MSG(isnanf(f), "nan_string=\"%s\" f=%g=%a", nan_string, 233 f, f); 234 ATF_CHECK_MSG(strcmp(end, "y") == 0, "nan_string=\"%s\"@%p end=%p", 235 nan_string, nan_string, end); 236 } 237 238 ATF_TC(strtold_nan); 239 ATF_TC_HEAD(strtold_nan, tc) 240 { 241 atf_tc_set_md_var(tc, "descr", "A strtold(3) with NaN (PR lib/45020)"); 242 } 243 244 ATF_TC_BODY(strtold_nan, tc) 245 { 246 char *end; 247 248 #ifndef NAN 249 atf_tc_skip("no NaNs on this architecture"); 250 #endif 251 252 volatile long double ld = strtold(nan_string, &end); 253 ATF_CHECK_MSG(isnan(ld), "nan_string=\"%s\" ld=%Lg=%La", 254 nan_string, ld, ld); 255 ATF_CHECK_MSG(isnanf(ld), "nan_string=\"%s\" ld=%Lg=%La", 256 nan_string, ld, ld); 257 ATF_CHECK_MSG(strcmp(end, "y") == 0, "nan_string=\"%s\"@%p end=%p", 258 nan_string, nan_string, end); 259 } 260 261 ATF_TC(strtod_round); 262 ATF_TC_HEAD(strtod_round, tc) 263 { 264 atf_tc_set_md_var(tc, "descr", "Test rounding in strtod(3)"); 265 } 266 267 ATF_TC_BODY(strtod_round, tc) 268 { 269 #ifdef __HAVE_FENV 270 271 /* 272 * Test that strtod(3) honors the current rounding mode. 273 * The used value is somewhere near 1 + DBL_EPSILON + FLT_EPSILON. 274 */ 275 const char *val = 276 "1.00000011920928977282585492503130808472633361816406"; 277 278 (void)fesetround(FE_UPWARD); 279 volatile double d1 = strtod(val, NULL); 280 (void)fesetround(FE_DOWNWARD); 281 volatile double d2 = strtod(val, NULL); 282 ATF_CHECK_MSG(d1 > d2, "d1=%g=%a d2=%g=%a", d1, d1, d2, d2); 283 #else 284 atf_tc_skip("Requires <fenv.h> support"); 285 #endif 286 } 287 288 ATF_TC(strtod_underflow); 289 ATF_TC_HEAD(strtod_underflow, tc) 290 { 291 atf_tc_set_md_var(tc, "descr", "Test underflow in strtod(3)"); 292 } 293 294 ATF_TC_BODY(strtod_underflow, tc) 295 { 296 297 const char *tmp = 298 "0.0000000000000000000000000000000000000000000000000000" 299 "000000000000000000000000000000000000000000000000000000" 300 "000000000000000000000000000000000000000000000000000000" 301 "000000000000000000000000000000000000000000000000000000" 302 "000000000000000000000000000000000000000000000000000000" 303 "000000000000000000000000000000000000000000000000000000" 304 "000000000000000000000000000000000000000000000000000000" 305 "000000000000000002"; 306 307 errno = 0; 308 volatile double d = strtod(tmp, NULL); 309 310 if (d != 0 || errno != ERANGE) 311 atf_tc_fail("strtod(3) did not detect underflow"); 312 } 313 314 /* 315 * Bug found by Geza Herman. 316 * See 317 * http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/ 318 */ 319 ATF_TC(strtod_gherman_bug); 320 ATF_TC_HEAD(strtod_gherman_bug, tc) 321 { 322 atf_tc_set_md_var(tc, "descr", "Test a bug found by Geza Herman"); 323 } 324 325 ATF_TC_BODY(strtod_gherman_bug, tc) 326 { 327 328 const char *str = 329 "1.8254370818746402660437411213933955878019332885742187"; 330 331 errno = 0; 332 volatile double d = strtod(str, NULL); 333 334 ATF_CHECK_EQ_MSG(d, 0x1.d34fd8378ea83p+0, "d=%g=%a", d, d); 335 } 336 337 ATF_TP_ADD_TCS(tp) 338 { 339 340 ATF_TP_ADD_TC(tp, strtod_basic); 341 ATF_TP_ADD_TC(tp, strtold_basic); 342 ATF_TP_ADD_TC(tp, strtod_hex); 343 ATF_TP_ADD_TC(tp, strtod_inf); 344 ATF_TP_ADD_TC(tp, strtof_inf); 345 ATF_TP_ADD_TC(tp, strtold_inf); 346 ATF_TP_ADD_TC(tp, strtod_nan); 347 ATF_TP_ADD_TC(tp, strtof_nan); 348 ATF_TP_ADD_TC(tp, strtold_nan); 349 ATF_TP_ADD_TC(tp, strtod_round); 350 ATF_TP_ADD_TC(tp, strtod_underflow); 351 ATF_TP_ADD_TC(tp, strtod_gherman_bug); 352 353 return atf_no_error(); 354 } 355