Expand Up
@@ -8,12 +8,12 @@
#include " LibcTest.h"
#include " src/__support/CPP/string.h"
#include " src/__support/CPP/string_view.h"
#include " src/__support/UInt128.h"
#include " test/UnitTest/TestLogger.h"
#include " utils/testutils/ExecuteFunction.h"
#include < cassert>
#include < iostream>
#include < string>
namespace __llvm_libc {
namespace testing {
Expand Down
Expand Up
@@ -41,56 +41,58 @@ namespace internal {
// be able to unittest UInt<128> on platforms where UInt128 resolves to
// UInt128.
template <typename T>
cpp::enable_if_t <cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, cpp ::string>
cpp::enable_if_t <cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, std ::string>
describeValueUInt (T Value) {
static_assert (sizeof (T) % 8 == 0 , " Unsupported size of UInt" );
cpp ::string S (sizeof (T) * 2 , ' 0' );
std ::string S (sizeof (T) * 2 , ' 0' );
constexpr char HEXADECIMALS[16 ] = {' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' ,
' 8' , ' 9' , ' a' , ' b' , ' c' , ' d' , ' e' , ' f' };
const size_t Size = S. size ();
for (size_t I = 0 ; I < Size ; I += 2 , Value >>= 8 ) {
for (auto I = S. rbegin (), End = S. rend () ; I != End; ++I , Value >>= 8 ) {
unsigned char Mod = static_cast <unsigned char >(Value) & 0xFF ;
S[ Size - I] = HEXADECIMALS[Mod & 0x0F ];
S[ Size - (I + 1 )] = HEXADECIMALS[Mod & 0x0F ];
*(I++) = HEXADECIMALS[Mod & 0x0F ];
*I = HEXADECIMALS[Mod >> 4 ];
}
return " 0x" + S;
}
// When the value is of integral type, just display it as normal.
template <typename ValType>
cpp::enable_if_t <cpp::is_integral_v<ValType>, cpp ::string>
cpp::enable_if_t <cpp::is_integral_v<ValType>, std ::string>
describeValue (ValType Value) {
if constexpr (sizeof (ValType) <= sizeof (uint64_t )) {
return cpp ::to_string (Value);
return std ::to_string (Value);
} else {
return describeValueUInt (Value);
}
}
cpp::string describeValue (cpp::string Value) { return Value; }
cpp::string_view describeValue (cpp::string_view Value) { return Value; }
std::string describeValue (std::string Value) { return std::string (Value); }
std::string describeValue (cpp::string_view Value) {
return std::string (Value.data (), Value.size ());
}
template <typename ValType>
void explainDifference (ValType LHS, ValType RHS, const char *LHSStr,
const char *RHSStr, const char *File, unsigned long Line,
cpp ::string OpString) {
std ::string OpString) {
size_t OffsetLength = OpString.size () > 2 ? OpString.size () - 2 : 0 ;
cpp ::string Offset (OffsetLength, ' ' );
std ::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 ' ;
std::cout << 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,
const char *LHSStr, const char *RHSStr, const char *File,
unsigned long Line) {
auto ExplainDifference = [=](cpp ::string OpString) {
auto ExplainDifference = [=](std ::string OpString) {
explainDifference (LHS, RHS, LHSStr, RHSStr, File, Line, OpString);
};
Expand Down
Expand Up
@@ -139,7 +141,7 @@ bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS,
return false ;
default :
Ctx->markFail ();
tlog << " Unexpected test condition.\n " ;
std::cout << " Unexpected test condition.\n " ;
return false ;
}
}
Expand All
@@ -165,14 +167,14 @@ int Test::runTests(const char *TestFilter) {
int FailCount = 0 ;
for (Test *T = Start; T != nullptr ; T = T->Next ) {
const char *TestName = T->getName ();
cpp ::string StrTestName (TestName);
std ::string StrTestName (TestName);
constexpr auto GREEN = " \033 [32m" ;
constexpr auto RED = " \033 [31m" ;
constexpr auto RESET = " \033 [0m" ;
if ((TestFilter != nullptr ) && (StrTestName != TestFilter)) {
continue ;
}
tlog << GREEN << " [ RUN ] " << RESET << TestName << ' \n ' ;
std::cout << GREEN << " [ RUN ] " << RESET << TestName << ' \n ' ;
RunContext Ctx;
T->SetUp ();
T->setContext (&Ctx);
Expand All
@@ -181,24 +183,24 @@ int Test::runTests(const char *TestFilter) {
auto Result = Ctx.status ();
switch (Result) {
case RunContext::Result_Fail:
tlog << RED << " [ FAILED ] " << RESET << TestName << ' \n ' ;
std::cout << RED << " [ FAILED ] " << RESET << TestName << ' \n ' ;
++FailCount;
break ;
case RunContext::Result_Pass:
tlog << GREEN << " [ OK ] " << RESET << TestName << ' \n ' ;
std::cout << GREEN << " [ OK ] " << RESET << TestName << ' \n ' ;
break ;
}
++TestCount;
}
if (TestCount > 0 ) {
tlog << " Ran " << TestCount << " tests. "
<< " PASS: " << TestCount - FailCount << ' ' << " FAIL: " << FailCount
<< ' \n ' ;
std::cout << " Ran " << TestCount << " tests. "
<< " PASS: " << TestCount - FailCount << ' '
<< " FAIL: " << FailCount << ' \n ' ;
} else {
tlog << " No tests run.\n " ;
std::cout << " No tests run.\n " ;
if (TestFilter) {
tlog << " No matching test for " << TestFilter << ' \n ' ;
std::cout << " No matching test for " << TestFilter << ' \n ' ;
}
}
Expand Down
Expand Up
@@ -300,15 +302,15 @@ template bool test<__llvm_libc::cpp::string_view>(
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,
return internal::test (Ctx, Cond_EQ, LHS ? std ::string (LHS) : std ::string (),
RHS ? std ::string (RHS) : std ::string (), 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,
return internal::test (Ctx, Cond_NE, LHS ? std ::string (LHS) : std ::string (),
RHS ? std ::string (RHS) : std ::string (), LHSStr, RHSStr,
File, Line);
}
Expand All
@@ -319,8 +321,8 @@ bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
Ctx->markFail ();
if (!Matcher.is_silent ()) {
tlog << File << " :" << Line << " : FAILURE\n "
<< " Failed to match " << LHSStr << " against " << RHSStr << " .\n " ;
std::cout << File << " :" << Line << " : FAILURE\n "
<< " Failed to match " << LHSStr << " against " << RHSStr << " .\n " ;
testutils::StreamWrapper OutsWrapper = testutils::outs ();
Matcher.explainError (OutsWrapper);
}
Expand All
@@ -336,22 +338,22 @@ bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
if (const char *error = Result.get_error ()) {
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n " << error << ' \n ' ;
std::cout << File << " :" << Line << " : FAILURE\n " << error << ' \n ' ;
return false ;
}
if (Result.timed_out ()) {
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n "
<< " Process timed out after " << 500 << " milliseconds.\n " ;
std::cout << File << " :" << Line << " : FAILURE\n "
<< " Process timed out after " << 500 << " milliseconds.\n " ;
return false ;
}
if (Result.exited_normally ()) {
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n "
<< " Expected " << LHSStr
<< " to be killed by a signal\n But it exited normally!\n " ;
std::cout << File << " :" << Line << " : FAILURE\n "
<< " Expected " << LHSStr
<< " to be killed by a signal\n But it exited normally!\n " ;
return false ;
}
Expand All
@@ -362,12 +364,12 @@ bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
using testutils::signal_as_string;
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n "
<< " Expected: " << LHSStr << ' \n '
<< " To be killed by signal: " << Signal << ' \n '
<< " Which is: " << signal_as_string (Signal) << ' \n '
<< " But it was killed by: " << KilledBy << ' \n '
<< " Which is: " << signal_as_string (KilledBy) << ' \n ' ;
std::cout << File << " :" << Line << " : FAILURE\n "
<< " Expected: " << LHSStr << ' \n '
<< " To be killed by signal: " << Signal << ' \n '
<< " Which is: " << signal_as_string (Signal) << ' \n '
<< " But it was killed by: " << KilledBy << ' \n '
<< " Which is: " << signal_as_string (KilledBy) << ' \n ' ;
return false ;
}
Expand All
@@ -378,23 +380,23 @@ bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
if (const char *error = Result.get_error ()) {
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n " << error << ' \n ' ;
std::cout << File << " :" << Line << " : FAILURE\n " << error << ' \n ' ;
return false ;
}
if (Result.timed_out ()) {
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n "
<< " Process timed out after " << 500 << " milliseconds.\n " ;
std::cout << File << " :" << Line << " : FAILURE\n "
<< " Process timed out after " << 500 << " milliseconds.\n " ;
return false ;
}
if (!Result.exited_normally ()) {
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n "
<< " Expected " << LHSStr << ' \n '
<< " to exit with exit code " << ExitCode << ' \n '
<< " But it exited abnormally!\n " ;
std::cout << File << " :" << Line << " : FAILURE\n "
<< " Expected " << LHSStr << ' \n '
<< " to exit with exit code " << ExitCode << ' \n '
<< " But it exited abnormally!\n " ;
return false ;
}
Expand All
@@ -403,11 +405,11 @@ bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
return true ;
Ctx->markFail ();
tlog << File << " :" << Line << " : FAILURE\n "
<< " Expected exit code of: " << LHSStr << ' \n '
<< " Which is: " << ActualExit << ' \n '
<< " To be equal to: " << RHSStr << ' \n '
<< " Which is: " << ExitCode << ' \n ' ;
std::cout << File << " :" << Line << " : FAILURE\n "
<< " Expected exit code of: " << LHSStr << ' \n '
<< " Which is: " << ActualExit << ' \n '
<< " To be equal to: " << RHSStr << ' \n '
<< " Which is: " << ExitCode << ' \n ' ;
return false ;
}
Expand Down