[libc-commits] [libc] 1427550 - [libc] Add a testing macro for MPFR matchers skipping `explainError` calls.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Wed Oct 26 15:12:02 PDT 2022


Author: Tue Ly
Date: 2022-10-26T18:11:45-04:00
New Revision: 14275501516fe6ca896a0428ab8e5e0dc7bff137

URL: https://github.com/llvm/llvm-project/commit/14275501516fe6ca896a0428ab8e5e0dc7bff137
DIFF: https://github.com/llvm/llvm-project/commit/14275501516fe6ca896a0428ab8e5e0dc7bff137.diff

LOG: [libc] Add a testing macro for MPFR matchers skipping `explainError` calls.

Adding `EXPECT_MPFR_MATCH_ROUNDING_SILENTLY` macro that does not call
`explainError` when the tests fail.  This is useful to check the passing or
failing rates, such as hitting percentages of fast passes in math
implementations.

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D136731

Added: 
    

Modified: 
    libc/utils/MPFRWrapper/MPFRUtils.h
    libc/utils/UnitTest/LibcTest.cpp
    libc/utils/UnitTest/LibcTest.h

Removed: 
    


################################################################################
diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 7902da5e97737..3b9e24ae92660 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -176,7 +176,7 @@ void explain_ternary_operation_one_output_error(
     Operation op, const TernaryInput<T> &input, T match_value,
     double ulp_tolerance, RoundingMode rounding, testutils::StreamWrapper &OS);
 
-template <Operation op, typename InputType, typename OutputType>
+template <Operation op, bool silent, typename InputType, typename OutputType>
 class MPFRMatcher : public testing::Matcher<OutputType> {
   InputType input;
   OutputType match_value;
@@ -198,6 +198,9 @@ class MPFRMatcher : public testing::Matcher<OutputType> {
     explain_error(input, match_value, OS);
   }
 
+  // Whether the `explainError` step is skipped or not.
+  bool is_silent() const override { return silent; }
+
 private:
   template <typename T> bool match(T in, T out) {
     return compare_unary_operation_single_output(op, in, out, ulp_tolerance,
@@ -289,13 +292,23 @@ constexpr bool is_valid_operation() {
 }
 
 template <Operation op, typename InputType, typename OutputType>
-__attribute__((no_sanitize("address")))
-cpp::enable_if_t<is_valid_operation<op, InputType, OutputType>(),
-                 internal::MPFRMatcher<op, InputType, OutputType>>
+__attribute__((no_sanitize("address"))) cpp::enable_if_t<
+    is_valid_operation<op, InputType, OutputType>(),
+    internal::MPFRMatcher<op, /*is_silent*/ false, InputType, OutputType>>
 get_mpfr_matcher(InputType input, OutputType output_unused,
                  double ulp_tolerance, RoundingMode rounding) {
-  return internal::MPFRMatcher<op, InputType, OutputType>(input, ulp_tolerance,
-                                                          rounding);
+  return internal::MPFRMatcher<op, /*is_silent*/ false, InputType, OutputType>(
+      input, ulp_tolerance, rounding);
+}
+
+template <Operation op, typename InputType, typename OutputType>
+__attribute__((no_sanitize("address"))) cpp::enable_if_t<
+    is_valid_operation<op, InputType, OutputType>(),
+    internal::MPFRMatcher<op, /*is_silent*/ true, InputType, OutputType>>
+get_silent_mpfr_matcher(InputType input, OutputType output_unused,
+                        double ulp_tolerance, RoundingMode rounding) {
+  return internal::MPFRMatcher<op, /*is_silent*/ true, InputType, OutputType>(
+      input, ulp_tolerance, rounding);
 }
 
 template <typename T> T round(T x, RoundingMode mode);
@@ -346,6 +359,12 @@ template <typename T> bool round_to_long(T x, RoundingMode mode, long &result);
                       mpfr::RoundingMode::TowardZero);                         \
   }
 
+#define EXPECT_MPFR_MATCH_ROUNDING_SILENTLY(op, input, match_value,            \
+                                            ulp_tolerance, rounding)           \
+  EXPECT_THAT(match_value,                                                     \
+              __llvm_libc::testing::mpfr::get_silent_mpfr_matcher<op>(         \
+                  input, match_value, ulp_tolerance, rounding))
+
 #define ASSERT_MPFR_MATCH_DEFAULT(op, input, match_value, ulp_tolerance)       \
   ASSERT_THAT(match_value,                                                     \
               __llvm_libc::testing::mpfr::get_mpfr_matcher<op>(                \

diff  --git a/libc/utils/UnitTest/LibcTest.cpp b/libc/utils/UnitTest/LibcTest.cpp
index 10f35b589287c..8a90d8df9d90f 100644
--- a/libc/utils/UnitTest/LibcTest.cpp
+++ b/libc/utils/UnitTest/LibcTest.cpp
@@ -282,11 +282,10 @@ template bool test<__llvm_libc::cpp::UInt<128>>(
     __llvm_libc::cpp::UInt<128> RHS, const char *LHSStr, const char *RHSStr,
     const char *File, unsigned long Line);
 
-template bool test<__llvm_libc::cpp::string_view>(RunContext *Ctx, TestCondition Cond,
-                                       __llvm_libc::cpp::string_view LHS,
-                                       __llvm_libc::cpp::string_view RHS,
-                                       const char *LHSStr, const char *RHSStr,
-                                       const char *File, unsigned long Line);
+template bool test<__llvm_libc::cpp::string_view>(
+    RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::string_view LHS,
+    __llvm_libc::cpp::string_view RHS, const char *LHSStr, const char *RHSStr,
+    const char *File, unsigned long Line);
 
 } // namespace internal
 
@@ -310,10 +309,12 @@ bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
     return true;
 
   Ctx->markFail();
-  std::cout << File << ":" << Line << ": FAILURE\n"
-            << "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
-  testutils::StreamWrapper OutsWrapper = testutils::outs();
-  Matcher.explainError(OutsWrapper);
+  if (!Matcher.is_silent()) {
+    std::cout << File << ":" << Line << ": FAILURE\n"
+              << "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
+    testutils::StreamWrapper OutsWrapper = testutils::outs();
+    Matcher.explainError(OutsWrapper);
+  }
   return false;
 }
 

diff  --git a/libc/utils/UnitTest/LibcTest.h b/libc/utils/UnitTest/LibcTest.h
index b6a9ce6d4ee1b..9356bf92f375f 100644
--- a/libc/utils/UnitTest/LibcTest.h
+++ b/libc/utils/UnitTest/LibcTest.h
@@ -53,6 +53,8 @@ struct MatcherBase {
   virtual void explainError(testutils::StreamWrapper &OS) {
     OS << "unknown error\n";
   }
+  // Override and return true to skip `explainError` step.
+  virtual bool is_silent() const { return false; }
 };
 
 template <typename T> struct Matcher : public MatcherBase { bool match(T &t); };
@@ -106,9 +108,10 @@ class Test {
                           (unsigned long long)RHS, LHSStr, RHSStr, File, Line);
   }
 
-  template <typename ValType,
-            cpp::enable_if_t<
-                cpp::is_same_v<ValType, __llvm_libc::cpp::string_view>, int> = 0>
+  template <
+      typename ValType,
+      cpp::enable_if_t<cpp::is_same_v<ValType, __llvm_libc::cpp::string_view>,
+                       int> = 0>
   bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr,
             const char *RHSStr, const char *File, unsigned long Line) {
     return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, File, Line);


        


More information about the libc-commits mailing list