Skip to content

Commit

Permalink
[libc][NFC] Remove the StreamWrapper class and use the new test logger.
Browse files Browse the repository at this point in the history
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D148452
  • Loading branch information
Siva Chandra Reddy committed Apr 17, 2023
1 parent 21b6842 commit dcf296b
Show file tree
Hide file tree
Showing 24 changed files with 337 additions and 481 deletions.
17 changes: 10 additions & 7 deletions libc/test/ErrnoSetterMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
ErrnoSetterMatcher(T ExpectedReturn, int ExpectedErrno)
: ExpectedReturn(ExpectedReturn), ExpectedErrno(ExpectedErrno) {}

void explainError(testutils::StreamWrapper &OS) override {
void explainError() override {
if (ActualReturn != ExpectedReturn)
OS << "Expected return to be " << ExpectedReturn << " but got "
<< ActualReturn << ".\nExpecte errno to be " << strerror(ExpectedErrno)
<< " but got " << strerror(ActualErrno) << ".\n";
__llvm_libc::testing::tlog
<< "Expected return to be " << ExpectedReturn << " but got "
<< ActualReturn << ".\nExpecte errno to be "
<< strerror(ExpectedErrno) << " but got " << strerror(ActualErrno)
<< ".\n";
else
OS << "Correct value " << ExpectedReturn
<< " was returned\nBut errno was unexpectely set to "
<< strerror(ActualErrno) << ".\n";
__llvm_libc::testing::tlog
<< "Correct value " << ExpectedReturn
<< " was returned\nBut errno was unexpectely set to "
<< strerror(ActualErrno) << ".\n";
}

bool match(T Got) {
Expand Down
24 changes: 8 additions & 16 deletions libc/test/UnitTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
add_library(
TestLogger
TestLogger.cpp
TestLogger.h
)
target_include_directories(TestLogger PUBLIC ${LIBC_SOURCE_DIR})
add_dependencies(TestLogger
libc.src.__support.CPP.string
libc.src.__support.CPP.string_view
libc.src.__support.OSUtil.osutil
)

add_library(
LibcUnitTest
Test.h
LibcTest.cpp
LibcTest.h
TestLogger.cpp
TestLogger.h
)
target_include_directories(LibcUnitTest PUBLIC ${LIBC_SOURCE_DIR})
add_dependencies(
LibcUnitTest
libc.src.__support.CPP.string
libc.src.__support.CPP.string_view
libc.src.__support.CPP.type_traits
libc.src.__support.uint128 TestLogger)
target_link_libraries(LibcUnitTest PUBLIC libc_test_utils TestLogger)
libc.src.__support.OSUtil.osutil
libc.src.__support.uint128)
target_link_libraries(LibcUnitTest PUBLIC libc_test_utils)

add_library(
LibcUnitTestMain
Expand All @@ -39,14 +30,14 @@ add_header_library(
HDRS
StringUtils.h
DEPENDS
libc.src.__support.CPP.type_traits
libc.src.__support.CPP.string
libc.src.__support.CPP.type_traits
)

add_library(
LibcFPTestHelpers
FPExceptMatcher.cpp
FPExceptMatcher.h
FPMatcher.cpp
FPMatcher.h
)
target_include_directories(LibcFPTestHelpers PUBLIC ${LIBC_SOURCE_DIR})
Expand All @@ -57,6 +48,7 @@ add_dependencies(
libc.test.UnitTest.string_utils
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.fenv_impl
libc.test.UnitTest.string_utils
)

add_library(
Expand Down
7 changes: 4 additions & 3 deletions libc/test/UnitTest/FPExceptMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ class FPExceptMatcher : public __llvm_libc::testing::Matcher<bool> {

bool match(bool unused) { return exceptionRaised; }

void explainError(testutils::StreamWrapper &stream) override {
stream << "A floating point exception should have been raised but it "
<< "wasn't\n";
void explainError() override {
__llvm_libc::testing::tlog
<< "A floating point exception should have been raised but it "
<< "wasn't\n";
}
};

Expand Down
72 changes: 0 additions & 72 deletions libc/test/UnitTest/FPMatcher.cpp

This file was deleted.

44 changes: 39 additions & 5 deletions libc/test/UnitTest/FPMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/StringUtils.h"
#include "test/UnitTest/Test.h"
#include "utils/testutils/RoundingModeUtils.h"

Expand All @@ -20,9 +21,42 @@ namespace __llvm_libc {
namespace fputil {
namespace testing {

template <typename ValType, typename StreamType>
template <typename ValType>
cpp::enable_if_t<cpp::is_floating_point_v<ValType>, void>
describeValue(const char *label, ValType value, StreamType &stream);
describeValue(const char *label, ValType value) {
__llvm_libc::testing::tlog << label;

FPBits<ValType> bits(value);
if (bits.is_nan()) {
__llvm_libc::testing::tlog << "(NaN)";
} else if (bits.is_inf()) {
if (bits.get_sign())
__llvm_libc::testing::tlog << "(-Infinity)";
else
__llvm_libc::testing::tlog << "(+Infinity)";
} else {
constexpr int exponentWidthInHex =
(fputil::ExponentWidth<ValType>::VALUE - 1) / 4 + 1;
constexpr int mantissaWidthInHex =
(fputil::MantissaWidth<ValType>::VALUE - 1) / 4 + 1;
constexpr int bitsWidthInHex =
sizeof(typename fputil::FPBits<ValType>::UIntType) * 2;

__llvm_libc::testing::tlog
<< "0x"
<< int_to_hex<typename fputil::FPBits<ValType>::UIntType>(
bits.uintval(), bitsWidthInHex)
<< ", (S | E | M) = (" << (bits.get_sign() ? '1' : '0') << " | 0x"
<< int_to_hex<uint16_t>(bits.get_unbiased_exponent(),
exponentWidthInHex)
<< " | 0x"
<< int_to_hex<typename fputil::FPBits<ValType>::UIntType>(
bits.get_mantissa(), mantissaWidthInHex)
<< ")";
}

__llvm_libc::testing::tlog << '\n';
}

template <typename T, __llvm_libc::testing::TestCondition Condition>
class FPMatcher : public __llvm_libc::testing::Matcher<T> {
Expand Down Expand Up @@ -52,9 +86,9 @@ class FPMatcher : public __llvm_libc::testing::Matcher<T> {
(actualBits.uintval() != expectedBits.uintval());
}

void explainError(testutils::StreamWrapper &stream) override {
describeValue("Expected floating point value: ", expected, stream);
describeValue(" Actual floating point value: ", actual, stream);
void explainError() override {
describeValue("Expected floating point value: ", expected);
describeValue(" Actual floating point value: ", actual);
}
};

Expand Down
3 changes: 1 addition & 2 deletions libc/test/UnitTest/LibcTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
if (!Matcher.is_silent()) {
tlog << File << ":" << Line << ": FAILURE\n"
<< "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
testutils::StreamWrapper OutsWrapper = testutils::outs();
Matcher.explainError(OutsWrapper);
Matcher.explainError();
}
return false;
}
Expand Down
6 changes: 2 additions & 4 deletions libc/test/UnitTest/LibcTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include "src/__support/CPP/string.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/CPP/type_traits.h"
#include "test/UnitTest/TestLogger.h"
#include "utils/testutils/ExecuteFunction.h"
#include "utils/testutils/StreamWrapper.h"

namespace __llvm_libc {
namespace testing {
Expand Down Expand Up @@ -51,9 +51,7 @@ bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS,

struct MatcherBase {
virtual ~MatcherBase() {}
virtual void explainError(testutils::StreamWrapper &OS) {
OS << "unknown error\n";
}
virtual void explainError() { tlog << "unknown error\n"; }
// Override and return true to skip `explainError` step.
virtual bool is_silent() const { return false; }
};
Expand Down
54 changes: 29 additions & 25 deletions libc/test/UnitTest/MemoryMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include "MemoryMatcher.h"

#include "test/UnitTest/Test.h"

using __llvm_libc::testing::tlog;

namespace __llvm_libc {
namespace memory {
namespace testing {
Expand All @@ -32,42 +36,42 @@ bool MemoryMatcher::match(MemoryView actualValue) {
return equals(expected, actual, mismatch_size, mismatch_index);
}

void display(testutils::StreamWrapper &Stream, char C) {
const auto print = [&Stream](unsigned char I) {
Stream << static_cast<char>(I < 10 ? '0' + I : 'A' + I - 10);
static void display(char C) {
const auto print = [](unsigned char I) {
tlog << static_cast<char>(I < 10 ? '0' + I : 'A' + I - 10);
};
print(static_cast<unsigned char>(C) / 16);
print(static_cast<unsigned char>(C) & 15);
}

void display(testutils::StreamWrapper &Stream, MemoryView View) {
static void display(MemoryView View) {
for (auto C : View) {
Stream << ' ';
display(Stream, C);
tlog << ' ';
display(C);
}
}

void MemoryMatcher::explainError(testutils::StreamWrapper &Stream) {
void MemoryMatcher::explainError() {
if (mismatch_size) {
Stream << "Size mismatch :";
Stream << "expected : ";
Stream << expected.size();
Stream << '\n';
Stream << "actual : ";
Stream << actual.size();
Stream << '\n';
tlog << "Size mismatch :";
tlog << "expected : ";
tlog << expected.size();
tlog << '\n';
tlog << "actual : ";
tlog << actual.size();
tlog << '\n';
} else {
Stream << "Mismatch at position : ";
Stream << mismatch_index;
Stream << " / ";
Stream << expected.size();
Stream << "\n";
Stream << "expected :";
display(Stream, expected);
Stream << '\n';
Stream << "actual :";
display(Stream, actual);
Stream << '\n';
tlog << "Mismatch at position : ";
tlog << mismatch_index;
tlog << " / ";
tlog << expected.size();
tlog << "\n";
tlog << "expected :";
display(expected);
tlog << '\n';
tlog << "actual :";
display(actual);
tlog << '\n';
}
}

Expand Down
2 changes: 1 addition & 1 deletion libc/test/UnitTest/MemoryMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MemoryMatcher : public __llvm_libc::testing::Matcher<MemoryView> {

bool match(MemoryView actualValue);

void explainError(testutils::StreamWrapper &stream) override;
void explainError() override;
};

} // namespace __llvm_libc::memory::testing
Expand Down
Loading

0 comments on commit dcf296b

Please sign in to comment.