• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- TestMatchers.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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_UTILS_FPUTIL_TEST_HELPERS_H
10 #define LLVM_LIBC_UTILS_FPUTIL_TEST_HELPERS_H
11 
12 #include "FPBits.h"
13 
14 #include "utils/UnitTest/Test.h"
15 
16 namespace __llvm_libc {
17 namespace fputil {
18 namespace testing {
19 
20 template <typename ValType>
21 cpp::EnableIfType<cpp::IsFloatingPointType<ValType>::Value, void>
22 describeValue(const char *label, ValType value,
23               testutils::StreamWrapper &stream);
24 
25 template <typename T, __llvm_libc::testing::TestCondition Condition>
26 class FPMatcher : public __llvm_libc::testing::Matcher<T> {
27   static_assert(__llvm_libc::cpp::IsFloatingPointType<T>::Value,
28                 "FPMatcher can only be used with floating point values.");
29   static_assert(Condition == __llvm_libc::testing::Cond_EQ ||
30                     Condition == __llvm_libc::testing::Cond_NE,
31                 "Unsupported FPMathcer test condition.");
32 
33   T expected;
34   T actual;
35 
36 public:
FPMatcher(T expectedValue)37   FPMatcher(T expectedValue) : expected(expectedValue) {}
38 
match(T actualValue)39   bool match(T actualValue) {
40     actual = actualValue;
41     fputil::FPBits<T> actualBits(actual), expectedBits(expected);
42     if (Condition == __llvm_libc::testing::Cond_EQ)
43       return (actualBits.isNaN() && expectedBits.isNaN()) ||
44              (actualBits.bitsAsUInt() == expectedBits.bitsAsUInt());
45 
46     // If condition == Cond_NE.
47     if (actualBits.isNaN())
48       return !expectedBits.isNaN();
49     return expectedBits.isNaN() ||
50            (actualBits.bitsAsUInt() != expectedBits.bitsAsUInt());
51   }
52 
explainError(testutils::StreamWrapper & stream)53   void explainError(testutils::StreamWrapper &stream) override {
54     describeValue("Expected floating point value: ", expected, stream);
55     describeValue("  Actual floating point value: ", actual, stream);
56   }
57 };
58 
59 template <__llvm_libc::testing::TestCondition C, typename T>
getMatcher(T expectedValue)60 FPMatcher<T, C> getMatcher(T expectedValue) {
61   return FPMatcher<T, C>(expectedValue);
62 }
63 
64 } // namespace testing
65 } // namespace fputil
66 } // namespace __llvm_libc
67 
68 #define DECLARE_SPECIAL_CONSTANTS(T)                                           \
69   static const T zero = __llvm_libc::fputil::FPBits<T>::zero();                \
70   static const T negZero = __llvm_libc::fputil::FPBits<T>::negZero();          \
71   static const T nan = __llvm_libc::fputil::FPBits<T>::buildNaN(1);            \
72   static const T inf = __llvm_libc::fputil::FPBits<T>::inf();                  \
73   static const T negInf = __llvm_libc::fputil::FPBits<T>::negInf();
74 
75 #define EXPECT_FP_EQ(expected, actual)                                         \
76   EXPECT_THAT(                                                                 \
77       actual,                                                                  \
78       __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_EQ>( \
79           expected))
80 
81 #define ASSERT_FP_EQ(expected, actual)                                         \
82   ASSERT_THAT(                                                                 \
83       actual,                                                                  \
84       __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_EQ>( \
85           expected))
86 
87 #define EXPECT_FP_NE(expected, actual)                                         \
88   EXPECT_THAT(                                                                 \
89       actual,                                                                  \
90       __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_NE>( \
91           expected))
92 
93 #define ASSERT_FP_NE(expected, actual)                                         \
94   ASSERT_THAT(                                                                 \
95       actual,                                                                  \
96       __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_NE>( \
97           expected))
98 
99 #endif // LLVM_LIBC_UTILS_FPUTIL_TEST_HELPERS_H
100