1 //===-- Utility class to test trunc[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_TRUNCTEST_H 10 #define LLVM_LIBC_TEST_SRC_MATH_TRUNCTEST_H 11 12 #include "src/__support/CPP/algorithm.h" 13 #include "test/UnitTest/FEnvSafeTest.h" 14 #include "test/UnitTest/FPMatcher.h" 15 #include "test/UnitTest/Test.h" 16 #include "utils/MPFRWrapper/MPFRUtils.h" 17 18 #include "hdr/math_macros.h" 19 20 namespace mpfr = LIBC_NAMESPACE::testing::mpfr; 21 22 template <typename T> 23 class TruncTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { 24 25 DECLARE_SPECIAL_CONSTANTS(T) 26 27 public: 28 typedef T (*TruncFunc)(T); 29 testSpecialNumbers(TruncFunc func)30 void testSpecialNumbers(TruncFunc func) { 31 EXPECT_FP_EQ(zero, func(zero)); 32 EXPECT_FP_EQ(neg_zero, func(neg_zero)); 33 34 EXPECT_FP_EQ(inf, func(inf)); 35 EXPECT_FP_EQ(neg_inf, func(neg_inf)); 36 37 EXPECT_FP_EQ(aNaN, func(aNaN)); 38 } 39 testRoundedNumbers(TruncFunc func)40 void testRoundedNumbers(TruncFunc func) { 41 EXPECT_FP_EQ(T(1.0), func(T(1.0))); 42 EXPECT_FP_EQ(T(-1.0), func(T(-1.0))); 43 EXPECT_FP_EQ(T(10.0), func(T(10.0))); 44 EXPECT_FP_EQ(T(-10.0), func(T(-10.0))); 45 EXPECT_FP_EQ(T(1234.0), func(T(1234.0))); 46 EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0))); 47 } 48 testFractions(TruncFunc func)49 void testFractions(TruncFunc func) { 50 EXPECT_FP_EQ(T(0.0), func(T(0.5))); 51 EXPECT_FP_EQ(T(-0.0), func(T(-0.5))); 52 EXPECT_FP_EQ(T(0.0), func(T(0.115))); 53 EXPECT_FP_EQ(T(-0.0), func(T(-0.115))); 54 EXPECT_FP_EQ(T(0.0), func(T(0.715))); 55 EXPECT_FP_EQ(T(-0.0), func(T(-0.715))); 56 EXPECT_FP_EQ(T(1.0), func(T(1.3))); 57 EXPECT_FP_EQ(T(-1.0), func(T(-1.3))); 58 EXPECT_FP_EQ(T(1.0), func(T(1.5))); 59 EXPECT_FP_EQ(T(-1.0), func(T(-1.5))); 60 EXPECT_FP_EQ(T(1.0), func(T(1.75))); 61 EXPECT_FP_EQ(T(-1.0), func(T(-1.75))); 62 EXPECT_FP_EQ(T(10.0), func(T(10.32))); 63 EXPECT_FP_EQ(T(-10.0), func(T(-10.32))); 64 EXPECT_FP_EQ(T(10.0), func(T(10.65))); 65 EXPECT_FP_EQ(T(-10.0), func(T(-10.65))); 66 EXPECT_FP_EQ(T(123.0), func(T(123.38))); 67 EXPECT_FP_EQ(T(-123.0), func(T(-123.38))); 68 EXPECT_FP_EQ(T(123.0), func(T(123.96))); 69 EXPECT_FP_EQ(T(-123.0), func(T(-123.96))); 70 } 71 testRange(TruncFunc func)72 void testRange(TruncFunc func) { 73 constexpr int COUNT = 100'000; 74 constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( 75 static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1)); 76 StorageType v = 0; 77 for (int i = 0; i <= COUNT; ++i, v += STEP) { 78 FPBits xbits(v); 79 T x = xbits.get_val(); 80 if (xbits.is_inf_or_nan()) 81 continue; 82 83 ASSERT_MPFR_MATCH(mpfr::Operation::Trunc, x, func(x), 0.0); 84 } 85 } 86 }; 87 88 #define LIST_TRUNC_TESTS(T, func) \ 89 using LlvmLibcTruncTest = TruncTest<T>; \ 90 TEST_F(LlvmLibcTruncTest, SpecialNumbers) { testSpecialNumbers(&func); } \ 91 TEST_F(LlvmLibcTruncTest, RoundedNubmers) { testRoundedNumbers(&func); } \ 92 TEST_F(LlvmLibcTruncTest, Fractions) { testFractions(&func); } \ 93 TEST_F(LlvmLibcTruncTest, Range) { testRange(&func); } 94 95 #endif // LLVM_LIBC_TEST_SRC_MATH_TRUNCTEST_H 96