Update contrib.
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #undef G_DISABLE_ASSERT
30 #include <glib/gatomic.h>
31 #include <glib/galloca.h>
32 #include <glib/gprintf.h>
33 #include <glib-object.h>
39 #include "mrt2_glib2_test.h"
58 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
61 assert_failed = TRUE; \
63 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
65 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
69 if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \
74 void g_ascii_strdown_test()
78 str1 = g_ascii_strdown(str,4);
79 g_assert(str1[0] == 'a');
80 g_assert(str1[1] == 'b');
81 g_assert(str1[2] == 'c');
82 g_assert(str1[3] == 'd');
86 void g_assert_warning_test()
88 g_assert_warning(NULL, __FILE__,__LINE__,"g_assert_warning_test","'a'== 'a'");
90 testResultXml("extra_test");
92 /*following will abort!*/
93 g_assert_warning(NULL, __FILE__,__LINE__,"g_assert_warning_test","'a'== 'b'");
99 atomic_int_thread (gpointer data)
101 gint* val = (gint*)data;
102 g_atomic_int_set ((gint *) data,
110 test_g_atomic_int_set ()
112 GThread *threads[THREADS];
116 for (i = 0; i < THREADS; i++)
120 threads[i] = g_thread_create (atomic_int_thread,
123 g_usleep (G_USEC_PER_SEC * 5);
124 for (i = 0; i < THREADS; i++)
126 g_thread_join (threads[i]);
132 atomic_pointer_thread (gpointer data)
135 g_atomic_pointer_set ((gpointer*) &data,NULL);
142 test_g_atomic_pointer_set ()
144 GThread *threads[THREADS];
148 for (i = 0; i < THREADS; i++)
152 threads[i] = g_thread_create (atomic_pointer_thread,
155 g_usleep (G_USEC_PER_SEC * 5);
156 for (i = 0; i < THREADS; i++)
158 g_thread_join (threads[i]);
164 void test_g_get_codeset()
166 gchar* charset = g_get_codeset ();
167 g_assert(!strcmp(charset,"US-ASCII"));
173 void test_g_blow_chunks()
175 gchar *name = "chunk";
176 GMemChunk *mem_chunk = g_mem_chunk_new(name,2,10,G_ALLOC_ONLY);
180 void test_g_date_set_time_val()
182 GDate* d = g_date_new();
184 GTimeVal current_time;
185 g_get_current_time (¤t_time);
186 g_date_set_time_val(d, ¤t_time);
188 g_assert(g_date_valid(d));
195 gboolean func1(int *data)
201 gboolean func2(int *data)
207 gboolean func3(int *data)
213 gboolean func4(int *data)
223 GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook;
224 int data1 = 0,data2 = 0,data3 = 0,data4 = 0;
228 g_hook_list_init(&hooklist,sizeof(GHook));
230 hook1 = g_hook_alloc(&hooklist);
231 hook1->func = (gpointer)func1;
232 hook1->data = &data1;
234 hook2 = g_hook_alloc(&hooklist);
235 hook2->func = (gpointer)func2;
236 hook2->data = &data2;
238 hook3 = g_hook_alloc(&hooklist);
239 hook3->func = (gpointer)func3;
240 hook3->data = &data3;
242 hook4 = g_hook_alloc(&hooklist);
243 hook4->func = (gpointer)func4;
244 hook4->data = &data4;
246 g_hook_append(&hooklist,hook1);
247 g_hook_append(&hooklist,hook2);
248 g_hook_append(&hooklist,hook3);
249 g_hook_append(&hooklist,hook4);
251 g_hook_list_invoke_check(&hooklist,FALSE);
253 g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4);
255 //now only func3 must be in hooklist
256 data1 = 0,data2 = 0,data3 = 0,data4 = 0;
257 g_hook_list_invoke_check(&hooklist,FALSE);
259 //check for implemention behaviour as opposed to documented behaviour
261 //enable this to check for documented behaviour
262 //g_assert(data1 == 0 && data2 == 0 && data3 == 3 && data4 == 0);
264 //disable this to stop checking implemented behaviour
265 g_assert(data1 == 1 && data2 == 2 && data3 == 0 && data4 == 4);
267 g_hook_list_clear(&hooklist);
271 void test_g_mem_chunk_alloc0()
273 gchar *name = "chunk";
275 GMemChunk *mem_chunk = g_mem_chunk_new(name,2,10,G_ALLOC_ONLY);
276 gpointer data = g_mem_chunk_alloc0(mem_chunk);
277 g_assert(data != NULL);
279 g_assert( (*pchar) == '\0' && *(pchar+1) == '\0');
280 g_mem_chunk_print(mem_chunk);
281 g_mem_chunk_clean(mem_chunk);
282 g_mem_chunk_destroy(mem_chunk);
287 gpointer theFunc(gpointer data)
289 int* pval = (int*) data;
299 g_once_impl (&onceObject,theFunc, &val);
300 g_once_impl (&onceObject,theFunc, &val);
301 g_once_impl (&onceObject,theFunc, &val);
305 void test_g_return_if_fail_warning()
307 //currently not exported
308 //g_return_if_fail_warning (NULL,"extra_tests::main","1== 1");
311 void test_g_slist_alloc()
313 GSList* pList = g_slist_alloc();
314 g_assert(pList != NULL);
315 g_slist_free_1 (pList);
318 void test_g_string_insert_c()
320 GString* string1 = g_string_new ("firstlast");
321 g_string_insert_c (string1, 5, '_');
322 g_assert (strcmp (string1->str, "first_last") == 0);
323 g_string_free (string1, TRUE);
326 void test_g_strsignal()
328 const gchar* errmsg = g_strsignal(0);
329 g_assert(strcmp(errmsg, "unknown signal (0)")==0);
334 void test_g_generictype_get_type()
336 GType type_id, type_id2;
340 GType (*fnArray[])() =
345 g_hash_table_get_type,
346 g_io_channel_get_type,
347 g_io_condition_get_type,
354 #define NumFns sizeof(fnArray)/sizeof(GType (*)())
356 for(i =0; i<NumFns;i++)
358 type_id = fnArray[i]();
359 g_assert(type_id != 0);
360 type_id2 = fnArray[i]();
361 g_assert(type_id == type_id2);
362 //pInstance = g_type_create_instance(type_id);
363 //g_assert(g_type_name(type_id) == g_type_name_from_instance(pInstance) );
368 void test_enumClass()
371 GEnumClass* pPointer = NULL;
372 GEnumValue* retrievedValue;
373 static GEnumValue enum_array[] =
375 { 0, "EZero", "zero"},
383 type_id = g_enum_register_static("egEnum",enum_array);
384 pPointer = g_type_class_ref(type_id);
388 retrievedValue = g_enum_get_value(pPointer,1);
389 g_assert(retrievedValue && retrievedValue->value == 1);
390 retrievedValue = g_enum_get_value(pPointer,5);
391 g_assert(retrievedValue == NULL);
393 retrievedValue = g_enum_get_value_by_name(pPointer,"EOne");
394 g_assert(retrievedValue && retrievedValue->value == 1);
395 retrievedValue = g_enum_get_value_by_name(pPointer,"EFive");
396 g_assert(retrievedValue == NULL);
398 retrievedValue = g_enum_get_value_by_nick(pPointer,"One");
399 g_assert(retrievedValue && retrievedValue->value == 1);
400 retrievedValue = g_enum_get_value_by_nick(pPointer,"Five");
401 g_assert(retrievedValue == NULL);
407 void test_flagsClass()
410 GFlagsClass* pPointer = NULL;
411 GFlagsValue* retrievedValue;
412 static GFlagsValue flags_array[] =
416 { 4, "EFour", "Four"},
422 type_id = g_flags_register_static("egFlags",flags_array);
423 pPointer = g_type_class_ref(type_id);
427 retrievedValue = g_flags_get_value_by_name(pPointer,"EOne");
428 g_assert(retrievedValue && retrievedValue->value == 1);
429 retrievedValue = g_flags_get_value_by_name(pPointer,"EFive");
430 g_assert(retrievedValue == NULL);
432 retrievedValue = g_flags_get_value_by_nick(pPointer,"One");
433 g_assert(retrievedValue && retrievedValue->value == 1);
434 retrievedValue = g_flags_get_value_by_nick(pPointer,"Five");
435 g_assert(retrievedValue == NULL);
445 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);
451 g_ascii_strdown_test();
452 test_g_atomic_int_set();
453 test_g_atomic_pointer_set();
455 //test_g_get_codeset();
456 test_g_blow_chunks();
457 test_g_date_set_time_val();
460 test_g_mem_chunk_alloc0();
462 //test_g_return_if_fail_warning ();
463 test_g_slist_alloc();
464 test_g_string_insert_c();
467 test_g_generictype_get_type();
476 g_log_remove_handler (NULL, handler);
477 g_warning("This test message should have been printed on console\n");
478 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);
483 //g_assert_warning_test();
486 testResultXml("extra_tests");
487 #endif /* EMULATOR */