[libc][test] Better reporting for MemoryMatcher

This commit is contained in:
Guillaume Chatelet 2022-09-29 12:13:26 +00:00
parent 412141663c
commit e095c3ed7c
2 changed files with 34 additions and 10 deletions

View File

@ -13,18 +13,23 @@ namespace memory {
namespace testing {
template <typename T>
bool equals(const cpp::span<T> &Span1, const cpp::span<T> &Span2) {
if (Span1.size() != Span2.size())
bool equals(const cpp::span<T> &Span1, const cpp::span<T> &Span2,
bool &mismatch_size, size_t &mismatch_index) {
if (Span1.size() != Span2.size()) {
mismatch_size = true;
return false;
}
for (size_t Index = 0; Index < Span1.size(); ++Index)
if (Span1[Index] != Span2[Index])
if (Span1[Index] != Span2[Index]) {
mismatch_index = Index;
return false;
}
return true;
}
bool MemoryMatcher::match(MemoryView actualValue) {
actual = actualValue;
return equals(expected, actual);
return equals(expected, actual, mismatch_size, mismatch_index);
}
void display(testutils::StreamWrapper &Stream, char C) {
@ -43,12 +48,27 @@ void display(testutils::StreamWrapper &Stream, MemoryView View) {
}
void MemoryMatcher::explainError(testutils::StreamWrapper &Stream) {
Stream << "expected :";
display(Stream, expected);
Stream << '\n';
Stream << "actual :";
display(Stream, actual);
Stream << '\n';
if (mismatch_size) {
Stream << "Size mismatch :";
Stream << "expected : ";
Stream << expected.size();
Stream << '\n';
Stream << "actual : ";
Stream << actual.size();
Stream << '\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';
}
}
} // namespace testing

View File

@ -22,6 +22,8 @@ using MemoryView = __llvm_libc::cpp::span<const char>;
class MemoryMatcher : public __llvm_libc::testing::Matcher<MemoryView> {
MemoryView expected;
MemoryView actual;
bool mismatch_size = false;
size_t mismatch_index = -1;
public:
MemoryMatcher(MemoryView expectedValue) : expected(expectedValue) {}
@ -37,5 +39,7 @@ public:
#define EXPECT_MEM_EQ(expected, actual) \
EXPECT_THAT(actual, __llvm_libc::memory::testing::MemoryMatcher(expected))
#define ASSERT_MEM_EQ(expected, actual) \
ASSERT_THAT(actual, __llvm_libc::memory::testing::MemoryMatcher(expected))
#endif // LLVM_LIBC_UTILS_UNITTEST_MEMORY_MATCHER_H