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 static GType
dynamic_object_get_type (void);
133 G_DEFINE_DYNAMIC_TYPE(DynamicObject
, dynamic_object
, G_TYPE_OBJECT
);
136 dynamic_object_class_init (DynamicObjectClass
*class)
139 g_assert (loaded
== FALSE
);
144 dynamic_object_class_finalize (DynamicObjectClass
*class)
146 g_assert (loaded
== TRUE
);
151 dynamic_object_init (DynamicObject
*dynamic_object
)
157 module_register (GTypeModule
*module
)
159 dynamic_object_register_type (module
);
162 #define N_THREADS 100
166 ref_unref_thread (gpointer data
)
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 ())
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");
193 test_multithreaded_dynamic_type_init (void)
196 DynamicObjectClass
*class;
197 /* Create N_THREADS threads that are going to just ref/unref a class */
198 GThread
*threads
[N_THREADS
];
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
);
209 /* pause newly created threads */
210 g_mutex_lock (&sync_mutex
);
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
]);
231 typedef struct _DynObj DynObj
;
232 typedef struct _DynObjClass DynObjClass
;
233 typedef struct _DynIfaceInterface DynIfaceInterface
;
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
))
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
));
270 dyn_obj_iface_init (DynIfaceInterface
*iface
)
275 dyn_obj_init (DynObj
*obj
)
281 set_prop (GObject
*object
,
286 DynObj
*obj
= (DynObj
*)object
;
291 obj
->foo
= g_value_get_int (value
);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
300 get_prop (GObject
*object
,
305 DynObj
*obj
= (DynObj
*)object
;
310 g_value_set_int (value
, obj
->foo
);
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
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");
330 dyn_obj_class_finalize (DynObjClass
*class)
335 mod_register (GTypeModule
*module
)
337 dyn_obj_register_type (module
);
341 test_dynamic_interface_properties (void)
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
);
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
);