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