1*71db0c75SAndroid Build Coastguard Worker //===-- Utility class to test floor[f|l] ------------------------*- C++ -*-===// 2*71db0c75SAndroid Build Coastguard Worker // 3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information. 5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*71db0c75SAndroid Build Coastguard Worker // 7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 8*71db0c75SAndroid Build Coastguard Worker 9*71db0c75SAndroid Build Coastguard Worker #ifndef LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H 10*71db0c75SAndroid Build Coastguard Worker #define LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H 11*71db0c75SAndroid Build Coastguard Worker 12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/CPP/algorithm.h" 13*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FEnvSafeTest.h" 14*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FPMatcher.h" 15*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h" 16*71db0c75SAndroid Build Coastguard Worker #include "utils/MPFRWrapper/MPFRUtils.h" 17*71db0c75SAndroid Build Coastguard Worker 18*71db0c75SAndroid Build Coastguard Worker #include "hdr/math_macros.h" 19*71db0c75SAndroid Build Coastguard Worker 20*71db0c75SAndroid Build Coastguard Worker namespace mpfr = LIBC_NAMESPACE::testing::mpfr; 21*71db0c75SAndroid Build Coastguard Worker 22*71db0c75SAndroid Build Coastguard Worker template <typename T> 23*71db0c75SAndroid Build Coastguard Worker class FloorTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { 24*71db0c75SAndroid Build Coastguard Worker 25*71db0c75SAndroid Build Coastguard Worker DECLARE_SPECIAL_CONSTANTS(T) 26*71db0c75SAndroid Build Coastguard Worker 27*71db0c75SAndroid Build Coastguard Worker public: 28*71db0c75SAndroid Build Coastguard Worker typedef T (*FloorFunc)(T); 29*71db0c75SAndroid Build Coastguard Worker testSpecialNumbers(FloorFunc func)30*71db0c75SAndroid Build Coastguard Worker void testSpecialNumbers(FloorFunc func) { 31*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(zero, func(zero)); 32*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(neg_zero, func(neg_zero)); 33*71db0c75SAndroid Build Coastguard Worker 34*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(inf, func(inf)); 35*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(neg_inf, func(neg_inf)); 36*71db0c75SAndroid Build Coastguard Worker 37*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(aNaN, func(aNaN)); 38*71db0c75SAndroid Build Coastguard Worker } 39*71db0c75SAndroid Build Coastguard Worker testRoundedNumbers(FloorFunc func)40*71db0c75SAndroid Build Coastguard Worker void testRoundedNumbers(FloorFunc func) { 41*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(1.0), func(T(1.0))); 42*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-1.0), func(T(-1.0))); 43*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(10.0), func(T(10.0))); 44*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-10.0), func(T(-10.0))); 45*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(1234.0), func(T(1234.0))); 46*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0))); 47*71db0c75SAndroid Build Coastguard Worker } 48*71db0c75SAndroid Build Coastguard Worker testFractions(FloorFunc func)49*71db0c75SAndroid Build Coastguard Worker void testFractions(FloorFunc func) { 50*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(0.0), func(T(0.5))); 51*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-1.0), func(T(-0.5))); 52*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(0.0), func(T(0.115))); 53*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-1.0), func(T(-0.115))); 54*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(0.0), func(T(0.715))); 55*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-1.0), func(T(-0.715))); 56*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(1.0), func(T(1.3))); 57*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-2.0), func(T(-1.3))); 58*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(1.0), func(T(1.5))); 59*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-2.0), func(T(-1.5))); 60*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(1.0), func(T(1.75))); 61*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-2.0), func(T(-1.75))); 62*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(10.0), func(T(10.32))); 63*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-11.0), func(T(-10.32))); 64*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(10.0), func(T(10.65))); 65*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-11.0), func(T(-10.65))); 66*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(123.0), func(T(123.38))); 67*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-124.0), func(T(-123.38))); 68*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(123.0), func(T(123.96))); 69*71db0c75SAndroid Build Coastguard Worker EXPECT_FP_EQ(T(-124.0), func(T(-123.96))); 70*71db0c75SAndroid Build Coastguard Worker } 71*71db0c75SAndroid Build Coastguard Worker testRange(FloorFunc func)72*71db0c75SAndroid Build Coastguard Worker void testRange(FloorFunc func) { 73*71db0c75SAndroid Build Coastguard Worker constexpr int COUNT = 100'000; 74*71db0c75SAndroid Build Coastguard Worker constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( 75*71db0c75SAndroid Build Coastguard Worker static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1)); 76*71db0c75SAndroid Build Coastguard Worker StorageType v = 0; 77*71db0c75SAndroid Build Coastguard Worker for (int i = 0; i <= COUNT; ++i, v += STEP) { 78*71db0c75SAndroid Build Coastguard Worker FPBits xbits(v); 79*71db0c75SAndroid Build Coastguard Worker T x = xbits.get_val(); 80*71db0c75SAndroid Build Coastguard Worker if (xbits.is_inf_or_nan()) 81*71db0c75SAndroid Build Coastguard Worker continue; 82*71db0c75SAndroid Build Coastguard Worker 83*71db0c75SAndroid Build Coastguard Worker ASSERT_MPFR_MATCH(mpfr::Operation::Floor, x, func(x), 0.0); 84*71db0c75SAndroid Build Coastguard Worker } 85*71db0c75SAndroid Build Coastguard Worker } 86*71db0c75SAndroid Build Coastguard Worker }; 87*71db0c75SAndroid Build Coastguard Worker 88*71db0c75SAndroid Build Coastguard Worker #define LIST_FLOOR_TESTS(T, func) \ 89*71db0c75SAndroid Build Coastguard Worker using LlvmLibcFloorTest = FloorTest<T>; \ 90*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFloorTest, SpecialNumbers) { testSpecialNumbers(&func); } \ 91*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFloorTest, RoundedNubmers) { testRoundedNumbers(&func); } \ 92*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFloorTest, Fractions) { testFractions(&func); } \ 93*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFloorTest, Range) { testRange(&func); } 94*71db0c75SAndroid Build Coastguard Worker 95*71db0c75SAndroid Build Coastguard Worker #endif // LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H 96