From 22bc0fde556ea94a9682b03dcb18c130dcaf071f Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Mon, 22 Aug 2022 09:56:38 +0000 Subject: [PATCH] [NFC][libc] Switch mem* tests from ArrayRef to span --- libc/test/src/string/bzero_test.cpp | 8 ++++---- libc/test/src/string/memccpy_test.cpp | 8 ++++---- libc/test/src/string/memcpy_test.cpp | 10 +++++----- libc/test/src/string/memmove_test.cpp | 7 +++---- libc/test/src/string/memory_utils/backend_test.cpp | 4 ++-- libc/test/src/string/memory_utils/elements_test.cpp | 4 ++-- .../src/string/memory_utils/memory_access_test.cpp | 1 - libc/test/src/string/memset_test.cpp | 8 ++++---- libc/test/src/string/strncpy_test.cpp | 8 ++++---- libc/utils/UnitTest/CMakeLists.txt | 2 +- libc/utils/UnitTest/MemoryMatcher.cpp | 12 +++++++++++- libc/utils/UnitTest/MemoryMatcher.h | 4 ++-- 12 files changed, 42 insertions(+), 34 deletions(-) diff --git a/libc/test/src/string/bzero_test.cpp b/libc/test/src/string/bzero_test.cpp index beecf48920c0..5ae3a497ab2c 100644 --- a/libc/test/src/string/bzero_test.cpp +++ b/libc/test/src/string/bzero_test.cpp @@ -6,18 +6,18 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" +#include "src/__support/CPP/span.h" #include "src/string/bzero.h" #include "utils/UnitTest/Test.h" using __llvm_libc::cpp::array; -using __llvm_libc::cpp::ArrayRef; +using __llvm_libc::cpp::span; using Data = array; -static const ArrayRef k_deadcode("DEADC0DE", 8); +static const span k_deadcode("DEADC0DE", 8); // Returns a Data object filled with a repetition of `filler`. -Data get_data(ArrayRef filler) { +Data get_data(span filler) { Data out; for (size_t i = 0; i < out.size(); ++i) out[i] = filler[i % filler.size()]; diff --git a/libc/test/src/string/memccpy_test.cpp b/libc/test/src/string/memccpy_test.cpp index ac11e33a3d63..c5a32600dc39 100644 --- a/libc/test/src/string/memccpy_test.cpp +++ b/libc/test/src/string/memccpy_test.cpp @@ -6,17 +6,17 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" +#include "src/__support/CPP/span.h" #include "src/string/memccpy.h" #include "utils/UnitTest/Test.h" #include // For size_t. class LlvmLibcMemccpyTest : public __llvm_libc::testing::Test { public: - void check_memccpy(__llvm_libc::cpp::MutableArrayRef dst, - const __llvm_libc::cpp::ArrayRef src, int end, + void check_memccpy(__llvm_libc::cpp::span dst, + const __llvm_libc::cpp::span src, int end, size_t count, - const __llvm_libc::cpp::ArrayRef expected, + const __llvm_libc::cpp::span expected, size_t expectedCopied, bool shouldReturnNull = false) { // Making sure we don't overflow buffer. ASSERT_GE(dst.size(), count); diff --git a/libc/test/src/string/memcpy_test.cpp b/libc/test/src/string/memcpy_test.cpp index 875927b15cc7..5ccbb4dc4559 100644 --- a/libc/test/src/string/memcpy_test.cpp +++ b/libc/test/src/string/memcpy_test.cpp @@ -6,19 +6,19 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" +#include "src/__support/CPP/span.h" #include "src/string/memcpy.h" #include "utils/UnitTest/Test.h" using __llvm_libc::cpp::array; -using __llvm_libc::cpp::ArrayRef; +using __llvm_libc::cpp::span; using Data = array; -static const ArrayRef k_numbers("0123456789", 10); -static const ArrayRef k_deadcode("DEADC0DE", 8); +static const span k_numbers("0123456789", 10); +static const span k_deadcode("DEADC0DE", 8); // Returns a Data object filled with a repetition of `filler`. -Data get_data(ArrayRef filler) { +Data get_data(span filler) { Data out; for (size_t i = 0; i < out.size(); ++i) out[i] = filler[i % filler.size()]; diff --git a/libc/test/src/string/memmove_test.cpp b/libc/test/src/string/memmove_test.cpp index 0b753cd6118c..26b4d9e9d675 100644 --- a/libc/test/src/string/memmove_test.cpp +++ b/libc/test/src/string/memmove_test.cpp @@ -6,14 +6,13 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" +#include "src/__support/CPP/span.h" #include "src/string/memmove.h" #include "utils/UnitTest/MemoryMatcher.h" #include "utils/UnitTest/Test.h" using __llvm_libc::cpp::array; -using __llvm_libc::cpp::ArrayRef; -using __llvm_libc::cpp::MutableArrayRef; +using __llvm_libc::cpp::span; TEST(LlvmLibcMemmoveTest, MoveZeroByte) { char Buffer[] = {'a', 'b', 'y', 'z'}; @@ -86,7 +85,7 @@ char GetRandomChar() { return Seed; } -void Randomize(MutableArrayRef Buffer) { +void Randomize(span Buffer) { for (auto ¤t : Buffer) current = GetRandomChar(); } diff --git a/libc/test/src/string/memory_utils/backend_test.cpp b/libc/test/src/string/memory_utils/backend_test.cpp index 1ae33a663c1c..72fb7c4cf53b 100644 --- a/libc/test/src/string/memory_utils/backend_test.cpp +++ b/libc/test/src/string/memory_utils/backend_test.cpp @@ -6,9 +6,9 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/array.h" #include "src/__support/CPP/bit.h" +#include "src/__support/CPP/span.h" #include "src/__support/architectures.h" #include "src/string/memory_utils/backends.h" #include "utils/UnitTest/Test.h" @@ -30,7 +30,7 @@ static char GetRandomChar() { return seed; } -static void Randomize(cpp::MutableArrayRef buffer) { +static void Randomize(cpp::span buffer) { for (auto ¤t : buffer) current = GetRandomChar(); } diff --git a/libc/test/src/string/memory_utils/elements_test.cpp b/libc/test/src/string/memory_utils/elements_test.cpp index ef43475e5db1..218700137c11 100644 --- a/libc/test/src/string/memory_utils/elements_test.cpp +++ b/libc/test/src/string/memory_utils/elements_test.cpp @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/array.h" +#include "src/__support/CPP/span.h" #include "src/string/memory_utils/elements.h" #include "utils/UnitTest/Test.h" @@ -51,7 +51,7 @@ char GetRandomChar() { return seed; } -void Randomize(cpp::MutableArrayRef buffer) { +void Randomize(cpp::span buffer) { for (auto ¤t : buffer) current = GetRandomChar(); } diff --git a/libc/test/src/string/memory_utils/memory_access_test.cpp b/libc/test/src/string/memory_utils/memory_access_test.cpp index fe257bd2cbc5..b81700f0eb25 100644 --- a/libc/test/src/string/memory_utils/memory_access_test.cpp +++ b/libc/test/src/string/memory_utils/memory_access_test.cpp @@ -8,7 +8,6 @@ #define LLVM_LIBC_UNITTEST_OBSERVE 1 -#include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/array.h" #include "src/string/memory_utils/elements.h" #include "utils/UnitTest/Test.h" diff --git a/libc/test/src/string/memset_test.cpp b/libc/test/src/string/memset_test.cpp index b09a56a555c0..24c7c44a1649 100644 --- a/libc/test/src/string/memset_test.cpp +++ b/libc/test/src/string/memset_test.cpp @@ -6,18 +6,18 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" +#include "src/__support/CPP/span.h" #include "src/string/memset.h" #include "utils/UnitTest/Test.h" using __llvm_libc::cpp::array; -using __llvm_libc::cpp::ArrayRef; +using __llvm_libc::cpp::span; using Data = array; -static const ArrayRef k_deadcode("DEADC0DE", 8); +static const span k_deadcode("DEADC0DE", 8); // Returns a Data object filled with a repetition of `filler`. -Data get_data(ArrayRef filler) { +Data get_data(span filler) { Data out; for (size_t i = 0; i < out.size(); ++i) out[i] = filler[i % filler.size()]; diff --git a/libc/test/src/string/strncpy_test.cpp b/libc/test/src/string/strncpy_test.cpp index 7bb70e9865a3..6f08eecff8cb 100644 --- a/libc/test/src/string/strncpy_test.cpp +++ b/libc/test/src/string/strncpy_test.cpp @@ -6,16 +6,16 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/ArrayRef.h" +#include "src/__support/CPP/span.h" #include "src/string/strncpy.h" #include "utils/UnitTest/Test.h" #include // For size_t. class LlvmLibcStrncpyTest : public __llvm_libc::testing::Test { public: - void check_strncpy(__llvm_libc::cpp::MutableArrayRef dst, - const __llvm_libc::cpp::ArrayRef src, size_t n, - const __llvm_libc::cpp::ArrayRef expected) { + void check_strncpy(__llvm_libc::cpp::span dst, + const __llvm_libc::cpp::span src, size_t n, + const __llvm_libc::cpp::span expected) { // Making sure we don't overflow buffer. ASSERT_GE(dst.size(), n); // Making sure strncpy returns dst. diff --git a/libc/utils/UnitTest/CMakeLists.txt b/libc/utils/UnitTest/CMakeLists.txt index 34c49b9ec02a..2f0fbd28db5e 100644 --- a/libc/utils/UnitTest/CMakeLists.txt +++ b/libc/utils/UnitTest/CMakeLists.txt @@ -51,7 +51,7 @@ target_link_libraries(LibcMemoryHelpers LibcUnitTest) add_dependencies( LibcMemoryHelpers LibcUnitTest - libc.src.__support.CPP.array_ref + libc.src.__support.CPP.span ) add_library( diff --git a/libc/utils/UnitTest/MemoryMatcher.cpp b/libc/utils/UnitTest/MemoryMatcher.cpp index 195516a7d7b2..5eab75808715 100644 --- a/libc/utils/UnitTest/MemoryMatcher.cpp +++ b/libc/utils/UnitTest/MemoryMatcher.cpp @@ -12,9 +12,19 @@ namespace __llvm_libc { namespace memory { namespace testing { +template +bool equals(const cpp::span &Span1, const cpp::span &Span2) { + if (Span1.size() != Span2.size()) + return false; + for (size_t Index = 0; Index < Span1.size(); ++Index) + if (Span1[Index] != Span2[Index]) + return false; + return true; +} + bool MemoryMatcher::match(MemoryView actualValue) { actual = actualValue; - return expected.equals(actual); + return equals(expected, actual); } void display(testutils::StreamWrapper &Stream, char C) { diff --git a/libc/utils/UnitTest/MemoryMatcher.h b/libc/utils/UnitTest/MemoryMatcher.h index ea39ecb1c27a..773a0a0a201b 100644 --- a/libc/utils/UnitTest/MemoryMatcher.h +++ b/libc/utils/UnitTest/MemoryMatcher.h @@ -9,7 +9,7 @@ #ifndef LLVM_LIBC_UTILS_UNITTEST_MEMORY_MATCHER_H #define LLVM_LIBC_UTILS_UNITTEST_MEMORY_MATCHER_H -#include "src/__support/CPP/ArrayRef.h" +#include "src/__support/CPP/span.h" #include "utils/UnitTest/Test.h" @@ -17,7 +17,7 @@ namespace __llvm_libc { namespace memory { namespace testing { -using MemoryView = __llvm_libc::cpp::ArrayRef; +using MemoryView = __llvm_libc::cpp::span; class MemoryMatcher : public __llvm_libc::testing::Matcher { MemoryView expected;