[gjs/wip/3v1n0/toggle-queue-tests: 19/20] test: Move assert_equal definition into Gjs::Test and common header




commit 4b75c4bfcc3c51dc73f39972a90b777863b6de08
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Sun May 16 03:31:57 2021 +0200

    test: Move assert_equal definition into Gjs::Test and common header

 test/gjs-test-utils.h | 33 +++++++++++++++++++++++++++++++++
 test/gjs-tests.cpp    | 19 ++-----------------
 2 files changed, 35 insertions(+), 17 deletions(-)
---
diff --git a/test/gjs-test-utils.h b/test/gjs-test-utils.h
index f44298a6..c348526c 100644
--- a/test/gjs-test-utils.h
+++ b/test/gjs-test-utils.h
@@ -8,6 +8,12 @@
 
 #include <config.h>
 
+#include <glib.h>    // for g_assert_...
+#include <stdint.h>  // for uintptr_t
+#include <iterator>  // for pair
+#include <limits>    // for numeric_limits
+#include <string>
+
 #include "gjs/context.h"
 
 #include <js/TypeDecls.h>
@@ -32,4 +38,31 @@ void gjs_test_add_tests_for_rooting(void);
 
 void gjs_test_add_tests_for_jsapi_utils();
 
+namespace Gjs {
+namespace Test {
+
+template <typename T>
+constexpr void assert_equal(T a, T b) {
+    if constexpr (std::is_integral_v<T>) {
+        if constexpr (std::is_unsigned_v<T>)
+            g_assert_cmpuint(a, ==, b);
+        else
+            g_assert_cmpint(a, ==, b);
+    } else if constexpr (std::is_arithmetic_v<T>) {
+        g_assert_cmpfloat_with_epsilon(a, b, std::numeric_limits<T>::epsilon());
+    } else if constexpr (std::is_same_v<T, char*>) {
+        g_assert_cmpstr(a, ==, b);
+    } else if constexpr (std::is_same_v<T, std::string>) {
+        assert_equal(a.c_str(), b.c_str());
+    } else if constexpr (std::is_pointer_v<T>) {
+        assert_equal(reinterpret_cast<uintptr_t>(a),
+                     reinterpret_cast<uintptr_t>(b));
+    } else {
+        g_assert_true(a == b);
+    }
+}
+
+}  // namespace Test
+}  // namespace Gjs
+
 #endif  // TEST_GJS_TEST_UTILS_H_
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 325c967c..e335e635 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -47,6 +47,8 @@ void g_assertion_message(const char*, const char*, int, const char*,
 
 static unsigned cpp_random_seed = 0;
 
+using namespace Gjs::Test;
+
 template <typename T>
 T get_random_number() {
     std::mt19937_64 gen(cpp_random_seed);
@@ -68,23 +70,6 @@ T get_random_number() {
     }
 }
 
-template <typename T>
-constexpr void assert_equal(T a, T b) {
-    if constexpr (std::is_integral_v<T>) {
-        if constexpr (std::is_unsigned_v<T>)
-            g_assert_cmpuint(a, ==, b);
-        else
-            g_assert_cmpint(a, ==, b);
-    } else if constexpr (std::is_arithmetic_v<T>) {
-        g_assert_cmpfloat_with_epsilon(a, b, std::numeric_limits<T>::epsilon());
-    } else if constexpr (std::is_same_v<T, char*>) {
-        g_assert_cmpstr(a, ==, b);
-    } else if constexpr (std::is_pointer_v<T>) {
-        assert_equal(reinterpret_cast<uintptr_t>(a),
-                     reinterpret_cast<uintptr_t>(b));
-    }
-}
-
 static void
 gjstest_test_func_gjs_context_construct_destroy(void)
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]