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 __llvm_libc
{
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 } // namespace testing
63 } // namespace __llvm_libc
65 #define DECLARE_SPECIAL_CONSTANTS(T) \
66 using FPBits = __llvm_libc::fputil::FPBits<T>; \
67 using UIntType = typename FPBits::UIntType; \
68 const T zero = T(FPBits::zero()); \
69 const T neg_zero = T(FPBits::neg_zero()); \
70 const T aNaN = T(FPBits::build_quiet_nan(1)); \
71 const T inf = T(FPBits::inf()); \
72 const T neg_inf = T(FPBits::neg_inf());
74 #define EXPECT_FP_EQ(expected, actual) \
77 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::EQ>( \
80 #define EXPECT_FP_IS_NAN(actual) EXPECT_TRUE((actual) != (actual))
82 #define ASSERT_FP_EQ(expected, actual) \
85 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::EQ>( \
88 #define EXPECT_FP_NE(expected, actual) \
91 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::NE>( \
94 #define ASSERT_FP_NE(expected, actual) \
97 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::NE>( \
100 #define EXPECT_MATH_ERRNO(expected) \
102 if (math_errhandling & MATH_ERRNO) { \
103 int actual = libc_errno; \
105 EXPECT_EQ(actual, expected); \
109 #define ASSERT_MATH_ERRNO(expected) \
111 if (math_errhandling & MATH_ERRNO) { \
112 int actual = libc_errno; \
114 ASSERT_EQ(actual, expected); \
118 #define EXPECT_FP_EXCEPTION(expected) \
120 if (math_errhandling & MATH_ERREXCEPT) { \
121 EXPECT_GE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & expected, \
126 #define ASSERT_FP_EXCEPTION(expected) \
128 if (math_errhandling & MATH_ERREXCEPT) { \
129 ASSERT_GE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & expected, \
134 #define EXPECT_FP_EQ_WITH_EXCEPTION(expected_val, actual_val, expected_except) \
136 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT); \
137 EXPECT_FP_EQ(expected_val, actual_val); \
138 if (math_errhandling & MATH_ERREXCEPT) { \
139 EXPECT_GE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & \
142 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT); \
146 #define EXPECT_FP_IS_NAN_WITH_EXCEPTION(actual_val, expected_except) \
148 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT); \
149 EXPECT_FP_IS_NAN(actual_val); \
150 if (math_errhandling & MATH_ERREXCEPT) { \
151 EXPECT_GE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & \
154 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT); \
158 #define EXPECT_FP_EQ_ALL_ROUNDING(expected, actual) \
160 using namespace __llvm_libc::fputil::testing; \
161 ForceRoundingMode __r1(RoundingMode::Nearest); \
162 EXPECT_FP_EQ((expected), (actual)); \
163 ForceRoundingMode __r2(RoundingMode::Upward); \
164 EXPECT_FP_EQ((expected), (actual)); \
165 ForceRoundingMode __r3(RoundingMode::Downward); \
166 EXPECT_FP_EQ((expected), (actual)); \
167 ForceRoundingMode __r4(RoundingMode::TowardZero); \
168 EXPECT_FP_EQ((expected), (actual)); \
171 #endif // LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H