[libc-commits] [libc] fd2c74c - [libc][NFC] Simplify LibcTest and trim down string allocations
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Fri Jun 9 02:36:28 PDT 2023
Author: Guillaume Chatelet
Date: 2023-06-09T09:36:18Z
New Revision: fd2c74c8ed9d311840df5ce2e9106cab2d681212
URL: https://github.com/llvm/llvm-project/commit/fd2c74c8ed9d311840df5ce2e9106cab2d681212
DIFF: https://github.com/llvm/llvm-project/commit/fd2c74c8ed9d311840df5ce2e9106cab2d681212.diff
LOG: [libc][NFC] Simplify LibcTest and trim down string allocations
This is a bit of cleanup before working on logging via stream operator (i.e., `EXPECT_XXX() << ...`).
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D152503
Added:
Modified:
libc/test/UnitTest/FPMatcher.h
libc/test/UnitTest/LibcTest.cpp
libc/test/UnitTest/LibcTest.h
Removed:
################################################################################
diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index d3e83384ba1ce..ae50d50cdb15d 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -18,15 +18,12 @@
#include <math.h>
namespace __llvm_libc {
-namespace fputil {
namespace testing {
-template <typename T, __llvm_libc::testing::TestCondition Condition>
-class FPMatcher : public __llvm_libc::testing::Matcher<T> {
- static_assert(__llvm_libc::cpp::is_floating_point_v<T>,
+template <typename T, TestCond Condition> class FPMatcher : public Matcher<T> {
+ static_assert(cpp::is_floating_point_v<T>,
"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 @@ class FPMatcher : public __llvm_libc::testing::Matcher<T> {
bool match(T actualValue) {
actual = actualValue;
fputil::FPBits<T> 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 @@ class FPMatcher : public __llvm_libc::testing::Matcher<T> {
}
void explainError() override {
- __llvm_libc::testing::tlog
- << "Expected floating point value: " << FPBits<T>(expected).str()
- << '\n';
- __llvm_libc::testing::tlog
- << "Actual floating point value: " << FPBits<T>(actual).str() << '\n';
+ tlog << "Expected floating point value: "
+ << fputil::FPBits<T>(expected).str() << '\n';
+ tlog << "Actual floating point value: " << fputil::FPBits<T>(actual).str()
+ << '\n';
}
};
-template <__llvm_libc::testing::TestCondition C, typename T>
-FPMatcher<T, C> getMatcher(T expectedValue) {
+template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
return FPMatcher<T, C>(expectedValue);
}
} // namespace testing
-} // namespace fputil
} // namespace __llvm_libc
#define DECLARE_SPECIAL_CONSTANTS(T) \
@@ -79,7 +73,7 @@ FPMatcher<T, C> getMatcher(T expectedValue) {
#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 @@ FPMatcher<T, C> getMatcher(T expectedValue) {
#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.cpp b/libc/test/UnitTest/LibcTest.cpp
index 9555a179707eb..8fdc7ee3a6eb7 100644
--- a/libc/test/UnitTest/LibcTest.cpp
+++ b/libc/test/UnitTest/LibcTest.cpp
@@ -46,78 +46,41 @@ describeValue(ValType Value) {
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 <typename ValType>
-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 <typename ValType>
-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 @@ int Test::runTests(const char *TestFilter) {
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 @@ int Test::runTests(const char *TestFilter) {
namespace internal {
-template bool test<char>(RunContext *Ctx, TestCondition Cond, char LHS,
- char RHS, const char *LHSStr, const char *RHSStr,
+template bool test<char>(RunContext *Ctx, TestCond Cond, char LHS, char RHS,
+ const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);
-template bool test<short>(RunContext *Ctx, TestCondition Cond, short LHS,
- short RHS, const char *LHSStr, const char *RHSStr,
+template bool test<short>(RunContext *Ctx, TestCond Cond, short LHS, short RHS,
+ const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);
-template bool test<int>(RunContext *Ctx, TestCondition Cond, int LHS, int RHS,
+template bool test<int>(RunContext *Ctx, TestCond Cond, int LHS, int RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);
-template bool test<long>(RunContext *Ctx, TestCondition Cond, long LHS,
- long RHS, const char *LHSStr, const char *RHSStr,
+template bool test<long>(RunContext *Ctx, TestCond Cond, long LHS, long RHS,
+ const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);
-template bool test<long long>(RunContext *Ctx, TestCondition Cond,
- long long LHS, long long RHS, const char *LHSStr,
+template bool test<long long>(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<unsigned char>(RunContext *Ctx, TestCondition Cond,
+template bool test<unsigned char>(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<unsigned short>(RunContext *Ctx, TestCondition Cond,
+template bool test<unsigned short>(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<unsigned int>(RunContext *Ctx, TestCondition Cond,
+template bool test<unsigned int>(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<unsigned long>(RunContext *Ctx, TestCondition Cond,
+template bool test<unsigned long>(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<bool>(RunContext *Ctx, TestCondition Cond, bool LHS,
- bool RHS, const char *LHSStr, const char *RHSStr,
+template bool test<bool>(RunContext *Ctx, TestCond Cond, bool LHS, bool RHS,
+ const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);
-template bool test<unsigned long long>(RunContext *Ctx, TestCondition Cond,
+template bool test<unsigned long long>(RunContext *Ctx, TestCond Cond,
unsigned long long LHS,
unsigned long long RHS,
const char *LHSStr, const char *RHSStr,
@@ -262,39 +224,39 @@ template bool test<unsigned long long>(RunContext *Ctx, TestCondition Cond,
#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 @@ template bool test<__llvm_libc::cpp::string>(
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,
diff --git a/libc/test/UnitTest/LibcTest.h b/libc/test/UnitTest/LibcTest.h
index 955ee93225145..5eacf4e20e524 100644
--- a/libc/test/UnitTest/LibcTest.h
+++ b/libc/test/UnitTest/LibcTest.h
@@ -40,32 +40,23 @@ namespace testing {
// 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 <typename ValType>
-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 @@ class Test {
// of type promotion.
template <typename ValType,
cpp::enable_if_t<cpp::is_integral_v<ValType>, 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 <typename ValType,
cpp::enable_if_t<cpp::is_enum<ValType>::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 @@ class Test {
template <typename ValType,
cpp::enable_if_t<cpp::is_pointer_v<ValType>, 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 @@ class Test {
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,
+ 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 @@ class Test {
template <typename ValType,
cpp::enable_if_t<cpp::is_same_v<ValType, __llvm_libc::cpp::string>,
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 @@ CString libc_make_test_file_path_func(const char *file_name);
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)) \
More information about the libc-commits
mailing list