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 #ifndef LLVM_LIBC_TEST_SRC_MATH_ROUNDTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_ROUNDTEST_H
12 #include "src/__support/CPP/algorithm.h"
13 #include "test/UnitTest/FEnvSafeTest.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 #include "utils/MPFRWrapper/MPFRUtils.h"
18 #include "hdr/math_macros.h"
20 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
23 class RoundTest
: public LIBC_NAMESPACE::testing::FEnvSafeTest
{
25 DECLARE_SPECIAL_CONSTANTS(T
)
28 typedef T (*RoundFunc
)(T
);
30 void testSpecialNumbers(RoundFunc func
) {
31 EXPECT_FP_EQ(zero
, func(zero
));
32 EXPECT_FP_EQ(neg_zero
, func(neg_zero
));
34 EXPECT_FP_EQ(inf
, func(inf
));
35 EXPECT_FP_EQ(neg_inf
, func(neg_inf
));
37 EXPECT_FP_EQ(aNaN
, func(aNaN
));
40 void testRoundedNumbers(RoundFunc func
) {
41 EXPECT_FP_EQ(T(1.0), func(T(1.0)));
42 EXPECT_FP_EQ(T(-1.0), func(T(-1.0)));
43 EXPECT_FP_EQ(T(10.0), func(T(10.0)));
44 EXPECT_FP_EQ(T(-10.0), func(T(-10.0)));
45 EXPECT_FP_EQ(T(1234.0), func(T(1234.0)));
46 EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0)));
49 void testFractions(RoundFunc func
) {
50 EXPECT_FP_EQ(T(1.0), func(T(0.5)));
51 EXPECT_FP_EQ(T(-1.0), func(T(-0.5)));
52 EXPECT_FP_EQ(T(0.0), func(T(0.115)));
53 EXPECT_FP_EQ(T(-0.0), func(T(-0.115)));
54 EXPECT_FP_EQ(T(1.0), func(T(0.715)));
55 EXPECT_FP_EQ(T(-1.0), func(T(-0.715)));
56 EXPECT_FP_EQ(T(1.0), func(T(1.3)));
57 EXPECT_FP_EQ(T(-1.0), func(T(-1.3)));
58 EXPECT_FP_EQ(T(2.0), func(T(1.5)));
59 EXPECT_FP_EQ(T(-2.0), func(T(-1.5)));
60 EXPECT_FP_EQ(T(2.0), func(T(1.75)));
61 EXPECT_FP_EQ(T(-2.0), func(T(-1.75)));
62 EXPECT_FP_EQ(T(10.0), func(T(10.32)));
63 EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
64 EXPECT_FP_EQ(T(11.0), func(T(10.65)));
65 EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
66 EXPECT_FP_EQ(T(123.0), func(T(123.38)));
67 EXPECT_FP_EQ(T(-123.0), func(T(-123.38)));
68 EXPECT_FP_EQ(T(124.0), func(T(123.96)));
69 EXPECT_FP_EQ(T(-124.0), func(T(-123.96)));
72 void testRange(RoundFunc func
) {
73 constexpr int COUNT
= 100'000;
74 constexpr StorageType STEP
= LIBC_NAMESPACE::cpp::max(
75 static_cast<StorageType
>(STORAGE_MAX
/ COUNT
), StorageType(1));
77 for (int i
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
79 T x
= xbits
.get_val();
80 if (xbits
.is_inf_or_nan())
83 ASSERT_MPFR_MATCH(mpfr::Operation::Round
, x
, func(x
), 0.0);
88 #define LIST_ROUND_TESTS(T, func) \
89 using LlvmLibcRoundTest = RoundTest<T>; \
90 TEST_F(LlvmLibcRoundTest, SpecialNumbers) { testSpecialNumbers(&func); } \
91 TEST_F(LlvmLibcRoundTest, RoundedNubmers) { testRoundedNumbers(&func); } \
92 TEST_F(LlvmLibcRoundTest, Fractions) { testFractions(&func); } \
93 TEST_F(LlvmLibcRoundTest, Range) { testRange(&func); }
95 #endif // LLVM_LIBC_TEST_SRC_MATH_ROUNDTEST_H