gtestutils: Fix a typo in a documentation comment
[glib.git] / tests / gobject / ifaceinit.c
blob55ef355ffc12e9336c44001000381b8db69e8e9b
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 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, see <http://www.gnu.org/licenses/>.
18 #undef G_LOG_DOMAIN
19 #define G_LOG_DOMAIN "TestIfaceInit"
21 #undef G_DISABLE_ASSERT
22 #undef G_DISABLE_CHECKS
23 #undef G_DISABLE_CAST_CHECKS
25 #include <glib-object.h>
27 #include "testcommon.h"
29 /* What this test tests is the ability to add interfaces dynamically; in
30 * particular adding interfaces to a class while that class is being
31 * initialized.
33 * The test defines 5 interfaces:
35 * - TestIface1 is added before the class is initialized
36 * - TestIface2 is added in base_object_base_init()
37 * - TestIface3 is added in test_iface1_base_init()
38 * - TestIface4 is added in test_object_class_init()
39 * - TestIface5 is added in test_object_test_iface1_init()
40 * - TestIface6 is added after the class is initialized
43 /* All 6 interfaces actually share the same class structure, though
44 * we use separate typedefs
46 typedef struct _TestIfaceClass TestIfaceClass;
48 struct _TestIfaceClass
50 GTypeInterface base_iface;
51 guint val;
52 guint base_val;
53 guint default_val;
56 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
57 #define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
58 typedef struct _TestIface1 TestIface1;
59 typedef struct _TestIfaceClass TestIface1Class;
61 static void test_iface1_base_init (TestIface1Class *iface);
62 static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
64 static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
66 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
67 #define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
68 typedef struct _TestIface2 TestIface2;
69 typedef struct _TestIfaceClass TestIface2Class;
71 static void test_iface2_base_init (TestIface2Class *iface);
73 static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
75 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
76 #define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
77 typedef struct _TestIface3 TestIface3;
78 typedef struct _TestIfaceClass TestIface3Class;
80 static void test_iface3_base_init (TestIface3Class *iface);
82 static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
84 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
85 #define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
86 typedef struct _TestIface4 TestIface4;
87 typedef struct _TestIfaceClass TestIface4Class;
89 static void test_iface4_base_init (TestIface4Class *iface);
91 static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
93 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
94 #define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
95 typedef struct _TestIface5 TestIface5;
96 typedef struct _TestIfaceClass TestIface5Class;
98 static void test_iface5_base_init (TestIface5Class *iface);
100 static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
102 #define TEST_TYPE_IFACE6 (test_iface6_get_type ())
103 #define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
104 typedef struct _TestIface6 TestIface6;
105 typedef struct _TestIfaceClass TestIface6Class;
107 static void test_iface6_base_init (TestIface6Class *iface);
109 static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
112 * BaseObject, a parent class for TestObject
114 #define BASE_TYPE_OBJECT (base_object_get_type ())
115 typedef struct _BaseObject BaseObject;
116 typedef struct _BaseObjectClass BaseObjectClass;
118 struct _BaseObject
120 GObject parent_instance;
122 struct _BaseObjectClass
124 GObjectClass parent_class;
128 * TestObject, a parent class for TestObject
130 #define TEST_TYPE_OBJECT (test_object_get_type ())
131 typedef struct _TestObject TestObject;
132 typedef struct _TestObjectClass TestObjectClass;
134 struct _TestObject
136 BaseObject parent_instance;
138 struct _TestObjectClass
140 BaseObjectClass parent_class;
143 #define TEST_CALLED_ONCE() G_STMT_START { \
144 static gboolean called = 0; \
145 g_assert (!called); \
146 called = TRUE; \
147 } G_STMT_END
149 #define CHECK_IFACE_TWICE(iface) G_STMT_START { \
150 static guint n_calls = 0; \
151 n_calls++; \
152 g_assert (n_calls <= 2); \
153 g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type)); \
154 if (n_calls == 1) \
155 g_assert (((GTypeInterface*) iface)->g_instance_type == 0); \
156 else \
157 g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type)); \
158 } G_STMT_END
160 #define ADD_IFACE(n) G_STMT_START { \
161 GInterfaceInfo iface_info = { \
162 (GInterfaceInitFunc)test_object_test_iface##n##_init, \
163 NULL, NULL }; \
165 g_type_add_interface_static (TEST_TYPE_OBJECT, \
166 test_iface##n##_get_type (), \
167 &iface_info); \
169 } G_STMT_END
171 static gboolean base1, base2, base3, base4, base5, base6;
172 static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
174 static void test_object_test_iface1_init (TestIface1Class *iface);
175 static void test_object_test_iface2_init (TestIface1Class *iface);
176 static void test_object_test_iface3_init (TestIface3Class *iface);
177 static void test_object_test_iface4_init (TestIface4Class *iface);
178 static void test_object_test_iface5_init (TestIface5Class *iface);
179 static void test_object_test_iface6_init (TestIface6Class *iface);
181 static GType test_object_get_type (void);
183 static void
184 test_object_test_iface1_init (TestIface1Class *iface)
186 TEST_CALLED_ONCE();
188 g_assert (iface->default_val == 0x111111);
190 iface->val = 0x10001;
192 ADD_IFACE(5);
194 iface1 = TRUE;
197 static void
198 test_object_test_iface2_init (TestIface2Class *iface)
200 TEST_CALLED_ONCE();
202 iface->val = 0x20002;
204 iface2 = TRUE;
207 static void
208 test_object_test_iface3_init (TestIface3Class *iface)
210 TEST_CALLED_ONCE();
212 iface->val = 0x30003;
214 iface3 = TRUE;
217 static void
218 test_object_test_iface4_init (TestIface4Class *iface)
220 TEST_CALLED_ONCE();
222 iface->val = 0x40004;
224 iface4 = TRUE;
227 static void
228 test_object_test_iface5_init (TestIface5Class *iface)
230 TEST_CALLED_ONCE();
232 iface->val = 0x50005;
234 iface5 = TRUE;
237 static void
238 test_object_test_iface6_init (TestIface6Class *iface)
240 TEST_CALLED_ONCE();
242 iface->val = 0x60006;
244 iface6 = TRUE;
247 static void
248 test_iface1_default_init (TestIface1Class *iface,
249 gpointer class_data)
251 TEST_CALLED_ONCE();
252 g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1);
253 g_assert (iface->base_iface.g_instance_type == 0);
254 g_assert (iface->base_val == 0x110011);
255 g_assert (iface->val == 0);
256 g_assert (iface->default_val == 0);
257 iface->default_val = 0x111111;
260 static void
261 test_iface1_base_init (TestIface1Class *iface)
263 static guint n_calls = 0;
264 n_calls++;
265 g_assert (n_calls <= 2);
267 if (n_calls == 1)
269 iface->base_val = 0x110011;
270 g_assert (iface->default_val == 0);
272 else
274 g_assert (iface->base_val == 0x110011);
275 g_assert (iface->default_val == 0x111111);
278 if (n_calls == 1)
279 ADD_IFACE(3);
281 base1 = TRUE;
284 static void
285 test_iface2_base_init (TestIface2Class *iface)
287 CHECK_IFACE_TWICE (iface);
289 iface->base_val = 0x220022;
291 base2 = TRUE;
294 static void
295 test_iface3_base_init (TestIface3Class *iface)
297 CHECK_IFACE_TWICE (iface);
299 iface->base_val = 0x330033;
301 base3 = TRUE;
304 static void
305 test_iface4_base_init (TestIface4Class *iface)
307 CHECK_IFACE_TWICE (iface);
309 iface->base_val = 0x440044;
311 base4 = TRUE;
314 static void
315 test_iface5_base_init (TestIface5Class *iface)
317 CHECK_IFACE_TWICE (iface);
319 iface->base_val = 0x550055;
321 base5 = TRUE;
324 static void
325 test_iface6_base_init (TestIface6Class *iface)
327 CHECK_IFACE_TWICE (iface);
329 iface->base_val = 0x660066;
331 base6 = TRUE;
334 static void
335 base_object_base_init (BaseObjectClass *class)
337 static int n_called = 0;
338 n_called++;
340 /* The second time this is called is for TestObject */
341 if (n_called == 2)
343 ADD_IFACE(2);
345 /* No interface base init functions should have been called yet
347 g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
348 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
352 static void
353 test_object_class_init (TestObjectClass *class)
355 ADD_IFACE(4);
357 /* At this point, the base init functions for all interfaces that have
358 * been added should be called, but no interface init functions.
360 g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
361 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
364 static DEFINE_TYPE(BaseObject, base_object,
365 NULL, base_object_base_init, NULL,
366 G_TYPE_OBJECT)
367 static DEFINE_TYPE(TestObject, test_object,
368 test_object_class_init, NULL, NULL,
369 BASE_TYPE_OBJECT)
372 main (int argc,
373 char *argv[])
375 TestObject *object;
376 TestObjectClass *object_class;
377 TestIfaceClass *iface;
379 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
380 G_LOG_LEVEL_WARNING |
381 G_LOG_LEVEL_CRITICAL);
383 /* We force the interfaces to be registered in a different order
384 * than we add them, so our logic doesn't always deal with interfaces
385 * added at the end.
387 (void)TEST_TYPE_IFACE4;
388 (void)TEST_TYPE_IFACE2;
389 (void)TEST_TYPE_IFACE6;
390 (void)TEST_TYPE_IFACE5;
391 (void)TEST_TYPE_IFACE3;
392 (void)TEST_TYPE_IFACE1;
394 ADD_IFACE(1);
396 object_class = g_type_class_ref (TEST_TYPE_OBJECT);
398 ADD_IFACE(6);
400 /* All base and interface init functions should have been called
402 g_assert (base1 && base2 && base3 && base4 && base5 && base6);
403 g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
405 object = g_object_new (TEST_TYPE_OBJECT, NULL);
407 iface = TEST_IFACE1_GET_CLASS (object);
408 g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011);
409 iface = TEST_IFACE3_GET_CLASS (object);
410 g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033);
411 iface = TEST_IFACE4_GET_CLASS (object);
412 g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044);
413 iface = TEST_IFACE5_GET_CLASS (object);
414 g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055);
415 iface = TEST_IFACE6_GET_CLASS (object);
416 g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066);
418 g_type_class_unref (object_class);
420 return 0;