1 | //===-- Unittests for strtof ----------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/__support/FPUtil/FPBits.h" |
10 | #include "src/stdlib/strtof.h" |
11 | |
12 | #include "test/UnitTest/ErrnoCheckingTest.h" |
13 | #include "test/UnitTest/FPMatcher.h" |
14 | #include "test/UnitTest/RoundingModeUtils.h" |
15 | #include "test/UnitTest/Test.h" |
16 | |
17 | #include <stddef.h> |
18 | |
19 | using LIBC_NAMESPACE::fputil::testing::ForceRoundingModeTest; |
20 | using LIBC_NAMESPACE::fputil::testing::RoundingMode; |
21 | |
22 | class LlvmLibcStrToFTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest, |
23 | ForceRoundingModeTest<RoundingMode::Nearest> { |
24 | public: |
25 | void run_test(const char *inputString, const ptrdiff_t expectedStrLen, |
26 | const uint32_t expectedRawData, const int expectedErrno = 0) { |
27 | // expectedRawData is the expected float result as a uint32_t, organized |
28 | // according to IEEE754: |
29 | // |
30 | // +-- 1 Sign Bit +-- 23 Mantissa bits |
31 | // | | |
32 | // | +----------+----------+ |
33 | // | | | |
34 | // SEEEEEEEEMMMMMMMMMMMMMMMMMMMMMMM |
35 | // | | |
36 | // +--+---+ |
37 | // | |
38 | // +-- 8 Exponent Bits |
39 | // |
40 | // This is so that the result can be compared in parts. |
41 | char *str_end = nullptr; |
42 | |
43 | LIBC_NAMESPACE::fputil::FPBits<float> expected_fp = |
44 | LIBC_NAMESPACE::fputil::FPBits<float>(expectedRawData); |
45 | |
46 | float result = LIBC_NAMESPACE::strtof(inputString, &str_end); |
47 | |
48 | EXPECT_EQ(str_end - inputString, expectedStrLen); |
49 | EXPECT_FP_EQ(result, expected_fp.get_val()); |
50 | ASSERT_ERRNO_EQ(expectedErrno); |
51 | } |
52 | }; |
53 | |
54 | // This is the set of tests that I have working (verified correct when compared |
55 | // to system libc). This is here so I don't break more things when I try to fix |
56 | // them. |
57 | |
58 | TEST_F(LlvmLibcStrToFTest, BasicDecimalTests) { |
59 | run_test("1" , 1, 0x3f800000); |
60 | run_test("123" , 3, 0x42f60000); |
61 | run_test("1234567890" , 10, 0x4e932c06u); |
62 | run_test("123456789012345678901" , 21, 0x60d629d4); |
63 | run_test("0.1" , 3, 0x3dcccccdu); |
64 | run_test(".1" , 2, 0x3dcccccdu); |
65 | run_test("-0.123456789" , 12, 0xbdfcd6eau); |
66 | run_test("0.11111111111111111111" , 22, 0x3de38e39u); |
67 | run_test("0.0000000000000000000000001" , 27, 0x15f79688u); |
68 | } |
69 | |
70 | TEST_F(LlvmLibcStrToFTest, DecimalOutOfRangeTests) { |
71 | run_test("555E36" , 6, 0x7f800000, ERANGE); |
72 | run_test("1e-10000" , 8, 0x0, ERANGE); |
73 | } |
74 | |
75 | TEST_F(LlvmLibcStrToFTest, DecimalsWithRoundingProblems) { |
76 | run_test("20040229" , 8, 0x4b98e512); |
77 | run_test("20040401" , 8, 0x4b98e568); |
78 | run_test("9E9" , 3, 0x50061c46); |
79 | } |
80 | |
81 | TEST_F(LlvmLibcStrToFTest, DecimalSubnormals) { |
82 | run_test("1.4012984643248170709237295832899161312802619418765e-45" , 55, 0x1, |
83 | ERANGE); |
84 | } |
85 | |
86 | TEST_F(LlvmLibcStrToFTest, DecimalWithLongExponent) { |
87 | run_test("1e2147483648" , 12, 0x7f800000, ERANGE); |
88 | run_test("1e2147483646" , 12, 0x7f800000, ERANGE); |
89 | run_test("100e2147483646" , 14, 0x7f800000, ERANGE); |
90 | run_test("1e-2147483647" , 13, 0x0, ERANGE); |
91 | run_test("1e-2147483649" , 13, 0x0, ERANGE); |
92 | } |
93 | |
94 | TEST_F(LlvmLibcStrToFTest, BasicHexadecimalTests) { |
95 | run_test("0x1" , 3, 0x3f800000); |
96 | run_test("0x10" , 4, 0x41800000); |
97 | run_test("0x11" , 4, 0x41880000); |
98 | run_test("0x0.1234" , 8, 0x3d91a000); |
99 | } |
100 | |
101 | TEST_F(LlvmLibcStrToFTest, HexadecimalSubnormalTests) { |
102 | run_test("0x0.0000000000000000000000000000000002" , 38, 0x4000, ERANGE); |
103 | |
104 | // This is the largest subnormal number as represented in hex |
105 | run_test("0x0.00000000000000000000000000000003fffff8" , 42, 0x7fffff, ERANGE); |
106 | } |
107 | |
108 | TEST_F(LlvmLibcStrToFTest, HexadecimalSubnormalRoundingTests) { |
109 | // This is the largest subnormal number that gets rounded down to 0 (as a |
110 | // float) |
111 | run_test("0x0.00000000000000000000000000000000000004" , 42, 0x0, ERANGE); |
112 | |
113 | // This is slightly larger, and thus rounded up |
114 | run_test("0x0.000000000000000000000000000000000000041" , 43, 0x00000001, |
115 | ERANGE); |
116 | |
117 | // These check that we're rounding to even properly |
118 | run_test("0x0.0000000000000000000000000000000000000b" , 42, 0x00000001, |
119 | ERANGE); |
120 | run_test("0x0.0000000000000000000000000000000000000c" , 42, 0x00000002, |
121 | ERANGE); |
122 | |
123 | // These check that we're rounding to even properly even when the input bits |
124 | // are longer than the bit fields can contain. |
125 | run_test("0x1.000000000000000000000p-150" , 30, 0x00000000, ERANGE); |
126 | run_test("0x1.000010000000000001000p-150" , 30, 0x00000001, ERANGE); |
127 | run_test("0x1.000100000000000001000p-134" , 30, 0x00008001, ERANGE); |
128 | run_test("0x1.FFFFFC000000000001000p-127" , 30, 0x007FFFFF, ERANGE); |
129 | run_test("0x1.FFFFFE000000000000000p-127" , 30, 0x00800000); |
130 | } |
131 | |
132 | TEST_F(LlvmLibcStrToFTest, HexadecimalNormalRoundingTests) { |
133 | // This also checks the round to even behavior by checking three adjacent |
134 | // numbers. |
135 | // This gets rounded down to even |
136 | run_test("0x123456500" , 11, 0x4f91a2b2); |
137 | // This doesn't get rounded at all |
138 | run_test("0x123456600" , 11, 0x4f91a2b3); |
139 | // This gets rounded up to even |
140 | run_test("0x123456700" , 11, 0x4f91a2b4); |
141 | // Correct rounding for long input |
142 | run_test("0x1.000001000000000000000" , 25, 0x3f800000); |
143 | run_test("0x1.000001000000000000100" , 25, 0x3f800001); |
144 | } |
145 | |
146 | TEST_F(LlvmLibcStrToFTest, HexadecimalsWithRoundingProblems) { |
147 | run_test("0xFFFFFFFF" , 10, 0x4f800000); |
148 | } |
149 | |
150 | TEST_F(LlvmLibcStrToFTest, HexadecimalOutOfRangeTests) { |
151 | run_test("0x123456789123456789123456789123456789" , 38, 0x7f800000, ERANGE); |
152 | run_test("-0x123456789123456789123456789123456789" , 39, 0xff800000, ERANGE); |
153 | run_test("0x0.00000000000000000000000000000000000001" , 42, 0x0, ERANGE); |
154 | } |
155 | |
156 | TEST_F(LlvmLibcStrToFTest, InfTests) { |
157 | run_test("INF" , 3, 0x7f800000); |
158 | run_test("INFinity" , 8, 0x7f800000); |
159 | run_test("infnity" , 3, 0x7f800000); |
160 | run_test("infinit" , 3, 0x7f800000); |
161 | run_test("infinfinit" , 3, 0x7f800000); |
162 | run_test("innf" , 0, 0x0); |
163 | run_test("-inf" , 4, 0xff800000); |
164 | run_test("-iNfInItY" , 9, 0xff800000); |
165 | } |
166 | |
167 | TEST_F(LlvmLibcStrToFTest, SimpleNaNTests) { |
168 | run_test("NaN" , 3, 0x7fc00000); |
169 | run_test("-nAn" , 4, 0xffc00000); |
170 | } |
171 | |
172 | // These NaNs are of the form `NaN(n-character-sequence)` where the |
173 | // n-character-sequence is 0 or more letters or numbers. If there is anything |
174 | // other than a letter or a number, then the valid number is just `NaN`. If |
175 | // the sequence is valid, then the interpretation of them is implementation |
176 | // defined, in this case it's passed to strtoll with an automatic base, and |
177 | // the result is put into the mantissa if it takes up the whole width of the |
178 | // parentheses. |
179 | TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesEmptyTest) { |
180 | run_test("NaN()" , 5, 0x7fc00000); |
181 | } |
182 | |
183 | TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesValidNumberTests) { |
184 | run_test("NaN(1234)" , 9, 0x7fc004d2); |
185 | run_test("NaN(0x1234)" , 11, 0x7fc01234); |
186 | run_test("NaN(01234)" , 10, 0x7fc0029c); |
187 | } |
188 | |
189 | TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesInvalidSequenceTests) { |
190 | run_test("NaN( 1234)" , 3, 0x7fc00000); |
191 | run_test("NaN(-1234)" , 3, 0x7fc00000); |
192 | run_test("NaN(asd&f)" , 3, 0x7fc00000); |
193 | run_test("NaN(123 )" , 3, 0x7fc00000); |
194 | run_test("NaN(123+asdf)" , 3, 0x7fc00000); |
195 | run_test("NaN(123" , 3, 0x7fc00000); |
196 | } |
197 | |
198 | TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesValidSequenceInvalidNumberTests) { |
199 | run_test("NaN(1a)" , 7, 0x7fc00000); |
200 | run_test("NaN(asdf)" , 9, 0x7fc00000); |
201 | run_test("NaN(1A1)" , 8, 0x7fc00000); |
202 | run_test("NaN(underscores_are_ok)" , 23, 0x7fc00000); |
203 | run_test( |
204 | "NaN(1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_)" , |
205 | 68, 0x7fc00000); |
206 | } |
207 | |