| 1 | //===-- Utility class to test fabs[f|l] -------------------------*- C++ -*-===// |
| 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 | #ifndef LLVM_LIBC_TEST_SRC_MATH_FABSTEST_H |
| 10 | #define LLVM_LIBC_TEST_SRC_MATH_FABSTEST_H |
| 11 | |
| 12 | #include "test/UnitTest/FEnvSafeTest.h" |
| 13 | #include "test/UnitTest/FPMatcher.h" |
| 14 | #include "test/UnitTest/Test.h" |
| 15 | #include "utils/MPFRWrapper/MPFRUtils.h" |
| 16 | |
| 17 | #include "hdr/math_macros.h" |
| 18 | |
| 19 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
| 20 | |
| 21 | template <typename T> |
| 22 | class FAbsTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
| 23 | |
| 24 | DECLARE_SPECIAL_CONSTANTS(T) |
| 25 | |
| 26 | public: |
| 27 | typedef T (*FabsFunc)(T); |
| 28 | |
| 29 | void testSpecialNumbers(FabsFunc func) { |
| 30 | EXPECT_FP_EQ(aNaN, func(aNaN)); |
| 31 | |
| 32 | EXPECT_FP_EQ(inf, func(inf)); |
| 33 | EXPECT_FP_EQ(inf, func(neg_inf)); |
| 34 | |
| 35 | EXPECT_FP_EQ(zero, func(zero)); |
| 36 | EXPECT_FP_EQ(zero, func(neg_zero)); |
| 37 | } |
| 38 | |
| 39 | void testRange(FabsFunc func) { |
| 40 | constexpr StorageType COUNT = 100'000; |
| 41 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
| 42 | for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
| 43 | T x = FPBits(v).get_val(); |
| 44 | if (FPBits(v).is_nan() || FPBits(v).is_inf()) |
| 45 | continue; |
| 46 | ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, func(x), 0.0); |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | #define LIST_FABS_TESTS(T, func) \ |
| 52 | using LlvmLibcFAbsTest = FAbsTest<T>; \ |
| 53 | TEST_F(LlvmLibcFAbsTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
| 54 | TEST_F(LlvmLibcFAbsTest, Range) { testRange(&func); } |
| 55 | |
| 56 | #endif // LLVM_LIBC_TEST_SRC_MATH_FABSTEST_H |
| 57 | |