1 | //===-- MemoryMatcher.h -----------------------------------------*- C++ -*-===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_LIBC_TEST_UNITTEST_MEMORYMATCHER_H |
10 | #define LLVM_LIBC_TEST_UNITTEST_MEMORYMATCHER_H |
11 | |
12 | #include "src/__support/CPP/span.h" |
13 | |
14 | #include "test/UnitTest/Test.h" |
15 | |
16 | namespace LIBC_NAMESPACE { |
17 | namespace testing { |
18 | |
19 | using MemoryView = LIBC_NAMESPACE::cpp::span<const char>; |
20 | |
21 | } // namespace testing |
22 | } // namespace LIBC_NAMESPACE |
23 | |
24 | #if !LIBC_TEST_HAS_MATCHERS() |
25 | |
26 | #define EXPECT_MEM_EQ(expected, actual) \ |
27 | do { \ |
28 | LIBC_NAMESPACE::testing::MemoryView e = (expected); \ |
29 | LIBC_NAMESPACE::testing::MemoryView a = (actual); \ |
30 | ASSERT_EQ(e.size(), a.size()); \ |
31 | EXPECT_BYTES_EQ(e.data(), a.data(), e.size()); \ |
32 | } while (0) |
33 | |
34 | #define ASSERT_MEM_EQ(expected, actual) \ |
35 | do { \ |
36 | LIBC_NAMESPACE::testing::MemoryView e = (expected); \ |
37 | LIBC_NAMESPACE::testing::MemoryView a = (actual); \ |
38 | ASSERT_EQ(e.size(), a.size()); \ |
39 | ASSERT_BYTES_EQ(e.data(), a.data(), e.size()); \ |
40 | } while (0) |
41 | |
42 | #else // LIBC_TEST_HAS_MATCHERS() |
43 | |
44 | namespace LIBC_NAMESPACE::testing { |
45 | |
46 | class MemoryMatcher : public Matcher<MemoryView> { |
47 | MemoryView expected; |
48 | MemoryView actual; |
49 | bool mismatch_size = false; |
50 | size_t mismatch_index = -1; |
51 | |
52 | public: |
53 | MemoryMatcher(MemoryView expectedValue) : expected(expectedValue) {} |
54 | |
55 | bool match(MemoryView actualValue); |
56 | |
57 | void explainError() override; |
58 | }; |
59 | |
60 | } // namespace LIBC_NAMESPACE::testing |
61 | |
62 | #define EXPECT_MEM_EQ(expected, actual) \ |
63 | EXPECT_THAT(actual, LIBC_NAMESPACE::testing::MemoryMatcher(expected)) |
64 | #define ASSERT_MEM_EQ(expected, actual) \ |
65 | ASSERT_THAT(actual, LIBC_NAMESPACE::testing::MemoryMatcher(expected)) |
66 | |
67 | #endif // !LIBC_TEST_HAS_MATCHERS() |
68 | |
69 | #endif // LLVM_LIBC_TEST_UNITTEST_MEMORYMATCHER_H |
70 | |