[RDF] Add RegisterRef::idx and make toUnitId constexpr
[llvm-project.git] / libc / test / UnitTest / FPMatcher.h
blobf5294203c39fc8db5190c0cb37c34d63756cd4e8
1 //===-- FPMatchers.h --------------------------------------------*- 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 //===----------------------------------------------------------------------===//
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"
19 #include <math.h>
21 namespace __llvm_libc {
22 namespace testing {
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.");
30 T expected;
31 T actual;
33 public:
34 FPMatcher(T expectedValue) : expected(expectedValue) {}
36 bool match(T actualValue) {
37 actual = 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))
54 << '\n';
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) \
75 EXPECT_THAT( \
76 actual, \
77 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::EQ>( \
78 expected))
80 #define EXPECT_FP_IS_NAN(actual) EXPECT_TRUE((actual) != (actual))
82 #define ASSERT_FP_EQ(expected, actual) \
83 ASSERT_THAT( \
84 actual, \
85 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::EQ>( \
86 expected))
88 #define EXPECT_FP_NE(expected, actual) \
89 EXPECT_THAT( \
90 actual, \
91 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::NE>( \
92 expected))
94 #define ASSERT_FP_NE(expected, actual) \
95 ASSERT_THAT( \
96 actual, \
97 __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::NE>( \
98 expected))
100 #define EXPECT_MATH_ERRNO(expected) \
101 do { \
102 if (math_errhandling & MATH_ERRNO) { \
103 int actual = libc_errno; \
104 libc_errno = 0; \
105 EXPECT_EQ(actual, expected); \
107 } while (0)
109 #define ASSERT_MATH_ERRNO(expected) \
110 do { \
111 if (math_errhandling & MATH_ERRNO) { \
112 int actual = libc_errno; \
113 libc_errno = 0; \
114 ASSERT_EQ(actual, expected); \
116 } while (0)
118 #define EXPECT_FP_EXCEPTION(expected) \
119 do { \
120 if (math_errhandling & MATH_ERREXCEPT) { \
121 EXPECT_GE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & expected, \
122 expected); \
124 } while (0)
126 #define ASSERT_FP_EXCEPTION(expected) \
127 do { \
128 if (math_errhandling & MATH_ERREXCEPT) { \
129 ASSERT_GE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & expected, \
130 expected); \
132 } while (0)
134 #define EXPECT_FP_EQ_WITH_EXCEPTION(expected_val, actual_val, expected_except) \
135 do { \
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) & \
140 expected_except, \
141 expected_except); \
142 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT); \
144 } while (0)
146 #define EXPECT_FP_IS_NAN_WITH_EXCEPTION(actual_val, expected_except) \
147 do { \
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) & \
152 expected_except, \
153 expected_except); \
154 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT); \
156 } while (0)
158 #define EXPECT_FP_EQ_ALL_ROUNDING(expected, actual) \
159 do { \
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)); \
169 } while (0)
171 #endif // LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H