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