Skip to content

Commit

Permalink
[libc] Make string tests compatible with the Fuchsia build
Browse files Browse the repository at this point in the history
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
  • Loading branch information
frobtech committed Mar 20, 2023
1 parent dfb40d3 commit 4e298c3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
32 changes: 29 additions & 3 deletions libc/test/UnitTest/MemoryMatcher.h
Expand Up @@ -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;
Expand All @@ -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
20 changes: 4 additions & 16 deletions libc/test/src/string/bcopy_test.cpp
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down
20 changes: 4 additions & 16 deletions libc/test/src/string/memmove_test.cpp
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/string/memory_utils/memory_check_utils.h
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/string/strsignal_test.cpp
Expand Up @@ -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]);
}
}
Expand Down

0 comments on commit 4e298c3

Please sign in to comment.