1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Imendio AB
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
25 #include <glib-object.h>
27 /* This test tests the macros for defining dynamic types.
30 static GMutex sync_mutex;
31 static gboolean loaded = FALSE;
34 typedef struct _TestModule TestModule;
35 typedef struct _TestModuleClass TestModuleClass;
37 #define TEST_TYPE_MODULE (test_module_get_type ())
38 #define TEST_MODULE(module) (G_TYPE_CHECK_INSTANCE_CAST ((module), TEST_TYPE_MODULE, TestModule))
39 #define TEST_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), TEST_TYPE_MODULE, TestModuleClass))
40 #define TEST_IS_MODULE(module) (G_TYPE_CHECK_INSTANCE_TYPE ((module), TEST_TYPE_MODULE))
41 #define TEST_IS_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), TEST_TYPE_MODULE))
42 #define TEST_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), TEST_TYPE_MODULE, TestModuleClass))
43 typedef void (*TestModuleRegisterFunc) (GTypeModule *module);
47 GTypeModule parent_instance;
49 TestModuleRegisterFunc register_func;
52 struct _TestModuleClass
54 GTypeModuleClass parent_class;
57 static GType test_module_get_type (void);
60 test_module_load (GTypeModule *module)
62 TestModule *test_module = TEST_MODULE (module);
64 test_module->register_func (module);
70 test_module_unload (GTypeModule *module)
75 test_module_class_init (TestModuleClass *class)
77 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
79 module_class->load = test_module_load;
80 module_class->unload = test_module_unload;
83 static GType test_module_get_type (void)
85 static GType object_type = 0;
88 static const GTypeInfo object_info =
90 sizeof (TestModuleClass),
92 (GBaseFinalizeFunc) NULL,
93 (GClassInitFunc) test_module_class_init,
94 (GClassFinalizeFunc) NULL,
98 (GInstanceInitFunc)NULL,
101 object_type = g_type_register_static (G_TYPE_TYPE_MODULE, "TestModule", &object_info, 0);
108 test_module_new (TestModuleRegisterFunc register_func)
110 TestModule *test_module = g_object_new (TEST_TYPE_MODULE, NULL);
111 GTypeModule *module = G_TYPE_MODULE (test_module);
113 test_module->register_func = register_func;
115 /* Register the types initially */
116 g_type_module_use (module);
117 g_type_module_unuse (module);
119 return G_TYPE_MODULE (module);
124 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
126 typedef GObject DynamicObject;
127 typedef struct _DynamicObjectClass DynamicObjectClass;
129 struct _DynamicObjectClass
131 GObjectClass parent_class;
135 static GType dynamic_object_get_type (void);
136 G_DEFINE_DYNAMIC_TYPE(DynamicObject, dynamic_object, G_TYPE_OBJECT)
139 dynamic_object_class_init (DynamicObjectClass *class)
142 g_assert (loaded == FALSE);
147 dynamic_object_class_finalize (DynamicObjectClass *class)
149 g_assert (loaded == TRUE);
154 dynamic_object_init (DynamicObject *dynamic_object)
160 module_register (GTypeModule *module)
162 dynamic_object_register_type (module);
165 #define N_THREADS 100
169 ref_unref_thread (gpointer data)
172 /* first, synchronize with other threads,
174 if (g_test_verbose())
175 g_printerr ("WAITING!\n");
176 g_mutex_lock (&sync_mutex);
177 g_mutex_unlock (&sync_mutex);
178 if (g_test_verbose ())
179 g_printerr ("STARTING\n");
181 /* ref/unref the klass 10000000 times */
182 for (i = N_REFS; i; i--) {
183 if (g_test_verbose ())
185 g_printerr ("%d\n", i);
186 g_type_class_unref (g_type_class_ref ((GType) data));
189 if (g_test_verbose())
190 g_printerr ("DONE !\n");
196 test_multithreaded_dynamic_type_init (void)
199 DynamicObjectClass *class;
200 /* Create N_THREADS threads that are going to just ref/unref a class */
201 GThread *threads[N_THREADS];
204 module = test_module_new (module_register);
205 g_assert (module != NULL);
207 /* Not loaded until we call ref for the first time */
208 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
209 g_assert (class == NULL);
212 /* pause newly created threads */
213 g_mutex_lock (&sync_mutex);
216 for (i = 0; i < N_THREADS; i++) {
217 threads[i] = g_thread_new ("test", ref_unref_thread, (gpointer) DYNAMIC_OBJECT_TYPE);
220 /* execute threads */
221 g_mutex_unlock (&sync_mutex);
223 for (i = 0; i < N_THREADS; i++) {
224 g_thread_join (threads[i]);
234 typedef struct _DynObj DynObj;
235 typedef struct _DynObjClass DynObjClass;
236 typedef struct _DynIfaceInterface DynIfaceInterface;
250 struct _DynIfaceInterface
252 GTypeInterface iface;
255 static void dyn_obj_iface_init (DynIfaceInterface *iface);
257 static GType dyn_iface_get_type (void);
258 G_DEFINE_INTERFACE (DynIface, dyn_iface, G_TYPE_OBJECT)
260 static GType dyn_obj_get_type (void);
261 G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynObj, dyn_obj, G_TYPE_OBJECT, 0,
262 G_IMPLEMENT_INTERFACE_DYNAMIC(dyn_iface_get_type (), dyn_obj_iface_init))
266 dyn_iface_default_init (DynIfaceInterface *iface)
268 g_object_interface_install_property (iface,
269 g_param_spec_int ("foo", NULL, NULL, 0, 100, 0, G_PARAM_READWRITE));
273 dyn_obj_iface_init (DynIfaceInterface *iface)
278 dyn_obj_init (DynObj *obj)
284 set_prop (GObject *object,
289 DynObj *obj = (DynObj *)object;
294 obj->foo = g_value_get_int (value);
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 get_prop (GObject *object,
308 DynObj *obj = (DynObj *)object;
313 g_value_set_int (value, obj->foo);
316 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 dyn_obj_class_init (DynObjClass *class)
324 GObjectClass *object_class = G_OBJECT_CLASS (class);
326 object_class->set_property = set_prop;
327 object_class->get_property = get_prop;
329 g_object_class_override_property (object_class, PROP_FOO, "foo");
333 dyn_obj_class_finalize (DynObjClass *class)
338 mod_register (GTypeModule *module)
340 dyn_obj_register_type (module);
344 test_dynamic_interface_properties (void)
350 module = test_module_new (mod_register);
351 g_assert (module != NULL);
353 obj = g_object_new (dyn_obj_get_type (), "foo", 1, NULL);
354 g_object_get (obj, "foo", &val, NULL);
355 g_assert_cmpint (val, ==, 1);
357 g_object_unref (obj);
364 g_test_init (&argc, &argv, NULL);
366 g_test_add_func ("/GObject/threaded-dynamic-ref-unref-init", test_multithreaded_dynamic_type_init);
367 g_test_add_func ("/GObject/dynamic-interface-properties", test_dynamic_interface_properties);