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

source code of libc/test/src/stdlib/strtof_test.cpp