1 //===-- MemoryMatcher.h -----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_UTILS_UNITTEST_MEMORY_MATCHER_H
10 #define LLVM_LIBC_UTILS_UNITTEST_MEMORY_MATCHER_H
12 #include "src/__support/CPP/span.h"
14 #include "test/UnitTest/Test.h"
16 namespace __llvm_libc
{
19 using MemoryView
= __llvm_libc::cpp::span
<const char>;
21 } // namespace testing
22 } // namespace __llvm_libc
24 #ifdef LIBC_COPT_TEST_USE_FUCHSIA
26 #define EXPECT_MEM_EQ(expected, actual) \
28 __llvm_libc::testing::MemoryView e = (expected); \
29 __llvm_libc::testing::MemoryView a = (actual); \
30 ASSERT_EQ(e.size(), a.size()); \
31 EXPECT_BYTES_EQ(e.data(), a.data(), e.size()); \
34 #define ASSERT_MEM_EQ(expected, actual) \
36 __llvm_libc::testing::MemoryView e = (expected); \
37 __llvm_libc::testing::MemoryView a = (actual); \
38 ASSERT_EQ(e.size(), a.size()); \
39 ASSERT_BYTES_EQ(e.data(), a.data(), e.size()); \
44 namespace __llvm_libc::testing
{
46 class MemoryMatcher
: public Matcher
<MemoryView
> {
49 bool mismatch_size
= false;
50 size_t mismatch_index
= -1;
53 MemoryMatcher(MemoryView expectedValue
) : expected(expectedValue
) {}
55 bool match(MemoryView actualValue
);
57 void explainError() override
;
60 } // namespace __llvm_libc::testing
62 #define EXPECT_MEM_EQ(expected, actual) \
63 EXPECT_THAT(actual, __llvm_libc::testing::MemoryMatcher(expected))
64 #define ASSERT_MEM_EQ(expected, actual) \
65 ASSERT_THAT(actual, __llvm_libc::testing::MemoryMatcher(expected))
69 #endif // LLVM_LIBC_UTILS_UNITTEST_MEMORY_MATCHER_H