diff --git a/libc/test/UnitTest/MemoryMatcher.h b/libc/test/UnitTest/MemoryMatcher.h --- a/libc/test/UnitTest/MemoryMatcher.h +++ b/libc/test/UnitTest/MemoryMatcher.h @@ -19,6 +19,32 @@ using MemoryView = __llvm_libc::cpp::span; +} // 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); \ + 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); \ + ASSERT_EQ(e.size(), a.size()); \ + ASSERT_BYTES_EQ(e.data(), a.data(), e.size()); \ + } while (0) + +#else + +namespace __llvm_libc::memory::testing { + class MemoryMatcher : public __llvm_libc::testing::Matcher { MemoryView expected; MemoryView actual; @@ -33,13 +59,13 @@ void explainError(testutils::StreamWrapper &stream) override; }; -} // namespace testing -} // namespace memory -} // namespace __llvm_libc +} // namespace __llvm_libc::memory::testing #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 + #endif // LLVM_LIBC_UTILS_UNITTEST_MEMORY_MATCHER_H diff --git a/libc/test/src/string/bcopy_test.cpp b/libc/test/src/string/bcopy_test.cpp --- a/libc/test/src/string/bcopy_test.cpp +++ b/libc/test/src/string/bcopy_test.cpp @@ -6,8 +6,10 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/span.h" #include "src/string/bcopy.h" + +#include "memory_utils/memory_check_utils.h" +#include "src/__support/CPP/span.h" #include "test/UnitTest/MemoryMatcher.h" #include "test/UnitTest/Test.h" @@ -70,24 +72,10 @@ static constexpr int kMaxSize = 512; -char GetRandomChar() { - static constexpr const uint64_t A = 1103515245; - static constexpr const uint64_t C = 12345; - static constexpr const uint64_t M = 1ULL << 31; - static uint64_t Seed = 123456789; - Seed = (A * Seed + C) % M; - return Seed; -} - -void Randomize(span Buffer) { - for (auto ¤t : Buffer) - current = GetRandomChar(); -} - TEST(LlvmLibcBcopyTest, SizeSweep) { using LargeBuffer = array; LargeBuffer GroundTruth; - Randomize(GroundTruth); + __llvm_libc::Randomize(GroundTruth); for (int Size = 0; Size < kMaxSize; ++Size) { for (int Offset = -Size; Offset < Size; ++Offset) { LargeBuffer Buffer = GroundTruth; diff --git a/libc/test/src/string/memmove_test.cpp b/libc/test/src/string/memmove_test.cpp --- a/libc/test/src/string/memmove_test.cpp +++ b/libc/test/src/string/memmove_test.cpp @@ -6,8 +6,10 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/span.h" #include "src/string/memmove.h" + +#include "memory_utils/memory_check_utils.h" +#include "src/__support/CPP/span.h" #include "test/UnitTest/MemoryMatcher.h" #include "test/UnitTest/Test.h" @@ -76,24 +78,10 @@ static constexpr int kMaxSize = 512; -char GetRandomChar() { - static constexpr const uint64_t A = 1103515245; - static constexpr const uint64_t C = 12345; - static constexpr const uint64_t M = 1ULL << 31; - static uint64_t Seed = 123456789; - Seed = (A * Seed + C) % M; - return Seed; -} - -void Randomize(span Buffer) { - for (auto ¤t : Buffer) - current = GetRandomChar(); -} - TEST(LlvmLibcMemmoveTest, SizeSweep) { using LargeBuffer = array; LargeBuffer GroundTruth; - Randomize(GroundTruth); + __llvm_libc::Randomize(GroundTruth); for (int Size = 0; Size < kMaxSize; ++Size) { for (int Offset = -Size; Offset < Size; ++Offset) { LargeBuffer Buffer = GroundTruth; 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 @@ -65,7 +65,7 @@ static constexpr const uint64_t m = 1ULL << 31; static uint64_t seed = 123456789; seed = (a * seed + c) % m; - return seed; + return static_cast(seed); } // Randomize the content of the buffer. diff --git a/libc/test/src/string/strsignal_test.cpp b/libc/test/src/string/strsignal_test.cpp --- a/libc/test/src/string/strsignal_test.cpp +++ b/libc/test/src/string/strsignal_test.cpp @@ -66,10 +66,11 @@ }; for (size_t i = 0; i < (sizeof(message_array) / sizeof(char *)); ++i) { - EXPECT_STREQ(__llvm_libc::strsignal(i), message_array[i]); + ASSERT_EQ(static_cast(static_cast(i)), i); + EXPECT_STREQ(__llvm_libc::strsignal(static_cast(i)), message_array[i]); } - for (size_t i = 0; i < SIGRTMAX - SIGRTMIN; ++i) { + for (int i = 0; i < SIGRTMAX - SIGRTMIN; ++i) { EXPECT_STREQ(__llvm_libc::strsignal(i + SIGRTMIN), rt_message_array[i]); } }