1 #include <glib-object.h>
3 /* --------------------------------- */
4 /* test_object_constructor_singleton */
6 typedef GObject MySingletonObject
;
7 typedef GObjectClass MySingletonObjectClass
;
9 GType
my_singleton_object_get_type (void);
11 G_DEFINE_TYPE (MySingletonObject
, my_singleton_object
, G_TYPE_OBJECT
)
13 static MySingletonObject
*singleton
;
16 my_singleton_object_init (MySingletonObject
*obj
)
21 my_singleton_object_constructor (GType type
,
22 guint n_construct_properties
,
23 GObjectConstructParam
*construct_params
)
28 return g_object_ref (singleton
);
30 object
= G_OBJECT_CLASS (my_singleton_object_parent_class
)->
31 constructor (type
, n_construct_properties
, construct_params
);
32 singleton
= (MySingletonObject
*)object
;
38 my_singleton_object_finalize (MySingletonObject
*obj
)
42 G_OBJECT_CLASS (my_singleton_object_parent_class
)->finalize (obj
);
46 my_singleton_object_class_init (MySingletonObjectClass
*klass
)
48 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
50 object_class
->constructor
= my_singleton_object_constructor
;
51 object_class
->finalize
= my_singleton_object_finalize
;
55 test_object_constructor_singleton (void)
57 MySingletonObject
*one
, *two
, *three
;
59 one
= g_object_new (my_singleton_object_get_type (), NULL
);
60 g_assert_cmpint (G_OBJECT (one
)->ref_count
, ==, 1);
62 two
= g_object_new (my_singleton_object_get_type (), NULL
);
63 g_assert (one
== two
);
64 g_assert_cmpint (G_OBJECT (two
)->ref_count
, ==, 2);
66 three
= g_object_new (my_singleton_object_get_type (), NULL
);
67 g_assert (one
== three
);
68 g_assert_cmpint (G_OBJECT (three
)->ref_count
, ==, 3);
70 g_object_add_weak_pointer (G_OBJECT (one
), (gpointer
*)&one
);
73 g_assert (one
!= NULL
);
75 g_object_unref (three
);
78 g_assert (one
== NULL
);
81 /* ----------------------------------- */
82 /* test_object_constructor_infanticide */
84 typedef GObject MyInfanticideObject
;
85 typedef GObjectClass MyInfanticideObjectClass
;
87 GType
my_infanticide_object_get_type (void);
89 G_DEFINE_TYPE (MyInfanticideObject
, my_infanticide_object
, G_TYPE_OBJECT
)
92 my_infanticide_object_init (MyInfanticideObject
*obj
)
97 my_infanticide_object_constructor (GType type
,
98 guint n_construct_properties
,
99 GObjectConstructParam
*construct_params
)
103 object
= G_OBJECT_CLASS (my_infanticide_object_parent_class
)->
104 constructor (type
, n_construct_properties
, construct_params
);
106 g_object_unref (object
);
112 my_infanticide_object_class_init (MyInfanticideObjectClass
*klass
)
114 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
116 object_class
->constructor
= my_infanticide_object_constructor
;
120 test_object_constructor_infanticide (void)
125 g_test_bug ("661576");
127 for (i
= 0; i
< 1000; i
++)
129 g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_CRITICAL
,
130 "*finalized while still in-construction*");
131 g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_CRITICAL
,
132 "*Custom constructor*returned NULL*");
133 obj
= g_object_new (my_infanticide_object_get_type (), NULL
);
135 g_test_assert_expected_messages ();
139 /* --------------------------------- */
142 main (int argc
, char *argv
[])
144 g_test_init (&argc
, &argv
, NULL
);
145 g_test_bug_base ("http://bugzilla.gnome.org/");
147 g_test_add_func ("/object/constructor/singleton", test_object_constructor_singleton
);
148 g_test_add_func ("/object/constructor/infanticide", test_object_constructor_infanticide
);
150 return g_test_run ();