summaryrefslogtreecommitdiff
path: root/libc/test/UnitTest/MemoryMatcher.cpp
diff options
context:
space:
mode:
authorSiva Chandra Reddy <sivachandra@google.com>2023-02-06 07:07:23 +0000
committerSiva Chandra Reddy <sivachandra@google.com>2023-02-07 19:45:51 +0000
commitaf1315c28f9bab76d3c2e1492a3d41e7c48215f8 (patch)
treec899fe8fa2e230d7a8006d553340a044405b98c9 /libc/test/UnitTest/MemoryMatcher.cpp
parentd27fb5efc5f2086fa157e7d836b61bae4d5f3734 (diff)
[libc][NFC] Move UnitTest and IntegrationTest to the 'test' directory.
This part of the effort to make all test related pieces into the `test` directory. This helps is excluding test related pieces in a straight forward manner if LLVM_INCLUDE_TESTS is OFF. Future patches will also move the MPFR wrapper and testutils into the 'test' directory.
Diffstat (limited to 'libc/test/UnitTest/MemoryMatcher.cpp')
-rw-r--r--libc/test/UnitTest/MemoryMatcher.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/libc/test/UnitTest/MemoryMatcher.cpp b/libc/test/UnitTest/MemoryMatcher.cpp
new file mode 100644
index 000000000000..f4fc677b4f9d
--- /dev/null
+++ b/libc/test/UnitTest/MemoryMatcher.cpp
@@ -0,0 +1,76 @@
+//===-- MemoryMatcher.cpp ---------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "MemoryMatcher.h"
+
+namespace __llvm_libc {
+namespace memory {
+namespace testing {
+
+template <typename T>
+bool equals(const cpp::span<T> &Span1, const cpp::span<T> &Span2,
+ bool &mismatch_size, size_t &mismatch_index) {
+ if (Span1.size() != Span2.size()) {
+ mismatch_size = true;
+ return false;
+ }
+ for (size_t Index = 0; Index < Span1.size(); ++Index)
+ if (Span1[Index] != Span2[Index]) {
+ mismatch_index = Index;
+ return false;
+ }
+ return true;
+}
+
+bool MemoryMatcher::match(MemoryView actualValue) {
+ actual = actualValue;
+ return equals(expected, actual, mismatch_size, mismatch_index);
+}
+
+void display(testutils::StreamWrapper &Stream, char C) {
+ const auto print = [&Stream](unsigned char I) {
+ Stream << static_cast<char>(I < 10 ? '0' + I : 'A' + I - 10);
+ };
+ print(static_cast<unsigned char>(C) / 16);
+ print(static_cast<unsigned char>(C) & 15);
+}
+
+void display(testutils::StreamWrapper &Stream, MemoryView View) {
+ for (auto C : View) {
+ Stream << ' ';
+ display(Stream, C);
+ }
+}
+
+void MemoryMatcher::explainError(testutils::StreamWrapper &Stream) {
+ if (mismatch_size) {
+ Stream << "Size mismatch :";
+ Stream << "expected : ";
+ Stream << expected.size();
+ Stream << '\n';
+ Stream << "actual : ";
+ Stream << actual.size();
+ Stream << '\n';
+ } else {
+ Stream << "Mismatch at position : ";
+ Stream << mismatch_index;
+ Stream << " / ";
+ Stream << expected.size();
+ Stream << "\n";
+ Stream << "expected :";
+ display(Stream, expected);
+ Stream << '\n';
+ Stream << "actual :";
+ display(Stream, actual);
+ Stream << '\n';
+ }
+}
+
+} // namespace testing
+} // namespace memory
+} // namespace __llvm_libc