1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2005 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #define G_LOG_DOMAIN "TestReferences"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
27 #include <glib-object.h>
29 /* This test tests weak and toggle references
32 static GObject *global_object;
34 static gboolean object_destroyed;
35 static gboolean weak_ref1_notified;
36 static gboolean weak_ref2_notified;
37 static gboolean toggle_ref1_weakened;
38 static gboolean toggle_ref1_strengthened;
39 static gboolean toggle_ref2_weakened;
40 static gboolean toggle_ref2_strengthened;
41 static gboolean toggle_ref3_weakened;
42 static gboolean toggle_ref3_strengthened;
45 * TestObject, a parent class for TestObject
47 static GType test_object_get_type (void);
48 #define TEST_TYPE_OBJECT (test_object_get_type ())
49 typedef struct _TestObject TestObject;
50 typedef struct _TestObjectClass TestObjectClass;
54 GObject parent_instance;
56 struct _TestObjectClass
58 GObjectClass parent_class;
61 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
64 test_object_finalize (GObject *object)
66 object_destroyed = TRUE;
68 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
72 test_object_class_init (TestObjectClass *class)
74 GObjectClass *object_class = G_OBJECT_CLASS (class);
76 object_class->finalize = test_object_finalize;
80 test_object_init (TestObject *test_object)
87 object_destroyed = FALSE;
88 weak_ref1_notified = FALSE;
89 weak_ref2_notified = FALSE;
90 toggle_ref1_weakened = FALSE;
91 toggle_ref1_strengthened = FALSE;
92 toggle_ref2_weakened = FALSE;
93 toggle_ref2_strengthened = FALSE;
94 toggle_ref3_weakened = FALSE;
95 toggle_ref3_strengthened = FALSE;
99 weak_ref1 (gpointer data,
102 g_assert (object == global_object);
103 g_assert (data == GUINT_TO_POINTER (42));
105 weak_ref1_notified = TRUE;
109 weak_ref2 (gpointer data,
112 g_assert (object == global_object);
113 g_assert (data == GUINT_TO_POINTER (24));
115 weak_ref2_notified = TRUE;
119 toggle_ref1 (gpointer data,
121 gboolean is_last_ref)
123 g_assert (object == global_object);
124 g_assert (data == GUINT_TO_POINTER (42));
127 toggle_ref1_weakened = TRUE;
129 toggle_ref1_strengthened = TRUE;
133 toggle_ref2 (gpointer data,
135 gboolean is_last_ref)
137 g_assert (object == global_object);
138 g_assert (data == GUINT_TO_POINTER (24));
141 toggle_ref2_weakened = TRUE;
143 toggle_ref2_strengthened = TRUE;
147 toggle_ref3 (gpointer data,
149 gboolean is_last_ref)
151 g_assert (object == global_object);
152 g_assert (data == GUINT_TO_POINTER (34));
156 toggle_ref3_weakened = TRUE;
157 g_object_remove_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
160 toggle_ref3_strengthened = TRUE;
169 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
170 G_LOG_LEVEL_WARNING |
171 G_LOG_LEVEL_CRITICAL);
173 /* Test basic weak reference operation
175 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
177 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
180 g_object_unref (object);
181 g_assert (weak_ref1_notified == TRUE);
182 g_assert (object_destroyed == TRUE);
184 /* Test two weak references at once
186 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
188 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
189 g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
192 g_object_unref (object);
193 g_assert (weak_ref1_notified == TRUE);
194 g_assert (weak_ref2_notified == TRUE);
195 g_assert (object_destroyed == TRUE);
197 /* Test remove weak references
199 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
201 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
202 g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
203 g_object_weak_unref (object, weak_ref1, GUINT_TO_POINTER (42));
206 g_object_unref (object);
207 g_assert (weak_ref1_notified == FALSE);
208 g_assert (weak_ref2_notified == TRUE);
209 g_assert (object_destroyed == TRUE);
211 /* Test basic toggle reference operation
213 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
215 g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
218 g_object_unref (object);
219 g_assert (toggle_ref1_weakened == TRUE);
220 g_assert (toggle_ref1_strengthened == FALSE);
221 g_assert (object_destroyed == FALSE);
224 g_object_ref (object);
225 g_assert (toggle_ref1_weakened == FALSE);
226 g_assert (toggle_ref1_strengthened == TRUE);
227 g_assert (object_destroyed == FALSE);
229 g_object_unref (object);
232 g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
233 g_assert (toggle_ref1_weakened == FALSE);
234 g_assert (toggle_ref1_strengthened == FALSE);
235 g_assert (object_destroyed == TRUE);
237 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
239 /* Test two toggle references at once
241 g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
242 g_object_add_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
245 g_object_unref (object);
246 g_assert (toggle_ref1_weakened == FALSE);
247 g_assert (toggle_ref1_strengthened == FALSE);
248 g_assert (toggle_ref2_weakened == FALSE);
249 g_assert (toggle_ref2_strengthened == FALSE);
250 g_assert (object_destroyed == FALSE);
253 g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
254 g_assert (toggle_ref1_weakened == FALSE);
255 g_assert (toggle_ref1_strengthened == FALSE);
256 g_assert (toggle_ref2_weakened == TRUE);
257 g_assert (toggle_ref2_strengthened == FALSE);
258 g_assert (object_destroyed == FALSE);
261 g_object_remove_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
262 g_assert (toggle_ref1_weakened == FALSE);
263 g_assert (toggle_ref1_strengthened == FALSE);
264 g_assert (toggle_ref2_weakened == FALSE);
265 g_assert (toggle_ref2_strengthened == FALSE);
266 g_assert (object_destroyed == TRUE);
268 /* Test a toggle reference that removes itself
270 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
272 g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
275 g_object_unref (object);
276 g_assert (toggle_ref3_weakened == TRUE);
277 g_assert (toggle_ref3_strengthened == FALSE);
278 g_assert (object_destroyed == TRUE);