Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / UnitTest / MemoryMatcher.cpp
blobd9d89504dbeba7960d5e8d4bbae0fa7e7b502539
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 //===----------------------------------------------------------------------===//
9 #include "MemoryMatcher.h"
11 #include "test/UnitTest/Test.h"
13 using LIBC_NAMESPACE::testing::tlog;
15 namespace LIBC_NAMESPACE {
16 namespace testing {
18 template <typename T>
19 bool equals(const cpp::span<T> &Span1, const cpp::span<T> &Span2,
20 bool &mismatch_size, size_t &mismatch_index) {
21 if (Span1.size() != Span2.size()) {
22 mismatch_size = true;
23 return false;
25 for (size_t Index = 0; Index < Span1.size(); ++Index)
26 if (Span1[Index] != Span2[Index]) {
27 mismatch_index = Index;
28 return false;
30 return true;
33 bool MemoryMatcher::match(MemoryView actualValue) {
34 actual = actualValue;
35 return equals(expected, actual, mismatch_size, mismatch_index);
38 static void display(char C) {
39 const auto print = [](unsigned char I) {
40 tlog << static_cast<char>(I < 10 ? '0' + I : 'A' + I - 10);
42 print(static_cast<unsigned char>(C) / 16);
43 print(static_cast<unsigned char>(C) & 15);
46 static void display(MemoryView View) {
47 for (auto C : View) {
48 tlog << ' ';
49 display(C);
53 void MemoryMatcher::explainError() {
54 if (mismatch_size) {
55 tlog << "Size mismatch :";
56 tlog << "expected : ";
57 tlog << expected.size();
58 tlog << '\n';
59 tlog << "actual : ";
60 tlog << actual.size();
61 tlog << '\n';
62 } else {
63 tlog << "Mismatch at position : ";
64 tlog << mismatch_index;
65 tlog << " / ";
66 tlog << expected.size();
67 tlog << "\n";
68 tlog << "expected :";
69 display(expected);
70 tlog << '\n';
71 tlog << "actual :";
72 display(actual);
73 tlog << '\n';
77 } // namespace testing
78 } // namespace LIBC_NAMESPACE