| 1 | //===-- Unittests for str_to_float ----------------------------------------===// |
| 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/__support/libc_errno.h" |
| 11 | #include "src/__support/macros/config.h" |
| 12 | #include "src/__support/str_to_float.h" |
| 13 | #include "src/__support/uint128.h" |
| 14 | |
| 15 | #include "test/UnitTest/Test.h" |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | |
| 19 | template <typename T> struct LlvmLibcStrToFloatTest : public testing::Test { |
| 20 | using StorageType = typename fputil::FPBits<T>::StorageType; |
| 21 | |
| 22 | void clinger_fast_path_test(const StorageType inputMantissa, |
| 23 | const int32_t inputExp10, |
| 24 | const StorageType expectedOutputMantissa, |
| 25 | const uint32_t expectedOutputExp2) { |
| 26 | StorageType actual_output_mantissa = 0; |
| 27 | uint32_t actual_output_exp2 = 0; |
| 28 | |
| 29 | auto result = internal::clinger_fast_path<T>({inputMantissa, inputExp10}); |
| 30 | |
| 31 | ASSERT_TRUE(result.has_value()); |
| 32 | |
| 33 | actual_output_mantissa = result->mantissa; |
| 34 | actual_output_exp2 = static_cast<uint32_t>(result->exponent); |
| 35 | |
| 36 | EXPECT_EQ(actual_output_mantissa, expectedOutputMantissa); |
| 37 | EXPECT_EQ(actual_output_exp2, expectedOutputExp2); |
| 38 | } |
| 39 | |
| 40 | void clinger_fast_path_fails_test(const StorageType inputMantissa, |
| 41 | const int32_t inputExp10) { |
| 42 | ASSERT_FALSE(internal::clinger_fast_path<T>({inputMantissa, inputExp10}) |
| 43 | .has_value()); |
| 44 | } |
| 45 | |
| 46 | void eisel_lemire_test(const StorageType inputMantissa, |
| 47 | const int32_t inputExp10, |
| 48 | const StorageType expectedOutputMantissa, |
| 49 | const uint32_t expectedOutputExp2) { |
| 50 | StorageType actual_output_mantissa = 0; |
| 51 | uint32_t actual_output_exp2 = 0; |
| 52 | |
| 53 | auto result = internal::eisel_lemire<T>({inputMantissa, inputExp10}); |
| 54 | |
| 55 | ASSERT_TRUE(result.has_value()); |
| 56 | |
| 57 | actual_output_mantissa = result->mantissa; |
| 58 | actual_output_exp2 = static_cast<uint32_t>(result->exponent); |
| 59 | |
| 60 | EXPECT_EQ(actual_output_mantissa, expectedOutputMantissa); |
| 61 | EXPECT_EQ(actual_output_exp2, expectedOutputExp2); |
| 62 | } |
| 63 | |
| 64 | void simple_decimal_conversion_test(const char *__restrict numStart, |
| 65 | const StorageType expectedOutputMantissa, |
| 66 | const uint32_t expectedOutputExp2, |
| 67 | const int expectedErrno = 0) { |
| 68 | StorageType actual_output_mantissa = 0; |
| 69 | uint32_t actual_output_exp2 = 0; |
| 70 | |
| 71 | auto result = internal::simple_decimal_conversion<T>(numStart); |
| 72 | |
| 73 | actual_output_mantissa = result.num.mantissa; |
| 74 | actual_output_exp2 = static_cast<uint32_t>(result.num.exponent); |
| 75 | |
| 76 | EXPECT_EQ(actual_output_mantissa, expectedOutputMantissa); |
| 77 | EXPECT_EQ(actual_output_exp2, expectedOutputExp2); |
| 78 | EXPECT_EQ(result.error, expectedErrno); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | } // namespace LIBC_NAMESPACE_DECL |
| 83 | |