From 0d36ea7d585fb66a1df85dfb8e78c90995073121 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Tue, 19 Oct 2021 09:11:21 -0700 Subject: [PATCH] util: Convert mesa-sha1_test to use gtest Reviewed-by: Dylan Baker Acked-by: Matt Turner Part-of: --- .../{mesa-sha1_test.c => mesa-sha1_test.cpp} | 60 +++++++++---------- src/util/meson.build | 22 +++---- 2 files changed, 34 insertions(+), 48 deletions(-) rename src/util/{mesa-sha1_test.c => mesa-sha1_test.cpp} (54%) 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 -#include -#include - -#include "macros.h" #include "mesa-sha1.h" +#include + #define SHA1_LENGTH 40 -int main(int argc, char *argv[]) +struct Params { + const char *string; + const char *expected_sha1; +}; + +static const Params test_data[] = { + {"Mesa Rocks! 273", "7fb99737373d65a73f049cdabc01e73aa6bc60f3"}, + {"Mesa Rocks! 300", "b2180263e37d3bed6a4be0afe41b1a82ebbcf4c3"}, + {"Mesa Rocks! 583", "7fb9734108a62503e8a149c1051facd7fb112d05"}, +}; + +class MesaSHA1TestFixture : public testing::TestWithParam {}; +INSTANTIATE_TEST_CASE_P(MesaSHA1Test, MesaSHA1TestFixture, + testing::ValuesIn(test_data)); + +TEST_P(MesaSHA1TestFixture, Match) { - static const struct { - const char *string; - const char *sha1; - } test_data[] = { - {"Mesa Rocks! 273", "7fb99737373d65a73f049cdabc01e73aa6bc60f3"}, - {"Mesa Rocks! 300", "b2180263e37d3bed6a4be0afe41b1a82ebbcf4c3"}, - {"Mesa Rocks! 583", "7fb9734108a62503e8a149c1051facd7fb112d05"}, - }; + Params p = GetParam(); - bool failed = false; - int i; + unsigned char sha1[20]; + _mesa_sha1_compute(p.string, strlen(p.string), sha1); - 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); + char buf[41]; + _mesa_sha1_format(buf, sha1); - char buf[41]; - _mesa_sha1_format(buf, 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; - } - } - - 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(