Mesa (main): gtest: Add mesa-gtest-extras.h with array ASSERT/EXPECT macros

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Nov 8 18:09:38 UTC 2021


Module: Mesa
Branch: main
Commit: 015383d1d78435edde87bf07bb8bb8aabd3c22fd
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=015383d1d78435edde87bf07bb8bb8aabd3c22fd

Author: Caio Oliveira <caio.oliveira at intel.com>
Date:   Wed Nov  3 23:22:39 2021 -0700

gtest: Add mesa-gtest-extras.h with array ASSERT/EXPECT macros

There are no similar macros in gtest.  They do recommend pulling
another library (gmock) and use that, but having our own let us
control the output more precisely.  Extracted the code from a similar
existing macro in blob_test.cpp, making changes to the output.

A check like

   ASSERT_U8_ARRAY_EQUAL(expected_u8, result_u8, 32);

that fails will output

```
Expected 32 values to be equal but found 1 that differ:

expected_u8 values are:

  [000]  29  44  00  00  44  60  cb  80  93  65  07  c0  08  00  40  29
  [016]  03  81  00  00  5c  a0  21  87 *b0  31  00  00  00  00  00  60

result_u8 values are:

  [000]  29  44  00  00  44  60  cb  80  93  65  07  c0  08  00  40  29
  [016]  03  81  00  00  5c  a0  21  87 *af  31  00  00  00  00  00  60
```

and a check like

   ASSERT_U64_ARRAY_EQUAL(expected_u64, result_u64, 4);

```
Expected 4 values to be equal but found 1 that differ:

expected_u64 values are:

  [000]  80cb604400004429  29400008c0076593
  [002]  8721a05c00008103 *60000000000031b0

result_u64 values are:

  [000]  80cb604400004429  29400008c0076593
  [002]  8721a05c00008103 *60000000000031af
```

Note the asterisk indicating wrong values.

The new header for extra macros is in src/gtest/include/ so it
doesn't get in the way when updating the real gtest headers that are
in src/gtest/include/gtest/.

Reviewed-by: Dylan Baker <dylan at pnwbakers.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13672>

---

 src/gtest/include/mesa-gtest-extras.h | 89 +++++++++++++++++++++++++++++++++++
 1 file changed, 89 insertions(+)

diff --git a/src/gtest/include/mesa-gtest-extras.h b/src/gtest/include/mesa-gtest-extras.h
new file mode 100644
index 00000000000..ca3c5a9654d
--- /dev/null
+++ b/src/gtest/include/mesa-gtest-extras.h
@@ -0,0 +1,89 @@
+/*
+ * Copyright © 2021 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef MESA_GTEST_EXTRAS_H
+#define MESA_GTEST_EXTRAS_H
+
+#include <gtest/gtest.h>
+
+template <typename T>
+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)
+{
+   if (memcmp(a, b, count * sizeof(T))) {
+      std::stringstream result;
+
+      unsigned mismatches = 0;
+      for (size_t i = 0; i < count; i++) {
+         if (a[i] != b[i])
+            mismatches++;
+      }
+
+      result << "Expected " << count << " values to be equal but found "
+             << mismatches << " that differ:\n\n";
+
+      result << std::right << std::setfill('0');
+
+      const unsigned values_per_line = 16 / sizeof(T);
+
+      result << a_expr << " values are:\n";
+      for (size_t i = 0; i < count; i++) {
+         if (i % values_per_line == 0)
+            result << "\n  [" << std::dec << std::setw(3) << i << "]";
+         result << " "
+                << (a[i] == b[i] ? ' ' : '*')
+                << std::hex << std::setw(2) << +a[i];
+      }
+      result << "\n\n";
+
+      result << b_expr << " values are:\n";
+      for (size_t i = 0; i < count; i++) {
+         if (i % values_per_line == 0)
+            result << "\n  [" << std::dec << std::setw(3) << i << "]";
+         result << " "
+                << (a[i] == b[i] ? ' ' : '*')
+                << std::hex << std::setw(sizeof(T) * 2) << +b[i];
+      }
+      result << "\n";
+
+      return testing::AssertionFailure() << result.str();
+   } else {
+      return testing::AssertionSuccess();
+   }
+}
+
+#define EXPECT_U8_ARRAY_EQUAL(a, b, count)  EXPECT_PRED_FORMAT3(array_equal_pred<uint8_t>, a, b, count)
+#define ASSERT_U8_ARRAY_EQUAL(a, b, count)  ASSERT_PRED_FORMAT3(array_equal_pred<uint8_t>, a, b, count)
+#define EXPECT_U16_ARRAY_EQUAL(a, b, count) EXPECT_PRED_FORMAT3(array_equal_pred<uint16_t>, a, b, count)
+#define ASSERT_U16_ARRAY_EQUAL(a, b, count) ASSERT_PRED_FORMAT3(array_equal_pred<uint16_t>, a, b, count)
+#define EXPECT_U32_ARRAY_EQUAL(a, b, count) EXPECT_PRED_FORMAT3(array_equal_pred<uint32_t>, a, b, count)
+#define ASSERT_U32_ARRAY_EQUAL(a, b, count) ASSERT_PRED_FORMAT3(array_equal_pred<uint32_t>, a, b, count)
+#define EXPECT_U64_ARRAY_EQUAL(a, b, count) EXPECT_PRED_FORMAT3(array_equal_pred<uint64_t>, a, b, count)
+#define ASSERT_U64_ARRAY_EQUAL(a, b, count) ASSERT_PRED_FORMAT3(array_equal_pred<uint64_t>, a, b, count)
+
+#endif /* MESA_GTEST_EXTRAS_H */



More information about the mesa-commit mailing list