strtod: add zero test
[libc-test] / src / stdlib / strtod.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include "test.h"
6
7 /* r = place to store result
8  * f = function call to test (or any expression)
9  * x = expected result
10  * m = message to print on failure (with formats for r & x)
11 **/
12
13 #define TEST(r, f, x, m) ( \
14         ((r) = (f)) == (x) || \
15         (error("%s failed (" m ")\n", #f, r, x, r-x), 0) )
16
17 void test_strtod_simple(void) {
18         int i;
19         double d, d2;
20         char buf[1000];
21
22         for (i=0; i<100; i++) {
23                 d = sin(i);
24                 snprintf(buf, sizeof buf, "%.300f", d);
25                 TEST(d2, strtod(buf, 0), d, "round trip fail %a != %a (%a)");
26         }
27
28         TEST(d, strtod("0x1p4", 0), 16.0, "hex float %a != %a");
29         TEST(d, strtod("0x1.1p4", 0), 17.0, "hex float %a != %a");
30 }
31
32 #define length(x) (sizeof(x) / sizeof(*(x)))
33
34 /* TODO: float exceptions, rounding mode, endptr check */
35
36 static struct {
37         char *s;
38         long double f;
39 } tl[] = {
40         {"12.345", 12.345L},
41         {"1.2345e1", 12.345L},
42         // 2^-16445 * 0.5 - eps
43         {".1822599765941237301264202966809709908199525407846781671860490243514185844316698e-4950", 0},
44         // 2^-16445 * 0.5 + eps
45         {".1822599765941237301264202966809709908199525407846781671860490243514185844316699e-4950", 0x1p-16445L},
46         // 2^-16445 * 1.5 - eps
47         {".5467799297823711903792608900429129724598576223540345015581470730542557532950096e-4950", 0x1p-16445L},
48         // 2^-16445 * 1.5 + eps
49         {".5467799297823711903792608900429129724598576223540345015581470730542557532950097e-4950", 0x1p-16444L},
50         // 2^-16382 + 2^-16446 - eps
51         {".3362103143112093506444937793915876332724499641527442230928779770593420866576777e-4931", 0x1p-16382L},
52         // 2^-16382 + 2^-16446 + eps
53         {".3362103143112093506444937793915876332724499641527442230928779770593420866576778e-4931", 0x1.0000000000000002p-16382L},
54         // 2^16384 - 2^16319 - eps
55         {"118973149535723176505351158982948.86679662540046955672e4900", 0x1.fffffffffffffffep16383L},
56         // 2^16384 - 2^16319 + eps
57         {"118973149535723176505351158982948.86679662540046955673e4900", INFINITY},
58 };
59 static struct {
60         char *s;
61         double f;
62 } t[] = {
63         {"0", 0.0},
64         {"00.00", 0.0},
65         {"-.00000", -0.0},
66         {"1e+1000000", INFINITY},
67         {"1e-1000000", 0},
68         // 2^-1074 * 0.5 - eps
69         {".2470328229206232720882843964341106861825299013071623822127928412503377536351043e-323", 0},
70         // 2^-1074 * 0.5 + eps
71         {".2470328229206232720882843964341106861825299013071623822127928412503377536351044e-323", 0x1p-1074},
72         // 2^-1074 * 1.5 - eps
73         {".7410984687618698162648531893023320585475897039214871466383785237510132609053131e-323", 0x1p-1074},
74         // 2^-1074 * 1.5 + eps
75         {".7410984687618698162648531893023320585475897039214871466383785237510132609053132e-323", 0x1p-1073},
76         // 2^-1022 + 2^-1075 - eps
77         {".2225073858507201630123055637955676152503612414573018013083228724049586647606759e-307", 0x1p-1022},
78         // 2^-1022 + 2^-1075 + eps
79         {".2225073858507201630123055637955676152503612414573018013083228724049586647606760e-307", 0x1.0000000000001p-1022},
80         // 2^1024 - 2^970 - eps
81         {"17976931348623158079372897140530341507993413271003782693617377898044"
82         "49682927647509466490179775872070963302864166928879109465555478519404"
83         "02630657488671505820681908902000708383676273854845817711531764475730"
84         "27006985557136695962284291481986083493647529271907416844436551070434"
85         "2711559699508093042880177904174497791.999999999999999999999999999999", 0x1.fffffffffffffp1023},
86         // 2^1024 - 2^970
87         {"17976931348623158079372897140530341507993413271003782693617377898044"
88         "49682927647509466490179775872070963302864166928879109465555478519404"
89         "02630657488671505820681908902000708383676273854845817711531764475730"
90         "27006985557136695962284291481986083493647529271907416844436551070434"
91         "2711559699508093042880177904174497792", INFINITY},
92 };
93 static struct {
94         char *s;
95         float f;
96 } tf[] = {
97         // 2^-149 * 0.5 - eps
98         {".7006492321624085354618647916449580656401309709382578858785341419448955413429303e-45", 0},
99         // 2^-149 * 0.5 + eps
100         {".7006492321624085354618647916449580656401309709382578858785341419448955413429304e-45", 0x1p-149},
101         // 2^-149 * 0.5 - eps
102         {".2101947696487225606385594374934874196920392912814773657635602425834686624028790e-44", 0x1p-149},
103         // 2^-149 * 0.5 + eps
104         {".2101947696487225606385594374934874196920392912814773657635602425834686624028791e-44", 0x1p-148},
105         // 2^-126 + 2^-150 - eps
106         {".1175494420887210724209590083408724842314472120785184615334540294131831453944281e-37", 0x1p-126},
107         // 2^-126 + 2^-150 + eps
108         {".1175494420887210724209590083408724842314472120785184615334540294131831453944282e-37", 0x1.000002p-126},
109         // 2^128 - 2^103 - eps
110         {"340282356779733661637539395458142568447.9999999999999999999", 0x1.fffffep127},
111         // 2^128 - 2^103
112         {"340282356779733661637539395458142568448", INFINITY},
113 };
114
115
116 void test_strtold()
117 {
118         int i;
119         long double x;
120         char *p;
121
122         for (i = 0; i < length(tl); i++) {
123                 x = strtold(tl[i].s, &p);
124                 if (x != tl[i].f)
125                         error("strtold(\"%s\") want %La got %La\n", tl[i].s, tl[i].f, x);
126         }
127 }
128
129 void test_strtod()
130 {
131         int i;
132         double x;
133         char *p;
134
135         for (i = 0; i < length(t); i++) {
136                 x = strtod(t[i].s, &p);
137                 if (x != t[i].f)
138                         error("strtod(\"%s\") want %a got %a\n", t[i].s, t[i].f, x);
139         }
140 }
141
142 void test_strtof()
143 {
144         int i;
145         float x;
146         char *p;
147
148         for (i = 0; i < length(tf); i++) {
149                 x = strtof(tf[i].s, &p);
150                 if (x != tf[i].f)
151                         error("strtof(\"%s\") want %a got %a\n", tf[i].s, tf[i].f, x);
152         }
153 }