1 //===-- Utility class to test floor[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 "src/__support/FPUtil/BasicOperations.h"
10 #include "src/__support/FPUtil/NearestIntegerOperations.h"
11 #include "test/UnitTest/FEnvSafeTest.h"
12 #include "test/UnitTest/FPMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
16 #include "hdr/math_macros.h"
18 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
21 class ModfTest
: public LIBC_NAMESPACE::testing::FEnvSafeTest
{
23 DECLARE_SPECIAL_CONSTANTS(T
)
26 typedef T (*ModfFunc
)(T
, T
*);
28 void testSpecialNumbers(ModfFunc func
) {
31 EXPECT_FP_EQ(zero
, func(zero
, &integral
));
32 EXPECT_FP_EQ(integral
, zero
);
33 EXPECT_FP_EQ(neg_zero
, func(neg_zero
, &integral
));
34 EXPECT_FP_EQ(integral
, neg_zero
);
36 EXPECT_FP_EQ(zero
, func(inf
, &integral
));
37 EXPECT_FP_EQ(inf
, integral
);
38 EXPECT_FP_EQ(neg_zero
, func(neg_inf
, &integral
));
39 EXPECT_FP_EQ(neg_inf
, integral
);
41 EXPECT_FP_EQ(aNaN
, func(aNaN
, &integral
));
44 void testIntegers(ModfFunc func
) {
47 EXPECT_FP_EQ(T(0.0), func(T(1.0), &integral
));
48 EXPECT_FP_EQ(T(1.0), integral
);
50 EXPECT_FP_EQ(T(-0.0), func(T(-1.0), &integral
));
51 EXPECT_FP_EQ(T(-1.0), integral
);
53 EXPECT_FP_EQ(T(0.0), func(T(10.0), &integral
));
54 EXPECT_FP_EQ(T(10.0), integral
);
56 EXPECT_FP_EQ(T(-0.0), func(T(-10.0), &integral
));
57 EXPECT_FP_EQ(T(-10.0), integral
);
59 EXPECT_FP_EQ(T(0.0), func(T(12345.0), &integral
));
60 EXPECT_FP_EQ(T(12345.0), integral
);
62 EXPECT_FP_EQ(T(-0.0), func(T(-12345.0), &integral
));
63 EXPECT_FP_EQ(T(-12345.0), integral
);
66 void testFractions(ModfFunc func
) {
69 EXPECT_FP_EQ(T(0.5), func(T(1.5), &integral
));
70 EXPECT_FP_EQ(integral
, T(1.0));
72 EXPECT_FP_EQ(T(-0.5), func(T(-1.5), &integral
));
73 EXPECT_FP_EQ(integral
, T(-1.0));
75 EXPECT_FP_EQ(T(0.75), func(T(10.75), &integral
));
76 EXPECT_FP_EQ(integral
, T(10.0));
78 EXPECT_FP_EQ(T(-0.75), func(T(-10.75), &integral
));
79 EXPECT_FP_EQ(integral
, T(-10.0));
81 EXPECT_FP_EQ(T(0.125), func(T(100.125), &integral
));
82 EXPECT_FP_EQ(integral
, T(100.0));
84 EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral
));
85 EXPECT_FP_EQ(integral
, T(-100.0));
88 void testRange(ModfFunc func
) {
89 constexpr StorageType COUNT
= 100'000;
90 constexpr StorageType STEP
= STORAGE_MAX
/ COUNT
;
91 for (StorageType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
92 T x
= FPBits(v
).get_val();
93 if (FPBits(v
).is_nan() || FPBits(v
).is_inf() || x
== T(0.0))
97 T frac
= func(x
, &integral
);
98 ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac
) < 1.0l);
99 ASSERT_TRUE(LIBC_NAMESPACE::fputil::trunc(x
) == integral
);
100 ASSERT_TRUE(integral
+ frac
== x
);
105 #define LIST_MODF_TESTS(T, func) \
106 using LlvmLibcModfTest = ModfTest<T>; \
107 TEST_F(LlvmLibcModfTest, SpecialNumbers) { testSpecialNumbers(&func); } \
108 TEST_F(LlvmLibcModfTest, RoundedNubmers) { testIntegers(&func); } \
109 TEST_F(LlvmLibcModfTest, Fractions) { testFractions(&func); } \
110 TEST_F(LlvmLibcModfTest, Range) { testRange(&func); }