Skip to content

Commit

Permalink
In refcount test don't rely on PsyGlCanvas
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maartenuni committed Oct 30, 2024
1 parent 999e24a commit 3568f5b
Showing 1 changed file with 34 additions and 59 deletions.
93 changes: 34 additions & 59 deletions tests/test-ref-count.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,111 +5,86 @@
#include <CUnit/CUnit.h>
#include <psylib.h>

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;

Expand Down

0 comments on commit 3568f5b

Please sign in to comment.