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