1 #include <glib-object.h>
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_VARIANT == g_variant_get_gtype ());
29 g_assert (G_TYPE_IS_DERIVED (G_TYPE_INITIALLY_UNOWNED));
31 g_assert (g_type_fundamental_next () == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
35 test_type_qdata (void)
39 g_type_set_qdata (G_TYPE_ENUM, g_quark_from_string ("bla"), "bla");
40 data = g_type_get_qdata (G_TYPE_ENUM, g_quark_from_string ("bla"));
41 g_assert_cmpstr (data, ==, "bla");
45 test_type_query (void)
49 g_type_query (G_TYPE_ENUM, &query);
50 g_assert_cmpint (query.type, ==, G_TYPE_ENUM);
51 g_assert_cmpstr (query.type_name, ==, "GEnum");
52 g_assert_cmpint (query.class_size, ==, sizeof (GEnumClass));
53 g_assert_cmpint (query.instance_size, ==, 0);
56 typedef struct _MyObject MyObject;
57 typedef struct _MyObjectClass MyObjectClass;
58 typedef struct _MyObjectClassPrivate MyObjectClassPrivate;
62 GObject parent_instance;
69 GObjectClass parent_class;
72 struct _MyObjectClassPrivate
74 gint secret_class_count;
77 G_DEFINE_TYPE_WITH_CODE (MyObject, my_object, G_TYPE_OBJECT,
78 g_type_add_class_private (g_define_type_id, sizeof (MyObjectClassPrivate)) );
81 my_object_init (MyObject *obj)
87 my_object_class_init (MyObjectClass *klass)
92 test_class_private (void)
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"));
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);
126 g_assert_cmpint (tmp->ref_count, ==, 1);
127 g_assert (o == NULL);
129 g_object_unref (tmp);
133 test_clear_function (void)
135 volatile GObject *o = NULL;
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);
155 test_object_value (void)
159 GValue value = { 0, };
161 g_value_init (&value, G_TYPE_OBJECT);
163 v = g_object_new (G_TYPE_OBJECT, NULL);
164 g_value_take_object (&value, v);
166 v2 = g_value_get_object (&value);
169 v2 = g_value_dup_object (&value);
170 g_assert (v2 == v); /* objects use ref/unref for copy/free */
173 g_value_unset (&value);
177 test_initially_unowned (void)
181 obj = g_object_new (G_TYPE_INITIALLY_UNOWNED, NULL);
182 g_assert (g_object_is_floating (obj));
183 g_assert_cmpint (obj->ref_count, ==, 1);
185 g_object_ref_sink (obj);
186 g_assert (!g_object_is_floating (obj));
187 g_assert_cmpint (obj->ref_count, ==, 1);
189 g_object_ref_sink (obj);
190 g_assert (!g_object_is_floating (obj));
191 g_assert_cmpint (obj->ref_count, ==, 2);
193 g_object_unref (obj);
194 g_assert_cmpint (obj->ref_count, ==, 1);
196 g_object_force_floating (obj);
197 g_assert (g_object_is_floating (obj));
198 g_assert_cmpint (obj->ref_count, ==, 1);
200 g_object_ref_sink (obj);
201 g_object_unref (obj);
205 test_weak_pointer (void)
211 weak = weak2 = obj = g_object_new (G_TYPE_OBJECT, NULL);
212 g_assert_cmpint (obj->ref_count, ==, 1);
214 g_object_add_weak_pointer (obj, &weak);
215 g_object_add_weak_pointer (obj, &weak2);
216 g_assert_cmpint (obj->ref_count, ==, 1);
217 g_assert (weak == obj);
218 g_assert (weak2 == obj);
220 g_object_remove_weak_pointer (obj, &weak2);
221 g_assert_cmpint (obj->ref_count, ==, 1);
222 g_assert (weak == obj);
223 g_assert (weak2 == obj);
225 g_object_unref (obj);
226 g_assert (weak == NULL);
227 g_assert (weak2 == obj);
231 main (int argc, char **argv)
233 g_test_init (&argc, &argv, NULL);
237 g_test_add_func ("/type/fundamentals", test_fundamentals);
238 g_test_add_func ("/type/qdata", test_type_qdata);
239 g_test_add_func ("/type/query", test_type_query);
240 g_test_add_func ("/type/class-private", test_class_private);
241 g_test_add_func ("/object/clear", test_clear);
242 g_test_add_func ("/object/clear-function", test_clear_function);
243 g_test_add_func ("/object/value", test_object_value);
244 g_test_add_func ("/object/initially-unowned", test_initially_unowned);
245 g_test_add_func ("/object/weak-pointer", test_weak_pointer);
247 return g_test_run ();