Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / tests / gobject / defaultiface.c
blobeccb79ced2879769dbd5c719919b9d16d8fa28c7
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.1 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 "TestDefaultIface"
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"
28 #include "testmodule.h"
30 /* This test tests getting the default vtable for an interface
31 * and the initialization and finalization of such default
32 * interfaces.
34 * We test this both for static and for dynamic interfaces.
37 /**********************************************************************
38 * Static interface tests
39 **********************************************************************/
41 typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
43 struct _TestStaticIfaceClass
45 GTypeInterface base_iface;
46 guint val;
49 GType test_static_iface_get_type (void);
50 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
52 static void
53 test_static_iface_default_init (TestStaticIfaceClass *iface)
55 iface->val = 42;
58 DEFINE_IFACE (TestStaticIface, test_static_iface,
59 NULL, test_static_iface_default_init)
61 static void
62 test_static_iface (void)
64 TestStaticIfaceClass *static_iface;
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);
70 /* Ref loads */
71 static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
72 g_assert (static_iface && static_iface->val == 42);
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);
78 /* Unref does nothing */
79 g_type_default_interface_unref (static_iface);
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);
86 /**********************************************************************
87 * Dynamic interface tests
88 **********************************************************************/
90 typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
92 struct _TestDynamicIfaceClass
94 GTypeInterface base_iface;
95 guint val;
98 static GType test_dynamic_iface_type;
99 static gboolean dynamic_iface_init = FALSE;
101 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
103 static void
104 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
106 dynamic_iface_init = TRUE;
107 iface->val = 42;
110 static void
111 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
113 dynamic_iface_init = FALSE;
116 static void
117 test_dynamic_iface_register (GTypeModule *module)
119 const GTypeInfo iface_info =
121 sizeof (TestDynamicIfaceClass),
122 (GBaseInitFunc) NULL,
123 (GBaseFinalizeFunc) NULL,
124 (GClassInitFunc) test_dynamic_iface_default_init,
125 (GClassFinalizeFunc) test_dynamic_iface_default_finalize
128 test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
129 "TestDynamicIface", &iface_info, 0);
132 static void
133 module_register (GTypeModule *module)
135 test_dynamic_iface_register (module);
138 static void
139 test_dynamic_iface (void)
141 TestDynamicIfaceClass *dynamic_iface;
143 test_module_new (module_register);
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);
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);
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);
158 /* Unref causes finalize */
159 g_type_default_interface_unref (dynamic_iface);
160 #if 0
161 g_assert (!dynamic_iface_init);
162 #endif
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
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);
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
183 main (int argc,
184 char *argv[])
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);
190 test_static_iface ();
191 test_dynamic_iface ();
193 return 0;