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