Mesa (main): util: Convert mesa-sha1_test to use gtest

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Oct 21 05:16:18 UTC 2021


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

Author: Caio Marcelo de Oliveira Filho <caio.oliveira at intel.com>
Date:   Tue Oct 19 09:11:21 2021 -0700

util: Convert mesa-sha1_test to use gtest

Reviewed-by: Dylan Baker <dylan at pnwbakers.com>
Acked-by: Matt Turner <mattst88 at gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13425>

---

 src/util/{mesa-sha1_test.c => mesa-sha1_test.cpp} | 58 ++++++++++-------------
 src/util/meson.build                              | 22 +++------
 2 files changed, 33 insertions(+), 47 deletions(-)

diff --git a/src/util/mesa-sha1_test.c b/src/util/mesa-sha1_test.cpp
similarity index 54%
rename from src/util/mesa-sha1_test.c
rename to src/util/mesa-sha1_test.cpp
index 9b3b477c7f2..6a95323f631 100644
--- a/src/util/mesa-sha1_test.c
+++ b/src/util/mesa-sha1_test.cpp
@@ -21,45 +21,39 @@
  * IN THE SOFTWARE.
  */
 
-#include <stdio.h>
-#include <stdbool.h>
-#include <string.h>
-
-#include "macros.h"
 #include "mesa-sha1.h"
 
+#include <gtest/gtest.h>
+
 #define SHA1_LENGTH 40
 
-int main(int argc, char *argv[])
-{
-   static const struct {
-      const char *string;
-      const char *sha1;
-   } test_data[] = {
-      {"Mesa Rocks! 273", "7fb99737373d65a73f049cdabc01e73aa6bc60f3"},
-      {"Mesa Rocks! 300", "b2180263e37d3bed6a4be0afe41b1a82ebbcf4c3"},
-      {"Mesa Rocks! 583", "7fb9734108a62503e8a149c1051facd7fb112d05"},
-   };
+struct Params {
+   const char *string;
+   const char *expected_sha1;
+};
 
-   bool failed = false;
-   int i;
+static const Params test_data[] = {
+   {"Mesa Rocks! 273", "7fb99737373d65a73f049cdabc01e73aa6bc60f3"},
+   {"Mesa Rocks! 300", "b2180263e37d3bed6a4be0afe41b1a82ebbcf4c3"},
+   {"Mesa Rocks! 583", "7fb9734108a62503e8a149c1051facd7fb112d05"},
+};
 
-   for (i = 0; i < ARRAY_SIZE(test_data); i++) {
-      unsigned char sha1[20];
-      _mesa_sha1_compute(test_data[i].string, strlen(test_data[i].string),
-                         sha1);
+class MesaSHA1TestFixture : public testing::TestWithParam<Params> {};
+INSTANTIATE_TEST_CASE_P(MesaSHA1Test, MesaSHA1TestFixture,
+                        testing::ValuesIn(test_data));
+
+TEST_P(MesaSHA1TestFixture, Match)
+{
+   Params p = GetParam();
 
-      char buf[41];
-      _mesa_sha1_format(buf, sha1);
+   unsigned char sha1[20];
+   _mesa_sha1_compute(p.string, strlen(p.string), sha1);
 
-      if (memcmp(test_data[i].sha1, buf, SHA1_LENGTH) != 0) {
-         printf("For string \"%s\", length %zu:\n"
-                "\tExpected: %s\n\t     Got: %s\n",
-                test_data[i].string, strlen(test_data[i].string),
-                test_data[i].sha1, buf);
-         failed = true;
-      }
-   }
+   char buf[41];
+   _mesa_sha1_format(buf, sha1);
 
-   return failed;
+   ASSERT_TRUE(memcmp(buf, p.expected_sha1, SHA1_LENGTH) == 0)
+      << "For string \"" << p.string << "\", length " << strlen(p.string) << ":\n"
+      << "\t  Actual: " << buf << "\n"
+      << "\tExpected: " << p.expected_sha1 << "\n";
 }
diff --git a/src/util/meson.build b/src/util/meson.build
index 84ea701bb3e..ea6f342a401 100644
--- a/src/util/meson.build
+++ b/src/util/meson.build
@@ -319,21 +319,6 @@ if with_tests
     should_fail : meson.get_cross_property('xfail', '').contains('roundeven'),
   )
 
-  # FIXME: this test crashes on windows
-  if host_machine.system() != 'windows'
-    test(
-      'mesa-sha1',
-      executable(
-        'mesa-sha1_test',
-        files('mesa-sha1_test.c'),
-        include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux],
-        link_with : _libmesa_util,
-        c_args : [c_msvc_compat_args],
-      ),
-      suite : ['util'],
-    )
-  endif
-
   files_util_tests = files(
     'bitset_test.cpp',
     'blob_test.cpp',
@@ -362,6 +347,13 @@ if with_tests
     )
   endif
 
+  # FIXME: this test crashes on windows
+  if host_machine.system() != 'windows'
+    files_util_tests += files(
+      'mesa-sha1_test.cpp',
+    )
+  endif
+
   test(
     'util_tests',
     executable(



More information about the mesa-commit mailing list