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
15#include "hdr/math_macros.h"
16
17template <typename T>
18class ModfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
19
20 DECLARE_SPECIAL_CONSTANTS(T)
21
22public:
23 typedef T (*ModfFunc)(T, T *);
24
25 void testSpecialNumbers(ModfFunc func) {
26 T integral;
27
28 EXPECT_FP_EQ(zero, func(zero, &integral));
29 EXPECT_FP_EQ(integral, zero);
30 EXPECT_FP_EQ(neg_zero, func(neg_zero, &integral));
31 EXPECT_FP_EQ(integral, neg_zero);
32
33 EXPECT_FP_EQ(zero, func(inf, &integral));
34 EXPECT_FP_EQ(inf, integral);
35 EXPECT_FP_EQ(neg_zero, func(neg_inf, &integral));
36 EXPECT_FP_EQ(neg_inf, integral);
37
38 EXPECT_FP_EQ(aNaN, func(aNaN, &integral));
39 }
40
41 void testIntegers(ModfFunc func) {
42 T integral;
43
44 EXPECT_FP_EQ(T(0.0), func(T(1.0), &integral));
45 EXPECT_FP_EQ(T(1.0), 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(10.0), &integral));
51 EXPECT_FP_EQ(T(10.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(12345.0), &integral));
57 EXPECT_FP_EQ(T(12345.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
63 void testFractions(ModfFunc func) {
64 T integral;
65
66 EXPECT_FP_EQ(T(0.5), func(T(1.5), &integral));
67 EXPECT_FP_EQ(integral, T(1.0));
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.75), func(T(10.75), &integral));
73 EXPECT_FP_EQ(integral, T(10.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.125), func(T(100.125), &integral));
79 EXPECT_FP_EQ(integral, T(100.0));
80
81 EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral));
82 EXPECT_FP_EQ(integral, T(-100.0));
83 }
84
85 void testRange(ModfFunc func) {
86 constexpr StorageType COUNT = 100'000;
87 constexpr StorageType STEP = STORAGE_MAX / COUNT;
88 for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
89 FPBits x_bits = FPBits(v);
90 if (x_bits.is_zero() || x_bits.is_inf_or_nan())
91 continue;
92
93 T x = x_bits.get_val();
94
95 T integral;
96 T frac = func(x, &integral);
97 ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac) < 1.0l);
98 ASSERT_TRUE(LIBC_NAMESPACE::fputil::trunc(x) == integral);
99 ASSERT_TRUE(integral + frac == x);
100 }
101 }
102};
103
104#define LIST_MODF_TESTS(T, func) \
105 using LlvmLibcModfTest = ModfTest<T>; \
106 TEST_F(LlvmLibcModfTest, SpecialNumbers) { testSpecialNumbers(&func); } \
107 TEST_F(LlvmLibcModfTest, RoundedNubmers) { testIntegers(&func); } \
108 TEST_F(LlvmLibcModfTest, Fractions) { testFractions(&func); } \
109 TEST_F(LlvmLibcModfTest, Range) { testRange(&func); }
110

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