1 //===-- FPMatchers.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H
10 #define LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H
12 #include "src/__support/FPUtil/FEnvImpl.h"
13 #include "src/__support/FPUtil/FPBits.h"
14 #include "src/__support/FPUtil/fpbits_str.h"
15 #include "test/UnitTest/RoundingModeUtils.h"
16 #include "test/UnitTest/StringUtils.h"
17 #include "test/UnitTest/Test.h"
21 namespace LIBC_NAMESPACE
{
24 template <typename T
, TestCond Condition
> class FPMatcher
: public Matcher
<T
> {
25 static_assert(cpp::is_floating_point_v
<T
>,
26 "FPMatcher can only be used with floating point values.");
27 static_assert(Condition
== TestCond::EQ
|| Condition
== TestCond::NE
,
28 "Unsupported FPMatcher test condition.");
34 FPMatcher(T expectedValue
) : expected(expectedValue
) {}
36 bool match(T actualValue
) {
38 fputil::FPBits
<T
> actualBits(actual
), expectedBits(expected
);
39 if (Condition
== TestCond::EQ
)
40 return (actualBits
.is_nan() && expectedBits
.is_nan()) ||
41 (actualBits
.uintval() == expectedBits
.uintval());
43 // If condition == TestCond::NE.
44 if (actualBits
.is_nan())
45 return !expectedBits
.is_nan();
46 return expectedBits
.is_nan() ||
47 (actualBits
.uintval() != expectedBits
.uintval());
50 void explainError() override
{
51 tlog
<< "Expected floating point value: "
52 << str(fputil::FPBits
<T
>(expected
)) << '\n';
53 tlog
<< "Actual floating point value: " << str(fputil::FPBits
<T
>(actual
))
58 template <TestCond C
, typename T
> FPMatcher
<T
, C
> getMatcher(T expectedValue
) {
59 return FPMatcher
<T
, C
>(expectedValue
);
62 template <typename T
> struct FPTest
: public Test
{
63 using FPBits
= LIBC_NAMESPACE::fputil::FPBits
<T
>;
64 using UIntType
= typename
FPBits::UIntType
;
65 static constexpr T zero
= T(FPBits::zero());
66 static constexpr T neg_zero
= T(FPBits::neg_zero());
67 static constexpr T aNaN
= T(FPBits::build_quiet_nan(1));
68 static constexpr T inf
= T(FPBits::inf());
69 static constexpr T neg_inf
= T(FPBits::neg_inf());
72 } // namespace testing
73 } // namespace LIBC_NAMESPACE
75 #define DECLARE_SPECIAL_CONSTANTS(T) \
76 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; \
77 using UIntType = typename FPBits::UIntType; \
78 const T zero = T(FPBits::zero()); \
79 const T neg_zero = T(FPBits::neg_zero()); \
80 const T aNaN = T(FPBits::build_quiet_nan(1)); \
81 const T inf = T(FPBits::inf()); \
82 const T neg_inf = T(FPBits::neg_inf());
84 #define EXPECT_FP_EQ(expected, actual) \
85 EXPECT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
86 LIBC_NAMESPACE::testing::TestCond::EQ>(expected))
88 #define TEST_FP_EQ(expected, actual) \
89 LIBC_NAMESPACE::testing::getMatcher<LIBC_NAMESPACE::testing::TestCond::EQ>( \
93 #define EXPECT_FP_IS_NAN(actual) EXPECT_TRUE((actual) != (actual))
95 #define ASSERT_FP_EQ(expected, actual) \
96 ASSERT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
97 LIBC_NAMESPACE::testing::TestCond::EQ>(expected))
99 #define EXPECT_FP_NE(expected, actual) \
100 EXPECT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
101 LIBC_NAMESPACE::testing::TestCond::NE>(expected))
103 #define ASSERT_FP_NE(expected, actual) \
104 ASSERT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
105 LIBC_NAMESPACE::testing::TestCond::NE>(expected))
107 #define EXPECT_MATH_ERRNO(expected) \
109 if (math_errhandling & MATH_ERRNO) { \
110 int actual = libc_errno; \
112 EXPECT_EQ(actual, expected); \
116 #define ASSERT_MATH_ERRNO(expected) \
118 if (math_errhandling & MATH_ERRNO) { \
119 int actual = libc_errno; \
121 ASSERT_EQ(actual, expected); \
125 #define EXPECT_FP_EXCEPTION(expected) \
127 if (math_errhandling & MATH_ERREXCEPT) { \
128 EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & expected, \
133 #define ASSERT_FP_EXCEPTION(expected) \
135 if (math_errhandling & MATH_ERREXCEPT) { \
136 ASSERT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & expected, \
141 #define EXPECT_FP_EQ_WITH_EXCEPTION(expected_val, actual_val, expected_except) \
143 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
144 EXPECT_FP_EQ(expected_val, actual_val); \
145 if (math_errhandling & MATH_ERREXCEPT) { \
146 EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & \
149 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
153 #define EXPECT_FP_IS_NAN_WITH_EXCEPTION(actual_val, expected_except) \
155 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
156 EXPECT_FP_IS_NAN(actual_val); \
157 if (math_errhandling & MATH_ERREXCEPT) { \
158 EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & \
161 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
165 #define EXPECT_FP_EQ_ALL_ROUNDING(expected, actual) \
167 using namespace LIBC_NAMESPACE::fputil::testing; \
168 ForceRoundingMode __r1(RoundingMode::Nearest); \
169 if (__r1.success) { \
170 EXPECT_FP_EQ((expected), (actual)); \
172 ForceRoundingMode __r2(RoundingMode::Upward); \
173 if (__r2.success) { \
174 EXPECT_FP_EQ((expected), (actual)); \
176 ForceRoundingMode __r3(RoundingMode::Downward); \
177 if (__r3.success) { \
178 EXPECT_FP_EQ((expected), (actual)); \
180 ForceRoundingMode __r4(RoundingMode::TowardZero); \
181 if (__r4.success) { \
182 EXPECT_FP_EQ((expected), (actual)); \
186 #endif // LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H