Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / UnitTest / FPMatcher.h
blob14c8a85ba7ad4804c1c65f4eb12b294448e00c7b
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 LIBC_NAMESPACE {
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 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>( \
90 expected) \
91 .match(actual)
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) \
108 do { \
109 if (math_errhandling & MATH_ERRNO) { \
110 int actual = libc_errno; \
111 libc_errno = 0; \
112 EXPECT_EQ(actual, expected); \
114 } while (0)
116 #define ASSERT_MATH_ERRNO(expected) \
117 do { \
118 if (math_errhandling & MATH_ERRNO) { \
119 int actual = libc_errno; \
120 libc_errno = 0; \
121 ASSERT_EQ(actual, expected); \
123 } while (0)
125 #define EXPECT_FP_EXCEPTION(expected) \
126 do { \
127 if (math_errhandling & MATH_ERREXCEPT) { \
128 EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & expected, \
129 expected); \
131 } while (0)
133 #define ASSERT_FP_EXCEPTION(expected) \
134 do { \
135 if (math_errhandling & MATH_ERREXCEPT) { \
136 ASSERT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & expected, \
137 expected); \
139 } while (0)
141 #define EXPECT_FP_EQ_WITH_EXCEPTION(expected_val, actual_val, expected_except) \
142 do { \
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) & \
147 expected_except, \
148 expected_except); \
149 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
151 } while (0)
153 #define EXPECT_FP_IS_NAN_WITH_EXCEPTION(actual_val, expected_except) \
154 do { \
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) & \
159 expected_except, \
160 expected_except); \
161 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
163 } while (0)
165 #define EXPECT_FP_EQ_ALL_ROUNDING(expected, actual) \
166 do { \
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)); \
184 } while (0)
186 #endif // LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H