diff --git a/libc/test/src/string/memcmp_test.cpp b/libc/test/src/string/memcmp_test.cpp --- a/libc/test/src/string/memcmp_test.cpp +++ b/libc/test/src/string/memcmp_test.cpp @@ -9,7 +9,6 @@ #include "memory_utils/memory_check_utils.h" #include "src/string/memcmp.h" #include "test/UnitTest/Test.h" -#include "test/UnitTest/TestLogger.h" namespace __llvm_libc { @@ -52,10 +51,7 @@ for (size_t size = 0; size < kMaxSize; ++size) { auto span1 = Buffer1.span().subspan(0, size); auto span2 = Buffer2.span().subspan(0, size); - const bool OK = CheckMemcmp(span1, span2, size); - if (!OK) - testing::tlog << "Failed at size=" << size << '\n'; - ASSERT_TRUE(OK); + ASSERT_TRUE(CheckMemcmp(span1, span2, size)); } } diff --git a/libc/test/src/string/memory_utils/memory_check_utils.h b/libc/test/src/string/memory_utils/memory_check_utils.h --- a/libc/test/src/string/memory_utils/memory_check_utils.h +++ b/libc/test/src/string/memory_utils/memory_check_utils.h @@ -12,6 +12,7 @@ #include "src/__support/CPP/span.h" #include "src/__support/macros/sanitizer.h" #include "src/string/memory_utils/utils.h" +#include "test/UnitTest/TestLogger.h" #include // size_t #include // uintxx_t #include // malloc/free @@ -82,8 +83,11 @@ Randomize(dst); FnImpl(dst, src, size); for (size_t i = 0; i < size; ++i) - if (dst[i] != src[i]) + if (dst[i] != src[i]) { + testing::tlog << "memcpy error\n" + << "size=" << size << ", mismatch_index=" << i << '\n'; return false; + } return true; } @@ -92,26 +96,36 @@ bool CheckMemset(cpp::span dst, uint8_t value, size_t size) { Randomize(dst); FnImpl(dst, value, size); - for (char c : dst) - if (c != (char)value) + for (size_t i = 0; i < size; ++i) + if (dst[i] != (char)value) { + testing::tlog << "memset error\n" + << "size=" << size << ", mismatch_index=" << i << '\n'; return false; + } return true; } // Checks that FnImpl implements the bcmp semantic. template bool CheckBcmp(cpp::span span1, cpp::span span2, size_t size) { + const auto error = [=](const char *msg, int cmp, + size_t mismatch_index) -> bool { + testing::tlog << "bcmp error : " << msg << '\n' + << "cmp=" << cmp << ", size=" << size + << ", mismatch_index=" << mismatch_index << '\n'; + return false; + }; ReferenceCopy(span2, span1); // Compare equal if (int cmp = FnImpl(span1, span2, size); cmp != 0) - return false; + return error("buffer are equal expected cmp == 0", cmp, 0); // Compare not equal if any byte differs for (size_t i = 0; i < size; ++i) { ++span2[i]; if (int cmp = FnImpl(span1, span2, size); cmp == 0) - return false; + return error("buffer are equal expected cmp != 0", cmp, i); if (int cmp = FnImpl(span2, span1, size); cmp == 0) - return false; + return error("buffer are equal expected cmp != 0", cmp, i); --span2[i]; } return true; @@ -120,24 +134,31 @@ // Checks that FnImpl implements the memcmp semantic. template bool CheckMemcmp(cpp::span span1, cpp::span span2, size_t size) { + const auto error = [=](const char *msg, int cmp, + size_t mismatch_index) -> bool { + testing::tlog << "memcmp error : " << msg << '\n' + << "cmp=" << cmp << ", size=" << size + << ", mismatch_index=" << mismatch_index << '\n'; + return false; + }; ReferenceCopy(span2, span1); // Compare equal if (int cmp = FnImpl(span1, span2, size); cmp != 0) - return false; + return error("buffer are equal expected cmp == 0", cmp, 0); // Compare not equal if any byte differs for (size_t i = 0; i < size; ++i) { ++span2[i]; int ground_truth = __builtin_memcmp(span1.data(), span2.data(), size); if (ground_truth > 0) { if (int cmp = FnImpl(span1, span2, size); cmp <= 0) - return false; + return error("buffer are different, expected cmp > 0", cmp, i); if (int cmp = FnImpl(span2, span1, size); cmp >= 0) - return false; + return error("buffer are different, expected cmp < 0", cmp, i); } else { if (int cmp = FnImpl(span1, span2, size); cmp >= 0) - return false; + return error("buffer are different, expected cmp < 0", cmp, i); if (int cmp = FnImpl(span2, span1, size); cmp <= 0) - return false; + return error("buffer are different, expected cmp > 0", cmp, i); } --span2[i]; }