1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2022 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <math.h>
39 #include <string.h>
40 
41 #if defined (TINY_STDIO) || !defined(__PICOLIBC__)
42 #define FULL_TESTS
43 #endif
44 
45 #pragma GCC diagnostic ignored "-Wpragmas"
46 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
47 #pragma GCC diagnostic ignored "-Wunused-value"
48 #pragma GCC diagnostic ignored "-Woverflow"
49 #pragma GCC diagnostic ignored "-Wliteral-range"
50 
51 static const struct {
52     char        *string;
53     double      dvalue;
54     float       fvalue;
55     long double ldvalue;
56     char        *end_test;
57 } tests[] = {
58     { "1e2@", 100.0, 100.0f, 100.0l, "@" },
59 #ifdef FULL_TESTS
60 #if !defined(TINY_STDIO) || defined(_IO_FLOAT_EXACT)
61     { "3752432815e-39%", 0x1.306efbp-98, 0x1.306efcp-98f, 3752432815e-39l, "%" },
62     { "3.752432815e-30%", 0x1.306efbp-98, 0x1.306efcp-98f, 3752432815e-39l, "%" },
63     { "3752432814e-39^", 3752432814e-39, 0x1.306efap-98f, 3752432814e-39l, "^" },
64     { "3.752432814e-30^", 3752432814e-39, 0x1.306efap-98f, 3752432814e-39l, "^" },
65 #endif
66     { "0x10.000@", 16.0, 16.0f, 16.0l, "@" },
67     { "0x10.000p@", 16.0, 16.0f, 16.0l, "p@" },
68     { "0x10.000p+@", 16.0, 16.0f, 16.0l, "p+@" },
69     { "0x10.000p+1@", 32.0, 32.0f, 32.0l, "@" },
70     { "0x10.000p-@", 16.0, 16.0f, 16.0l, "p-@" },
71     { "0x10.000p-1@", 8.0, 8.0f, 8.0l, "@" },
72     { "0x11.000@", 17.0, 17.0f, 17.0l, "@" },
73     { "0x0.01@", 1/256.0, 1/256.0f, 1/256.0l, "@" },
74     { "0x0.01p", 1/256.0, 1/256.0f, 1/256.0l, "p" },
75     { "0x0.01p+", 1/256.0, 1/256.0f, 1/256.0l, "p+" },
76     { "0x1p-1@", 0.5, 0.5f, 0.5l, "@" },
77     { "0x1p-10000000000000000000000000@", 0.0, 0.0f, 0.0l, "@" },
78     { "0x1p0@", 1.0, 1.0f, 1.0l, "@" },
79     { "0x10p0@", 16.0, 16.0f, 16.0l, "@" },
80     { "0x1p-1023@", 0x1p-1023, 0.0f, 0x1p-1023l, "@" },
81     /* Check round-to-even for floats */
82     { "0x1.000002p0@", 0x1.000002p0, 0x1.000002p0f, 0x1.000002p0l, "@" },
83     { "0x1.000003p0@", 0x1.000003p0, 0x1.000004p0f, 0x1.000003p0l, "@" },
84     { "0x1.000001p0@", 0x1.000001p0, 0x1.000000p0f, 0x1.000001p0l, "@" },
85     { "0x1.000001000000001p0@", 0x1.000001000000001p0, 0x1.000002p0f, 0x1.000001000000001p0l, "@" },
86 #if __SIZEOF_DOUBLE__ == 8
87     /* Check round-to-even for doubles */
88     { "0x100000000000008p0@", 0x1p56, 0x1p56f, 0x1.00000000000008p56l, "@" },
89     { "0x100000000000008.p0@", 0x1p56, 0x1p56f, 0x1.00000000000008p56l, "@" },
90     { "0x100000000000008.00p0@", 0x1p56,0x1p56f, 0x1.00000000000008p56l, "@" },
91     { "0x10000000000000800p0@", 0x1p64, 0x1p64f, 0x1.00000000000008p64l, "@" },
92     { "0x10000000000000801p0@", 0x1.0000000000001p64, 0x1p64f, 0x1.0000000000000801p64l, "@" },
93     { "0x10000000000000800.0000000000001p0@", 0x1.0000000000001p64, 0x1p64f, 0x1.00000000000008p64l, "@" },
94     { "0x10000000000001800p0@", 0x1.0000000000002p64, 0x1p64f, 0x1.00000000000018p64l, "@" },
95 #endif
96     /* Check max values for floats */
97     { "0x1.fffffep126@", 0x1.fffffep126,  0x1.fffffep126f, 0x1.fffffep126l, "@" },
98     { "0x1.ffffffp126@", 0x1.ffffffp126,  0x1.000000p127f, 0x1.ffffffp126l, "@" },
99     { "0x1.fffffep127@", 0x1.fffffep127,  0x1.fffffep127f, 0x1.fffffep127l, "@" },
100     { "0x1.ffffffp127@", 0x1.ffffffp127, (float) INFINITY, 0x1.ffffffp127l, "@" },   /* rounds up to INFINITY for float */
101 #if __SIZEOF_DOUBLE__ == 8
102     /* Check max values for doubles */
103     { "0x1.fffffffffffffp1022@",  0x1.fffffffffffffp1022, (float) INFINITY, 0x1.fffffffffffffp1022l, "@" },
104     { "0x1.fffffffffffff8p1022@", 0x1.0000000000000p1023, (float) INFINITY, 0x1.fffffffffffff8p1022l, "@" }, /* rounds up for double */
105     { "0x1.fffffffffffffp1023@",  0x1.fffffffffffffp1023, (float) INFINITY, 0x1.fffffffffffffp1023l, "@" },
106 #endif
107 #if defined(_TEST_LONG_DOUBLE) && __SIZEOF_LONG_DOUBLE__ > 8
108 #if __LDBL_MANT_DIG__ > __DBL_MANT_DIG__
109     { "0x1.fffffffffffff8p1023@",      (double) INFINITY, (float) INFINITY, 0x1.fffffffffffff8p1023l, "@" }, /* rounds up to INFINITY for double */
110 #else
111     { "0x1.fffffffffffff8p1023@",      (double) INFINITY, (float) INFINITY, (long double) INFINITY, "@" }, /* rounds up to INFINITY for double */
112 #endif
113     /* Check max values for long doubles */
114 #if __LDBL_MANT_DIG__ == 113
115     { "0x1.ffffffffffffffffffffffffffffp16382@",  (double) INFINITY, (float) INFINITY, 0x1.ffffffffffffffffffffffffffffp16382l, "@" },
116     { "0x1.ffffffffffffffffffffffffffff8p16382@", (double) INFINITY, (float) INFINITY, 0x1.0000000000000000000000000000p16383l, "@" }, /* rounds up for long double */
117     { "0x1.ffffffffffffffffffffffffffffp16383@",  (double) INFINITY, (float) INFINITY, 0x1.ffffffffffffffffffffffffffffp16383l, "@" },
118     { "0x1.ffffffffffffffffffffffffffff8p16383@", (double) INFINITY, (float) INFINITY, (long double) INFINITY, "@" },                  /* rounds up to INFINITY for long oduble */
119 #elif __LDBL_MANT_DIG__ == 64
120     { "0x1.fffffffffffffffep16382@", (double) INFINITY, (float) INFINITY, 0x1.fffffffffffffffep16382l, "@" },
121     { "0x1.ffffffffffffffffp16382@", (double) INFINITY, (float) INFINITY, 0x1.0000000000000000p16383l, "@" },        /* rounds up for long double */
122     { "0x1.fffffffffffffffep16383@", (double) INFINITY, (float) INFINITY, 0x1.fffffffffffffffep16383l, "@" },
123     { "0x1.ffffffffffffffffp16383@", (double) INFINITY, (float) INFINITY, (long double) INFINITY, "@" },             /* rounds up to INFINITY for long double */
124 #endif
125 #endif
126 #endif
127 };
128 
129 #define NTESTS (sizeof(tests)/sizeof(tests[0]))
130 
main(void)131 int main(void)
132 {
133     int i;
134     double d;
135     float f;
136 #ifdef _TEST_LONG_DOUBLE
137 #ifdef FULL_TESTS
138     long double ld;
139 #endif
140 #endif
141     char *end;
142     int ret = 0;
143 
144     for (i = 0; i < (int) NTESTS; i++) {
145         d = strtod(tests[i].string, &end);
146         if (d != tests[i].dvalue) {
147             printf("strtod(\"%s\"): got %.17e %a want %.17e %a\n", tests[i].string,
148                    d, d, tests[i].dvalue, tests[i].dvalue);
149             ret = 1;
150         }
151         if (strcmp(end, tests[i].end_test) != 0) {
152             printf("strtod(\"%s\") end is \"%s\" expected \"%s\"\n",
153                    tests[i].string, end, tests[i].end_test);
154             ret = 1;
155         }
156         f = strtof(tests[i].string, &end);
157         if (f != tests[i].fvalue) {
158             printf("strtof(\"%s\"): got %.17e %a want %.17e %a\n", tests[i].string,
159                    (double) f, (double) f, (double) tests[i].fvalue, (double) tests[i].fvalue);
160             ret = 1;
161         }
162         if (strcmp(end, tests[i].end_test) != 0) {
163             printf("strtof(\"%s\") end is \"%s\" expected \"%s\"\n",
164                    tests[i].string, end, tests[i].end_test);
165             ret = 1;
166         }
167         if (tests[i].end_test[0] == '%') {
168             if ((float) d != f) {
169                 printf("strtof(\"%s\") got %a strtod got %a\n", tests[i].string, (double) f, d);
170                 ret = 1;
171             }
172         }
173 #ifdef _TEST_LONG_DOUBLE
174 #ifdef FULL_TESTS
175         if (sizeof(long double) > sizeof(double)
176             && tests[i].end_test[0] != '%'
177             && tests[i].end_test[0] != '^')
178         {
179             ld = strtold(tests[i].string, &end);
180             if (ld != tests[i].ldvalue) {
181                 printf("strtold(\"%s\"): got %.17Le %La want %.17Le %La\n", tests[i].string,
182                        ld, ld, tests[i].ldvalue, tests[i].ldvalue);
183                 ret = 1;
184             }
185             if (strcmp(end, tests[i].end_test) != 0) {
186                 printf("strtold(\"%s\") end is \"%s\" expected \"%s\"\n",
187                        tests[i].string, end, tests[i].end_test);
188                 ret = 1;
189             }
190         }
191 #endif
192 #endif
193     }
194     return ret;
195 }
196