1 | //===-- Utility class to test floor[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_SMOKE_FLOORTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_FLOORTEST_H |
11 | |
12 | #include "test/UnitTest/FEnvSafeTest.h" |
13 | #include "test/UnitTest/FPMatcher.h" |
14 | #include "test/UnitTest/Test.h" |
15 | |
16 | #include "hdr/math_macros.h" |
17 | |
18 | template <typename T> |
19 | class FloorTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
20 | |
21 | DECLARE_SPECIAL_CONSTANTS(T) |
22 | |
23 | public: |
24 | typedef T (*FloorFunc)(T); |
25 | |
26 | void testSpecialNumbers(FloorFunc func) { |
27 | EXPECT_FP_EQ(zero, func(zero)); |
28 | EXPECT_FP_EQ(neg_zero, func(neg_zero)); |
29 | |
30 | EXPECT_FP_EQ(inf, func(inf)); |
31 | EXPECT_FP_EQ(neg_inf, func(neg_inf)); |
32 | |
33 | EXPECT_FP_EQ(aNaN, func(aNaN)); |
34 | } |
35 | |
36 | void testRoundedNumbers(FloorFunc func) { |
37 | EXPECT_FP_EQ(T(1.0), func(T(1.0))); |
38 | EXPECT_FP_EQ(T(-1.0), func(T(-1.0))); |
39 | EXPECT_FP_EQ(T(10.0), func(T(10.0))); |
40 | EXPECT_FP_EQ(T(-10.0), func(T(-10.0))); |
41 | EXPECT_FP_EQ(T(1234.0), func(T(1234.0))); |
42 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0))); |
43 | } |
44 | |
45 | void testFractions(FloorFunc func) { |
46 | EXPECT_FP_EQ(T(0.0), func(T(0.5))); |
47 | EXPECT_FP_EQ(T(-1.0), func(T(-0.5))); |
48 | EXPECT_FP_EQ(T(0.0), func(T(0.115))); |
49 | EXPECT_FP_EQ(T(-1.0), func(T(-0.115))); |
50 | EXPECT_FP_EQ(T(0.0), func(T(0.715))); |
51 | EXPECT_FP_EQ(T(-1.0), func(T(-0.715))); |
52 | EXPECT_FP_EQ(T(1.0), func(T(1.3))); |
53 | EXPECT_FP_EQ(T(-2.0), func(T(-1.3))); |
54 | EXPECT_FP_EQ(T(1.0), func(T(1.5))); |
55 | EXPECT_FP_EQ(T(-2.0), func(T(-1.5))); |
56 | EXPECT_FP_EQ(T(1.0), func(T(1.75))); |
57 | EXPECT_FP_EQ(T(-2.0), func(T(-1.75))); |
58 | EXPECT_FP_EQ(T(10.0), func(T(10.32))); |
59 | EXPECT_FP_EQ(T(-11.0), func(T(-10.32))); |
60 | EXPECT_FP_EQ(T(10.0), func(T(10.65))); |
61 | EXPECT_FP_EQ(T(-11.0), func(T(-10.65))); |
62 | EXPECT_FP_EQ(T(1234.0), func(T(1234.38))); |
63 | EXPECT_FP_EQ(T(-1235.0), func(T(-1234.38))); |
64 | EXPECT_FP_EQ(T(1234.0), func(T(1234.96))); |
65 | EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96))); |
66 | } |
67 | }; |
68 | |
69 | #define LIST_FLOOR_TESTS(T, func) \ |
70 | using LlvmLibcFloorTest = FloorTest<T>; \ |
71 | TEST_F(LlvmLibcFloorTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
72 | TEST_F(LlvmLibcFloorTest, RoundedNubmers) { testRoundedNumbers(&func); } \ |
73 | TEST_F(LlvmLibcFloorTest, Fractions) { testFractions(&func); } |
74 | |
75 | #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_FLOORTEST_H |
76 | |