diff --git a/libc/test/UnitTest/MemoryMatcher.h b/libc/test/UnitTest/MemoryMatcher.h index e08b452cf9a84..48d630a39e887 100644 --- a/libc/test/UnitTest/MemoryMatcher.h +++ b/libc/test/UnitTest/MemoryMatcher.h @@ -19,6 +19,32 @@ namespace testing { 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 @@ class MemoryMatcher : public __llvm_libc::testing::Matcher { 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 index c1c0dae4fcd6e..affd23b1bd8b1 100644 --- 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 @@ TEST(LlvmLibcBcopyTest, DstFollowSrc) { 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 index ab5d8ad210727..dad834c091fcc 100644 --- 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 @@ TEST(LlvmLibcMemmoveTest, DstFollowSrc) { 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 index 325cba30c61bb..930161a95d929 100644 --- 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 inline char GetRandomChar() { 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 index f939b2376603f..aa55230d5286b 100644 --- a/libc/test/src/string/strsignal_test.cpp +++ b/libc/test/src/string/strsignal_test.cpp @@ -66,10 +66,11 @@ TEST(LlvmLibcStrSignalTest, KnownSignals) { }; 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]); } }