1 /*
2  * Copyright © 2005-2020 Rich Felker
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <math.h>
28 
29 /* r = place to store result
30  * f = function call to test (or any expression)
31  * x = expected result
32  * m = message to print on failure (with formats for r & x)
33 **/
34 
35 #pragma GCC diagnostic ignored "-Wpragmas"
36 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
37 #pragma GCC diagnostic ignored "-Wformat-extra-args"
38 
39 #define TEST(r, f, x, m) ( \
40 msg = #f, ((r) = (f)) == (x) || \
41 (printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x, r-x), err++, 0) )
42 
43 #define TEST2(r, f, x, m) ( \
44 ((r) = (f)) == (x) || \
45 (printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, msg, r, x), err++, 0) )
46 
test_strtod(void)47 int test_strtod(void)
48 {
49 	int i;
50 	double d, d2;
51 	char buf[1000];
52 	char *msg="";
53 	int err=0;
54 
55         (void) msg;
56 	for (i=0; i<100; i++) {
57 		d = sin(i);
58 		snprintf(buf, sizeof buf, "%.300f", d);
59 		TEST(d2, strtod(buf, 0), d, "round trip fail %a != %a (%a)");
60 	}
61 
62 #if defined(__PICOLIBC__) && !defined(TINY_STDIO) && __SIZEOF_DOUBLE__ != 8
63         /* skip strtod tests for legacy stdio on systems with atypical doubles */
64 #else
65 	TEST(d, strtod("0x1p4", 0), 16.0, "hex float %a != %a");
66 	TEST(d, strtod("0x1.1p4", 0), 17.0, "hex float %a != %a");
67 #endif
68 
69 	return err;
70 }
71 
72 #define TEST_NAME strtod
73 #include "testcase.h"
74