1//===-- MemoryMatcher.cpp ---------------------------------------*- 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#include "MemoryMatcher.h"
10
11#include "src/__support/ctype_utils.h"
12#include "src/__support/macros/config.h"
13#include "test/UnitTest/Test.h"
14
15#if LIBC_TEST_HAS_MATCHERS()
16
17using LIBC_NAMESPACE::testing::tlog;
18
19namespace LIBC_NAMESPACE_DECL {
20namespace testing {
21
22template <typename T>
23bool equals(const cpp::span<T> &Span1, const cpp::span<T> &Span2,
24 bool &mismatch_size, size_t &mismatch_index) {
25 if (Span1.size() != Span2.size()) {
26 mismatch_size = true;
27 return false;
28 }
29 for (size_t Index = 0; Index < Span1.size(); ++Index)
30 if (Span1[Index] != Span2[Index]) {
31 mismatch_index = Index;
32 return false;
33 }
34 return true;
35}
36
37bool MemoryMatcher::match(MemoryView actualValue) {
38 actual = actualValue;
39 return equals(expected, actual, mismatch_size, mismatch_index);
40}
41
42static void display(char C) {
43 const auto print = [](unsigned char I) {
44 tlog << static_cast<char>(LIBC_NAMESPACE::internal::toupper(
45 LIBC_NAMESPACE::internal::int_to_b36_char(I)));
46 };
47 print(static_cast<unsigned char>(C) / 16);
48 print(static_cast<unsigned char>(C) & 15);
49}
50
51static void display(MemoryView View) {
52 for (auto C : View) {
53 tlog << ' ';
54 display(C);
55 }
56}
57
58void MemoryMatcher::explainError() {
59 if (mismatch_size) {
60 tlog << "Size mismatch :";
61 tlog << "expected : ";
62 tlog << expected.size();
63 tlog << '\n';
64 tlog << "actual : ";
65 tlog << actual.size();
66 tlog << '\n';
67 } else {
68 tlog << "Mismatch at position : ";
69 tlog << mismatch_index;
70 tlog << " / ";
71 tlog << expected.size();
72 tlog << "\n";
73 tlog << "expected :";
74 display(expected);
75 tlog << '\n';
76 tlog << "actual :";
77 display(actual);
78 tlog << '\n';
79 }
80}
81
82} // namespace testing
83} // namespace LIBC_NAMESPACE_DECL
84
85#endif // LIBC_TEST_HAS_MATCHERS()
86

source code of libc/test/UnitTest/MemoryMatcher.cpp