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/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"
15
16#include "hdr/math_macros.h"
17
18namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
19
20template <typename T>
21class ModfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
22
23 DECLARE_SPECIAL_CONSTANTS(T)
24
25public:
26 typedef T (*ModfFunc)(T, T *);
27
28 void testSpecialNumbers(ModfFunc func) {
29 T integral;
30
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);
35
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);
40
41 EXPECT_FP_EQ(aNaN, func(aNaN, &integral));
42 }
43
44 void testIntegers(ModfFunc func) {
45 T integral;
46
47 EXPECT_FP_EQ(T(0.0), func(T(1.0), &integral));
48 EXPECT_FP_EQ(T(1.0), integral);
49
50 EXPECT_FP_EQ(T(-0.0), func(T(-1.0), &integral));
51 EXPECT_FP_EQ(T(-1.0), integral);
52
53 EXPECT_FP_EQ(T(0.0), func(T(10.0), &integral));
54 EXPECT_FP_EQ(T(10.0), integral);
55
56 EXPECT_FP_EQ(T(-0.0), func(T(-10.0), &integral));
57 EXPECT_FP_EQ(T(-10.0), integral);
58
59 EXPECT_FP_EQ(T(0.0), func(T(12345.0), &integral));
60 EXPECT_FP_EQ(T(12345.0), integral);
61
62 EXPECT_FP_EQ(T(-0.0), func(T(-12345.0), &integral));
63 EXPECT_FP_EQ(T(-12345.0), integral);
64 }
65
66 void testFractions(ModfFunc func) {
67 T integral;
68
69 EXPECT_FP_EQ(T(0.5), func(T(1.5), &integral));
70 EXPECT_FP_EQ(integral, T(1.0));
71
72 EXPECT_FP_EQ(T(-0.5), func(T(-1.5), &integral));
73 EXPECT_FP_EQ(integral, T(-1.0));
74
75 EXPECT_FP_EQ(T(0.75), func(T(10.75), &integral));
76 EXPECT_FP_EQ(integral, T(10.0));
77
78 EXPECT_FP_EQ(T(-0.75), func(T(-10.75), &integral));
79 EXPECT_FP_EQ(integral, T(-10.0));
80
81 EXPECT_FP_EQ(T(0.125), func(T(100.125), &integral));
82 EXPECT_FP_EQ(integral, T(100.0));
83
84 EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral));
85 EXPECT_FP_EQ(integral, T(-100.0));
86 }
87
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 (isnan(x) || isinf(x) || x == T(0.0))
94 continue;
95
96 T integral;
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);
101 }
102 }
103};
104
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); }
111

source code of libc/test/src/math/ModfTest.h