|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #undef G_DISABLE_ASSERT |
|
22 #undef G_LOG_DOMAIN |
|
23 |
|
24 #include <stdio.h> |
|
25 #include <glib.h> |
|
26 #include <glib/gatomic.h> |
|
27 #include <glib/galloca.h> |
|
28 #include <glib/gprintf.h> |
|
29 #include <glib-object.h> |
|
30 |
|
31 #include <stdlib.h> |
|
32 |
|
33 |
|
34 #ifdef __SYMBIAN32__ |
|
35 #include "mrt2_glib2_test.h" |
|
36 #endif /*__SYMBIAN32__*/ |
|
37 |
|
38 #define THREADS 10 |
|
39 |
|
40 typedef enum |
|
41 { |
|
42 MY_ENUM_FOO, |
|
43 MY_ENUM_BAR, |
|
44 } myEnum; |
|
45 |
|
46 typedef enum |
|
47 { |
|
48 MY_FLAG_FOO = 1 << 0, |
|
49 MY_FLAG_BAR = 1 << 1, |
|
50 } myFlags; |
|
51 |
|
52 |
|
53 |
|
54 #define TEST(m,cond) G_STMT_START { failed = !(cond); \ |
|
55 if (failed) \ |
|
56 { ++notpassed; \ |
|
57 assert_failed = TRUE; \ |
|
58 if (!m) \ |
|
59 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ |
|
60 else \ |
|
61 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ |
|
62 } \ |
|
63 else \ |
|
64 ++passed; \ |
|
65 if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \ |
|
66 } G_STMT_END |
|
67 |
|
68 |
|
69 |
|
70 void g_ascii_strdown_test() |
|
71 { |
|
72 gchar str[] = "ABCd"; |
|
73 gchar *str1; |
|
74 str1 = g_ascii_strdown(str,4); |
|
75 g_assert(str1[0] == 'a'); |
|
76 g_assert(str1[1] == 'b'); |
|
77 g_assert(str1[2] == 'c'); |
|
78 g_assert(str1[3] == 'd'); |
|
79 } |
|
80 |
|
81 #if 0 |
|
82 void g_assert_warning_test() |
|
83 { |
|
84 g_assert_warning(NULL, __FILE__,__LINE__,"g_assert_warning_test","'a'== 'a'"); |
|
85 #ifdef __SYMBIAN32__ |
|
86 testResultXml("extra_test"); |
|
87 #endif /* EMULATOR */ |
|
88 /*following will abort!*/ |
|
89 g_assert_warning(NULL, __FILE__,__LINE__,"g_assert_warning_test","'a'== 'b'"); |
|
90 } |
|
91 #endif |
|
92 |
|
93 |
|
94 static gpointer |
|
95 atomic_int_thread (gpointer data) |
|
96 { |
|
97 gint* val = (gint*)data; |
|
98 g_atomic_int_set ((gint *) data, |
|
99 (*val)+1); |
|
100 |
|
101 return NULL; |
|
102 } |
|
103 |
|
104 |
|
105 static void |
|
106 test_g_atomic_int_set () |
|
107 { |
|
108 GThread *threads[THREADS]; |
|
109 guint i; |
|
110 |
|
111 |
|
112 for (i = 0; i < THREADS; i++) |
|
113 { |
|
114 int data; |
|
115 data = i; |
|
116 threads[i] = g_thread_create (atomic_int_thread, |
|
117 &data, TRUE, NULL); |
|
118 } |
|
119 g_usleep (G_USEC_PER_SEC * 5); |
|
120 for (i = 0; i < THREADS; i++) |
|
121 { |
|
122 g_thread_join (threads[i]); |
|
123 } |
|
124 } |
|
125 |
|
126 |
|
127 static gpointer |
|
128 atomic_pointer_thread (gpointer data) |
|
129 { |
|
130 |
|
131 g_atomic_pointer_set ((gpointer*) &data,NULL); |
|
132 |
|
133 return NULL; |
|
134 } |
|
135 |
|
136 |
|
137 static void |
|
138 test_g_atomic_pointer_set () |
|
139 { |
|
140 GThread *threads[THREADS]; |
|
141 guint i; |
|
142 |
|
143 |
|
144 for (i = 0; i < THREADS; i++) |
|
145 { |
|
146 int data; |
|
147 data = i; |
|
148 threads[i] = g_thread_create (atomic_pointer_thread, |
|
149 &data, TRUE, NULL); |
|
150 } |
|
151 g_usleep (G_USEC_PER_SEC * 5); |
|
152 for (i = 0; i < THREADS; i++) |
|
153 { |
|
154 g_thread_join (threads[i]); |
|
155 } |
|
156 } |
|
157 |
|
158 |
|
159 #if 0 |
|
160 void test_g_get_codeset() |
|
161 { |
|
162 gchar* charset = g_get_codeset (); |
|
163 g_assert(!strcmp(charset,"US-ASCII")); |
|
164 g_free(charset); |
|
165 } |
|
166 #endif |
|
167 |
|
168 |
|
169 void test_g_blow_chunks() |
|
170 { |
|
171 gchar *name = "chunk"; |
|
172 GMemChunk *mem_chunk = g_mem_chunk_new(name,2,10,G_ALLOC_ONLY); |
|
173 g_blow_chunks(); |
|
174 } |
|
175 |
|
176 void test_g_date_set_time_val() |
|
177 { |
|
178 GDate* d = g_date_new(); |
|
179 |
|
180 GTimeVal current_time; |
|
181 g_get_current_time (¤t_time); |
|
182 g_date_set_time_val(d, ¤t_time); |
|
183 |
|
184 g_assert(g_date_valid(d)); |
|
185 |
|
186 |
|
187 } |
|
188 |
|
189 |
|
190 |
|
191 gboolean func1(int *data) |
|
192 { |
|
193 *data = 1; |
|
194 return TRUE; |
|
195 } |
|
196 |
|
197 gboolean func2(int *data) |
|
198 { |
|
199 *data = 2; |
|
200 return TRUE; |
|
201 } |
|
202 |
|
203 gboolean func3(int *data) |
|
204 { |
|
205 *data = 3; |
|
206 return FALSE; |
|
207 } |
|
208 |
|
209 gboolean func4(int *data) |
|
210 { |
|
211 *data = 4; |
|
212 return TRUE; |
|
213 } |
|
214 |
|
215 |
|
216 void hook_test() |
|
217 { |
|
218 GHookList hooklist; |
|
219 GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook; |
|
220 int data1 = 0,data2 = 0,data3 = 0,data4 = 0; |
|
221 int comp_value; |
|
222 gboolean val; |
|
223 |
|
224 g_hook_list_init(&hooklist,sizeof(GHook)); |
|
225 |
|
226 hook1 = g_hook_alloc(&hooklist); |
|
227 hook1->func = (gpointer)func1; |
|
228 hook1->data = &data1; |
|
229 |
|
230 hook2 = g_hook_alloc(&hooklist); |
|
231 hook2->func = (gpointer)func2; |
|
232 hook2->data = &data2; |
|
233 |
|
234 hook3 = g_hook_alloc(&hooklist); |
|
235 hook3->func = (gpointer)func3; |
|
236 hook3->data = &data3; |
|
237 |
|
238 hook4 = g_hook_alloc(&hooklist); |
|
239 hook4->func = (gpointer)func4; |
|
240 hook4->data = &data4; |
|
241 |
|
242 g_hook_append(&hooklist,hook1); |
|
243 g_hook_append(&hooklist,hook2); |
|
244 g_hook_append(&hooklist,hook3); |
|
245 g_hook_append(&hooklist,hook4); |
|
246 |
|
247 g_hook_list_invoke_check(&hooklist,FALSE); |
|
248 |
|
249 g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4); |
|
250 |
|
251 //now only func3 must be in hooklist |
|
252 data1 = 0,data2 = 0,data3 = 0,data4 = 0; |
|
253 g_hook_list_invoke_check(&hooklist,FALSE); |
|
254 |
|
255 //check for implemention behaviour as opposed to documented behaviour |
|
256 |
|
257 //enable this to check for documented behaviour |
|
258 //g_assert(data1 == 0 && data2 == 0 && data3 == 3 && data4 == 0); |
|
259 |
|
260 //disable this to stop checking implemented behaviour |
|
261 g_assert(data1 == 1 && data2 == 2 && data3 == 0 && data4 == 4); |
|
262 |
|
263 g_hook_list_clear(&hooklist); |
|
264 |
|
265 } |
|
266 |
|
267 void test_g_mem_chunk_alloc0() |
|
268 { |
|
269 gchar *name = "chunk"; |
|
270 char* pchar; |
|
271 GMemChunk *mem_chunk = g_mem_chunk_new(name,2,10,G_ALLOC_ONLY); |
|
272 gpointer data = g_mem_chunk_alloc0(mem_chunk); |
|
273 g_assert(data != NULL); |
|
274 pchar = (char*)data; |
|
275 g_assert( (*pchar) == '\0' && *(pchar+1) == '\0'); |
|
276 g_mem_chunk_print(mem_chunk); |
|
277 g_mem_chunk_clean(mem_chunk); |
|
278 g_mem_chunk_destroy(mem_chunk); |
|
279 } |
|
280 |
|
281 |
|
282 |
|
283 gpointer theFunc(gpointer data) |
|
284 { |
|
285 int* pval = (int*) data; |
|
286 (*pval)++; |
|
287 return NULL; |
|
288 } |
|
289 |
|
290 void test_gonce() |
|
291 { |
|
292 GOnce onceObject; |
|
293 int val = 1; |
|
294 |
|
295 g_once_impl (&onceObject,theFunc, &val); |
|
296 g_once_impl (&onceObject,theFunc, &val); |
|
297 g_once_impl (&onceObject,theFunc, &val); |
|
298 g_assert(val == 2); |
|
299 } |
|
300 |
|
301 void test_g_return_if_fail_warning() |
|
302 { |
|
303 //currently not exported |
|
304 //g_return_if_fail_warning (NULL,"extra_tests::main","1== 1"); |
|
305 } |
|
306 |
|
307 void test_g_slist_alloc() |
|
308 { |
|
309 GSList* pList = g_slist_alloc(); |
|
310 g_assert(pList != NULL); |
|
311 g_slist_free_1 (pList); |
|
312 } |
|
313 |
|
314 void test_g_string_insert_c() |
|
315 { |
|
316 GString* string1 = g_string_new ("firstlast"); |
|
317 g_string_insert_c (string1, 5, '_'); |
|
318 g_assert (strcmp (string1->str, "first_last") == 0); |
|
319 g_string_free (string1, TRUE); |
|
320 } |
|
321 |
|
322 void test_g_strsignal() |
|
323 { |
|
324 const gchar* errmsg = g_strsignal(0); |
|
325 g_assert(strcmp(errmsg, "unknown signal (0)")==0); |
|
326 } |
|
327 |
|
328 |
|
329 |
|
330 void test_g_generictype_get_type() |
|
331 { |
|
332 GType type_id, type_id2; |
|
333 int i; |
|
334 |
|
335 |
|
336 GType (*fnArray[])() = |
|
337 { |
|
338 g_closure_get_type, |
|
339 g_date_get_type, |
|
340 g_gstring_get_type, |
|
341 g_hash_table_get_type, |
|
342 g_io_channel_get_type, |
|
343 g_io_condition_get_type, |
|
344 g_strv_get_type, |
|
345 g_value_get_type, |
|
346 }; |
|
347 |
|
348 |
|
349 |
|
350 #define NumFns sizeof(fnArray)/sizeof(GType (*)()) |
|
351 |
|
352 for(i =0; i<NumFns;i++) |
|
353 { |
|
354 type_id = fnArray[i](); |
|
355 g_assert(type_id != 0); |
|
356 type_id2 = fnArray[i](); |
|
357 g_assert(type_id == type_id2); |
|
358 //pInstance = g_type_create_instance(type_id); |
|
359 //g_assert(g_type_name(type_id) == g_type_name_from_instance(pInstance) ); |
|
360 } |
|
361 |
|
362 } |
|
363 |
|
364 void test_enumClass() |
|
365 { |
|
366 GType type_id = 0; |
|
367 GEnumClass* pPointer = NULL; |
|
368 GEnumValue* retrievedValue; |
|
369 static GEnumValue enum_array[] = |
|
370 { |
|
371 { 0, "EZero", "zero"}, |
|
372 { 1, "EOne", "One"}, |
|
373 { 2, "ETwo", "Two"}, |
|
374 { 0, NULL, NULL}, |
|
375 }; |
|
376 |
|
377 //g_type_init(); |
|
378 |
|
379 type_id = g_enum_register_static("egEnum",enum_array); |
|
380 pPointer = g_type_class_ref(type_id); |
|
381 if(pPointer) |
|
382 { |
|
383 |
|
384 retrievedValue = g_enum_get_value(pPointer,1); |
|
385 g_assert(retrievedValue && retrievedValue->value == 1); |
|
386 retrievedValue = g_enum_get_value(pPointer,5); |
|
387 g_assert(retrievedValue == NULL); |
|
388 |
|
389 retrievedValue = g_enum_get_value_by_name(pPointer,"EOne"); |
|
390 g_assert(retrievedValue && retrievedValue->value == 1); |
|
391 retrievedValue = g_enum_get_value_by_name(pPointer,"EFive"); |
|
392 g_assert(retrievedValue == NULL); |
|
393 |
|
394 retrievedValue = g_enum_get_value_by_nick(pPointer,"One"); |
|
395 g_assert(retrievedValue && retrievedValue->value == 1); |
|
396 retrievedValue = g_enum_get_value_by_nick(pPointer,"Five"); |
|
397 g_assert(retrievedValue == NULL); |
|
398 |
|
399 } |
|
400 }//fn |
|
401 |
|
402 |
|
403 void test_flagsClass() |
|
404 { |
|
405 GType type_id = 0; |
|
406 GFlagsClass* pPointer = NULL; |
|
407 GFlagsValue* retrievedValue; |
|
408 static GFlagsValue flags_array[] = |
|
409 { |
|
410 { 1, "EOne", "One"}, |
|
411 { 2, "ETwo", "Two"}, |
|
412 { 4, "EFour", "Four"}, |
|
413 { 0, NULL, NULL}, |
|
414 }; |
|
415 |
|
416 //g_type_init(); |
|
417 |
|
418 type_id = g_flags_register_static("egFlags",flags_array); |
|
419 pPointer = g_type_class_ref(type_id); |
|
420 if(pPointer) |
|
421 { |
|
422 |
|
423 retrievedValue = g_flags_get_value_by_name(pPointer,"EOne"); |
|
424 g_assert(retrievedValue && retrievedValue->value == 1); |
|
425 retrievedValue = g_flags_get_value_by_name(pPointer,"EFive"); |
|
426 g_assert(retrievedValue == NULL); |
|
427 |
|
428 retrievedValue = g_flags_get_value_by_nick(pPointer,"One"); |
|
429 g_assert(retrievedValue && retrievedValue->value == 1); |
|
430 retrievedValue = g_flags_get_value_by_nick(pPointer,"Five"); |
|
431 g_assert(retrievedValue == NULL); |
|
432 } |
|
433 }//fn |
|
434 |
|
435 |
|
436 |
|
437 int main (int argc, |
|
438 char *argv[]) |
|
439 { |
|
440 #ifdef __SYMBIAN32__ |
|
441 int handler = 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); |
|
442 #endif /*__SYMBIAN32__*/ |
|
443 |
|
444 g_thread_init(NULL); |
|
445 g_type_init(); |
|
446 |
|
447 g_ascii_strdown_test(); |
|
448 test_g_atomic_int_set(); |
|
449 test_g_atomic_pointer_set(); |
|
450 |
|
451 //test_g_get_codeset(); |
|
452 test_g_blow_chunks(); |
|
453 test_g_date_set_time_val(); |
|
454 hook_test(); |
|
455 |
|
456 test_g_mem_chunk_alloc0(); |
|
457 test_gonce(); |
|
458 //test_g_return_if_fail_warning (); |
|
459 test_g_slist_alloc(); |
|
460 test_g_string_insert_c(); |
|
461 test_g_strsignal(); |
|
462 |
|
463 test_g_generictype_get_type(); |
|
464 |
|
465 test_enumClass(); |
|
466 test_flagsClass(); |
|
467 |
|
468 |
|
469 |
|
470 //test |
|
471 #ifdef __SYMBIAN32__ |
|
472 g_log_remove_handler (NULL, handler); |
|
473 g_warning("This test message should have been printed on console\n"); |
|
474 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); |
|
475 #endif /*__SYMBIAN32__*/ |
|
476 |
|
477 |
|
478 |
|
479 //g_assert_warning_test(); |
|
480 |
|
481 #ifdef __SYMBIAN32__ |
|
482 testResultXml("extra_tests"); |
|
483 #endif /* EMULATOR */ |
|
484 |
|
485 return 0; |
|
486 } |