[gjs/wip/3v1n0/toggle-queue-tests: 9/13] test: Move assert_equal definition into Gjs::Test and common header
- From: Marco Trevisan <marcotrevi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/3v1n0/toggle-queue-tests: 9/13] test: Move assert_equal definition into Gjs::Test and common header
- Date: Mon, 17 May 2021 17:32:06 +0000 (UTC)
commit eedcceff27959757a9335f60c4f863cf38172f3e
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 | 34 ++++++++++++++++++++++++++++++++++
test/gjs-tests.cpp | 19 ++-----------------
2 files changed, 36 insertions(+), 17 deletions(-)
---
diff --git a/test/gjs-test-utils.h b/test/gjs-test-utils.h
index f44298a6..5ec140db 100644
--- a/test/gjs-test-utils.h
+++ b/test/gjs-test-utils.h
@@ -8,6 +8,13 @@
#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 <utility> // IWYU pragma: keep
+
#include "gjs/context.h"
#include <js/TypeDecls.h>
@@ -32,4 +39,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..79098610 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 Gjs::Test::assert_equal;
+
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]