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

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