[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / tests / gobject / defaultiface.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 "TestDefaultIface"
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 #include "testmodule.h"
29
30 /* This test tests getting the default vtable for an interface
31  * and the initialization and finalization of such default
32  * interfaces.
33  *
34  * We test this both for static and for dynamic interfaces.
35  */
36
37 /**********************************************************************
38  * Static interface tests
39  **********************************************************************/
40
41 typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
42
43 struct _TestStaticIfaceClass
44 {
45   GTypeInterface base_iface;
46   guint val;
47 };
48
49 GType test_static_iface_get_type (void);
50 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
51
52 static void
53 test_static_iface_default_init (TestStaticIfaceClass *iface)
54 {
55   iface->val = 42;
56 }
57
58 DEFINE_IFACE (TestStaticIface, test_static_iface,
59               NULL, test_static_iface_default_init)
60
61 static void
62 test_static_iface (void)
63 {
64   TestStaticIfaceClass *static_iface;
65
66   /* Not loaded until we call ref for the first time */
67   static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
68   g_assert (static_iface == NULL);
69
70   /* Ref loads */
71   static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
72   g_assert (static_iface && static_iface->val == 42);
73
74   /* Peek then works */
75   static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
76   g_assert (static_iface && static_iface->val == 42);
77   
78   /* Unref does nothing */
79   g_type_default_interface_unref (static_iface);
80   
81   /* And peek still works */
82   static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
83   g_assert (static_iface && static_iface->val == 42);
84 }
85
86 /**********************************************************************
87  * Dynamic interface tests
88  **********************************************************************/
89
90 typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
91
92 struct _TestDynamicIfaceClass
93 {
94   GTypeInterface base_iface;
95   guint val;
96 };
97
98 static GType test_dynamic_iface_type;
99 static gboolean dynamic_iface_init = FALSE;
100
101 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
102
103 static void
104 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
105 {
106   dynamic_iface_init = TRUE;
107   iface->val = 42;
108 }
109
110 static void
111 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
112 {
113   dynamic_iface_init = FALSE;
114 }
115
116 static void
117 test_dynamic_iface_register (GTypeModule *module)
118 {
119   const GTypeInfo iface_info =                  
120     {                                                           
121       sizeof (TestDynamicIfaceClass),
122       (GBaseInitFunc)      NULL,
123       (GBaseFinalizeFunc)  NULL,                                
124       (GClassInitFunc)     test_dynamic_iface_default_init,
125       (GClassFinalizeFunc) test_dynamic_iface_default_finalize
126     };                                                  
127
128   test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
129                                                          "TestDynamicIface", &iface_info, 0);
130 }
131
132 static void
133 module_register (GTypeModule *module)
134 {
135   test_dynamic_iface_register (module);
136 }
137
138 static void
139 test_dynamic_iface (void)
140 {
141   TestDynamicIfaceClass *dynamic_iface;
142
143   test_module_new (module_register);
144
145   /* Not loaded until we call ref for the first time */
146   dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
147   g_assert (dynamic_iface == NULL);
148
149   /* Ref loads */
150   dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
151   g_assert (dynamic_iface_init);
152   g_assert (dynamic_iface && dynamic_iface->val == 42);
153
154   /* Peek then works */
155   dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
156   g_assert (dynamic_iface && dynamic_iface->val == 42);
157   
158   /* Unref causes finalize */
159   g_type_default_interface_unref (dynamic_iface);
160 #if 0
161   g_assert (!dynamic_iface_init);
162 #endif
163
164   /* Peek returns NULL */
165   dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
166 #if 0
167   g_assert (dynamic_iface == NULL);
168 #endif
169   
170   /* Ref reloads */
171   dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
172   g_assert (dynamic_iface_init);
173   g_assert (dynamic_iface && dynamic_iface->val == 42);
174
175   /* And Unref causes finalize once more*/
176   g_type_default_interface_unref (dynamic_iface);
177 #if 0
178   g_assert (!dynamic_iface_init);
179 #endif
180 }
181
182 int
183 main (int   argc,
184       char *argv[])
185 {
186   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
187                           G_LOG_LEVEL_WARNING |
188                           G_LOG_LEVEL_CRITICAL);
189
190   test_static_iface ();
191   test_dynamic_iface ();
192   
193   return 0;
194 }