Skip to content

Commit

Permalink
[libc][NFC] Make test macros callable from helper methods of test cla…
Browse files Browse the repository at this point in the history
…sses.

This is acheived by making the RunContext a state variable of the test
classes.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D90805
  • Loading branch information
Siva Chandra Reddy committed Nov 6, 2020
1 parent 41e74e4 commit 0e3532d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 130 deletions.
140 changes: 66 additions & 74 deletions libc/utils/UnitTest/Test.cpp
Expand Up @@ -70,7 +70,7 @@ void explainDifference(ValType LHS, ValType RHS, const char *LHSStr,
}

template <typename ValType>
bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS,
bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS,
const char *LHSStr, const char *RHSStr, const char *File,
unsigned long Line) {
auto ExplainDifference = [=](llvm::StringRef OpString) {
Expand All @@ -82,46 +82,46 @@ bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS,
if (LHS == RHS)
return true;

Ctx.markFail();
Ctx->markFail();
ExplainDifference("equal to");
return false;
case Cond_NE:
if (LHS != RHS)
return true;

Ctx.markFail();
Ctx->markFail();
ExplainDifference("not equal to");
return false;
case Cond_LT:
if (LHS < RHS)
return true;

Ctx.markFail();
Ctx->markFail();
ExplainDifference("less than");
return false;
case Cond_LE:
if (LHS <= RHS)
return true;

Ctx.markFail();
Ctx->markFail();
ExplainDifference("less than or equal to");
return false;
case Cond_GT:
if (LHS > RHS)
return true;

Ctx.markFail();
Ctx->markFail();
ExplainDifference("greater than");
return false;
case Cond_GE:
if (LHS >= RHS)
return true;

Ctx.markFail();
Ctx->markFail();
ExplainDifference("greater than or equal to");
return false;
default:
Ctx.markFail();
Ctx->markFail();
llvm::outs() << "Unexpected test condition.\n";
return false;
}
Expand Down Expand Up @@ -154,7 +154,8 @@ int Test::runTests() {
llvm::outs() << GREEN << "[ RUN ] " << RESET << TestName << '\n';
RunContext Ctx;
T->SetUp();
T->Run(Ctx);
T->setContext(&Ctx);
T->Run();
T->TearDown();
auto Result = Ctx.status();
switch (Result) {
Expand All @@ -175,91 +176,83 @@ int Test::runTests() {
return FailCount > 0 ? 1 : 0;
}

template bool Test::test<char, 0>(RunContext &Ctx, TestCondition Cond, char LHS,
char RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);
template bool Test::test<char, 0>(TestCondition Cond, char LHS, char RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);

template bool Test::test<short, 0>(RunContext &Ctx, TestCondition Cond,
short LHS, short RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);
template bool Test::test<short, 0>(TestCondition Cond, short LHS, short RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);

template bool Test::test<int, 0>(RunContext &Ctx, TestCondition Cond, int LHS,
int RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);
template bool Test::test<int, 0>(TestCondition Cond, int LHS, int RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);

template bool Test::test<long, 0>(RunContext &Ctx, TestCondition Cond, long LHS,
long RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);
template bool Test::test<long, 0>(TestCondition Cond, long LHS, long RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);

template bool Test::test<long long, 0>(RunContext &Ctx, TestCondition Cond,
long long LHS, long long RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);
template bool Test::test<long long, 0>(TestCondition Cond, long long LHS,
long long RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);

template bool Test::test<unsigned char, 0>(RunContext &Ctx, TestCondition Cond,
template bool Test::test<unsigned char, 0>(TestCondition Cond,
unsigned char LHS, unsigned char RHS,
const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);

template bool
Test::test<unsigned short, 0>(RunContext &Ctx, TestCondition Cond,
unsigned short LHS, unsigned short RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);

template bool Test::test<unsigned int, 0>(RunContext &Ctx, TestCondition Cond,
unsigned int LHS, unsigned int RHS,
const char *LHSStr,
Test::test<unsigned short, 0>(TestCondition Cond, unsigned short LHS,
unsigned short RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);

template bool Test::test<unsigned int, 0>(TestCondition Cond, unsigned int LHS,
unsigned int RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);

template bool Test::test<unsigned long, 0>(RunContext &Ctx, TestCondition Cond,
template bool Test::test<unsigned long, 0>(TestCondition Cond,
unsigned long LHS, unsigned long RHS,
const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);

template bool Test::test<bool, 0>(RunContext &Ctx, TestCondition Cond, bool LHS,
bool RHS, const char *LHSStr,
template bool Test::test<bool, 0>(TestCondition Cond, bool LHS, bool RHS,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);

template bool
Test::test<unsigned long long, 0>(TestCondition Cond, unsigned long long LHS,
unsigned long long RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);

template bool Test::test<unsigned long long, 0>(
RunContext &Ctx, TestCondition Cond, unsigned long long LHS,
unsigned long long RHS, const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line);

template bool Test::test<__uint128_t, 0>(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::test<__uint128_t, 0>(TestCondition Cond, __uint128_t LHS,
__uint128_t RHS, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line);

bool Test::testStrEq(RunContext &Ctx, const char *LHS, const char *RHS,
const char *LHSStr, const char *RHSStr, const char *File,
unsigned long Line) {
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, llvm::StringRef(LHS),
llvm::StringRef(RHS), LHSStr, RHSStr, File, Line);
}

bool Test::testStrNe(RunContext &Ctx, const char *LHS, const char *RHS,
const char *LHSStr, const char *RHSStr, const char *File,
unsigned long 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, llvm::StringRef(LHS),
llvm::StringRef(RHS), LHSStr, RHSStr, File, Line);
}

bool Test::testMatch(RunContext &Ctx, bool MatchResult, MatcherBase &Matcher,
const char *LHSStr, const char *RHSStr, const char *File,
unsigned long Line) {
bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
const char *RHSStr, const char *File, unsigned long Line) {
if (MatchResult)
return true;

Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n"
<< "Failed to match " << LHSStr << " against " << RHSStr
<< ".\n";
Expand All @@ -268,26 +261,26 @@ bool Test::testMatch(RunContext &Ctx, bool MatchResult, MatcherBase &Matcher,
return false;
}

bool Test::testProcessKilled(RunContext &Ctx, testutils::FunctionCaller *Func,
int Signal, const char *LHSStr, const char *RHSStr,
bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line) {
testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500);

if (const char *error = Result.getError()) {
Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n" << error << '\n';
return false;
}

if (Result.timedOut()) {
Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n"
<< "Process timed out after " << 500 << " milliseconds.\n";
return false;
}

if (Result.exitedNormally()) {
Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n"
<< "Expected " << LHSStr
<< " to be killed by a signal\nBut it exited normally!\n";
Expand All @@ -300,7 +293,7 @@ bool Test::testProcessKilled(RunContext &Ctx, testutils::FunctionCaller *Func,
return true;

using testutils::signalAsString;
Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n"
<< " Expected: " << LHSStr << '\n'
<< "To be killed by signal: " << Signal << '\n'
Expand All @@ -311,27 +304,26 @@ bool Test::testProcessKilled(RunContext &Ctx, testutils::FunctionCaller *Func,
return false;
}

bool Test::testProcessExits(RunContext &Ctx, testutils::FunctionCaller *Func,
int ExitCode, const char *LHSStr,
const char *RHSStr, const char *File,
unsigned long Line) {
bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
const char *LHSStr, const char *RHSStr,
const char *File, unsigned long Line) {
testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500);

if (const char *error = Result.getError()) {
Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n" << error << '\n';
return false;
}

if (Result.timedOut()) {
Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n"
<< "Process timed out after " << 500 << " milliseconds.\n";
return false;
}

if (!Result.exitedNormally()) {
Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n"
<< "Expected " << LHSStr << '\n'
<< "to exit with exit code " << ExitCode << '\n'
Expand All @@ -343,7 +335,7 @@ bool Test::testProcessExits(RunContext &Ctx, testutils::FunctionCaller *Func,
if (ActualExit == ExitCode)
return true;

Ctx.markFail();
Ctx->markFail();
llvm::outs() << File << ":" << Line << ": FAILURE\n"
<< "Expected exit code of: " << LHSStr << '\n'
<< " Which is: " << ActualExit << '\n'
Expand Down

0 comments on commit 0e3532d

Please sign in to comment.