unit testing - Check if a gobject was correctly freed -
i'm using glib's testing framework unit testing. library uses gobject, in test unities want check if after last _unref of each object object correctly freed. when g_test_trap_fork available used after each _unref, calling _unref second time , checking g_test_trap_assert_failed ().
however, g_test_trap_fork becoming deprecated i'm moving towards g_test_trap_subprocess. problem have write separated test case each _unref checked , since each case contain several objects imply on repetition of every test case adding second _unref each 1 present.
for example, tried fix this:
ncmvector *v = test->v; gvariant *var = ncm_vector_get_variant (v); g_assert (!g_variant_is_floating (var)); g_assert (g_variant_is_container (var)); g_assert_cmpuint (ncm_vector_len (v), ==, g_variant_n_children (var)); { ncmvector *nv = ncm_vector_new_variant (var); gint i; g_assert_cmpuint (ncm_vector_len (v), ==, ncm_vector_len (nv)); (i = 0; < ncm_vector_len (v); i++) { ncm_assert_cmpdouble (ncm_vector_get (v, i), ==, ncm_vector_get (nv, i)); } ncm_vector_free (nv); ncm_test_fail (ncm_vector_free (nv)); } g_variant_unref (var); ncm_test_fail (g_variant_unref (var); fprintf (stderr, "fail (%s)", g_variant_get_type_string (var)));
where macro ncm_test_fail given by:
#define ncm_test_fail(cmd) \ g_stmt_start { \ if (g_test_subprocess ()) \ { \ cmd; \ exit (0); \ } \ else \ { \ g_test_trap_subprocess (null, 0, 0); \ g_test_trap_assert_failed (); \ } \ } g_stmt_end
the problem solution can used once in each test case. if used second time, in example above, test first appearance of g_test_subprocess ().
i thought checking inside of gobject structure reference count before last _unref check if == 1. involve accessing private part of structure avoid if possible.
any ideas of how check erroneous code several times inside same test case?
if need check gobject
disposed correctly, can use g_object_add_weak_pointer()
, instance:
fooobject *o = g_object_new (foo_object_get_type (), null); g_assert_nonnull (o); // contents of pointer reset null when last reference // gobject instance goes away; passing pointer same instance // can check null later g_object_add_weak_pointer (g_object (o), (gpointer *) &o); // ...test fooobject... // drop last reference g_object_unref (o); // @ point, object should null if nothing holding // additional reference. g_assert_null (o);
in example above, though, you're using gvariant
, not gobject
, , not have weak references.
glib not have common reference counted type binary compatibility reasons; common code reference counted type in gobject
.
Comments
Post a Comment