Convert tests/gobject/references.c to glib test framework

This commit is contained in:
Emmanuel Fleury 2022-03-13 17:45:50 +01:00 committed by Philip Withnall
parent 662d8458f5
commit 2767e1614d

View File

@ -15,17 +15,9 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>. * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/ */
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestReferences"
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#include <glib-object.h> #include <glib-object.h>
/* This test tests weak and toggle references /* This test tests weak and toggle references */
*/
static GObject *global_object; static GObject *global_object;
@ -39,9 +31,7 @@ static gboolean toggle_ref2_strengthened;
static gboolean toggle_ref3_weakened; static gboolean toggle_ref3_weakened;
static gboolean toggle_ref3_strengthened; static gboolean toggle_ref3_strengthened;
/* /* TestObject, a parent class for TestObject */
* TestObject, a parent class for TestObject
*/
static GType test_object_get_type (void); static GType test_object_get_type (void);
#define TEST_TYPE_OBJECT (test_object_get_type ()) #define TEST_TYPE_OBJECT (test_object_get_type ())
typedef struct _TestObject TestObject; typedef struct _TestObject TestObject;
@ -97,8 +87,8 @@ static void
weak_ref1 (gpointer data, weak_ref1 (gpointer data,
GObject *object) GObject *object)
{ {
g_assert (object == global_object); g_assert_true (object == global_object);
g_assert (data == GUINT_TO_POINTER (42)); g_assert_cmpint (GPOINTER_TO_INT (data), ==, 42);
weak_ref1_notified = TRUE; weak_ref1_notified = TRUE;
} }
@ -107,8 +97,8 @@ static void
weak_ref2 (gpointer data, weak_ref2 (gpointer data,
GObject *object) GObject *object)
{ {
g_assert (object == global_object); g_assert_true (object == global_object);
g_assert (data == GUINT_TO_POINTER (24)); g_assert_cmpint (GPOINTER_TO_INT (data), ==, 24);
weak_ref2_notified = TRUE; weak_ref2_notified = TRUE;
} }
@ -118,8 +108,8 @@ toggle_ref1 (gpointer data,
GObject *object, GObject *object,
gboolean is_last_ref) gboolean is_last_ref)
{ {
g_assert (object == global_object); g_assert_true (object == global_object);
g_assert (data == GUINT_TO_POINTER (42)); g_assert_cmpint (GPOINTER_TO_INT (data), ==, 42);
if (is_last_ref) if (is_last_ref)
toggle_ref1_weakened = TRUE; toggle_ref1_weakened = TRUE;
@ -132,8 +122,8 @@ toggle_ref2 (gpointer data,
GObject *object, GObject *object,
gboolean is_last_ref) gboolean is_last_ref)
{ {
g_assert (object == global_object); g_assert_true (object == global_object);
g_assert (data == GUINT_TO_POINTER (24)); g_assert_cmpint (GPOINTER_TO_INT (data), ==, 24);
if (is_last_ref) if (is_last_ref)
toggle_ref2_weakened = TRUE; toggle_ref2_weakened = TRUE;
@ -146,8 +136,8 @@ toggle_ref3 (gpointer data,
GObject *object, GObject *object,
gboolean is_last_ref) gboolean is_last_ref)
{ {
g_assert (object == global_object); g_assert_true (object == global_object);
g_assert (data == GUINT_TO_POINTER (34)); g_assert_cmpint (GPOINTER_TO_INT (data), ==, 34);
if (is_last_ref) if (is_last_ref)
{ {
@ -158,26 +148,20 @@ toggle_ref3 (gpointer data,
toggle_ref3_strengthened = TRUE; toggle_ref3_strengthened = TRUE;
} }
int static void
main (int argc, test_references (void)
char *argv[])
{ {
GObject *object; GObject *object;
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) | /* Test basic weak reference operation */
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
/* Test basic weak reference operation
*/
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42)); g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
clear_flags (); clear_flags ();
g_object_unref (object); g_object_unref (object);
g_assert (weak_ref1_notified == TRUE); g_assert_true (weak_ref1_notified);
g_assert (object_destroyed == TRUE); g_assert_true (object_destroyed);
/* Test two weak references at once /* Test two weak references at once
*/ */
@ -188,12 +172,11 @@ main (int argc,
clear_flags (); clear_flags ();
g_object_unref (object); g_object_unref (object);
g_assert (weak_ref1_notified == TRUE); g_assert_true (weak_ref1_notified);
g_assert (weak_ref2_notified == TRUE); g_assert_true (weak_ref2_notified);
g_assert (object_destroyed == TRUE); g_assert_true (object_destroyed);
/* Test remove weak references /* Test remove weak references */
*/
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42)); g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
@ -202,79 +185,89 @@ main (int argc,
clear_flags (); clear_flags ();
g_object_unref (object); g_object_unref (object);
g_assert (weak_ref1_notified == FALSE); g_assert_false (weak_ref1_notified);
g_assert (weak_ref2_notified == TRUE); g_assert_true (weak_ref2_notified);
g_assert (object_destroyed == TRUE); g_assert_true (object_destroyed);
/* Test basic toggle reference operation /* Test basic toggle reference operation */
*/
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
clear_flags (); clear_flags ();
g_object_unref (object); g_object_unref (object);
g_assert (toggle_ref1_weakened == TRUE); g_assert_true (toggle_ref1_weakened);
g_assert (toggle_ref1_strengthened == FALSE); g_assert_false (toggle_ref1_strengthened);
g_assert (object_destroyed == FALSE); g_assert_false (object_destroyed);
clear_flags (); clear_flags ();
g_object_ref (object); g_object_ref (object);
g_assert (toggle_ref1_weakened == FALSE); g_assert_false (toggle_ref1_weakened);
g_assert (toggle_ref1_strengthened == TRUE); g_assert_true (toggle_ref1_strengthened);
g_assert (object_destroyed == FALSE); g_assert_false (object_destroyed);
g_object_unref (object); g_object_unref (object);
clear_flags (); clear_flags ();
g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
g_assert (toggle_ref1_weakened == FALSE); g_assert_false (toggle_ref1_weakened);
g_assert (toggle_ref1_strengthened == FALSE); g_assert_false (toggle_ref1_strengthened);
g_assert (object_destroyed == TRUE); g_assert_true (object_destroyed);
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
/* Test two toggle references at once /* Test two toggle references at once */
*/
g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
g_object_add_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24)); g_object_add_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
clear_flags (); clear_flags ();
g_object_unref (object); g_object_unref (object);
g_assert (toggle_ref1_weakened == FALSE); g_assert_false (toggle_ref1_weakened);
g_assert (toggle_ref1_strengthened == FALSE); g_assert_false (toggle_ref1_strengthened);
g_assert (toggle_ref2_weakened == FALSE); g_assert_false (toggle_ref2_weakened);
g_assert (toggle_ref2_strengthened == FALSE); g_assert_false (toggle_ref2_strengthened);
g_assert (object_destroyed == FALSE); g_assert_false (object_destroyed);
clear_flags (); clear_flags ();
g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
g_assert (toggle_ref1_weakened == FALSE); g_assert_false (toggle_ref1_weakened);
g_assert (toggle_ref1_strengthened == FALSE); g_assert_false (toggle_ref1_strengthened);
g_assert (toggle_ref2_weakened == TRUE); g_assert_true (toggle_ref2_weakened);
g_assert (toggle_ref2_strengthened == FALSE); g_assert_false (toggle_ref2_strengthened);
g_assert (object_destroyed == FALSE); g_assert_false (object_destroyed);
clear_flags (); clear_flags ();
/* Check that removing a toggle ref with %NULL data works fine. */ /* Check that removing a toggle ref with %NULL data works fine. */
g_object_remove_toggle_ref (object, toggle_ref2, NULL); g_object_remove_toggle_ref (object, toggle_ref2, NULL);
g_assert (toggle_ref1_weakened == FALSE); g_assert_false (toggle_ref1_weakened);
g_assert (toggle_ref1_strengthened == FALSE); g_assert_false (toggle_ref1_strengthened);
g_assert (toggle_ref2_weakened == FALSE); g_assert_false (toggle_ref2_weakened);
g_assert (toggle_ref2_strengthened == FALSE); g_assert_false (toggle_ref2_strengthened);
g_assert (object_destroyed == TRUE); g_assert_true (object_destroyed);
/* Test a toggle reference that removes itself /* Test a toggle reference that removes itself */
*/
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34)); g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
clear_flags (); clear_flags ();
g_object_unref (object); g_object_unref (object);
g_assert (toggle_ref3_weakened == TRUE); g_assert_true (toggle_ref3_weakened);
g_assert (toggle_ref3_strengthened == FALSE); g_assert_false (toggle_ref3_strengthened);
g_assert (object_destroyed == TRUE); g_assert_true (object_destroyed);
}
return 0;
int
main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/references", test_references);
return g_test_run ();
} }