1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef MESA_GTEST_EXTRAS_H
25bf215546Sopenharmony_ci#define MESA_GTEST_EXTRAS_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <gtest/gtest.h>
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_citemplate <typename T>
30bf215546Sopenharmony_cistatic testing::AssertionResult
31bf215546Sopenharmony_ciarray_equal_pred(const char *a_expr,
32bf215546Sopenharmony_ci                 const char *b_expr,
33bf215546Sopenharmony_ci                 const char *c_expr,
34bf215546Sopenharmony_ci                 const T *a,
35bf215546Sopenharmony_ci                 const T *b,
36bf215546Sopenharmony_ci                 size_t count)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   if (memcmp(a, b, count * sizeof(T))) {
39bf215546Sopenharmony_ci      std::stringstream result;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci      unsigned mismatches = 0;
42bf215546Sopenharmony_ci      for (size_t i = 0; i < count; i++) {
43bf215546Sopenharmony_ci         if (a[i] != b[i])
44bf215546Sopenharmony_ci            mismatches++;
45bf215546Sopenharmony_ci      }
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci      result << "Expected " << count << " values to be equal but found "
48bf215546Sopenharmony_ci             << mismatches << " that differ:\n\n";
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci      result << std::right << std::setfill('0');
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci      const unsigned values_per_line = 16 / sizeof(T);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci      result << a_expr << " values are:\n";
55bf215546Sopenharmony_ci      for (size_t i = 0; i < count; i++) {
56bf215546Sopenharmony_ci         if (i % values_per_line == 0)
57bf215546Sopenharmony_ci            result << "\n  [" << std::dec << std::setw(3) << i << "]";
58bf215546Sopenharmony_ci         result << " "
59bf215546Sopenharmony_ci                << (a[i] == b[i] ? ' ' : '*')
60bf215546Sopenharmony_ci                << std::hex << std::setw(sizeof(T) * 2) << +a[i];
61bf215546Sopenharmony_ci      }
62bf215546Sopenharmony_ci      result << "\n\n";
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci      result << b_expr << " values are:\n";
65bf215546Sopenharmony_ci      for (size_t i = 0; i < count; i++) {
66bf215546Sopenharmony_ci         if (i % values_per_line == 0)
67bf215546Sopenharmony_ci            result << "\n  [" << std::dec << std::setw(3) << i << "]";
68bf215546Sopenharmony_ci         result << " "
69bf215546Sopenharmony_ci                << (a[i] == b[i] ? ' ' : '*')
70bf215546Sopenharmony_ci                << std::hex << std::setw(sizeof(T) * 2) << +b[i];
71bf215546Sopenharmony_ci      }
72bf215546Sopenharmony_ci      result << "\n";
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci      return testing::AssertionFailure() << result.str();
75bf215546Sopenharmony_ci   } else {
76bf215546Sopenharmony_ci      return testing::AssertionSuccess();
77bf215546Sopenharmony_ci   }
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci#define EXPECT_U8_ARRAY_EQUAL(a, b, count)  EXPECT_PRED_FORMAT3(array_equal_pred<uint8_t>, a, b, count)
81bf215546Sopenharmony_ci#define ASSERT_U8_ARRAY_EQUAL(a, b, count)  ASSERT_PRED_FORMAT3(array_equal_pred<uint8_t>, a, b, count)
82bf215546Sopenharmony_ci#define EXPECT_U16_ARRAY_EQUAL(a, b, count) EXPECT_PRED_FORMAT3(array_equal_pred<uint16_t>, a, b, count)
83bf215546Sopenharmony_ci#define ASSERT_U16_ARRAY_EQUAL(a, b, count) ASSERT_PRED_FORMAT3(array_equal_pred<uint16_t>, a, b, count)
84bf215546Sopenharmony_ci#define EXPECT_U32_ARRAY_EQUAL(a, b, count) EXPECT_PRED_FORMAT3(array_equal_pred<uint32_t>, a, b, count)
85bf215546Sopenharmony_ci#define ASSERT_U32_ARRAY_EQUAL(a, b, count) ASSERT_PRED_FORMAT3(array_equal_pred<uint32_t>, a, b, count)
86bf215546Sopenharmony_ci#define EXPECT_U64_ARRAY_EQUAL(a, b, count) EXPECT_PRED_FORMAT3(array_equal_pred<uint64_t>, a, b, count)
87bf215546Sopenharmony_ci#define ASSERT_U64_ARRAY_EQUAL(a, b, count) ASSERT_PRED_FORMAT3(array_equal_pred<uint64_t>, a, b, count)
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci#endif /* MESA_GTEST_EXTRAS_H */
90