diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h --- a/libc/test/UnitTest/FPMatcher.h +++ b/libc/test/UnitTest/FPMatcher.h @@ -18,15 +18,12 @@ #include namespace __llvm_libc { -namespace fputil { namespace testing { -template -class FPMatcher : public __llvm_libc::testing::Matcher { - static_assert(__llvm_libc::cpp::is_floating_point_v, +template class FPMatcher : public Matcher { + static_assert(cpp::is_floating_point_v, "FPMatcher can only be used with floating point values."); - static_assert(Condition == __llvm_libc::testing::Cond_EQ || - Condition == __llvm_libc::testing::Cond_NE, + static_assert(Condition == TestCond::EQ || Condition == TestCond::NE, "Unsupported FPMathcer test condition."); T expected; @@ -38,11 +35,11 @@ bool match(T actualValue) { actual = actualValue; fputil::FPBits actualBits(actual), expectedBits(expected); - if (Condition == __llvm_libc::testing::Cond_EQ) + if (Condition == TestCond::EQ) return (actualBits.is_nan() && expectedBits.is_nan()) || (actualBits.uintval() == expectedBits.uintval()); - // If condition == Cond_NE. + // If condition == TestCond::NE. if (actualBits.is_nan()) return !expectedBits.is_nan(); return expectedBits.is_nan() || @@ -50,21 +47,18 @@ } void explainError() override { - __llvm_libc::testing::tlog - << "Expected floating point value: " << FPBits(expected).str() - << '\n'; - __llvm_libc::testing::tlog - << "Actual floating point value: " << FPBits(actual).str() << '\n'; + tlog << "Expected floating point value: " + << fputil::FPBits(expected).str() << '\n'; + tlog << "Actual floating point value: " << fputil::FPBits(actual).str() + << '\n'; } }; -template <__llvm_libc::testing::TestCondition C, typename T> -FPMatcher getMatcher(T expectedValue) { +template FPMatcher getMatcher(T expectedValue) { return FPMatcher(expectedValue); } } // namespace testing -} // namespace fputil } // namespace __llvm_libc #define DECLARE_SPECIAL_CONSTANTS(T) \ @@ -79,7 +73,7 @@ #define EXPECT_FP_EQ(expected, actual) \ EXPECT_THAT( \ actual, \ - __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_EQ>( \ + __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::EQ>( \ expected)) #define EXPECT_FP_IS_NAN(actual) EXPECT_TRUE((actual) != (actual)) @@ -87,19 +81,19 @@ #define ASSERT_FP_EQ(expected, actual) \ ASSERT_THAT( \ actual, \ - __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_EQ>( \ + __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::EQ>( \ expected)) #define EXPECT_FP_NE(expected, actual) \ EXPECT_THAT( \ actual, \ - __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_NE>( \ + __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::NE>( \ expected)) #define ASSERT_FP_NE(expected, actual) \ ASSERT_THAT( \ actual, \ - __llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_NE>( \ + __llvm_libc::testing::getMatcher<__llvm_libc::testing::TestCond::NE>( \ expected)) #define EXPECT_MATH_ERRNO(expected) \ diff --git a/libc/test/UnitTest/LibcTest.h b/libc/test/UnitTest/LibcTest.h --- a/libc/test/UnitTest/LibcTest.h +++ b/libc/test/UnitTest/LibcTest.h @@ -40,32 +40,23 @@ // return boolean values, but use integral return values to indicate true or // false conditions. Hence, it is more appropriate to use the other comparison // conditions for such cases. -enum TestCondition { - Cond_None, - Cond_EQ, - Cond_NE, - Cond_LT, - Cond_LE, - Cond_GT, - Cond_GE, -}; +enum class TestCond { EQ, NE, LT, LE, GT, GE }; namespace internal { -class RunContext { -public: - enum RunResult { Result_Pass = 1, Result_Fail = 2 }; +struct RunContext { + enum class RunResult : bool { Pass, Fail }; RunResult status() const { return Status; } - void markFail() { Status = Result_Fail; } + void markFail() { Status = RunResult::Fail; } private: - RunResult Status = Result_Pass; + RunResult Status = RunResult::Pass; }; template -bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS, +bool test(RunContext *Ctx, TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); @@ -110,14 +101,14 @@ // of type promotion. template , int> = 0> - bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, + bool test(TestCond 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); } template ::value, int> = 0> - bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, + bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { return internal::test(Ctx, Cond, (long long)LHS, (long long)RHS, LHSStr, RHSStr, File, Line); @@ -125,7 +116,7 @@ template , ValType> = nullptr> - bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, + bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { return internal::test(Ctx, Cond, (unsigned long long)LHS, (unsigned long long)RHS, LHSStr, RHSStr, File, Line); @@ -135,7 +126,7 @@ typename ValType, cpp::enable_if_t, int> = 0> - bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, + bool test(TestCond 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); } @@ -143,7 +134,7 @@ template , int> = 0> - bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, + bool test(TestCond 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); } @@ -353,42 +344,42 @@ void SuiteClass##_##TestName::Run() #define EXPECT_EQ(LHS, RHS) \ - this->test(__llvm_libc::testing::Cond_EQ, (LHS), (RHS), #LHS, #RHS, \ + this->test(__llvm_libc::testing::TestCond::EQ, (LHS), (RHS), #LHS, #RHS, \ __FILE__, __LINE__) #define ASSERT_EQ(LHS, RHS) \ if (!EXPECT_EQ(LHS, RHS)) \ return #define EXPECT_NE(LHS, RHS) \ - this->test(__llvm_libc::testing::Cond_NE, (LHS), (RHS), #LHS, #RHS, \ + this->test(__llvm_libc::testing::TestCond::NE, (LHS), (RHS), #LHS, #RHS, \ __FILE__, __LINE__) #define ASSERT_NE(LHS, RHS) \ if (!EXPECT_NE(LHS, RHS)) \ return #define EXPECT_LT(LHS, RHS) \ - this->test(__llvm_libc::testing::Cond_LT, (LHS), (RHS), #LHS, #RHS, \ + this->test(__llvm_libc::testing::TestCond::LT, (LHS), (RHS), #LHS, #RHS, \ __FILE__, __LINE__) #define ASSERT_LT(LHS, RHS) \ if (!EXPECT_LT(LHS, RHS)) \ return #define EXPECT_LE(LHS, RHS) \ - this->test(__llvm_libc::testing::Cond_LE, (LHS), (RHS), #LHS, #RHS, \ + this->test(__llvm_libc::testing::TestCond::LE, (LHS), (RHS), #LHS, #RHS, \ __FILE__, __LINE__) #define ASSERT_LE(LHS, RHS) \ if (!EXPECT_LE(LHS, RHS)) \ return #define EXPECT_GT(LHS, RHS) \ - this->test(__llvm_libc::testing::Cond_GT, (LHS), (RHS), #LHS, #RHS, \ + this->test(__llvm_libc::testing::TestCond::GT, (LHS), (RHS), #LHS, #RHS, \ __FILE__, __LINE__) #define ASSERT_GT(LHS, RHS) \ if (!EXPECT_GT(LHS, RHS)) \ return #define EXPECT_GE(LHS, RHS) \ - this->test(__llvm_libc::testing::Cond_GE, (LHS), (RHS), #LHS, #RHS, \ + this->test(__llvm_libc::testing::TestCond::GE, (LHS), (RHS), #LHS, #RHS, \ __FILE__, __LINE__) #define ASSERT_GE(LHS, RHS) \ if (!EXPECT_GE(LHS, RHS)) \ diff --git a/libc/test/UnitTest/LibcTest.cpp b/libc/test/UnitTest/LibcTest.cpp --- a/libc/test/UnitTest/LibcTest.cpp +++ b/libc/test/UnitTest/LibcTest.cpp @@ -46,78 +46,41 @@ return cpp::to_string(Value); } -cpp::string describeValue(cpp::string Value) { return Value; } +cpp::string_view describeValue(const cpp::string &Value) { return Value; } cpp::string_view describeValue(cpp::string_view Value) { return Value; } template -void explainDifference(ValType LHS, ValType RHS, const char *LHSStr, - const char *RHSStr, const char *File, unsigned long Line, - cpp::string OpString) { - size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0; - cpp::string Offset(OffsetLength, ' '); - - tlog << File << ":" << Line << ": FAILURE\n" - << Offset << "Expected: " << LHSStr << '\n' - << Offset << "Which is: " << describeValue(LHS) << '\n' - << "To be " << OpString << ": " << RHSStr << '\n' - << Offset << "Which is: " << describeValue(RHS) << '\n'; -} - -template -bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS, +bool test(RunContext *Ctx, TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { - auto ExplainDifference = [=](cpp::string OpString) { - explainDifference(LHS, RHS, LHSStr, RHSStr, File, Line, OpString); - }; - - switch (Cond) { - case Cond_EQ: - if (LHS == RHS) - return true; - - Ctx->markFail(); - ExplainDifference("equal to"); - return false; - case Cond_NE: - if (LHS != RHS) - return true; - - Ctx->markFail(); - ExplainDifference("not equal to"); - return false; - case Cond_LT: - if (LHS < RHS) + auto ExplainDifference = [=, &Ctx](bool Cond, + cpp::string_view OpString) -> bool { + if (Cond) return true; - Ctx->markFail(); - ExplainDifference("less than"); - return false; - case Cond_LE: - if (LHS <= RHS) - return true; - - Ctx->markFail(); - ExplainDifference("less than or equal to"); - return false; - case Cond_GT: - if (LHS > RHS) - return true; - - Ctx->markFail(); - ExplainDifference("greater than"); + size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0; + cpp::string Offset(OffsetLength, ' '); + tlog << File << ":" << Line << ": FAILURE\n" + << Offset << "Expected: " << LHSStr << '\n' + << Offset << "Which is: " << describeValue(LHS) << '\n' + << "To be " << OpString << ": " << RHSStr << '\n' + << Offset << "Which is: " << describeValue(RHS) << '\n'; return false; - case Cond_GE: - if (LHS >= RHS) - return true; + }; - Ctx->markFail(); - ExplainDifference("greater than or equal to"); - return false; - default: - Ctx->markFail(); - tlog << "Unexpected test condition.\n"; - return false; + switch (Cond) { + case TestCond::EQ: + return ExplainDifference(LHS == RHS, "equal to"); + case TestCond::NE: + return ExplainDifference(LHS != RHS, "not equal to"); + case TestCond::LT: + return ExplainDifference(LHS < RHS, "less than"); + case TestCond::LE: + return ExplainDifference(LHS <= RHS, "less than or equal to"); + case TestCond::GT: + return ExplainDifference(LHS > RHS, "greater than"); + case TestCond::GE: + return ExplainDifference(LHS >= RHS, "greater than or equal to"); } } @@ -163,13 +126,12 @@ T->Run(); T->TearDown(); [[maybe_unused]] const auto end_time = clock(); - auto Result = Ctx.status(); - switch (Result) { - case RunContext::Result_Fail: + switch (Ctx.status()) { + case RunContext::RunResult::Fail: tlog << RED << "[ FAILED ] " << RESET << TestName << '\n'; ++FailCount; break; - case RunContext::Result_Pass: + case RunContext::RunResult::Pass: tlog << GREEN << "[ OK ] " << RESET << TestName; #if __STDC_HOSTED__ tlog << " (took "; @@ -204,52 +166,52 @@ namespace internal { -template bool test(RunContext *Ctx, TestCondition Cond, char LHS, - char RHS, const char *LHSStr, const char *RHSStr, +template bool test(RunContext *Ctx, TestCond Cond, char LHS, char RHS, + const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, short LHS, - short RHS, const char *LHSStr, const char *RHSStr, +template bool test(RunContext *Ctx, TestCond Cond, short LHS, short RHS, + const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, int LHS, int RHS, +template bool test(RunContext *Ctx, TestCond Cond, int LHS, int RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, long LHS, - long RHS, const char *LHSStr, const char *RHSStr, +template bool test(RunContext *Ctx, TestCond Cond, long LHS, long RHS, + const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, - long long LHS, long long RHS, const char *LHSStr, +template bool test(RunContext *Ctx, TestCond Cond, long long LHS, + long long RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, +template bool test(RunContext *Ctx, TestCond Cond, unsigned char LHS, unsigned char RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, +template bool test(RunContext *Ctx, TestCond Cond, unsigned short LHS, unsigned short RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, +template bool test(RunContext *Ctx, TestCond Cond, unsigned int LHS, unsigned int RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, +template bool test(RunContext *Ctx, TestCond Cond, unsigned long LHS, unsigned long RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, bool LHS, - bool RHS, const char *LHSStr, const char *RHSStr, +template bool test(RunContext *Ctx, TestCond Cond, bool LHS, bool RHS, + const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); -template bool test(RunContext *Ctx, TestCondition Cond, +template bool test(RunContext *Ctx, TestCond Cond, unsigned long long LHS, unsigned long long RHS, const char *LHSStr, const char *RHSStr, @@ -262,39 +224,39 @@ #ifdef __SIZEOF_INT128__ // When builtin __uint128_t type is available, include its specialization // also. -template bool test<__uint128_t>(RunContext *Ctx, TestCondition Cond, - __uint128_t LHS, __uint128_t RHS, - const char *LHSStr, const char *RHSStr, - const char *File, unsigned long Line); +template bool test<__uint128_t>(RunContext *Ctx, TestCond Cond, __uint128_t LHS, + __uint128_t RHS, const char *LHSStr, + const char *RHSStr, const char *File, + unsigned long Line); #endif template bool test<__llvm_libc::cpp::UInt<128>>( - RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<128> LHS, + RunContext *Ctx, TestCond Cond, __llvm_libc::cpp::UInt<128> LHS, __llvm_libc::cpp::UInt<128> RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); template bool test<__llvm_libc::cpp::UInt<192>>( - RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<192> LHS, + RunContext *Ctx, TestCond Cond, __llvm_libc::cpp::UInt<192> LHS, __llvm_libc::cpp::UInt<192> RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); template bool test<__llvm_libc::cpp::UInt<256>>( - RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<256> LHS, + RunContext *Ctx, TestCond Cond, __llvm_libc::cpp::UInt<256> LHS, __llvm_libc::cpp::UInt<256> RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); template bool test<__llvm_libc::cpp::UInt<320>>( - RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<320> LHS, + RunContext *Ctx, TestCond Cond, __llvm_libc::cpp::UInt<320> LHS, __llvm_libc::cpp::UInt<320> 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, + RunContext *Ctx, TestCond 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>( - RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::string LHS, + RunContext *Ctx, TestCond Cond, __llvm_libc::cpp::string LHS, __llvm_libc::cpp::string RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); @@ -302,16 +264,18 @@ bool Test::testStrEq(const char *LHS, const char *RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { - return internal::test(Ctx, Cond_EQ, LHS ? cpp::string(LHS) : cpp::string(), - RHS ? cpp::string(RHS) : cpp::string(), LHSStr, RHSStr, - File, Line); + return internal::test(Ctx, TestCond::EQ, + LHS ? cpp::string_view(LHS) : cpp::string_view(), + RHS ? cpp::string_view(RHS) : cpp::string_view(), + LHSStr, RHSStr, File, Line); } bool Test::testStrNe(const char *LHS, const char *RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { - return internal::test(Ctx, Cond_NE, LHS ? cpp::string(LHS) : cpp::string(), - RHS ? cpp::string(RHS) : cpp::string(), LHSStr, RHSStr, - File, Line); + return internal::test(Ctx, TestCond::NE, + LHS ? cpp::string_view(LHS) : cpp::string_view(), + RHS ? cpp::string_view(RHS) : cpp::string_view(), + LHSStr, RHSStr, File, Line); } bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,