From 3568f5b7892d4ff08217d00db995273e6ddc08f7 Mon Sep 17 00:00:00 2001 From: Maarten Duijndam Date: Wed, 30 Oct 2024 11:18:35 +0100 Subject: [PATCH] In refcount test don't rely on PsyGlCanvas In the unit tests in the/a CI framework, it might be hard to get an opengl canvas, and for this test another object might just suffice. --- tests/test-ref-count.c | 93 +++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 59 deletions(-) diff --git a/tests/test-ref-count.c b/tests/test-ref-count.c index 761051a..feeaf85 100644 --- a/tests/test-ref-count.c +++ b/tests/test-ref-count.c @@ -5,111 +5,86 @@ #include #include -static PsyCanvas *canvas = NULL; - -static int -init_window(void) -{ - canvas = PSY_CANVAS(psy_image_canvas_new(640, 480)); - if (canvas) - return 0; - else - return 1; -} - -static int -destoy_window(void) -{ - assert(G_OBJECT(canvas)->ref_count == 1); - g_object_unref(canvas); - canvas = NULL; - return 0; -} - static void ref_starts_with_one(void) { - PsyCircle *circle = g_object_new(PSY_TYPE_CIRCLE, "canvas", canvas, NULL); - PsyColor *color = g_object_new(PSY_TYPE_COLOR, NULL); - GObject *circle_gobj = G_OBJECT(circle); - GObject *color_gobj = G_OBJECT(color); + PsyColor *color = g_object_new(PSY_TYPE_COLOR, NULL); + GObject *color_gobj = G_OBJECT(color); - CU_ASSERT_EQUAL(circle_gobj->ref_count, 1); CU_ASSERT_EQUAL(color_gobj->ref_count, 1); - g_object_unref(circle); g_object_unref(color); } static void -ref_set_method(void) +transfer_none_method(void) { - PsyCircle *circle = g_object_new(PSY_TYPE_CIRCLE, "canvas", canvas, NULL); - PsyColor *color = g_object_new(PSY_TYPE_COLOR, NULL); + // clang-format off + PsyImage *image = g_object_new(PSY_TYPE_IMAGE, + "width", 640, + "height", 480, + "format", PSY_IMAGE_FORMAT_RGB, + NULL); + // clang-format on + + CU_ASSERT_PTR_NOT_NULL_FATAL(image); + PsyColor *color = g_object_new(PSY_TYPE_COLOR, NULL); // Cast to conveniently obtain the reference count - GObject *circle_gobj = G_OBJECT(circle); - GObject *color_gobj = G_OBJECT(color); + GObject *image_gobj = G_OBJECT(image); + GObject *color_gobj = G_OBJECT(color); /* * This is (transfer none), so we should free it. */ - psy_visual_stimulus_set_color(PSY_VISUAL_STIMULUS(circle), color); + psy_visual_stimulus_set_color(PSY_VISUAL_STIMULUS(image), color); - CU_ASSERT_EQUAL(circle_gobj->ref_count, 1); + CU_ASSERT_EQUAL(image_gobj->ref_count, 1); CU_ASSERT_EQUAL(color_gobj->ref_count, 1); - // The circle disposes its reference to color - g_object_unref(circle); - + g_object_unref(image); CU_ASSERT_EQUAL(color_gobj->ref_count, 1); - g_object_unref(color); } static void -ref_set_property(void) +set_property_transfer_full(void) { - PsyCircle *circle = g_object_new(PSY_TYPE_CIRCLE, "canvas", canvas, NULL); - PsyColor *color = g_object_new(PSY_TYPE_COLOR, NULL); - GObject *circle_gobj = G_OBJECT(circle); - GObject *color_gobj = G_OBJECT(color); + PsyTrial *trial = psy_trial_new(); + PsyLoop *loop = psy_loop_new(); - CU_ASSERT_EQUAL(circle_gobj->ref_count, 1); - CU_ASSERT_EQUAL(color_gobj->ref_count, 1); + // Cast to gobject to easily access refcount. + GObject *trial_gobj = G_OBJECT(trial); + GObject *loop_gobj = G_OBJECT(loop); - // Circle is owning a duplicate - g_object_set(circle, "color", color, NULL); + CU_ASSERT_EQUAL(trial_gobj->ref_count, 1); + CU_ASSERT_EQUAL(loop_gobj->ref_count, 1); - CU_ASSERT_EQUAL(circle_gobj->ref_count, 1); - CU_ASSERT_EQUAL(color_gobj->ref_count, 1); + g_object_set(loop, "child", trial, NULL); // transfer full - g_object_unref(circle); // Circle disposes its reference on color - CU_ASSERT_EQUAL(color_gobj->ref_count, 1); + CU_ASSERT_EQUAL(trial_gobj->ref_count, 1); // hence still one. - g_object_unref(color); + g_object_unref(loop); } int add_ref_count_suite(void) { - CU_Suite *suite - = CU_add_suite("test reference count", init_window, destoy_window); - CU_Test *test = NULL; + CU_Suite *suite = CU_add_suite("test reference count", NULL, NULL); + CU_Test *test = NULL; if (!suite) return 1; - test = CU_add_test( - suite, "Object start with reference of 1", ref_starts_with_one); + test = CU_ADD_TEST(suite, ref_starts_with_one); if (!test) return 1; - test = CU_add_test(suite, "Objects ref set method", ref_set_method); + test = CU_ADD_TEST(suite, transfer_none_method); if (!test) return 1; - test = CU_add_test(suite, "Objects ref set property", ref_set_property); + test = CU_ADD_TEST(suite, set_property_transfer_full); if (!test) return 1;