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 
40 #if defined (TINY_STDIO) || !defined(__PICOLIBC__)
41 #define FULL_TESTS
42 #endif
43 
44 #pragma GCC diagnostic ignored "-Wpragmas"
45 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
46 #pragma GCC diagnostic ignored "-Wunused-value"
47 #pragma GCC diagnostic ignored "-Woverflow"
48 #pragma GCC diagnostic ignored "-Wliteral-range"
49 
50 static const struct {
51     char        *string;
52     double      dvalue;
53     float       fvalue;
54     long double ldvalue;
55 } tests[] = {
56     { "1e2@", 100.0, 100.0f, 100.0l },
57 #ifdef FULL_TESTS
58     { "0x1p-1@", 0.5, 0.5f, 0.5l },
59     { "0x1p-10000000000000000000000000@", 0.0, 0.0f, 0.0l },
60     { "0x1p0@", 1.0, 1.0f, 1.0l },
61     { "0x10p0@", 16.0, 16.0f, 16.0l },
62     { "0x1p-1023@", 0x1p-1023, 0.0f, 0x1p-1023l },
63     /* Check round-to-even for floats */
64     { "0x1.000002p0@", 0x1.000002p0, 0x1.000002p0f, 0x1.000002p0l },
65     { "0x1.000003p0@", 0x1.000003p0, 0x1.000004p0f, 0x1.000003p0l },
66     { "0x1.000001p0@", 0x1.000001p0, 0x1.000000p0f, 0x1.000001p0l },
67     { "0x1.000001000000001p0@", 0x1.000001000000001p0, 0x1.000002p0f, 0x1.000001000000001p0l },
68 #if __SIZEOF_DOUBLE__ == 8
69     /* Check round-to-even for doubles */
70     { "0x100000000000008p0@", 0x1p56, 0x1p56f, 0x1.00000000000008p56l },
71     { "0x100000000000008.p0@", 0x1p56, 0x1p56f, 0x1.00000000000008p56l },
72     { "0x100000000000008.00p0@", 0x1p56,0x1p56f, 0x1.00000000000008p56l },
73     { "0x10000000000000800p0@", 0x1p64, 0x1p64f, 0x1.00000000000008p64l },
74     { "0x10000000000000801p0@", 0x1.0000000000001p64, 0x1p64f, 0x1.0000000000000801p64l },
75     { "0x10000000000000800.0000000000001p0@", 0x1.0000000000001p64, 0x1p64f, 0x1.00000000000008p64l },
76     { "0x10000000000001800p0@", 0x1.0000000000002p64, 0x1p64f, 0x1.00000000000018p64l },
77 #endif
78     /* Check max values for floats */
79     { "0x1.fffffep126@", 0x1.fffffep126,  0x1.fffffep126f, 0x1.fffffep126l },
80     { "0x1.ffffffp126@", 0x1.ffffffp126,  0x1.000000p127f, 0x1.ffffffp126l },
81     { "0x1.fffffep127@", 0x1.fffffep127,  0x1.fffffep127f, 0x1.fffffep127l },
82     { "0x1.ffffffp127@", 0x1.ffffffp127, (float) INFINITY, 0x1.ffffffp127l },   /* rounds up to INFINITY for float */
83 #if __SIZEOF_DOUBLE__ == 8
84     /* Check max values for doubles */
85     { "0x1.fffffffffffffp1022@",  0x1.fffffffffffffp1022, (float) INFINITY, 0x1.fffffffffffffp1022l },
86     { "0x1.fffffffffffff8p1022@", 0x1.0000000000000p1023, (float) INFINITY, 0x1.fffffffffffff8p1022l }, /* rounds up for double */
87     { "0x1.fffffffffffffp1023@",  0x1.fffffffffffffp1023, (float) INFINITY, 0x1.fffffffffffffp1023l },
88 #endif
89 #if defined(_TEST_LONG_DOUBLE) && __SIZEOF_LONG_DOUBLE__ > 8
90 #if __LDBL_MANT_DIG__ > __DBL_MANT_DIG__
91     { "0x1.fffffffffffff8p1023@",      (double) INFINITY, (float) INFINITY, 0x1.fffffffffffff8p1023l }, /* rounds up to INFINITY for double */
92 #else
93     { "0x1.fffffffffffff8p1023@",      (double) INFINITY, (float) INFINITY, (long double) INFINITY }, /* rounds up to INFINITY for double */
94 #endif
95     /* Check max values for long doubles */
96 #if __LDBL_MANT_DIG__ == 113
97     { "0x1.ffffffffffffffffffffffffffffp16382@",  (double) INFINITY, (float) INFINITY, 0x1.ffffffffffffffffffffffffffffp16382l },
98     { "0x1.ffffffffffffffffffffffffffff8p16382@", (double) INFINITY, (float) INFINITY, 0x1.0000000000000000000000000000p16383l }, /* rounds up for long double */
99     { "0x1.ffffffffffffffffffffffffffffp16383@",  (double) INFINITY, (float) INFINITY, 0x1.ffffffffffffffffffffffffffffp16383l },
100     { "0x1.ffffffffffffffffffffffffffff8p16383@", (double) INFINITY, (float) INFINITY, (long double) INFINITY },                  /* rounds up to INFINITY for long oduble */
101 #elif __LDBL_MANT_DIG__ == 64
102     { "0x1.fffffffffffffffep16382@", (double) INFINITY, (float) INFINITY, 0x1.fffffffffffffffep16382l },
103     { "0x1.ffffffffffffffffp16382@", (double) INFINITY, (float) INFINITY, 0x1.0000000000000000p16383l },        /* rounds up for long double */
104     { "0x1.fffffffffffffffep16383@", (double) INFINITY, (float) INFINITY, 0x1.fffffffffffffffep16383l },
105     { "0x1.ffffffffffffffffp16383@", (double) INFINITY, (float) INFINITY, (long double) INFINITY },             /* rounds up to INFINITY for long double */
106 #endif
107 #endif
108 #endif
109 };
110 
111 #define NTESTS (sizeof(tests)/sizeof(tests[0]))
112 
main(void)113 int main(void)
114 {
115     int i;
116     double d;
117     float f;
118 #ifdef _TEST_LONG_DOUBLE
119 #ifdef FULL_TESTS
120     long double ld;
121 #endif
122 #endif
123     char *end;
124     int ret = 0;
125 
126     for (i = 0; i < (int) NTESTS; i++) {
127         d = strtod(tests[i].string, &end);
128         if (d != tests[i].dvalue) {
129             printf("strtod(\"%s\"): got %.17e %a want %.17e %a\n", tests[i].string,
130                    d, d, tests[i].dvalue, tests[i].dvalue);
131             ret = 1;
132         }
133         if (*end != '@') {
134             printf("strtod(\"%s\") end is \"%s\"\n",
135                    tests[i].string, end);
136             ret = 1;
137         }
138         f = strtof(tests[i].string, &end);
139         if (f != tests[i].fvalue) {
140             printf("strtof(\"%s\"): got %.17e %a want %.17e %a\n", tests[i].string,
141                    (double) f, (double) f, (double) tests[i].fvalue, (double) tests[i].fvalue);
142             ret = 1;
143         }
144         if (*end != '@') {
145             printf("strtof(\"%s\") end is \"%s\"\n", tests[i].string, end);
146             ret = 1;
147         }
148 #ifdef _TEST_LONG_DOUBLE
149 #ifdef FULL_TESTS
150         if (sizeof(long double) > sizeof(double)) {
151             ld = strtold(tests[i].string, &end);
152             if (ld != tests[i].ldvalue) {
153                 printf("strtold(\"%s\"): got %.17Le %La want %.17Le %La\n", tests[i].string,
154                        ld, ld, tests[i].ldvalue, tests[i].ldvalue);
155                 ret = 1;
156             }
157             if (*end != '@') {
158                 printf("strtold(\"%s\") end is \"%s\"\n", tests[i].string, end);
159                 ret = 1;
160             }
161         }
162 #endif
163 #endif
164     }
165     return ret;
166 }
167