|
1 /* Copyright (C) 2005 Imendio AB |
|
2 * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
|
3 * This software is provided "as is"; redistribution and modification |
|
4 * is permitted, provided that the following disclaimer is retained. |
|
5 * |
|
6 * This software is distributed in the hope that it will be useful, |
|
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
9 * In no event shall the authors or contributors be liable for any |
|
10 * direct, indirect, incidental, special, exemplary, or consequential |
|
11 * damages (including, but not limited to, procurement of substitute |
|
12 * goods or services; loss of use, data, or profits; or business |
|
13 * interruption) however caused and on any theory of liability, whether |
|
14 * in contract, strict liability, or tort (including negligence or |
|
15 * otherwise) arising in any way out of the use of this software, even |
|
16 * if advised of the possibility of such damage. |
|
17 */ |
|
18 |
|
19 #include <glib-object.h> |
|
20 #include <unistd.h> |
|
21 |
|
22 #ifdef SYMBIAN |
|
23 #include <glib_global.h> |
|
24 #include "mrt2_glib2_test.h" |
|
25 #endif /*SYMBIAN*/ |
|
26 |
|
27 #define TEST_POINTER1 ((gpointer) 47) |
|
28 #define TEST_POINTER2 ((gpointer) 49) |
|
29 #define TEST_POINTER3 ((gpointer) 77) |
|
30 #define TEST_INT1 (-77) |
|
31 #define TEST_INT2 (78) |
|
32 #define TEST_INT3 (99) |
|
33 |
|
34 /* --- GTest class --- */ |
|
35 typedef struct { |
|
36 GObject object; |
|
37 gint value; |
|
38 gpointer test_pointer1; |
|
39 gpointer test_pointer2; |
|
40 } GTest; |
|
41 typedef struct { |
|
42 GObjectClass parent_class; |
|
43 void (*test_signal1) (GTest * test, gint an_int); |
|
44 void (*test_signal2) (GTest * test, gint an_int); |
|
45 void (*test_signal3) (GTest * test, gint an_int); |
|
46 } GTestClass; |
|
47 |
|
48 #define G_TYPE_TEST (g_test_get_type ()) |
|
49 #define G_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest)) |
|
50 #define G_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST)) |
|
51 #define G_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass)) |
|
52 #define G_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST)) |
|
53 #define G_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass)) |
|
54 |
|
55 G_DEFINE_TYPE (GTest, g_test, G_TYPE_OBJECT); |
|
56 |
|
57 /* --- variables --- */ |
|
58 static volatile gboolean stopping = FALSE; |
|
59 static guint test_signal1 = 0; |
|
60 static guint test_signal2 = 0; |
|
61 static guint test_signal3 = 0; |
|
62 static gboolean seen_signal_handler = FALSE; |
|
63 static gboolean seen_cleanup = FALSE; |
|
64 static gboolean seen_test_int1 = FALSE; |
|
65 static gboolean seen_test_int2 = FALSE; |
|
66 static gboolean seen_test_int3 = FALSE; |
|
67 static gboolean seen_thread1 = FALSE; |
|
68 static gboolean seen_thread2 = FALSE; |
|
69 |
|
70 /* --- functions --- */ |
|
71 static void |
|
72 g_test_init (GTest * test) |
|
73 { |
|
74 //g_print ("init %p\n", test); |
|
75 |
|
76 test->value = 0; |
|
77 test->test_pointer1 = TEST_POINTER1; |
|
78 test->test_pointer2 = TEST_POINTER2; |
|
79 } |
|
80 |
|
81 enum { |
|
82 ARG_0, |
|
83 ARG_TEST_PROP |
|
84 }; |
|
85 |
|
86 static void |
|
87 g_test_set_property (GObject *object, |
|
88 guint prop_id, |
|
89 const GValue *value, |
|
90 GParamSpec *pspec) |
|
91 { |
|
92 GTest *test = G_TEST (object); |
|
93 switch (prop_id) |
|
94 { |
|
95 case ARG_TEST_PROP: |
|
96 test->value = g_value_get_int (value); |
|
97 break; |
|
98 default: |
|
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
100 break; |
|
101 } |
|
102 } |
|
103 |
|
104 static void |
|
105 g_test_get_property (GObject *object, |
|
106 guint prop_id, |
|
107 GValue *value, |
|
108 GParamSpec *pspec) |
|
109 { |
|
110 GTest *test = G_TEST (object); |
|
111 switch (prop_id) |
|
112 { |
|
113 case ARG_TEST_PROP: |
|
114 g_value_set_int (value, test->value); |
|
115 break; |
|
116 default: |
|
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
118 break; |
|
119 } |
|
120 } |
|
121 |
|
122 static void |
|
123 g_test_test_signal2 (GTest *test, |
|
124 gint an_int) |
|
125 { |
|
126 } |
|
127 |
|
128 static void |
|
129 g_test_emit_test_signal1 (GTest *test, |
|
130 gint vint) |
|
131 { |
|
132 g_signal_emit (G_OBJECT (test), test_signal1, 0, vint); |
|
133 } |
|
134 |
|
135 static void |
|
136 g_test_emit_test_signal2 (GTest *test, |
|
137 gint vint) |
|
138 { |
|
139 g_signal_emit (G_OBJECT (test), test_signal2, 0, vint); |
|
140 } |
|
141 |
|
142 static void |
|
143 g_test_emit_test_signal3 (GTest *test, |
|
144 gint vint) |
|
145 { |
|
146 g_signal_emit (G_OBJECT (test), test_signal3, 0, vint); |
|
147 } |
|
148 static void |
|
149 g_test_class_init (GTestClass *klass) |
|
150 { |
|
151 GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
|
152 |
|
153 gobject_class->set_property = g_test_set_property; |
|
154 gobject_class->get_property = g_test_get_property; |
|
155 |
|
156 test_signal1 = g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, |
|
157 G_STRUCT_OFFSET (GTestClass, test_signal1), NULL, NULL, |
|
158 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); |
|
159 test_signal2 = g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, |
|
160 G_STRUCT_OFFSET (GTestClass, test_signal2), NULL, NULL, |
|
161 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); |
|
162 |
|
163 test_signal3 = g_signal_new ("test-signal3", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, |
|
164 G_STRUCT_OFFSET (GTestClass, test_signal3), NULL, NULL, |
|
165 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); |
|
166 |
|
167 |
|
168 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TEST_PROP, |
|
169 g_param_spec_int ("test-prop", "Test Prop", "Test property", |
|
170 0, 1, 0, G_PARAM_READWRITE)); |
|
171 klass->test_signal2 = g_test_test_signal2; |
|
172 klass->test_signal3 = g_test_test_signal2; |
|
173 } |
|
174 |
|
175 static inline guint32 |
|
176 quick_rand32 (void) |
|
177 { |
|
178 static guint32 accu = 2147483563; |
|
179 accu = 1664525 * accu + 1013904223; |
|
180 return accu; |
|
181 } |
|
182 |
|
183 static void |
|
184 test_closure (GClosure *closure) |
|
185 { |
|
186 /* try to produce high contention in closure->ref_count */ |
|
187 guint i = 0, n = quick_rand32() % 10; |
|
188 for (i = 0; i < n; i++) |
|
189 g_closure_ref (closure); |
|
190 g_closure_sink (closure); /* NOP */ |
|
191 for (i = 0; i < n; i++) |
|
192 g_closure_unref (closure); |
|
193 } |
|
194 |
|
195 static gpointer |
|
196 thread1_main (gpointer data) |
|
197 { |
|
198 GClosure *closure = data; |
|
199 do |
|
200 { |
|
201 static guint count = 0; |
|
202 test_closure (closure); |
|
203 //if (++count % 10 == 0) |
|
204 { |
|
205 //g_printerr ("c"); |
|
206 g_thread_yield(); /* force context switch */ |
|
207 seen_thread1 = TRUE; |
|
208 } |
|
209 }while (!stopping); |
|
210 return NULL; |
|
211 } |
|
212 |
|
213 static gpointer |
|
214 thread2_main (gpointer data) |
|
215 { |
|
216 GClosure *closure = data; |
|
217 do |
|
218 { |
|
219 static guint count = 0; |
|
220 test_closure (closure); |
|
221 //if (++count % 10 == 0) |
|
222 { |
|
223 //g_printerr ("C"); |
|
224 g_thread_yield(); /* force context switch */ |
|
225 seen_thread2 = TRUE; |
|
226 } |
|
227 } while (!stopping); |
|
228 |
|
229 return NULL; |
|
230 } |
|
231 |
|
232 static void |
|
233 test_signal_handler (GTest *test, |
|
234 gint vint, |
|
235 gpointer data) |
|
236 { |
|
237 g_assert (data == TEST_POINTER2); |
|
238 g_assert (test->test_pointer1 == TEST_POINTER1); |
|
239 seen_signal_handler = TRUE; |
|
240 seen_test_int1 |= vint == TEST_INT1; |
|
241 seen_test_int2 |= vint == TEST_INT2; |
|
242 } |
|
243 |
|
244 static void |
|
245 test_signal_handler_by_id (GTest *test, |
|
246 gint vint, |
|
247 gpointer data) |
|
248 { |
|
249 g_assert (data == TEST_POINTER3); |
|
250 //g_assert (test->test_pointer1 == TEST_POINTER1); |
|
251 seen_test_int3 |= vint == TEST_INT3; |
|
252 } |
|
253 |
|
254 |
|
255 static void |
|
256 destroy_data (gpointer data, |
|
257 GClosure *closure) |
|
258 { |
|
259 if(data == TEST_POINTER2) |
|
260 { |
|
261 seen_cleanup = TRUE; |
|
262 } |
|
263 g_assert (closure->ref_count == 0); |
|
264 } |
|
265 |
|
266 static void |
|
267 test_emissions (GTest *test) |
|
268 { |
|
269 g_test_emit_test_signal1 (test, TEST_INT1); |
|
270 g_test_emit_test_signal2 (test, TEST_INT2); |
|
271 g_test_emit_test_signal3 (test, TEST_INT3); |
|
272 } |
|
273 |
|
274 int |
|
275 main (int argc, |
|
276 char **argv) |
|
277 { |
|
278 GThread *thread1, *thread2; |
|
279 GClosure *closure, *closure1; |
|
280 GTest *object; |
|
281 guint i; |
|
282 |
|
283 #ifdef SYMBIAN |
|
284 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
285 g_set_print_handler(mrtPrintHandler); |
|
286 #endif /*SYMBIAN*/ |
|
287 |
|
288 g_thread_init (NULL); |
|
289 |
|
290 //g_print ("START: %s\n", argv[0]); |
|
291 |
|
292 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK)); |
|
293 g_type_init (); |
|
294 |
|
295 object = g_object_new (G_TYPE_TEST, NULL); |
|
296 closure = g_cclosure_new (G_CALLBACK (test_signal_handler), TEST_POINTER2, destroy_data); |
|
297 closure1 = g_cclosure_new (G_CALLBACK (test_signal_handler_by_id), TEST_POINTER3, destroy_data); |
|
298 g_assert(object != NULL && closure != NULL); |
|
299 g_assert(closure1 !=NULL); |
|
300 g_signal_connect_closure (object, "test-signal1", closure, FALSE); |
|
301 g_signal_connect_closure (object, "test-signal2", closure, FALSE); |
|
302 g_assert(test_signal3 !=0); |
|
303 i = g_signal_connect_closure_by_id(object, test_signal3, 0,closure1, FALSE); |
|
304 g_assert(i != 0); |
|
305 |
|
306 |
|
307 stopping = FALSE; |
|
308 |
|
309 thread1 = g_thread_create (thread1_main, closure, TRUE, NULL); |
|
310 thread2 = g_thread_create (thread2_main, closure, TRUE, NULL); |
|
311 |
|
312 for (i = 0; i < 10; i++) |
|
313 { |
|
314 static guint count = 0; |
|
315 test_emissions (object); |
|
316 if (++count % 100 == 0) |
|
317 { |
|
318 //g_print ("."); |
|
319 g_thread_yield(); /* force context switch */ |
|
320 } |
|
321 |
|
322 } |
|
323 |
|
324 stopping = TRUE; |
|
325 //g_print ("\nstopping\n"); |
|
326 |
|
327 /* wait for thread shutdown */ |
|
328 g_thread_join (thread1); |
|
329 g_thread_join (thread2); |
|
330 |
|
331 /* finalize object, destroy signals, run cleanup code */ |
|
332 g_object_unref (object); |
|
333 |
|
334 //g_print ("stopped\n"); |
|
335 |
|
336 g_assert (seen_thread1 != FALSE); |
|
337 g_assert (seen_thread2 != FALSE); |
|
338 g_assert (seen_test_int1 != FALSE); |
|
339 g_assert (seen_test_int2 != FALSE); |
|
340 g_assert (seen_test_int3 != FALSE); |
|
341 g_assert (seen_signal_handler != FALSE); |
|
342 g_assert (seen_cleanup != FALSE); |
|
343 |
|
344 #if SYMBIAN |
|
345 testResultXml("closures"); |
|
346 #endif /* EMULATOR */ |
|
347 |
|
348 return 0; |
|
349 } |