[libc-commits] [libc] 4e298c3 - [libc] Make string tests compatible with the Fuchsia build

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Mon Mar 20 10:23:07 PDT 2023


Author: Roland McGrath
Date: 2023-03-20T10:22:52-07:00
New Revision: 4e298c32d558cbe7059bb90e4306c22c6702016e

URL: https://github.com/llvm/llvm-project/commit/4e298c32d558cbe7059bb90e4306c22c6702016e
DIFF: https://github.com/llvm/llvm-project/commit/4e298c32d558cbe7059bb90e4306c22c6702016e.diff

LOG: [libc] Make string tests compatible with the Fuchsia build

Some test code was doing loose conversions caught by compiler
warnings in  the Fuchsia build.  This included duplicated code
in a few tests that was reconsolidated with the existing header
file copy of the same functions.

The MemoryMatcher abstraction presumes gtest-style matcher support,
which is not available in Fuchsia's zxtest library.  It's avoided
in favor of simpler memory-comparing assertions.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D146343

Added: 
    

Modified: 
    libc/test/UnitTest/MemoryMatcher.h
    libc/test/src/string/bcopy_test.cpp
    libc/test/src/string/memmove_test.cpp
    libc/test/src/string/memory_utils/memory_check_utils.h
    libc/test/src/string/strsignal_test.cpp

Removed: 
    


################################################################################
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<const char>;
 
+} // 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> {
   MemoryView expected;
   MemoryView actual;
@@ -33,13 +59,13 @@ class MemoryMatcher : public __llvm_libc::testing::Matcher<MemoryView> {
   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<char> Buffer) {
-  for (auto &current : Buffer)
-    current = GetRandomChar();
-}
-
 TEST(LlvmLibcBcopyTest, SizeSweep) {
   using LargeBuffer = array<char, 3 * kMaxSize>;
   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<char> Buffer) {
-  for (auto &current : Buffer)
-    current = GetRandomChar();
-}
-
 TEST(LlvmLibcMemmoveTest, SizeSweep) {
   using LargeBuffer = array<char, 3 * kMaxSize>;
   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<char>(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<size_t>(static_cast<int>(i)), i);
+    EXPECT_STREQ(__llvm_libc::strsignal(static_cast<int>(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]);
   }
 }


        


More information about the libc-commits mailing list