[libc-commits] [libc] e095c3e - [libc][test] Better reporting for MemoryMatcher
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Thu Sep 29 05:14:22 PDT 2022
Author: Guillaume Chatelet
Date: 2022-09-29T12:14:05Z
New Revision: e095c3ed7c26df8c6e95b616549c30099964a3ae
URL: https://github.com/llvm/llvm-project/commit/e095c3ed7c26df8c6e95b616549c30099964a3ae
DIFF: https://github.com/llvm/llvm-project/commit/e095c3ed7c26df8c6e95b616549c30099964a3ae.diff
LOG: [libc][test] Better reporting for MemoryMatcher
Added:
Modified:
libc/utils/UnitTest/MemoryMatcher.cpp
libc/utils/UnitTest/MemoryMatcher.h
Removed:
################################################################################
diff --git a/libc/utils/UnitTest/MemoryMatcher.cpp b/libc/utils/UnitTest/MemoryMatcher.cpp
index 5eab75808715d..f4fc677b4f9d3 100644
--- a/libc/utils/UnitTest/MemoryMatcher.cpp
+++ b/libc/utils/UnitTest/MemoryMatcher.cpp
@@ -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
diff --git a/libc/utils/UnitTest/MemoryMatcher.h b/libc/utils/UnitTest/MemoryMatcher.h
index 773a0a0a201be..0bd44071d7446 100644
--- a/libc/utils/UnitTest/MemoryMatcher.h
+++ b/libc/utils/UnitTest/MemoryMatcher.h
@@ -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 @@ class MemoryMatcher : public __llvm_libc::testing::Matcher<MemoryView> {
#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
More information about the libc-commits
mailing list