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#ifndef LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H
10#define LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H
11
12#include "test/UnitTest/FEnvSafeTest.h"
13#include "test/UnitTest/FPMatcher.h"
14#include "test/UnitTest/Test.h"
15#include "utils/MPFRWrapper/MPFRUtils.h"
16
17#include "hdr/math_macros.h"
18
19namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
20
21template <typename T>
22class FloorTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
23
24 DECLARE_SPECIAL_CONSTANTS(T)
25
26public:
27 typedef T (*FloorFunc)(T);
28
29 void testSpecialNumbers(FloorFunc func) {
30 EXPECT_FP_EQ(zero, func(zero));
31 EXPECT_FP_EQ(neg_zero, func(neg_zero));
32
33 EXPECT_FP_EQ(inf, func(inf));
34 EXPECT_FP_EQ(neg_inf, func(neg_inf));
35
36 EXPECT_FP_EQ(aNaN, func(aNaN));
37 }
38
39 void testRoundedNumbers(FloorFunc func) {
40 EXPECT_FP_EQ(T(1.0), func(T(1.0)));
41 EXPECT_FP_EQ(T(-1.0), func(T(-1.0)));
42 EXPECT_FP_EQ(T(10.0), func(T(10.0)));
43 EXPECT_FP_EQ(T(-10.0), func(T(-10.0)));
44 EXPECT_FP_EQ(T(1234.0), func(T(1234.0)));
45 EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0)));
46 }
47
48 void testFractions(FloorFunc func) {
49 EXPECT_FP_EQ(T(0.0), func(T(0.5)));
50 EXPECT_FP_EQ(T(-1.0), func(T(-0.5)));
51 EXPECT_FP_EQ(T(0.0), func(T(0.115)));
52 EXPECT_FP_EQ(T(-1.0), func(T(-0.115)));
53 EXPECT_FP_EQ(T(0.0), func(T(0.715)));
54 EXPECT_FP_EQ(T(-1.0), func(T(-0.715)));
55 EXPECT_FP_EQ(T(1.0), func(T(1.3)));
56 EXPECT_FP_EQ(T(-2.0), func(T(-1.3)));
57 EXPECT_FP_EQ(T(1.0), func(T(1.5)));
58 EXPECT_FP_EQ(T(-2.0), func(T(-1.5)));
59 EXPECT_FP_EQ(T(1.0), func(T(1.75)));
60 EXPECT_FP_EQ(T(-2.0), func(T(-1.75)));
61 EXPECT_FP_EQ(T(10.0), func(T(10.32)));
62 EXPECT_FP_EQ(T(-11.0), func(T(-10.32)));
63 EXPECT_FP_EQ(T(10.0), func(T(10.65)));
64 EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
65 EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
66 EXPECT_FP_EQ(T(-1235.0), func(T(-1234.38)));
67 EXPECT_FP_EQ(T(1234.0), func(T(1234.96)));
68 EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
69 }
70
71 void testRange(FloorFunc func) {
72 constexpr StorageType COUNT = 100'000;
73 constexpr StorageType STEP = STORAGE_MAX / COUNT;
74 for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
75 T x = FPBits(v).get_val();
76 if (isnan(x) || isinf(x))
77 continue;
78
79 ASSERT_MPFR_MATCH(mpfr::Operation::Floor, x, func(x), 0.0);
80 }
81 }
82};
83
84#define LIST_FLOOR_TESTS(T, func) \
85 using LlvmLibcFloorTest = FloorTest<T>; \
86 TEST_F(LlvmLibcFloorTest, SpecialNumbers) { testSpecialNumbers(&func); } \
87 TEST_F(LlvmLibcFloorTest, RoundedNubmers) { testRoundedNumbers(&func); } \
88 TEST_F(LlvmLibcFloorTest, Fractions) { testFractions(&func); } \
89 TEST_F(LlvmLibcFloorTest, Range) { testRange(&func); }
90
91#endif // LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H
92

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