r22114 - gnucash/trunk/src/test-core - Testing: Provide a test_destroy function/macro combination.

John Ralls jralls at code.gnucash.org
Sat Mar 24 18:20:19 EDT 2012


Author: jralls
Date: 2012-03-24 18:20:19 -0400 (Sat, 24 Mar 2012)
New Revision: 22114
Trac: http://svn.gnucash.org/trac/changeset/22114

Modified:
   gnucash/trunk/src/test-core/test-stuff.c
   gnucash/trunk/src/test-core/test-stuff.h
Log:
Testing: Provide a test_destroy function/macro combination.

Works on GObjects. Asserts if the object isn't a GObject (which may be the case if it has already been finalized) or if the object's finalize method isn't called (indicating a leak).

Modified: gnucash/trunk/src/test-core/test-stuff.c
===================================================================
--- gnucash/trunk/src/test-core/test-stuff.c	2012-03-24 22:20:08 UTC (rev 22113)
+++ gnucash/trunk/src/test-core/test-stuff.c	2012-03-24 22:20:19 UTC (rev 22114)
@@ -548,4 +548,19 @@
     return sig->hits;
 }
 
+static void
+notify_destroy (gpointer pdata, GObject *obj)
+{
+    gboolean *data = (gboolean*)pdata;
+    if (! (*data)) *data = TRUE;
 }
+
+gboolean
+test_object_checked_destroy (GObject *obj)
+{
+    gboolean is_destroyed = FALSE;
+    if (!obj || ! G_IS_OBJECT (obj)) return FALSE;
+    g_object_weak_ref (obj, notify_destroy, &is_destroyed);
+    g_object_unref (obj);
+    return is_destroyed;
+}

Modified: gnucash/trunk/src/test-core/test-stuff.h
===================================================================
--- gnucash/trunk/src/test-core/test-stuff.h	2012-03-24 22:20:08 UTC (rev 22113)
+++ gnucash/trunk/src/test-core/test-stuff.h	2012-03-24 22:20:19 UTC (rev 22114)
@@ -282,6 +282,25 @@
 
 void test_signal_free (TestSignal sig);
 
+/* test_object_checked_destroy unrefs obj and returns true if its finalize
+ * method was called.
+ */
+
+gboolean test_object_checked_destroy (GObject *obj);
+
+/**
+ * test_destroy() ensures that a GObject is still alive at the time
+ * it's called and that it is finalized. The first assertion will
+ * trigger if you pass it a ponter which isn't a GObject -- which
+ * could be the case if the object has already been finalized. Then it
+ * calls test_object_checked_destroy() on it, asserting if the
+ * finalize method wasn't called (which indicates a leak).
+ */
+
+#define test_destroy(obj) \
+    g_assert (obj != NULL && G_IS_OBJECT (obj));		\
+    g_assert (test_object_checked_destroy (G_OBJECT (obj)))
+
 /* For Scheme testing access:
 void gnc_log_init_filename_special (gchar *filename);
 void gnc_log_shutdown (void);



More information about the gnucash-changes mailing list