Skip to content

Commit

Permalink
[libc][NFC] Clean up matchers namespace
Browse files Browse the repository at this point in the history
This is a follow up to https://reviews.llvm.org/D152503

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D152533
  • Loading branch information
gchatelet committed Jun 10, 2023
1 parent 37458f6 commit 0fd0b74
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 86 deletions.
20 changes: 8 additions & 12 deletions libc/test/UnitTest/ErrnoSetterMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,20 @@ template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
void explainError() override {
if (ActualReturn != ExpectedReturn) {
if constexpr (cpp::is_floating_point_v<T>) {
__llvm_libc::testing::tlog
<< "Expected return value to be: "
<< str(__llvm_libc::fputil::FPBits<T>(ExpectedReturn)) << '\n';
__llvm_libc::testing::tlog
<< " But got: "
<< str(__llvm_libc::fputil::FPBits<T>(ActualReturn)) << '\n';
tlog << "Expected return value to be: "
<< str(fputil::FPBits<T>(ExpectedReturn)) << '\n';
tlog << " But got: "
<< str(fputil::FPBits<T>(ActualReturn)) << '\n';
} else {
__llvm_libc::testing::tlog << "Expected return value to be "
<< ExpectedReturn << " but got "
<< ActualReturn << ".\n";
tlog << "Expected return value to be " << ExpectedReturn << " but got "
<< ActualReturn << ".\n";
}
}

if constexpr (!ignore_errno()) {
if (ActualErrno != ExpectedErrno) {
__llvm_libc::testing::tlog
<< "Expected errno to be \"" << get_error_string(ExpectedErrno)
<< "\" but got \"" << get_error_string(ActualErrno) << "\".\n";
tlog << "Expected errno to be \"" << get_error_string(ExpectedErrno)
<< "\" but got \"" << get_error_string(ActualErrno) << "\".\n";
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions libc/test/UnitTest/FPExceptMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <signal.h>

namespace __llvm_libc {
namespace fputil {
namespace testing {

#if defined(_WIN32)
Expand Down Expand Up @@ -48,5 +47,4 @@ FPExceptMatcher::FPExceptMatcher(FunctionCaller *func) {
}

} // namespace testing
} // namespace fputil
} // namespace __llvm_libc
14 changes: 5 additions & 9 deletions libc/test/UnitTest/FPExceptMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
#include "test/UnitTest/Test.h"

namespace __llvm_libc {
namespace fputil {
namespace testing {

// TODO: Make the matcher match specific exceptions instead of just identifying
// that an exception was raised.
class FPExceptMatcher : public __llvm_libc::testing::Matcher<bool> {
class FPExceptMatcher : public Matcher<bool> {
bool exceptionRaised;

public:
Expand All @@ -45,22 +44,19 @@ class FPExceptMatcher : public __llvm_libc::testing::Matcher<bool> {
bool match(bool unused) { return exceptionRaised; }

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

} // namespace testing
} // namespace fputil
} // namespace __llvm_libc

#define ASSERT_RAISES_FP_EXCEPT(func) \
ASSERT_THAT( \
true, \
__llvm_libc::fputil::testing::FPExceptMatcher( \
__llvm_libc::fputil::testing::FPExceptMatcher::getFunctionCaller( \
func)))
__llvm_libc::testing::FPExceptMatcher( \
__llvm_libc::testing::FPExceptMatcher::getFunctionCaller(func)))
#else
#define ASSERT_RAISES_FP_EXCEPT(func) ASSERT_DEATH(func, WITH_SIGNAL(SIGFPE))
#endif // LIBC_COPT_TEST_USE_FUCHSIA
Expand Down
2 changes: 0 additions & 2 deletions libc/test/UnitTest/MemoryMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using __llvm_libc::testing::tlog;

namespace __llvm_libc {
namespace memory {
namespace testing {

template <typename T>
Expand Down Expand Up @@ -76,5 +75,4 @@ void MemoryMatcher::explainError() {
}

} // namespace testing
} // namespace memory
} // namespace __llvm_libc
20 changes: 9 additions & 11 deletions libc/test/UnitTest/MemoryMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,36 @@
#include "test/UnitTest/Test.h"

namespace __llvm_libc {
namespace memory {
namespace testing {

using MemoryView = __llvm_libc::cpp::span<const char>;

} // namespace testing
} // namespace memory
} // namespace __llvm_libc

#ifdef LIBC_COPT_TEST_USE_FUCHSIA

#define EXPECT_MEM_EQ(expected, actual) \
do { \
__llvm_libc::memory::testing::MemoryView e = (expected); \
__llvm_libc::memory::testing::MemoryView a = (actual); \
__llvm_libc::testing::MemoryView e = (expected); \
__llvm_libc::testing::MemoryView a = (actual); \
ASSERT_EQ(e.size(), a.size()); \
EXPECT_BYTES_EQ(e.data(), a.data(), e.size()); \
} while (0)

#define ASSERT_MEM_EQ(expected, actual) \
do { \
__llvm_libc::memory::testing::MemoryView e = (expected); \
__llvm_libc::memory::testing::MemoryView a = (actual); \
__llvm_libc::testing::MemoryView e = (expected); \
__llvm_libc::testing::MemoryView a = (actual); \
ASSERT_EQ(e.size(), a.size()); \
ASSERT_BYTES_EQ(e.data(), a.data(), e.size()); \
} while (0)

#else

namespace __llvm_libc::memory::testing {
namespace __llvm_libc::testing {

class MemoryMatcher : public __llvm_libc::testing::Matcher<MemoryView> {
class MemoryMatcher : public Matcher<MemoryView> {
MemoryView expected;
MemoryView actual;
bool mismatch_size = false;
Expand All @@ -59,12 +57,12 @@ class MemoryMatcher : public __llvm_libc::testing::Matcher<MemoryView> {
void explainError() override;
};

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

#define EXPECT_MEM_EQ(expected, actual) \
EXPECT_THAT(actual, __llvm_libc::memory::testing::MemoryMatcher(expected))
EXPECT_THAT(actual, __llvm_libc::testing::MemoryMatcher(expected))
#define ASSERT_MEM_EQ(expected, actual) \
ASSERT_THAT(actual, __llvm_libc::memory::testing::MemoryMatcher(expected))
ASSERT_THAT(actual, __llvm_libc::testing::MemoryMatcher(expected))

#endif

Expand Down
8 changes: 4 additions & 4 deletions libc/test/UnitTest/PrintfMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

#include <stdint.h>

using __llvm_libc::testing::tlog;

namespace __llvm_libc {
namespace printf_core {
namespace testing {

using printf_core::FormatFlags;
using printf_core::FormatSection;
using printf_core::LengthModifier;

bool FormatSectionMatcher::match(FormatSection actualValue) {
actual = actualValue;
return expected == actual;
Expand Down Expand Up @@ -93,5 +94,4 @@ void FormatSectionMatcher::explainError() {
}

} // namespace testing
} // namespace printf_core
} // namespace __llvm_libc
20 changes: 8 additions & 12 deletions libc/test/UnitTest/PrintfMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,28 @@
#include <errno.h>

namespace __llvm_libc {
namespace printf_core {
namespace testing {

class FormatSectionMatcher
: public __llvm_libc::testing::Matcher<FormatSection> {
FormatSection expected;
FormatSection actual;
class FormatSectionMatcher : public Matcher<printf_core::FormatSection> {
printf_core::FormatSection expected;
printf_core::FormatSection actual;

public:
FormatSectionMatcher(FormatSection expectedValue) : expected(expectedValue) {}
FormatSectionMatcher(printf_core::FormatSection expectedValue)
: expected(expectedValue) {}

bool match(FormatSection actualValue);
bool match(printf_core::FormatSection actualValue);

void explainError() override;
};

} // namespace testing
} // namespace printf_core
} // namespace __llvm_libc

#define EXPECT_PFORMAT_EQ(expected, actual) \
EXPECT_THAT(actual, __llvm_libc::printf_core::testing::FormatSectionMatcher( \
expected))
EXPECT_THAT(actual, __llvm_libc::testing::FormatSectionMatcher(expected))

#define ASSERT_PFORMAT_EQ(expected, actual) \
ASSERT_THAT(actual, __llvm_libc::printf_core::testing::FormatSectionMatcher( \
expected))
ASSERT_THAT(actual, __llvm_libc::testing::FormatSectionMatcher(expected))

#endif // LLVM_LIBC_UTILS_UNITTEST_PRINTF_MATCHER_H
8 changes: 4 additions & 4 deletions libc/test/UnitTest/ScanfMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

#include <stdint.h>

using __llvm_libc::testing::tlog;

namespace __llvm_libc {
namespace scanf_core {
namespace testing {

using scanf_core::FormatFlags;
using scanf_core::FormatSection;
using scanf_core::LengthModifier;

bool FormatSectionMatcher::match(FormatSection actualValue) {
actual = actualValue;
return expected == actual;
Expand Down Expand Up @@ -98,5 +99,4 @@ void FormatSectionMatcher::explainError() {
}

} // namespace testing
} // namespace scanf_core
} // namespace __llvm_libc
20 changes: 8 additions & 12 deletions libc/test/UnitTest/ScanfMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,28 @@
#include <errno.h>

namespace __llvm_libc {
namespace scanf_core {
namespace testing {

class FormatSectionMatcher
: public __llvm_libc::testing::Matcher<FormatSection> {
FormatSection expected;
FormatSection actual;
class FormatSectionMatcher : public Matcher<scanf_core::FormatSection> {
scanf_core::FormatSection expected;
scanf_core::FormatSection actual;

public:
FormatSectionMatcher(FormatSection expectedValue) : expected(expectedValue) {}
FormatSectionMatcher(scanf_core::FormatSection expectedValue)
: expected(expectedValue) {}

bool match(FormatSection actualValue);
bool match(scanf_core::FormatSection actualValue);

void explainError() override;
};

} // namespace testing
} // namespace scanf_core
} // namespace __llvm_libc

#define EXPECT_SFORMAT_EQ(expected, actual) \
EXPECT_THAT(actual, __llvm_libc::scanf_core::testing::FormatSectionMatcher( \
expected))
EXPECT_THAT(actual, __llvm_libc::testing::FormatSectionMatcher(expected))

#define ASSERT_SFORMAT_EQ(expected, actual) \
ASSERT_THAT(actual, __llvm_libc::scanf_core::testing::FormatSectionMatcher( \
expected))
ASSERT_THAT(actual, __llvm_libc::testing::FormatSectionMatcher(expected))

#endif // LLVM_LIBC_UTILS_UNITTEST_SCANF_MATCHER_H
2 changes: 1 addition & 1 deletion libc/test/src/__support/File/file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <stdlib.h>

using ModeFlags = __llvm_libc::File::ModeFlags;
using MemoryView = __llvm_libc::memory::testing::MemoryView;
using MemoryView = __llvm_libc::testing::MemoryView;
using __llvm_libc::ErrorOr;
using __llvm_libc::File;
using __llvm_libc::FileIOResult;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdio/fopencookie_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <stdio.h>
#include <stdlib.h>

using MemoryView = __llvm_libc::memory::testing::MemoryView;
using MemoryView = __llvm_libc::testing::MemoryView;

struct StringStream {
char *buf;
Expand Down
29 changes: 13 additions & 16 deletions libc/test/src/time/TmMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
#include "test/UnitTest/Test.h"

namespace __llvm_libc {
namespace tmmatcher {
namespace testing {

class StructTmMatcher : public __llvm_libc::testing::Matcher<::tm> {
class StructTmMatcher : public Matcher<::tm> {
::tm expected;
::tm actual;

Expand All @@ -38,17 +37,17 @@ class StructTmMatcher : public __llvm_libc::testing::Matcher<::tm> {
}

void describeValue(const char *label, ::tm value) {
__llvm_libc::testing::tlog << label;
__llvm_libc::testing::tlog << " sec: " << value.tm_sec;
__llvm_libc::testing::tlog << " min: " << value.tm_min;
__llvm_libc::testing::tlog << " hour: " << value.tm_hour;
__llvm_libc::testing::tlog << " mday: " << value.tm_mday;
__llvm_libc::testing::tlog << " mon: " << value.tm_mon;
__llvm_libc::testing::tlog << " year: " << value.tm_year;
__llvm_libc::testing::tlog << " wday: " << value.tm_wday;
__llvm_libc::testing::tlog << " yday: " << value.tm_yday;
__llvm_libc::testing::tlog << " isdst: " << value.tm_isdst;
__llvm_libc::testing::tlog << '\n';
tlog << label;
tlog << " sec: " << value.tm_sec;
tlog << " min: " << value.tm_min;
tlog << " hour: " << value.tm_hour;
tlog << " mday: " << value.tm_mday;
tlog << " mon: " << value.tm_mon;
tlog << " year: " << value.tm_year;
tlog << " wday: " << value.tm_wday;
tlog << " yday: " << value.tm_yday;
tlog << " isdst: " << value.tm_isdst;
tlog << '\n';
}

void explainError() override {
Expand All @@ -58,11 +57,9 @@ class StructTmMatcher : public __llvm_libc::testing::Matcher<::tm> {
};

} // namespace testing
} // namespace tmmatcher
} // namespace __llvm_libc

#define EXPECT_TM_EQ(expected, actual) \
EXPECT_THAT((actual), \
__llvm_libc::tmmatcher::testing::StructTmMatcher((expected)))
EXPECT_THAT((actual), __llvm_libc::testing::StructTmMatcher((expected)))

#endif // LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H

0 comments on commit 0fd0b74

Please sign in to comment.