xref: /aosp_15_r20/external/llvm-libc/test/src/math/smoke/ModfTest.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Utility class to test floor[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 "src/__support/CPP/algorithm.h"
10 #include "src/__support/FPUtil/BasicOperations.h"
11 #include "src/__support/FPUtil/NearestIntegerOperations.h"
12 #include "test/UnitTest/FEnvSafeTest.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 
16 #include "hdr/math_macros.h"
17 
18 template <typename T>
19 class ModfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
20 
21   DECLARE_SPECIAL_CONSTANTS(T)
22 
23 public:
24   typedef T (*ModfFunc)(T, T *);
25 
testSpecialNumbers(ModfFunc func)26   void testSpecialNumbers(ModfFunc func) {
27     T integral;
28 
29     EXPECT_FP_EQ(zero, func(zero, &integral));
30     EXPECT_FP_EQ(integral, zero);
31     EXPECT_FP_EQ(neg_zero, func(neg_zero, &integral));
32     EXPECT_FP_EQ(integral, neg_zero);
33 
34     EXPECT_FP_EQ(zero, func(inf, &integral));
35     EXPECT_FP_EQ(inf, integral);
36     EXPECT_FP_EQ(neg_zero, func(neg_inf, &integral));
37     EXPECT_FP_EQ(neg_inf, integral);
38 
39     EXPECT_FP_EQ(aNaN, func(aNaN, &integral));
40   }
41 
testIntegers(ModfFunc func)42   void testIntegers(ModfFunc func) {
43     T integral;
44 
45     EXPECT_FP_EQ(T(0.0), func(T(1.0), &integral));
46     EXPECT_FP_EQ(T(1.0), integral);
47 
48     EXPECT_FP_EQ(T(-0.0), func(T(-1.0), &integral));
49     EXPECT_FP_EQ(T(-1.0), integral);
50 
51     EXPECT_FP_EQ(T(0.0), func(T(10.0), &integral));
52     EXPECT_FP_EQ(T(10.0), integral);
53 
54     EXPECT_FP_EQ(T(-0.0), func(T(-10.0), &integral));
55     EXPECT_FP_EQ(T(-10.0), integral);
56 
57     EXPECT_FP_EQ(T(0.0), func(T(12345.0), &integral));
58     EXPECT_FP_EQ(T(12345.0), integral);
59 
60     EXPECT_FP_EQ(T(-0.0), func(T(-12345.0), &integral));
61     EXPECT_FP_EQ(T(-12345.0), integral);
62   }
63 
testFractions(ModfFunc func)64   void testFractions(ModfFunc func) {
65     T integral;
66 
67     EXPECT_FP_EQ(T(0.5), func(T(1.5), &integral));
68     EXPECT_FP_EQ(integral, T(1.0));
69 
70     EXPECT_FP_EQ(T(-0.5), func(T(-1.5), &integral));
71     EXPECT_FP_EQ(integral, T(-1.0));
72 
73     EXPECT_FP_EQ(T(0.75), func(T(10.75), &integral));
74     EXPECT_FP_EQ(integral, T(10.0));
75 
76     EXPECT_FP_EQ(T(-0.75), func(T(-10.75), &integral));
77     EXPECT_FP_EQ(integral, T(-10.0));
78 
79     EXPECT_FP_EQ(T(0.125), func(T(100.125), &integral));
80     EXPECT_FP_EQ(integral, T(100.0));
81 
82     EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral));
83     EXPECT_FP_EQ(integral, T(-100.0));
84   }
85 
testRange(ModfFunc func)86   void testRange(ModfFunc func) {
87     constexpr int COUNT = 100'000;
88     constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
89         static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
90     StorageType v = 0;
91     for (int i = 0; i <= COUNT; ++i, v += STEP) {
92       FPBits x_bits(v);
93       if (x_bits.is_zero() || x_bits.is_inf_or_nan())
94         continue;
95 
96       T x = x_bits.get_val();
97 
98       T integral;
99       T frac = func(x, &integral);
100       ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac) < T(1.0));
101       ASSERT_TRUE(LIBC_NAMESPACE::fputil::trunc(x) == integral);
102       ASSERT_TRUE(integral + frac == x);
103     }
104   }
105 };
106 
107 #define LIST_MODF_TESTS(T, func)                                               \
108   using LlvmLibcModfTest = ModfTest<T>;                                        \
109   TEST_F(LlvmLibcModfTest, SpecialNumbers) { testSpecialNumbers(&func); }      \
110   TEST_F(LlvmLibcModfTest, RoundedNubmers) { testIntegers(&func); }            \
111   TEST_F(LlvmLibcModfTest, Fractions) { testFractions(&func); }                \
112   TEST_F(LlvmLibcModfTest, Range) { testRange(&func); }
113