1 //===-- Utility class to test round[f|l] ------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "test/UnitTest/FPMatcher.h"
10 #include "test/UnitTest/Test.h"
11 #include "utils/MPFRWrapper/MPFRUtils.h"
15 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
17 template <typename T
> class RoundTest
: public LIBC_NAMESPACE::testing::Test
{
19 DECLARE_SPECIAL_CONSTANTS(T
)
22 typedef T (*RoundFunc
)(T
);
24 void testSpecialNumbers(RoundFunc func
) {
25 EXPECT_FP_EQ(zero
, func(zero
));
26 EXPECT_FP_EQ(neg_zero
, func(neg_zero
));
28 EXPECT_FP_EQ(inf
, func(inf
));
29 EXPECT_FP_EQ(neg_inf
, func(neg_inf
));
31 EXPECT_FP_EQ(aNaN
, func(aNaN
));
34 void testRoundedNumbers(RoundFunc func
) {
35 EXPECT_FP_EQ(T(1.0), func(T(1.0)));
36 EXPECT_FP_EQ(T(-1.0), func(T(-1.0)));
37 EXPECT_FP_EQ(T(10.0), func(T(10.0)));
38 EXPECT_FP_EQ(T(-10.0), func(T(-10.0)));
39 EXPECT_FP_EQ(T(1234.0), func(T(1234.0)));
40 EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0)));
43 void testFractions(RoundFunc func
) {
44 EXPECT_FP_EQ(T(1.0), func(T(0.5)));
45 EXPECT_FP_EQ(T(-1.0), func(T(-0.5)));
46 EXPECT_FP_EQ(T(0.0), func(T(0.115)));
47 EXPECT_FP_EQ(T(-0.0), func(T(-0.115)));
48 EXPECT_FP_EQ(T(1.0), func(T(0.715)));
49 EXPECT_FP_EQ(T(-1.0), func(T(-0.715)));
50 EXPECT_FP_EQ(T(1.0), func(T(1.3)));
51 EXPECT_FP_EQ(T(-1.0), func(T(-1.3)));
52 EXPECT_FP_EQ(T(2.0), func(T(1.5)));
53 EXPECT_FP_EQ(T(-2.0), func(T(-1.5)));
54 EXPECT_FP_EQ(T(2.0), func(T(1.75)));
55 EXPECT_FP_EQ(T(-2.0), func(T(-1.75)));
56 EXPECT_FP_EQ(T(10.0), func(T(10.32)));
57 EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
58 EXPECT_FP_EQ(T(11.0), func(T(10.65)));
59 EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
60 EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
61 EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
62 EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
63 EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
66 void testRange(RoundFunc func
) {
67 constexpr UIntType COUNT
= 100'000;
68 constexpr UIntType STEP
= UIntType(-1) / COUNT
;
69 for (UIntType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
71 if (isnan(x
) || isinf(x
))
74 ASSERT_MPFR_MATCH(mpfr::Operation::Round
, x
, func(x
), 0.0);
79 #define LIST_ROUND_TESTS(T, func) \
80 using LlvmLibcRoundTest = RoundTest<T>; \
81 TEST_F(LlvmLibcRoundTest, SpecialNumbers) { testSpecialNumbers(&func); } \
82 TEST_F(LlvmLibcRoundTest, RoundedNubmers) { testRoundedNumbers(&func); } \
83 TEST_F(LlvmLibcRoundTest, Fractions) { testFractions(&func); } \
84 TEST_F(LlvmLibcRoundTest, Range) { testRange(&func); }