[glib: 3/7] glib/tests/cxx: Add clear and steal pointer functions tests




commit 0abb82498ef7330d84fd9a8d86a2ab6d2e8f9a7d
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Sep 13 18:28:44 2022 +0200

    glib/tests/cxx: Add clear and steal pointer functions tests
    
    These functions may be defined as macros with different behaviors in
    different c++ versions (as they rely on glib_typeof), so let's ensure
    they work and compile everywhere.

 glib/tests/cxx.cpp | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
---
diff --git a/glib/tests/cxx.cpp b/glib/tests/cxx.cpp
index 08ed8e81b6..d3ec77b2a2 100644
--- a/glib/tests/cxx.cpp
+++ b/glib/tests/cxx.cpp
@@ -184,6 +184,35 @@ test_inline_no_inline_macros (void)
   g_assert_true (do_inline_this ());
 }
 
+static void
+clear_boolean_ptr (gboolean *val)
+{
+  *val = TRUE;
+}
+
+static void
+test_clear_pointer (void)
+{
+  gboolean value = FALSE;
+  gboolean *ptr = &value;
+
+  g_assert_true (ptr == &value);
+  g_assert_false (value);
+  g_clear_pointer (&ptr, clear_boolean_ptr);
+  g_assert_null (ptr);
+  g_assert_true (value);
+}
+
+static void
+test_steal_pointer (void)
+{
+  gpointer ptr = &ptr;
+
+  g_assert_true (ptr == &ptr);
+  g_assert_true (g_steal_pointer (&ptr) == &ptr);
+  g_assert_null (ptr);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -201,6 +230,8 @@ main (int argc, char *argv[])
   g_test_add_func ("/C++/atomic-pointer-exchange", test_atomic_pointer_exchange);
   g_test_add_func ("/C++/atomic-int-exchange", test_atomic_int_exchange);
   g_test_add_func ("/C++/inlined-not-inlined-functions", test_inline_no_inline_macros);
+  g_test_add_func ("/C++/clear-pointer", test_clear_pointer);
+  g_test_add_func ("/C++/steal-pointer", test_steal_pointer);
 
   return g_test_run ();
 }


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