Do not expand autoptr macros when running introspection
[glib.git] / gobject / tests / reference.c
blobe3f86315cfc2ef164d3cdea5b60b38ffbe3f6d4d
1 #include <glib-object.h>
3 static void
4 test_fundamentals (void)
6 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_NONE));
7 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_INTERFACE));
8 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_CHAR));
9 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_UCHAR));
10 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_BOOLEAN));
11 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_INT));
12 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_UINT));
13 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_LONG));
14 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_ULONG));
15 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_INT64));
16 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_UINT64));
17 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_ENUM));
18 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_FLAGS));
19 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_FLOAT));
20 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_DOUBLE));
21 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_STRING));
22 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_POINTER));
23 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_BOXED));
24 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_PARAM));
25 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_OBJECT));
26 g_assert (G_TYPE_OBJECT == g_object_get_type ());
27 g_assert (G_TYPE_IS_FUNDAMENTAL (G_TYPE_VARIANT));
28 g_assert (G_TYPE_IS_DERIVED (G_TYPE_INITIALLY_UNOWNED));
30 g_assert (g_type_fundamental_next () == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
33 static void
34 test_type_qdata (void)
36 gchar *data;
38 g_type_set_qdata (G_TYPE_ENUM, g_quark_from_string ("bla"), "bla");
39 data = g_type_get_qdata (G_TYPE_ENUM, g_quark_from_string ("bla"));
40 g_assert_cmpstr (data, ==, "bla");
43 static void
44 test_type_query (void)
46 GTypeQuery query;
48 g_type_query (G_TYPE_ENUM, &query);
49 g_assert_cmpint (query.type, ==, G_TYPE_ENUM);
50 g_assert_cmpstr (query.type_name, ==, "GEnum");
51 g_assert_cmpint (query.class_size, ==, sizeof (GEnumClass));
52 g_assert_cmpint (query.instance_size, ==, 0);
55 typedef struct _MyObject MyObject;
56 typedef struct _MyObjectClass MyObjectClass;
57 typedef struct _MyObjectClassPrivate MyObjectClassPrivate;
59 struct _MyObject
61 GObject parent_instance;
63 gint count;
66 struct _MyObjectClass
68 GObjectClass parent_class;
71 struct _MyObjectClassPrivate
73 gint secret_class_count;
76 static GType my_object_get_type (void);
77 G_DEFINE_TYPE_WITH_CODE (MyObject, my_object, G_TYPE_OBJECT,
78 g_type_add_class_private (g_define_type_id, sizeof (MyObjectClassPrivate)) );
80 static void
81 my_object_init (MyObject *obj)
83 obj->count = 42;
86 static void
87 my_object_class_init (MyObjectClass *klass)
91 static void
92 test_class_private (void)
94 GObject *obj;
95 MyObjectClass *class;
96 MyObjectClassPrivate *priv;
98 obj = g_object_new (my_object_get_type (), NULL);
100 class = g_type_class_ref (my_object_get_type ());
101 priv = G_TYPE_CLASS_GET_PRIVATE (class, my_object_get_type (), MyObjectClassPrivate);
102 priv->secret_class_count = 13;
103 g_type_class_unref (class);
105 g_object_unref (obj);
107 g_assert_cmpint (g_type_qname (my_object_get_type ()), ==, g_quark_from_string ("MyObject"));
110 static void
111 test_clear (void)
113 GObject *o = NULL;
114 GObject *tmp;
116 g_clear_object (&o);
117 g_assert (o == NULL);
119 tmp = g_object_new (G_TYPE_OBJECT, NULL);
120 g_assert_cmpint (tmp->ref_count, ==, 1);
121 o = g_object_ref (tmp);
122 g_assert (o != NULL);
124 g_assert_cmpint (tmp->ref_count, ==, 2);
125 g_clear_object (&o);
126 g_assert_cmpint (tmp->ref_count, ==, 1);
127 g_assert (o == NULL);
129 g_object_unref (tmp);
132 static void
133 test_clear_function (void)
135 volatile GObject *o = NULL;
136 GObject *tmp;
138 (g_clear_object) (&o);
139 g_assert (o == NULL);
141 tmp = g_object_new (G_TYPE_OBJECT, NULL);
142 g_assert_cmpint (tmp->ref_count, ==, 1);
143 o = g_object_ref (tmp);
144 g_assert (o != NULL);
146 g_assert_cmpint (tmp->ref_count, ==, 2);
147 (g_clear_object) (&o);
148 g_assert_cmpint (tmp->ref_count, ==, 1);
149 g_assert (o == NULL);
151 g_object_unref (tmp);
154 static void
155 test_set (void)
157 GObject *o = NULL;
158 GObject *tmp;
160 g_assert (!g_set_object (&o, NULL));
161 g_assert (o == NULL);
163 tmp = g_object_new (G_TYPE_OBJECT, NULL);
164 g_assert_cmpint (tmp->ref_count, ==, 1);
166 g_assert (g_set_object (&o, tmp));
167 g_assert (o == tmp);
168 g_assert_cmpint (tmp->ref_count, ==, 2);
170 g_object_unref (tmp);
171 g_assert_cmpint (tmp->ref_count, ==, 1);
173 /* Setting it again shouldn’t cause finalisation. */
174 g_assert (!g_set_object (&o, tmp));
175 g_assert (o == tmp);
176 g_assert_cmpint (tmp->ref_count, ==, 1);
178 g_assert (g_set_object (&o, NULL));
179 g_assert (o == NULL);
180 g_assert (!G_IS_OBJECT (tmp)); /* finalised */
183 static void
184 test_set_function (void)
186 GObject *o = NULL;
187 GObject *tmp;
189 g_assert (!(g_set_object) (&o, NULL));
190 g_assert (o == NULL);
192 tmp = g_object_new (G_TYPE_OBJECT, NULL);
193 g_assert_cmpint (tmp->ref_count, ==, 1);
195 g_assert ((g_set_object) (&o, tmp));
196 g_assert (o == tmp);
197 g_assert_cmpint (tmp->ref_count, ==, 2);
199 g_object_unref (tmp);
200 g_assert_cmpint (tmp->ref_count, ==, 1);
202 /* Setting it again shouldn’t cause finalisation. */
203 g_assert (!(g_set_object) (&o, tmp));
204 g_assert (o == tmp);
205 g_assert_cmpint (tmp->ref_count, ==, 1);
207 g_assert ((g_set_object) (&o, NULL));
208 g_assert (o == NULL);
209 g_assert (!G_IS_OBJECT (tmp)); /* finalised */
212 static void
213 toggle_cb (gpointer data, GObject *obj, gboolean is_last)
215 gboolean *b = data;
217 *b = TRUE;
220 static void
221 test_object_value (void)
223 GObject *v;
224 GObject *v2;
225 GValue value = G_VALUE_INIT;
226 gboolean toggled = FALSE;
228 g_value_init (&value, G_TYPE_OBJECT);
230 v = g_object_new (G_TYPE_OBJECT, NULL);
231 g_object_add_toggle_ref (v, toggle_cb, &toggled);
233 g_value_take_object (&value, v);
235 v2 = g_value_get_object (&value);
236 g_assert (v2 == v);
238 v2 = g_value_dup_object (&value);
239 g_assert (v2 == v); /* objects use ref/unref for copy/free */
240 g_object_unref (v2);
242 g_assert (!toggled);
243 g_value_unset (&value);
244 g_assert (toggled);
246 /* test the deprecated variant too */
247 g_value_init (&value, G_TYPE_OBJECT);
248 /* get a new reference */
249 g_object_ref (v);
251 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
252 g_value_set_object_take_ownership (&value, v);
253 G_GNUC_END_IGNORE_DEPRECATIONS
255 toggled = FALSE;
256 g_value_unset (&value);
257 g_assert (toggled);
259 g_object_remove_toggle_ref (v, toggle_cb, &toggled);
262 static void
263 test_initially_unowned (void)
265 GObject *obj;
267 obj = g_object_new (G_TYPE_INITIALLY_UNOWNED, NULL);
268 g_assert (g_object_is_floating (obj));
269 g_assert_cmpint (obj->ref_count, ==, 1);
271 g_object_ref_sink (obj);
272 g_assert (!g_object_is_floating (obj));
273 g_assert_cmpint (obj->ref_count, ==, 1);
275 g_object_ref_sink (obj);
276 g_assert (!g_object_is_floating (obj));
277 g_assert_cmpint (obj->ref_count, ==, 2);
279 g_object_unref (obj);
280 g_assert_cmpint (obj->ref_count, ==, 1);
282 g_object_force_floating (obj);
283 g_assert (g_object_is_floating (obj));
284 g_assert_cmpint (obj->ref_count, ==, 1);
286 g_object_ref_sink (obj);
287 g_object_unref (obj);
290 static void
291 test_weak_pointer (void)
293 GObject *obj;
294 gpointer weak;
295 gpointer weak2;
297 weak = weak2 = obj = g_object_new (G_TYPE_OBJECT, NULL);
298 g_assert_cmpint (obj->ref_count, ==, 1);
300 g_object_add_weak_pointer (obj, &weak);
301 g_object_add_weak_pointer (obj, &weak2);
302 g_assert_cmpint (obj->ref_count, ==, 1);
303 g_assert (weak == obj);
304 g_assert (weak2 == obj);
306 g_object_remove_weak_pointer (obj, &weak2);
307 g_assert_cmpint (obj->ref_count, ==, 1);
308 g_assert (weak == obj);
309 g_assert (weak2 == obj);
311 g_object_unref (obj);
312 g_assert (weak == NULL);
313 g_assert (weak2 == obj);
316 static void
317 test_weak_pointer_clear (void)
319 GObject *obj;
320 gpointer weak = NULL;
322 g_clear_weak_pointer (&weak);
323 g_assert_null (weak);
325 weak = obj = g_object_new (G_TYPE_OBJECT, NULL);
326 g_assert_cmpint (obj->ref_count, ==, 1);
328 g_object_add_weak_pointer (obj, &weak);
329 g_assert_cmpint (obj->ref_count, ==, 1);
330 g_assert_true (weak == obj);
332 g_clear_weak_pointer (&weak);
333 g_assert_cmpint (obj->ref_count, ==, 1);
334 g_assert_null (weak);
336 g_object_unref (obj);
339 static void
340 test_weak_pointer_clear_function (void)
342 GObject *obj;
343 gpointer weak = NULL;
345 (g_clear_weak_pointer) (&weak);
346 g_assert_null (weak);
348 weak = obj = g_object_new (G_TYPE_OBJECT, NULL);
349 g_assert_cmpint (obj->ref_count, ==, 1);
351 g_object_add_weak_pointer (obj, &weak);
352 g_assert_cmpint (obj->ref_count, ==, 1);
353 g_assert_true (weak == obj);
355 (g_clear_weak_pointer) (&weak);
356 g_assert_cmpint (obj->ref_count, ==, 1);
357 g_assert_null (weak);
359 g_object_unref (obj);
362 static void
363 test_weak_pointer_set (void)
365 GObject *obj;
366 gpointer weak = NULL;
368 g_assert_false (g_set_weak_pointer (&weak, NULL));
369 g_assert_null (weak);
371 obj = g_object_new (G_TYPE_OBJECT, NULL);
372 g_assert_cmpint (obj->ref_count, ==, 1);
374 g_assert_true (g_set_weak_pointer (&weak, obj));
375 g_assert_cmpint (obj->ref_count, ==, 1);
376 g_assert_true (weak == obj);
378 g_assert_true (g_set_weak_pointer (&weak, NULL));
379 g_assert_cmpint (obj->ref_count, ==, 1);
380 g_assert_null (weak);
382 g_assert_true (g_set_weak_pointer (&weak, obj));
383 g_assert_cmpint (obj->ref_count, ==, 1);
384 g_assert_true (weak == obj);
386 g_object_unref (obj);
387 g_assert_null (weak);
390 static void
391 test_weak_pointer_set_function (void)
393 GObject *obj;
394 gpointer weak = NULL;
396 g_assert_false ((g_set_weak_pointer) (&weak, NULL));
397 g_assert_null (weak);
399 obj = g_object_new (G_TYPE_OBJECT, NULL);
400 g_assert_cmpint (obj->ref_count, ==, 1);
402 g_assert_true ((g_set_weak_pointer) (&weak, obj));
403 g_assert_cmpint (obj->ref_count, ==, 1);
404 g_assert_true (weak == obj);
406 g_assert_true ((g_set_weak_pointer) (&weak, NULL));
407 g_assert_cmpint (obj->ref_count, ==, 1);
408 g_assert_null (weak);
410 g_assert_true ((g_set_weak_pointer) (&weak, obj));
411 g_assert_cmpint (obj->ref_count, ==, 1);
412 g_assert_true (weak == obj);
414 g_object_unref (obj);
415 g_assert_null (weak);
418 /* See gobject/tests/threadtests.c for the threaded version */
419 static void
420 test_weak_ref (void)
422 GObject *obj;
423 GObject *obj2;
424 GObject *tmp;
425 GWeakRef weak = { { GUINT_TO_POINTER (0xDEADBEEFU) } };
426 GWeakRef weak2 = { { GUINT_TO_POINTER (0xDEADBEEFU) } };
427 GWeakRef weak3 = { { GUINT_TO_POINTER (0xDEADBEEFU) } };
428 GWeakRef *dynamic_weak = g_new (GWeakRef, 1);
430 /* you can initialize to empty like this... */
431 g_weak_ref_init (&weak2, NULL);
432 g_assert (g_weak_ref_get (&weak2) == NULL);
434 /* ... or via an initializer */
435 g_weak_ref_init (&weak3, NULL);
436 g_assert (g_weak_ref_get (&weak3) == NULL);
438 obj = g_object_new (G_TYPE_OBJECT, NULL);
439 g_assert_cmpint (obj->ref_count, ==, 1);
441 obj2 = g_object_new (G_TYPE_OBJECT, NULL);
442 g_assert_cmpint (obj2->ref_count, ==, 1);
444 /* you can init with an object (even if uninitialized) */
445 g_weak_ref_init (&weak, obj);
446 g_weak_ref_init (dynamic_weak, obj);
447 /* or set to point at an object, if initialized (maybe to 0) */
448 g_weak_ref_set (&weak2, obj);
449 g_weak_ref_set (&weak3, obj);
450 /* none of this affects its refcount */
451 g_assert_cmpint (obj->ref_count, ==, 1);
453 /* getting the value takes a ref */
454 tmp = g_weak_ref_get (&weak);
455 g_assert (tmp == obj);
456 g_assert_cmpint (obj->ref_count, ==, 2);
457 g_object_unref (tmp);
458 g_assert_cmpint (obj->ref_count, ==, 1);
460 tmp = g_weak_ref_get (&weak2);
461 g_assert (tmp == obj);
462 g_assert_cmpint (obj->ref_count, ==, 2);
463 g_object_unref (tmp);
464 g_assert_cmpint (obj->ref_count, ==, 1);
466 tmp = g_weak_ref_get (&weak3);
467 g_assert (tmp == obj);
468 g_assert_cmpint (obj->ref_count, ==, 2);
469 g_object_unref (tmp);
470 g_assert_cmpint (obj->ref_count, ==, 1);
472 tmp = g_weak_ref_get (dynamic_weak);
473 g_assert (tmp == obj);
474 g_assert_cmpint (obj->ref_count, ==, 2);
475 g_object_unref (tmp);
476 g_assert_cmpint (obj->ref_count, ==, 1);
478 /* clearing a weak ref stops tracking */
479 g_weak_ref_clear (&weak);
481 /* setting a weak ref to NULL stops tracking too */
482 g_weak_ref_set (&weak2, NULL);
483 g_assert (g_weak_ref_get (&weak2) == NULL);
484 g_weak_ref_clear (&weak2);
486 /* setting a weak ref to a new object stops tracking the old one */
487 g_weak_ref_set (dynamic_weak, obj2);
488 tmp = g_weak_ref_get (dynamic_weak);
489 g_assert (tmp == obj2);
490 g_assert_cmpint (obj2->ref_count, ==, 2);
491 g_object_unref (tmp);
492 g_assert_cmpint (obj2->ref_count, ==, 1);
494 g_assert_cmpint (obj->ref_count, ==, 1);
496 /* free the object: weak3 is the only one left pointing there */
497 g_object_unref (obj);
498 g_assert (g_weak_ref_get (&weak3) == NULL);
500 /* setting a weak ref to a new object stops tracking the old one */
501 g_weak_ref_set (dynamic_weak, obj2);
502 tmp = g_weak_ref_get (dynamic_weak);
503 g_assert (tmp == obj2);
504 g_assert_cmpint (obj2->ref_count, ==, 2);
505 g_object_unref (tmp);
506 g_assert_cmpint (obj2->ref_count, ==, 1);
508 g_weak_ref_clear (&weak3);
510 /* clear and free dynamic_weak... */
511 g_weak_ref_clear (dynamic_weak);
513 /* ... to prove that doing so stops this from being a use-after-free */
514 g_object_unref (obj2);
515 g_free (dynamic_weak);
518 typedef struct
520 gboolean should_be_last;
521 gint count;
522 } Count;
524 static void
525 toggle_notify (gpointer data,
526 GObject *obj,
527 gboolean is_last)
529 Count *c = data;
531 g_assert (is_last == c->should_be_last);
533 c->count++;
536 static void
537 test_toggle_ref (void)
539 GObject *obj;
540 Count c, c2;
542 obj = g_object_new (G_TYPE_OBJECT, NULL);
544 g_object_add_toggle_ref (obj, toggle_notify, &c);
545 g_object_add_toggle_ref (obj, toggle_notify, &c2);
547 c.should_be_last = c2.should_be_last = TRUE;
548 c.count = c2.count = 0;
550 g_object_unref (obj);
552 g_assert_cmpint (c.count, ==, 0);
553 g_assert_cmpint (c2.count, ==, 0);
555 g_object_ref (obj);
557 g_assert_cmpint (c.count, ==, 0);
558 g_assert_cmpint (c2.count, ==, 0);
560 g_object_remove_toggle_ref (obj, toggle_notify, &c2);
562 g_object_unref (obj);
564 g_assert_cmpint (c.count, ==, 1);
566 c.should_be_last = FALSE;
568 g_object_ref (obj);
570 g_assert_cmpint (c.count, ==, 2);
572 c.should_be_last = TRUE;
574 g_object_unref (obj);
576 g_assert_cmpint (c.count, ==, 3);
578 g_object_remove_toggle_ref (obj, toggle_notify, &c);
581 static gboolean destroyed;
582 static gint value;
584 static void
585 data_destroy (gpointer data)
587 g_assert_cmpint (GPOINTER_TO_INT (data), ==, value);
589 destroyed = TRUE;
592 static void
593 test_object_qdata (void)
595 GObject *obj;
596 gpointer v;
597 GQuark quark;
599 obj = g_object_new (G_TYPE_OBJECT, NULL);
601 value = 1;
602 destroyed = FALSE;
603 g_object_set_data_full (obj, "test", GINT_TO_POINTER (1), data_destroy);
604 v = g_object_get_data (obj, "test");
605 g_assert_cmpint (GPOINTER_TO_INT (v), ==, 1);
606 g_object_set_data_full (obj, "test", GINT_TO_POINTER (2), data_destroy);
607 g_assert (destroyed);
608 value = 2;
609 destroyed = FALSE;
610 v = g_object_steal_data (obj, "test");
611 g_assert_cmpint (GPOINTER_TO_INT (v), ==, 2);
612 g_assert (!destroyed);
614 value = 1;
615 destroyed = FALSE;
616 quark = g_quark_from_string ("test");
617 g_object_set_qdata_full (obj, quark, GINT_TO_POINTER (1), data_destroy);
618 v = g_object_get_qdata (obj, quark);
619 g_assert_cmpint (GPOINTER_TO_INT (v), ==, 1);
620 g_object_set_qdata_full (obj, quark, GINT_TO_POINTER (2), data_destroy);
621 g_assert (destroyed);
622 value = 2;
623 destroyed = FALSE;
624 v = g_object_steal_qdata (obj, quark);
625 g_assert_cmpint (GPOINTER_TO_INT (v), ==, 2);
626 g_assert (!destroyed);
628 g_object_set_qdata_full (obj, quark, GINT_TO_POINTER (3), data_destroy);
629 value = 3;
630 destroyed = FALSE;
631 g_object_unref (obj);
633 g_assert (destroyed);
636 typedef struct {
637 const gchar *value;
638 gint refcount;
639 } Value;
641 static gpointer
642 ref_value (gpointer value, gpointer user_data)
644 Value *v = value;
645 Value **old_value_p = user_data;
647 if (old_value_p)
648 *old_value_p = v;
650 if (v)
651 v->refcount += 1;
653 return value;
656 static void
657 unref_value (gpointer value)
659 Value *v = value;
661 v->refcount -= 1;
662 if (v->refcount == 0)
663 g_free (value);
666 static
667 Value *
668 new_value (const gchar *s)
670 Value *v;
672 v = g_new (Value, 1);
673 v->value = s;
674 v->refcount = 1;
676 return v;
679 static void
680 test_object_qdata2 (void)
682 GObject *obj;
683 Value *v, *v1, *v2, *v3, *old_val;
684 GDestroyNotify old_destroy;
685 gboolean res;
687 obj = g_object_new (G_TYPE_OBJECT, NULL);
689 v1 = new_value ("bla");
691 g_object_set_data_full (obj, "test", v1, unref_value);
693 v = g_object_get_data (obj, "test");
694 g_assert_cmpstr (v->value, ==, "bla");
695 g_assert_cmpint (v->refcount, ==, 1);
697 v = g_object_dup_data (obj, "test", ref_value, &old_val);
698 g_assert (old_val == v1);
699 g_assert_cmpstr (v->value, ==, "bla");
700 g_assert_cmpint (v->refcount, ==, 2);
701 unref_value (v);
703 v = g_object_dup_data (obj, "nono", ref_value, &old_val);
704 g_assert (old_val == NULL);
705 g_assert (v == NULL);
707 v2 = new_value ("not");
709 res = g_object_replace_data (obj, "test", v1, v2, unref_value, &old_destroy);
710 g_assert (res == TRUE);
711 g_assert (old_destroy == unref_value);
712 g_assert_cmpstr (v1->value, ==, "bla");
713 g_assert_cmpint (v1->refcount, ==, 1);
715 v = g_object_get_data (obj, "test");
716 g_assert_cmpstr (v->value, ==, "not");
717 g_assert_cmpint (v->refcount, ==, 1);
719 v3 = new_value ("xyz");
720 res = g_object_replace_data (obj, "test", v1, v3, unref_value, &old_destroy);
721 g_assert (res == FALSE);
722 g_assert_cmpstr (v2->value, ==, "not");
723 g_assert_cmpint (v2->refcount, ==, 1);
725 unref_value (v1);
727 res = g_object_replace_data (obj, "test", NULL, v3, unref_value, &old_destroy);
728 g_assert (res == FALSE);
729 g_assert_cmpstr (v2->value, ==, "not");
730 g_assert_cmpint (v2->refcount, ==, 1);
732 res = g_object_replace_data (obj, "test", v2, NULL, unref_value, &old_destroy);
733 g_assert (res == TRUE);
734 g_assert (old_destroy == unref_value);
735 g_assert_cmpstr (v2->value, ==, "not");
736 g_assert_cmpint (v2->refcount, ==, 1);
738 unref_value (v2);
740 v = g_object_get_data (obj, "test");
741 g_assert (v == NULL);
743 res = g_object_replace_data (obj, "test", NULL, v3, unref_value, &old_destroy);
744 g_assert (res == TRUE);
746 v = g_object_get_data (obj, "test");
747 g_assert (v == v3);
749 ref_value (v3, NULL);
750 g_assert_cmpint (v3->refcount, ==, 2);
751 g_object_unref (obj);
752 g_assert_cmpint (v3->refcount, ==, 1);
753 unref_value (v3);
757 main (int argc, char **argv)
759 g_test_init (&argc, &argv, NULL);
761 g_test_add_func ("/type/fundamentals", test_fundamentals);
762 g_test_add_func ("/type/qdata", test_type_qdata);
763 g_test_add_func ("/type/query", test_type_query);
764 g_test_add_func ("/type/class-private", test_class_private);
765 g_test_add_func ("/object/clear", test_clear);
766 g_test_add_func ("/object/clear-function", test_clear_function);
767 g_test_add_func ("/object/set", test_set);
768 g_test_add_func ("/object/set-function", test_set_function);
769 g_test_add_func ("/object/value", test_object_value);
770 g_test_add_func ("/object/initially-unowned", test_initially_unowned);
771 g_test_add_func ("/object/weak-pointer", test_weak_pointer);
772 g_test_add_func ("/object/weak-pointer/clear", test_weak_pointer_clear);
773 g_test_add_func ("/object/weak-pointer/clear-function", test_weak_pointer_clear_function);
774 g_test_add_func ("/object/weak-pointer/set", test_weak_pointer_set);
775 g_test_add_func ("/object/weak-pointer/set-function", test_weak_pointer_set_function);
776 g_test_add_func ("/object/weak-ref", test_weak_ref);
777 g_test_add_func ("/object/toggle-ref", test_toggle_ref);
778 g_test_add_func ("/object/qdata", test_object_qdata);
779 g_test_add_func ("/object/qdata2", test_object_qdata2);
781 return g_test_run ();