1 | //===-- Utility class to test ceil[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 | #include "test/UnitTest/FEnvSafeTest.h" |
10 | #include "test/UnitTest/FPMatcher.h" |
11 | #include "test/UnitTest/Test.h" |
12 | #include "utils/MPFRWrapper/MPFRUtils.h" |
13 | |
14 | #include "hdr/math_macros.h" |
15 | |
16 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
17 | |
18 | template <typename T> |
19 | class CeilTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
20 | |
21 | DECLARE_SPECIAL_CONSTANTS(T) |
22 | |
23 | public: |
24 | typedef T (*CeilFunc)(T); |
25 | |
26 | void testSpecialNumbers(CeilFunc 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(CeilFunc 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(CeilFunc func) { |
46 | EXPECT_FP_EQ(T(1.0), func(T(0.5))); |
47 | EXPECT_FP_EQ(T(-0.0), func(T(-0.5))); |
48 | EXPECT_FP_EQ(T(1.0), func(T(0.115))); |
49 | EXPECT_FP_EQ(T(-0.0), func(T(-0.115))); |
50 | EXPECT_FP_EQ(T(1.0), func(T(0.715))); |
51 | EXPECT_FP_EQ(T(-0.0), func(T(-0.715))); |
52 | EXPECT_FP_EQ(T(2.0), func(T(1.3))); |
53 | EXPECT_FP_EQ(T(-1.0), func(T(-1.3))); |
54 | EXPECT_FP_EQ(T(2.0), func(T(1.5))); |
55 | EXPECT_FP_EQ(T(-1.0), func(T(-1.5))); |
56 | EXPECT_FP_EQ(T(2.0), func(T(1.75))); |
57 | EXPECT_FP_EQ(T(-1.0), func(T(-1.75))); |
58 | EXPECT_FP_EQ(T(11.0), func(T(10.32))); |
59 | EXPECT_FP_EQ(T(-10.0), func(T(-10.32))); |
60 | EXPECT_FP_EQ(T(11.0), func(T(10.65))); |
61 | EXPECT_FP_EQ(T(-10.0), func(T(-10.65))); |
62 | EXPECT_FP_EQ(T(1235.0), func(T(1234.38))); |
63 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38))); |
64 | EXPECT_FP_EQ(T(1235.0), func(T(1234.96))); |
65 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96))); |
66 | } |
67 | |
68 | void testRange(CeilFunc func) { |
69 | constexpr StorageType COUNT = 100'000; |
70 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
71 | for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
72 | T x = FPBits(v).get_val(); |
73 | if (isnan(x) || isinf(x)) |
74 | continue; |
75 | |
76 | ASSERT_MPFR_MATCH(mpfr::Operation::Ceil, x, func(x), 0.0); |
77 | } |
78 | } |
79 | }; |
80 | |
81 | #define LIST_CEIL_TESTS(T, func) \ |
82 | using LlvmLibcCeilTest = CeilTest<T>; \ |
83 | TEST_F(LlvmLibcCeilTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
84 | TEST_F(LlvmLibcCeilTest, RoundedNubmers) { testRoundedNumbers(&func); } \ |
85 | TEST_F(LlvmLibcCeilTest, Fractions) { testFractions(&func); } \ |
86 | TEST_F(LlvmLibcCeilTest, Range) { testRange(&func); } |
87 | |