1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Imendio AB
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work 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.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 #include <glib-object.h>
25 /* This test tests the macros for defining dynamic types.
28 static GMutex sync_mutex;
29 static gboolean loaded = FALSE;
32 typedef struct _TestModule TestModule;
33 typedef struct _TestModuleClass TestModuleClass;
35 #define TEST_TYPE_MODULE (test_module_get_type ())
36 #define TEST_MODULE(module) (G_TYPE_CHECK_INSTANCE_CAST ((module), TEST_TYPE_MODULE, TestModule))
37 #define TEST_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), TEST_TYPE_MODULE, TestModuleClass))
38 #define TEST_IS_MODULE(module) (G_TYPE_CHECK_INSTANCE_TYPE ((module), TEST_TYPE_MODULE))
39 #define TEST_IS_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), TEST_TYPE_MODULE))
40 #define TEST_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), TEST_TYPE_MODULE, TestModuleClass))
41 typedef void (*TestModuleRegisterFunc) (GTypeModule *module);
45 GTypeModule parent_instance;
47 TestModuleRegisterFunc register_func;
50 struct _TestModuleClass
52 GTypeModuleClass parent_class;
55 static GType test_module_get_type (void);
58 test_module_load (GTypeModule *module)
60 TestModule *test_module = TEST_MODULE (module);
62 test_module->register_func (module);
68 test_module_unload (GTypeModule *module)
73 test_module_class_init (TestModuleClass *class)
75 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
77 module_class->load = test_module_load;
78 module_class->unload = test_module_unload;
81 static GType test_module_get_type (void)
83 static GType object_type = 0;
86 static const GTypeInfo object_info =
88 sizeof (TestModuleClass),
90 (GBaseFinalizeFunc) NULL,
91 (GClassInitFunc) test_module_class_init,
92 (GClassFinalizeFunc) NULL,
96 (GInstanceInitFunc)NULL
98 object_type = g_type_register_static (G_TYPE_TYPE_MODULE, "TestModule", &object_info, 0);
105 test_module_new (TestModuleRegisterFunc register_func)
107 TestModule *test_module = g_object_new (TEST_TYPE_MODULE, NULL);
108 GTypeModule *module = G_TYPE_MODULE (test_module);
110 test_module->register_func = register_func;
112 /* Register the types initially */
113 g_type_module_use (module);
114 g_type_module_unuse (module);
116 return G_TYPE_MODULE (module);
121 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
123 typedef GObject DynamicObject;
124 typedef struct _DynamicObjectClass DynamicObjectClass;
126 struct _DynamicObjectClass
128 GObjectClass parent_class;
132 G_DEFINE_DYNAMIC_TYPE(DynamicObject, dynamic_object, G_TYPE_OBJECT);
135 dynamic_object_class_init (DynamicObjectClass *class)
138 g_assert (loaded == FALSE);
143 dynamic_object_class_finalize (DynamicObjectClass *class)
145 g_assert (loaded == TRUE);
150 dynamic_object_init (DynamicObject *dynamic_object)
156 module_register (GTypeModule *module)
158 dynamic_object_register_type (module);
161 #define N_THREADS 100
165 ref_unref_thread (gpointer data)
168 /* first, syncronize with other threads,
170 if (g_test_verbose())
171 g_print ("WAITING!\n");
172 g_mutex_lock (&sync_mutex);
173 g_mutex_unlock (&sync_mutex);
174 if (g_test_verbose ())
175 g_print ("STARTING\n");
177 /* ref/unref the klass 10000000 times */
178 for (i = N_REFS; i; i--) {
179 if (g_test_verbose ())
182 g_type_class_unref (g_type_class_ref ((GType) data));
185 if (g_test_verbose())
186 g_print ("DONE !\n");
192 test_multithreaded_dynamic_type_init (void)
195 DynamicObjectClass *class;
196 /* Create N_THREADS threads that are going to just ref/unref a class */
197 GThread *threads[N_THREADS];
200 module = test_module_new (module_register);
201 g_assert (module != NULL);
203 /* Not loaded until we call ref for the first time */
204 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
205 g_assert (class == NULL);
208 /* pause newly created threads */
209 g_mutex_lock (&sync_mutex);
212 for (i = 0; i < N_THREADS; i++) {
213 threads[i] = g_thread_new ("test", ref_unref_thread, (gpointer) DYNAMIC_OBJECT_TYPE);
216 /* execute threads */
217 g_mutex_unlock (&sync_mutex);
219 for (i = 0; i < N_THREADS; i++) {
220 g_thread_join (threads[i]);
230 typedef struct _DynObj DynObj;
231 typedef struct _DynObjClass DynObjClass;
232 typedef struct _DynIfaceInterface DynIfaceInterface;
246 struct _DynIfaceInterface
248 GTypeInterface iface;
251 static void dyn_obj_iface_init (DynIfaceInterface *iface);
253 G_DEFINE_INTERFACE (DynIface, dyn_iface, G_TYPE_OBJECT)
255 G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynObj, dyn_obj, G_TYPE_OBJECT, 0,
256 G_IMPLEMENT_INTERFACE_DYNAMIC(dyn_iface_get_type (), dyn_obj_iface_init))
260 dyn_iface_default_init (DynIfaceInterface *iface)
262 g_object_interface_install_property (iface,
263 g_param_spec_int ("foo", NULL, NULL, 0, 100, 0, G_PARAM_READWRITE));
267 dyn_obj_iface_init (DynIfaceInterface *iface)
272 dyn_obj_init (DynObj *obj)
278 set_prop (GObject *object,
283 DynObj *obj = (DynObj *)object;
288 obj->foo = g_value_get_int (value);
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 get_prop (GObject *object,
302 DynObj *obj = (DynObj *)object;
307 g_value_set_int (value, obj->foo);
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 dyn_obj_class_init (DynObjClass *class)
318 GObjectClass *object_class = G_OBJECT_CLASS (class);
320 object_class->set_property = set_prop;
321 object_class->get_property = get_prop;
323 g_object_class_override_property (object_class, PROP_FOO, "foo");
327 dyn_obj_class_finalize (DynObjClass *class)
332 mod_register (GTypeModule *module)
334 dyn_obj_register_type (module);
338 test_dynamic_interface_properties (void)
343 module = test_module_new (mod_register);
344 g_assert (module != NULL);
346 obj = g_object_new (dyn_obj_get_type (), "foo", 1, NULL);
348 g_object_unref (obj);
355 g_test_init (&argc, &argv, NULL);
358 g_test_add_func ("/GObject/threaded-dynamic-ref-unref-init", test_multithreaded_dynamic_type_init);
359 g_test_add_func ("/GObject/dynamic-interface-properties", test_dynamic_interface_properties);