1 | //===-- Utility class to test roundeven[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_ROUNDEVENTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_ROUNDEVENTEST_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 RoundEvenTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
23 | |
24 | DECLARE_SPECIAL_CONSTANTS(T) |
25 | |
26 | public: |
27 | typedef T (*RoundEvenFunc)(T); |
28 | |
29 | void testSpecialNumbers(RoundEvenFunc func) { |
30 | EXPECT_FP_EQ(zero, func(zero)); |
31 | EXPECT_FP_EQ(neg_zero, func(neg_zero)); |
32 | |
33 | EXPECT_FP_EQ(inf, func(inf)); |
34 | EXPECT_FP_EQ(neg_inf, func(neg_inf)); |
35 | |
36 | EXPECT_FP_EQ(aNaN, func(aNaN)); |
37 | } |
38 | |
39 | void testRoundedNumbers(RoundEvenFunc func) { |
40 | EXPECT_FP_EQ(T(1.0), func(T(1.0))); |
41 | EXPECT_FP_EQ(T(-1.0), func(T(-1.0))); |
42 | EXPECT_FP_EQ(T(10.0), func(T(10.0))); |
43 | EXPECT_FP_EQ(T(-10.0), func(T(-10.0))); |
44 | EXPECT_FP_EQ(T(1234.0), func(T(1234.0))); |
45 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0))); |
46 | } |
47 | |
48 | void testFractions(RoundEvenFunc func) { |
49 | EXPECT_FP_EQ(T(0.0), func(T(0.5))); |
50 | EXPECT_FP_EQ(T(-0.0), func(T(-0.5))); |
51 | EXPECT_FP_EQ(T(0.0), func(T(0.115))); |
52 | EXPECT_FP_EQ(T(-0.0), func(T(-0.115))); |
53 | EXPECT_FP_EQ(T(1.0), func(T(0.715))); |
54 | EXPECT_FP_EQ(T(-1.0), func(T(-0.715))); |
55 | EXPECT_FP_EQ(T(1.0), func(T(1.3))); |
56 | EXPECT_FP_EQ(T(-1.0), func(T(-1.3))); |
57 | EXPECT_FP_EQ(T(2.0), func(T(1.5))); |
58 | EXPECT_FP_EQ(T(-2.0), func(T(-1.5))); |
59 | EXPECT_FP_EQ(T(2.0), func(T(1.75))); |
60 | EXPECT_FP_EQ(T(-2.0), func(T(-1.75))); |
61 | EXPECT_FP_EQ(T(11.0), func(T(10.65))); |
62 | EXPECT_FP_EQ(T(-11.0), func(T(-10.65))); |
63 | EXPECT_FP_EQ(T(1233.0), func(T(1233.25))); |
64 | EXPECT_FP_EQ(T(1234.0), func(T(1233.50))); |
65 | EXPECT_FP_EQ(T(1234.0), func(T(1233.75))); |
66 | EXPECT_FP_EQ(T(-1233.0), func(T(-1233.25))); |
67 | EXPECT_FP_EQ(T(-1234.0), func(T(-1233.50))); |
68 | EXPECT_FP_EQ(T(-1234.0), func(T(-1233.75))); |
69 | EXPECT_FP_EQ(T(1234.0), func(T(1234.50))); |
70 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.50))); |
71 | } |
72 | |
73 | void testRange(RoundEvenFunc func) { |
74 | constexpr StorageType COUNT = 100'000; |
75 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
76 | for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
77 | T x = FPBits(v).get_val(); |
78 | if (isnan(x) || isinf(x)) |
79 | continue; |
80 | |
81 | ASSERT_MPFR_MATCH(mpfr::Operation::RoundEven, x, func(x), 0.0); |
82 | } |
83 | } |
84 | }; |
85 | |
86 | #define LIST_ROUNDEVEN_TESTS(T, func) \ |
87 | using LlvmLibcRoundEvenTest = RoundEvenTest<T>; \ |
88 | TEST_F(LlvmLibcRoundEvenTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
89 | TEST_F(LlvmLibcRoundEvenTest, RoundedNubmers) { testRoundedNumbers(&func); } \ |
90 | TEST_F(LlvmLibcRoundEvenTest, Fractions) { testFractions(&func); } \ |
91 | TEST_F(LlvmLibcRoundEvenTest, Range) { testRange(&func); } |
92 | |
93 | #endif // LLVM_LIBC_TEST_SRC_MATH_ROUNDEVENTEST_H |
94 | |