From aa68b3d6d6f6d6c51e753b26b0fdc67b0eeefa17 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Fri, 6 Feb 2015 12:41:57 +0100 Subject: [PATCH] tests: add a test case for g_steal_pointer() Just some basic checking to make sure it works as intended. https://bugzilla.gnome.org/show_bug.cgi?id=742456 --- glib/tests/utils.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/glib/tests/utils.c b/glib/tests/utils.c index 2e4a63b92..0441cd328 100644 --- a/glib/tests/utils.c +++ b/glib/tests/utils.c @@ -491,6 +491,44 @@ test_clear_pointer (void) g_assert (a == NULL); } +static int obj_count; + +static void +get_obj (gpointer *obj_out) +{ + gpointer obj = g_malloc (5); + obj_count++; + + if (obj_out) + *obj_out = g_steal_pointer (&obj); + + if (obj) + { + g_free (obj); + obj_count--; + } +} + +static void +test_take_pointer (void) +{ + gpointer a; + gpointer b; + + get_obj (NULL); + + get_obj (&a); + g_assert (a); + + /* ensure that it works to skip the macro */ + b = (g_steal_pointer) (&a); + g_assert (!a); + obj_count--; + g_free (b); + + g_assert (!obj_count); +} + static void test_misc_mem (void) { @@ -592,6 +630,7 @@ main (int argc, g_test_add_func ("/utils/specialdir", test_special_dir); g_test_add_func ("/utils/specialdir/desktop", test_desktop_special_dir); g_test_add_func ("/utils/clear-pointer", test_clear_pointer); + g_test_add_func ("/utils/take-pointer", test_take_pointer); g_test_add_func ("/utils/misc-mem", test_misc_mem); g_test_add_func ("/utils/nullify", test_nullify); g_test_add_func ("/utils/atexit", test_atexit);