Add myself to the DOAP for GLib
[glib.git] / gobject / tests / dynamictests.c
blob59a5aebed9bc971e8428b7f0eb210edb6818acd3
1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Imendio AB
3 * Authors: Tim Janik
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.
22 #include <glib.h>
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;
31 /* MODULE */
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);
43 struct _TestModule
45 GTypeModule parent_instance;
47 TestModuleRegisterFunc register_func;
50 struct _TestModuleClass
52 GTypeModuleClass parent_class;
55 static GType test_module_get_type (void);
57 static gboolean
58 test_module_load (GTypeModule *module)
60 TestModule *test_module = TEST_MODULE (module);
62 test_module->register_func (module);
64 return TRUE;
67 static void
68 test_module_unload (GTypeModule *module)
72 static void
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;
85 if (!object_type) {
86 static const GTypeInfo object_info =
88 sizeof (TestModuleClass),
89 (GBaseInitFunc) NULL,
90 (GBaseFinalizeFunc) NULL,
91 (GClassInitFunc) test_module_class_init,
92 (GClassFinalizeFunc) NULL,
93 NULL,
94 sizeof (TestModule),
96 (GInstanceInitFunc)NULL
98 object_type = g_type_register_static (G_TYPE_TYPE_MODULE, "TestModule", &object_info, 0);
100 return object_type;
104 static GTypeModule *
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;
129 guint val;
132 static GType dynamic_object_get_type (void);
133 G_DEFINE_DYNAMIC_TYPE(DynamicObject, dynamic_object, G_TYPE_OBJECT)
135 static void
136 dynamic_object_class_init (DynamicObjectClass *class)
138 class->val = 42;
139 g_assert (loaded == FALSE);
140 loaded = TRUE;
143 static void
144 dynamic_object_class_finalize (DynamicObjectClass *class)
146 g_assert (loaded == TRUE);
147 loaded = FALSE;
150 static void
151 dynamic_object_init (DynamicObject *dynamic_object)
156 static void
157 module_register (GTypeModule *module)
159 dynamic_object_register_type (module);
162 #define N_THREADS 100
163 #define N_REFS 10000
165 static gpointer
166 ref_unref_thread (gpointer data)
168 gint i;
169 /* first, syncronize with other threads,
171 if (g_test_verbose())
172 g_printerr ("WAITING!\n");
173 g_mutex_lock (&sync_mutex);
174 g_mutex_unlock (&sync_mutex);
175 if (g_test_verbose ())
176 g_printerr ("STARTING\n");
178 /* ref/unref the klass 10000000 times */
179 for (i = N_REFS; i; i--) {
180 if (g_test_verbose ())
181 if (i % 10)
182 g_printerr ("%d\n", i);
183 g_type_class_unref (g_type_class_ref ((GType) data));
186 if (g_test_verbose())
187 g_printerr ("DONE !\n");
189 return NULL;
192 static void
193 test_multithreaded_dynamic_type_init (void)
195 GTypeModule *module;
196 DynamicObjectClass *class;
197 /* Create N_THREADS threads that are going to just ref/unref a class */
198 GThread *threads[N_THREADS];
199 guint i;
201 module = test_module_new (module_register);
202 g_assert (module != NULL);
204 /* Not loaded until we call ref for the first time */
205 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
206 g_assert (class == NULL);
207 g_assert (!loaded);
209 /* pause newly created threads */
210 g_mutex_lock (&sync_mutex);
212 /* create threads */
213 for (i = 0; i < N_THREADS; i++) {
214 threads[i] = g_thread_new ("test", ref_unref_thread, (gpointer) DYNAMIC_OBJECT_TYPE);
217 /* execute threads */
218 g_mutex_unlock (&sync_mutex);
220 for (i = 0; i < N_THREADS; i++) {
221 g_thread_join (threads[i]);
225 enum
227 PROP_0,
228 PROP_FOO
231 typedef struct _DynObj DynObj;
232 typedef struct _DynObjClass DynObjClass;
233 typedef struct _DynIfaceInterface DynIfaceInterface;
235 struct _DynObj
237 GObject obj;
239 gint foo;
242 struct _DynObjClass
244 GObjectClass class;
247 struct _DynIfaceInterface
249 GTypeInterface iface;
252 static void dyn_obj_iface_init (DynIfaceInterface *iface);
254 static GType dyn_iface_get_type (void);
255 G_DEFINE_INTERFACE (DynIface, dyn_iface, G_TYPE_OBJECT)
257 static GType dyn_obj_get_type (void);
258 G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynObj, dyn_obj, G_TYPE_OBJECT, 0,
259 G_IMPLEMENT_INTERFACE_DYNAMIC(dyn_iface_get_type (), dyn_obj_iface_init))
262 static void
263 dyn_iface_default_init (DynIfaceInterface *iface)
265 g_object_interface_install_property (iface,
266 g_param_spec_int ("foo", NULL, NULL, 0, 100, 0, G_PARAM_READWRITE));
269 static void
270 dyn_obj_iface_init (DynIfaceInterface *iface)
274 static void
275 dyn_obj_init (DynObj *obj)
277 obj->foo = 0;
280 static void
281 set_prop (GObject *object,
282 guint prop_id,
283 const GValue *value,
284 GParamSpec *pspec)
286 DynObj *obj = (DynObj *)object;
288 switch (prop_id)
290 case PROP_FOO:
291 obj->foo = g_value_get_int (value);
292 break;
293 default:
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 break;
299 static void
300 get_prop (GObject *object,
301 guint prop_id,
302 GValue *value,
303 GParamSpec *pspec)
305 DynObj *obj = (DynObj *)object;
307 switch (prop_id)
309 case PROP_FOO:
310 g_value_set_int (value, obj->foo);
311 break;
312 default:
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314 break;
318 static void
319 dyn_obj_class_init (DynObjClass *class)
321 GObjectClass *object_class = G_OBJECT_CLASS (class);
323 object_class->set_property = set_prop;
324 object_class->get_property = get_prop;
326 g_object_class_override_property (object_class, PROP_FOO, "foo");
329 static void
330 dyn_obj_class_finalize (DynObjClass *class)
334 static void
335 mod_register (GTypeModule *module)
337 dyn_obj_register_type (module);
340 static void
341 test_dynamic_interface_properties (void)
343 GTypeModule *module;
344 DynObj *obj;
345 gint val;
347 module = test_module_new (mod_register);
348 g_assert (module != NULL);
350 obj = g_object_new (dyn_obj_get_type (), "foo", 1, NULL);
351 g_object_get (obj, "foo", &val, NULL);
352 g_assert_cmpint (val, ==, 1);
354 g_object_unref (obj);
358 main (int argc,
359 char *argv[])
361 g_test_init (&argc, &argv, NULL);
363 g_test_add_func ("/GObject/threaded-dynamic-ref-unref-init", test_multithreaded_dynamic_type_init);
364 g_test_add_func ("/GObject/dynamic-interface-properties", test_dynamic_interface_properties);
366 return g_test_run();