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