Skip to content

Commit dcf296b

Browse files
author
Siva Chandra Reddy
committed
[libc][NFC] Remove the StreamWrapper class and use the new test logger.
Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D148452
1 parent 21b6842 commit dcf296b

24 files changed

+337
-481
lines changed

libc/test/ErrnoSetterMatcher.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
2929
ErrnoSetterMatcher(T ExpectedReturn, int ExpectedErrno)
3030
: ExpectedReturn(ExpectedReturn), ExpectedErrno(ExpectedErrno) {}
3131

32-
void explainError(testutils::StreamWrapper &OS) override {
32+
void explainError() override {
3333
if (ActualReturn != ExpectedReturn)
34-
OS << "Expected return to be " << ExpectedReturn << " but got "
35-
<< ActualReturn << ".\nExpecte errno to be " << strerror(ExpectedErrno)
36-
<< " but got " << strerror(ActualErrno) << ".\n";
34+
__llvm_libc::testing::tlog
35+
<< "Expected return to be " << ExpectedReturn << " but got "
36+
<< ActualReturn << ".\nExpecte errno to be "
37+
<< strerror(ExpectedErrno) << " but got " << strerror(ActualErrno)
38+
<< ".\n";
3739
else
38-
OS << "Correct value " << ExpectedReturn
39-
<< " was returned\nBut errno was unexpectely set to "
40-
<< strerror(ActualErrno) << ".\n";
40+
__llvm_libc::testing::tlog
41+
<< "Correct value " << ExpectedReturn
42+
<< " was returned\nBut errno was unexpectely set to "
43+
<< strerror(ActualErrno) << ".\n";
4144
}
4245

4346
bool match(T Got) {

libc/test/UnitTest/CMakeLists.txt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
add_library(
2-
TestLogger
3-
TestLogger.cpp
4-
TestLogger.h
5-
)
6-
target_include_directories(TestLogger PUBLIC ${LIBC_SOURCE_DIR})
7-
add_dependencies(TestLogger
8-
libc.src.__support.CPP.string
9-
libc.src.__support.CPP.string_view
10-
libc.src.__support.OSUtil.osutil
11-
)
12-
131
add_library(
142
LibcUnitTest
153
Test.h
164
LibcTest.cpp
175
LibcTest.h
6+
TestLogger.cpp
7+
TestLogger.h
188
)
199
target_include_directories(LibcUnitTest PUBLIC ${LIBC_SOURCE_DIR})
2010
add_dependencies(
2111
LibcUnitTest
2212
libc.src.__support.CPP.string
2313
libc.src.__support.CPP.string_view
2414
libc.src.__support.CPP.type_traits
25-
libc.src.__support.uint128 TestLogger)
26-
target_link_libraries(LibcUnitTest PUBLIC libc_test_utils TestLogger)
15+
libc.src.__support.OSUtil.osutil
16+
libc.src.__support.uint128)
17+
target_link_libraries(LibcUnitTest PUBLIC libc_test_utils)
2718

2819
add_library(
2920
LibcUnitTestMain
@@ -39,14 +30,14 @@ add_header_library(
3930
HDRS
4031
StringUtils.h
4132
DEPENDS
42-
libc.src.__support.CPP.type_traits
33+
libc.src.__support.CPP.string
34+
libc.src.__support.CPP.type_traits
4335
)
4436

4537
add_library(
4638
LibcFPTestHelpers
4739
FPExceptMatcher.cpp
4840
FPExceptMatcher.h
49-
FPMatcher.cpp
5041
FPMatcher.h
5142
)
5243
target_include_directories(LibcFPTestHelpers PUBLIC ${LIBC_SOURCE_DIR})
@@ -57,6 +48,7 @@ add_dependencies(
5748
libc.test.UnitTest.string_utils
5849
libc.src.__support.FPUtil.fp_bits
5950
libc.src.__support.FPUtil.fenv_impl
51+
libc.test.UnitTest.string_utils
6052
)
6153

6254
add_library(

libc/test/UnitTest/FPExceptMatcher.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ class FPExceptMatcher : public __llvm_libc::testing::Matcher<bool> {
4444

4545
bool match(bool unused) { return exceptionRaised; }
4646

47-
void explainError(testutils::StreamWrapper &stream) override {
48-
stream << "A floating point exception should have been raised but it "
49-
<< "wasn't\n";
47+
void explainError() override {
48+
__llvm_libc::testing::tlog
49+
<< "A floating point exception should have been raised but it "
50+
<< "wasn't\n";
5051
}
5152
};
5253

libc/test/UnitTest/FPMatcher.cpp

Lines changed: 0 additions & 72 deletions
This file was deleted.

libc/test/UnitTest/FPMatcher.h

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "src/__support/FPUtil/FEnvImpl.h"
1313
#include "src/__support/FPUtil/FPBits.h"
14+
#include "test/UnitTest/StringUtils.h"
1415
#include "test/UnitTest/Test.h"
1516
#include "utils/testutils/RoundingModeUtils.h"
1617

@@ -20,9 +21,42 @@ namespace __llvm_libc {
2021
namespace fputil {
2122
namespace testing {
2223

23-
template <typename ValType, typename StreamType>
24+
template <typename ValType>
2425
cpp::enable_if_t<cpp::is_floating_point_v<ValType>, void>
25-
describeValue(const char *label, ValType value, StreamType &stream);
26+
describeValue(const char *label, ValType value) {
27+
__llvm_libc::testing::tlog << label;
28+
29+
FPBits<ValType> bits(value);
30+
if (bits.is_nan()) {
31+
__llvm_libc::testing::tlog << "(NaN)";
32+
} else if (bits.is_inf()) {
33+
if (bits.get_sign())
34+
__llvm_libc::testing::tlog << "(-Infinity)";
35+
else
36+
__llvm_libc::testing::tlog << "(+Infinity)";
37+
} else {
38+
constexpr int exponentWidthInHex =
39+
(fputil::ExponentWidth<ValType>::VALUE - 1) / 4 + 1;
40+
constexpr int mantissaWidthInHex =
41+
(fputil::MantissaWidth<ValType>::VALUE - 1) / 4 + 1;
42+
constexpr int bitsWidthInHex =
43+
sizeof(typename fputil::FPBits<ValType>::UIntType) * 2;
44+
45+
__llvm_libc::testing::tlog
46+
<< "0x"
47+
<< int_to_hex<typename fputil::FPBits<ValType>::UIntType>(
48+
bits.uintval(), bitsWidthInHex)
49+
<< ", (S | E | M) = (" << (bits.get_sign() ? '1' : '0') << " | 0x"
50+
<< int_to_hex<uint16_t>(bits.get_unbiased_exponent(),
51+
exponentWidthInHex)
52+
<< " | 0x"
53+
<< int_to_hex<typename fputil::FPBits<ValType>::UIntType>(
54+
bits.get_mantissa(), mantissaWidthInHex)
55+
<< ")";
56+
}
57+
58+
__llvm_libc::testing::tlog << '\n';
59+
}
2660

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

55-
void explainError(testutils::StreamWrapper &stream) override {
56-
describeValue("Expected floating point value: ", expected, stream);
57-
describeValue(" Actual floating point value: ", actual, stream);
89+
void explainError() override {
90+
describeValue("Expected floating point value: ", expected);
91+
describeValue(" Actual floating point value: ", actual);
5892
}
5993
};
6094

libc/test/UnitTest/LibcTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
321321
if (!Matcher.is_silent()) {
322322
tlog << File << ":" << Line << ": FAILURE\n"
323323
<< "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
324-
testutils::StreamWrapper OutsWrapper = testutils::outs();
325-
Matcher.explainError(OutsWrapper);
324+
Matcher.explainError();
326325
}
327326
return false;
328327
}

libc/test/UnitTest/LibcTest.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include "src/__support/CPP/string.h"
1818
#include "src/__support/CPP/string_view.h"
1919
#include "src/__support/CPP/type_traits.h"
20+
#include "test/UnitTest/TestLogger.h"
2021
#include "utils/testutils/ExecuteFunction.h"
21-
#include "utils/testutils/StreamWrapper.h"
2222

2323
namespace __llvm_libc {
2424
namespace testing {
@@ -51,9 +51,7 @@ bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS,
5151

5252
struct MatcherBase {
5353
virtual ~MatcherBase() {}
54-
virtual void explainError(testutils::StreamWrapper &OS) {
55-
OS << "unknown error\n";
56-
}
54+
virtual void explainError() { tlog << "unknown error\n"; }
5755
// Override and return true to skip `explainError` step.
5856
virtual bool is_silent() const { return false; }
5957
};

libc/test/UnitTest/MemoryMatcher.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#include "MemoryMatcher.h"
1010

11+
#include "test/UnitTest/Test.h"
12+
13+
using __llvm_libc::testing::tlog;
14+
1115
namespace __llvm_libc {
1216
namespace memory {
1317
namespace testing {
@@ -32,42 +36,42 @@ bool MemoryMatcher::match(MemoryView actualValue) {
3236
return equals(expected, actual, mismatch_size, mismatch_index);
3337
}
3438

35-
void display(testutils::StreamWrapper &Stream, char C) {
36-
const auto print = [&Stream](unsigned char I) {
37-
Stream << static_cast<char>(I < 10 ? '0' + I : 'A' + I - 10);
39+
static void display(char C) {
40+
const auto print = [](unsigned char I) {
41+
tlog << static_cast<char>(I < 10 ? '0' + I : 'A' + I - 10);
3842
};
3943
print(static_cast<unsigned char>(C) / 16);
4044
print(static_cast<unsigned char>(C) & 15);
4145
}
4246

43-
void display(testutils::StreamWrapper &Stream, MemoryView View) {
47+
static void display(MemoryView View) {
4448
for (auto C : View) {
45-
Stream << ' ';
46-
display(Stream, C);
49+
tlog << ' ';
50+
display(C);
4751
}
4852
}
4953

50-
void MemoryMatcher::explainError(testutils::StreamWrapper &Stream) {
54+
void MemoryMatcher::explainError() {
5155
if (mismatch_size) {
52-
Stream << "Size mismatch :";
53-
Stream << "expected : ";
54-
Stream << expected.size();
55-
Stream << '\n';
56-
Stream << "actual : ";
57-
Stream << actual.size();
58-
Stream << '\n';
56+
tlog << "Size mismatch :";
57+
tlog << "expected : ";
58+
tlog << expected.size();
59+
tlog << '\n';
60+
tlog << "actual : ";
61+
tlog << actual.size();
62+
tlog << '\n';
5963
} else {
60-
Stream << "Mismatch at position : ";
61-
Stream << mismatch_index;
62-
Stream << " / ";
63-
Stream << expected.size();
64-
Stream << "\n";
65-
Stream << "expected :";
66-
display(Stream, expected);
67-
Stream << '\n';
68-
Stream << "actual :";
69-
display(Stream, actual);
70-
Stream << '\n';
64+
tlog << "Mismatch at position : ";
65+
tlog << mismatch_index;
66+
tlog << " / ";
67+
tlog << expected.size();
68+
tlog << "\n";
69+
tlog << "expected :";
70+
display(expected);
71+
tlog << '\n';
72+
tlog << "actual :";
73+
display(actual);
74+
tlog << '\n';
7175
}
7276
}
7377

libc/test/UnitTest/MemoryMatcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class MemoryMatcher : public __llvm_libc::testing::Matcher<MemoryView> {
5656

5757
bool match(MemoryView actualValue);
5858

59-
void explainError(testutils::StreamWrapper &stream) override;
59+
void explainError() override;
6060
};
6161

6262
} // namespace __llvm_libc::memory::testing

0 commit comments

Comments
 (0)