[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / tests / gobject / ifaceinit.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001, 2003 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #undef  G_LOG_DOMAIN
19 #define G_LOG_DOMAIN "TestIfaceInit"
20
21 #undef G_DISABLE_ASSERT
22 #undef G_DISABLE_CHECKS
23 #undef G_DISABLE_CAST_CHECKS
24
25 #include <glib-object.h>
26
27 #include "testcommon.h"
28
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.
32  *
33  * The test defines 5 interfaces:
34  * 
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
41  */
42
43 /* All 6 interfaces actually share the same class structure, though
44  * we use separate typedefs
45  */
46 typedef struct _TestIfaceClass TestIfaceClass;
47
48 struct _TestIfaceClass
49 {
50   GTypeInterface base_iface;
51   guint val;
52   guint base_val;
53   guint default_val;
54 };
55
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;
60
61 static void test_iface1_base_init    (TestIface1Class *iface);
62 static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
63
64 static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
65
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;
70
71 static void test_iface2_base_init (TestIface2Class *iface);
72
73 static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
74
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;
79
80 static void  test_iface3_base_init (TestIface3Class *iface);
81
82 static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
83
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;
88
89 static void  test_iface4_base_init (TestIface4Class *iface);
90
91 static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
92
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;
97
98 static void  test_iface5_base_init (TestIface5Class *iface);
99
100 static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
101
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;
106
107 static void  test_iface6_base_init (TestIface6Class *iface);
108
109 static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
110
111 /*
112  * BaseObject, a parent class for TestObject
113  */
114 #define BASE_TYPE_OBJECT          (base_object_get_type ())
115 typedef struct _BaseObject        BaseObject;
116 typedef struct _BaseObjectClass   BaseObjectClass;
117
118 struct _BaseObject
119 {
120   GObject parent_instance;
121 };
122 struct _BaseObjectClass
123 {
124   GObjectClass parent_class;
125 };
126
127 /*
128  * TestObject, a parent class for TestObject
129  */
130 #define TEST_TYPE_OBJECT          (test_object_get_type ())
131 typedef struct _TestObject        TestObject;
132 typedef struct _TestObjectClass   TestObjectClass;
133
134 struct _TestObject
135 {
136   BaseObject parent_instance;
137 };
138 struct _TestObjectClass
139 {
140   BaseObjectClass parent_class;
141 };
142
143 #define TEST_CALLED_ONCE() G_STMT_START { \
144   static gboolean called = 0;           \
145   g_assert (!called);                   \
146   called = TRUE;                        \
147 } G_STMT_END
148
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
159
160 #define ADD_IFACE(n)  G_STMT_START {                            \
161   GInterfaceInfo iface_info = {                         \
162     (GInterfaceInitFunc)test_object_test_iface##n##_init,       \
163     NULL, NULL };                                               \
164                                                                 \
165   g_type_add_interface_static (TEST_TYPE_OBJECT,                \
166                                test_iface##n##_get_type (),     \
167                                &iface_info);                    \
168                                                                 \
169 } G_STMT_END
170
171 static gboolean base1, base2, base3, base4, base5, base6;
172 static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
173
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);
180
181 static GType test_object_get_type (void);
182
183 static void
184 test_object_test_iface1_init (TestIface1Class *iface)
185 {
186   TEST_CALLED_ONCE();
187
188   g_assert (iface->default_val == 0x111111);
189
190   iface->val = 0x10001;
191
192   ADD_IFACE(5);
193
194   iface1 = TRUE;
195 }
196
197 static void
198 test_object_test_iface2_init (TestIface2Class *iface)
199 {
200   TEST_CALLED_ONCE();
201   
202   iface->val = 0x20002;
203   
204   iface2 = TRUE;
205 }
206
207 static void
208 test_object_test_iface3_init (TestIface3Class *iface)
209 {
210   TEST_CALLED_ONCE();
211   
212   iface->val = 0x30003;
213   
214   iface3 = TRUE;
215 }
216
217 static void
218 test_object_test_iface4_init (TestIface4Class *iface)
219 {
220   TEST_CALLED_ONCE();
221
222   iface->val = 0x40004;
223   
224   iface4 = TRUE;
225 }
226
227 static void
228 test_object_test_iface5_init (TestIface5Class *iface)
229 {
230   TEST_CALLED_ONCE();
231
232   iface->val = 0x50005;
233   
234   iface5 = TRUE;
235 }
236
237 static void
238 test_object_test_iface6_init (TestIface6Class *iface)
239 {
240   TEST_CALLED_ONCE();
241
242   iface->val = 0x60006;
243   
244   iface6 = TRUE;
245 }
246
247 static void
248 test_iface1_default_init (TestIface1Class *iface,
249                           gpointer         class_data)
250 {
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;
258 }
259
260 static void
261 test_iface1_base_init (TestIface1Class *iface)
262 {
263   static guint n_calls = 0;
264   n_calls++;
265   g_assert (n_calls <= 2);
266
267   if (n_calls == 1)
268     {
269       iface->base_val = 0x110011;
270       g_assert (iface->default_val == 0);
271     }
272   else
273     {
274       g_assert (iface->base_val == 0x110011);
275       g_assert (iface->default_val == 0x111111);
276     }
277
278   if (n_calls == 1)
279     ADD_IFACE(3);
280   
281   base1 = TRUE;
282 }
283
284 static void
285 test_iface2_base_init (TestIface2Class *iface)
286 {
287   CHECK_IFACE_TWICE (iface);
288
289   iface->base_val = 0x220022;
290   
291   base2 = TRUE;
292 }
293
294 static void
295 test_iface3_base_init (TestIface3Class *iface)
296 {
297   CHECK_IFACE_TWICE (iface);
298
299   iface->base_val = 0x330033;
300   
301   base3 = TRUE;
302 }
303
304 static void
305 test_iface4_base_init (TestIface4Class *iface)
306 {
307   CHECK_IFACE_TWICE (iface);
308
309   iface->base_val = 0x440044;
310
311   base4 = TRUE;
312 }
313
314 static void
315 test_iface5_base_init (TestIface5Class *iface)
316 {
317   CHECK_IFACE_TWICE (iface);
318
319   iface->base_val = 0x550055;
320
321   base5 = TRUE;
322 }
323
324 static void
325 test_iface6_base_init (TestIface6Class *iface)
326 {
327   CHECK_IFACE_TWICE (iface);
328
329   iface->base_val = 0x660066;
330   
331   base6 = TRUE;
332 }
333
334 static void
335 base_object_base_init (BaseObjectClass *class)
336 {
337   static int n_called = 0;
338   n_called++;
339   
340   /* The second time this is called is for TestObject */
341   if (n_called == 2)
342     {
343       ADD_IFACE(2);
344       
345       /* No interface base init functions should have been called yet
346        */
347       g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
348       g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
349     }
350 }
351
352 static void
353 test_object_class_init (TestObjectClass *class)
354 {
355   ADD_IFACE(4);
356
357   /* At this point, the base init functions for all interfaces that have
358    * been added should be called, but no interface init functions.
359    */
360   g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
361   g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
362 }
363
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)
370
371 int
372 main (int   argc,
373       char *argv[])
374 {
375   TestObject *object;
376   TestObjectClass *object_class;
377   TestIfaceClass *iface;
378         
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);
382
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.
386    */
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;
393
394   ADD_IFACE(1);
395
396   object_class = g_type_class_ref (TEST_TYPE_OBJECT);
397
398   ADD_IFACE(6);
399
400   /* All base and interface init functions should have been called
401    */
402   g_assert (base1 && base2 && base3 && base4 && base5 && base6);
403   g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
404   
405   object = g_object_new (TEST_TYPE_OBJECT, NULL);
406
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);
417
418   g_type_class_unref (object_class);
419
420   return 0;
421 }