1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/src/extra_tests.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,490 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 +*
1.7 +* This library is free software; you can redistribute it and/or
1.8 +* modify it under the terms of the GNU Lesser General Public
1.9 +* License as published by the Free Software Foundation; either
1.10 +* version 2 of the License, or (at your option) any later version.
1.11 +*
1.12 +* This library is distributed in the hope that it will be useful,
1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 +* Lesser General Public License for more details.
1.16 +*
1.17 +* You should have received a copy of the GNU Lesser General Public
1.18 +* License along with this library; if not, write to the
1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 +* Boston, MA 02111-1307, USA.
1.21 +*
1.22 +* Description:
1.23 +*
1.24 +*/
1.25 +
1.26 +
1.27 +
1.28 +#undef G_DISABLE_ASSERT
1.29 +#undef G_LOG_DOMAIN
1.30 +
1.31 +#include <stdio.h>
1.32 +#include <glib.h>
1.33 +#include <glib/gatomic.h>
1.34 +#include <glib/galloca.h>
1.35 +#include <glib/gprintf.h>
1.36 +#include <glib-object.h>
1.37 +
1.38 +#include <stdlib.h>
1.39 +
1.40 +
1.41 +#ifdef SYMBIAN
1.42 +#include "mrt2_glib2_test.h"
1.43 +#endif /*SYMBIAN*/
1.44 +
1.45 +#define THREADS 10
1.46 +
1.47 +typedef enum
1.48 +{
1.49 + MY_ENUM_FOO,
1.50 + MY_ENUM_BAR,
1.51 +} myEnum;
1.52 +
1.53 +typedef enum
1.54 +{
1.55 + MY_FLAG_FOO = 1 << 0,
1.56 + MY_FLAG_BAR = 1 << 1,
1.57 +} myFlags;
1.58 +
1.59 +
1.60 +
1.61 +#define TEST(m,cond) G_STMT_START { failed = !(cond); \
1.62 +if (failed) \
1.63 + { ++notpassed; \
1.64 + assert_failed = TRUE; \
1.65 + if (!m) \
1.66 + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
1.67 + else \
1.68 + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
1.69 + } \
1.70 +else \
1.71 + ++passed; \
1.72 + if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \
1.73 +} G_STMT_END
1.74 +
1.75 +
1.76 +
1.77 +void g_ascii_strdown_test()
1.78 +{
1.79 + gchar str[] = "ABCd";
1.80 + gchar *str1;
1.81 + str1 = g_ascii_strdown(str,4);
1.82 + g_assert(str1[0] == 'a');
1.83 + g_assert(str1[1] == 'b');
1.84 + g_assert(str1[2] == 'c');
1.85 + g_assert(str1[3] == 'd');
1.86 +}
1.87 +
1.88 +#if 0
1.89 +void g_assert_warning_test()
1.90 +{
1.91 + g_assert_warning(NULL, __FILE__,__LINE__,"g_assert_warning_test","'a'== 'a'");
1.92 + #ifdef SYMBIAN
1.93 + testResultXml("extra_test");
1.94 + #endif /* EMULATOR */
1.95 + /*following will abort!*/
1.96 + g_assert_warning(NULL, __FILE__,__LINE__,"g_assert_warning_test","'a'== 'b'");
1.97 +}
1.98 +#endif
1.99 +
1.100 +
1.101 +static gpointer
1.102 +atomic_int_thread (gpointer data)
1.103 +{
1.104 + gint* val = (gint*)data;
1.105 + g_atomic_int_set ((gint *) data,
1.106 + (*val)+1);
1.107 +
1.108 + return NULL;
1.109 +}
1.110 +
1.111 +
1.112 +static void
1.113 +test_g_atomic_int_set ()
1.114 +{
1.115 + GThread *threads[THREADS];
1.116 + guint i;
1.117 +
1.118 +
1.119 + for (i = 0; i < THREADS; i++)
1.120 + {
1.121 + int data;
1.122 + data = i;
1.123 + threads[i] = g_thread_create (atomic_int_thread,
1.124 + &data, TRUE, NULL);
1.125 + }
1.126 + g_usleep (G_USEC_PER_SEC * 5);
1.127 + for (i = 0; i < THREADS; i++)
1.128 + {
1.129 + g_thread_join (threads[i]);
1.130 + }
1.131 +}
1.132 +
1.133 +
1.134 +static gpointer
1.135 +atomic_pointer_thread (gpointer data)
1.136 +{
1.137 +
1.138 + g_atomic_pointer_set ((gpointer*) &data,NULL);
1.139 +
1.140 + return NULL;
1.141 +}
1.142 +
1.143 +
1.144 +static void
1.145 +test_g_atomic_pointer_set ()
1.146 +{
1.147 + GThread *threads[THREADS];
1.148 + guint i;
1.149 +
1.150 +
1.151 + for (i = 0; i < THREADS; i++)
1.152 + {
1.153 + int data;
1.154 + data = i;
1.155 + threads[i] = g_thread_create (atomic_pointer_thread,
1.156 + &data, TRUE, NULL);
1.157 + }
1.158 + g_usleep (G_USEC_PER_SEC * 5);
1.159 + for (i = 0; i < THREADS; i++)
1.160 + {
1.161 + g_thread_join (threads[i]);
1.162 + }
1.163 +}
1.164 +
1.165 +
1.166 +#if 0
1.167 +void test_g_get_codeset()
1.168 +{
1.169 + gchar* charset = g_get_codeset ();
1.170 + g_assert(!strcmp(charset,"US-ASCII"));
1.171 + g_free(charset);
1.172 +}
1.173 +#endif
1.174 +
1.175 +
1.176 +void test_g_blow_chunks()
1.177 +{
1.178 + gchar *name = "chunk";
1.179 + GMemChunk *mem_chunk = g_mem_chunk_new(name,2,10,G_ALLOC_ONLY);
1.180 + g_blow_chunks();
1.181 +}
1.182 +
1.183 +void test_g_date_set_time_val()
1.184 +{
1.185 + GDate* d = g_date_new();
1.186 +
1.187 + GTimeVal current_time;
1.188 + g_get_current_time (¤t_time);
1.189 + g_date_set_time_val(d, ¤t_time);
1.190 +
1.191 + g_assert(g_date_valid(d));
1.192 +
1.193 +
1.194 +}
1.195 +
1.196 +
1.197 +
1.198 +gboolean func1(int *data)
1.199 +{
1.200 + *data = 1;
1.201 + return TRUE;
1.202 +}
1.203 +
1.204 +gboolean func2(int *data)
1.205 +{
1.206 + *data = 2;
1.207 + return TRUE;
1.208 +}
1.209 +
1.210 +gboolean func3(int *data)
1.211 +{
1.212 + *data = 3;
1.213 + return FALSE;
1.214 +}
1.215 +
1.216 +gboolean func4(int *data)
1.217 +{
1.218 + *data = 4;
1.219 + return TRUE;
1.220 +}
1.221 +
1.222 +
1.223 +void hook_test()
1.224 +{
1.225 + GHookList hooklist;
1.226 + GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook;
1.227 + int data1 = 0,data2 = 0,data3 = 0,data4 = 0;
1.228 + int comp_value;
1.229 + gboolean val;
1.230 +
1.231 + g_hook_list_init(&hooklist,sizeof(GHook));
1.232 +
1.233 + hook1 = g_hook_alloc(&hooklist);
1.234 + hook1->func = (gpointer)func1;
1.235 + hook1->data = &data1;
1.236 +
1.237 + hook2 = g_hook_alloc(&hooklist);
1.238 + hook2->func = (gpointer)func2;
1.239 + hook2->data = &data2;
1.240 +
1.241 + hook3 = g_hook_alloc(&hooklist);
1.242 + hook3->func = (gpointer)func3;
1.243 + hook3->data = &data3;
1.244 +
1.245 + hook4 = g_hook_alloc(&hooklist);
1.246 + hook4->func = (gpointer)func4;
1.247 + hook4->data = &data4;
1.248 +
1.249 + g_hook_append(&hooklist,hook1);
1.250 + g_hook_append(&hooklist,hook2);
1.251 + g_hook_append(&hooklist,hook3);
1.252 + g_hook_append(&hooklist,hook4);
1.253 +
1.254 + g_hook_list_invoke_check(&hooklist,FALSE);
1.255 +
1.256 + g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4);
1.257 +
1.258 + //now only func3 must be in hooklist
1.259 + data1 = 0,data2 = 0,data3 = 0,data4 = 0;
1.260 + g_hook_list_invoke_check(&hooklist,FALSE);
1.261 +
1.262 + //check for implemention behaviour as opposed to documented behaviour
1.263 +
1.264 + //enable this to check for documented behaviour
1.265 + //g_assert(data1 == 0 && data2 == 0 && data3 == 3 && data4 == 0);
1.266 +
1.267 + //disable this to stop checking implemented behaviour
1.268 + g_assert(data1 == 1 && data2 == 2 && data3 == 0 && data4 == 4);
1.269 +
1.270 + g_hook_list_clear(&hooklist);
1.271 +
1.272 +}
1.273 +
1.274 +void test_g_mem_chunk_alloc0()
1.275 +{
1.276 + gchar *name = "chunk";
1.277 + char* pchar;
1.278 + GMemChunk *mem_chunk = g_mem_chunk_new(name,2,10,G_ALLOC_ONLY);
1.279 + gpointer data = g_mem_chunk_alloc0(mem_chunk);
1.280 + g_assert(data != NULL);
1.281 + pchar = (char*)data;
1.282 + g_assert( (*pchar) == '\0' && *(pchar+1) == '\0');
1.283 + g_mem_chunk_print(mem_chunk);
1.284 + g_mem_chunk_clean(mem_chunk);
1.285 + g_mem_chunk_destroy(mem_chunk);
1.286 +}
1.287 +
1.288 +
1.289 +
1.290 +gpointer theFunc(gpointer data)
1.291 +{
1.292 + int* pval = (int*) data;
1.293 + (*pval)++;
1.294 + return NULL;
1.295 +}
1.296 +
1.297 +void test_gonce()
1.298 +{
1.299 + GOnce onceObject;
1.300 + int val = 1;
1.301 +
1.302 + g_once_impl (&onceObject,theFunc, &val);
1.303 + g_once_impl (&onceObject,theFunc, &val);
1.304 + g_once_impl (&onceObject,theFunc, &val);
1.305 + g_assert(val == 2);
1.306 +}
1.307 +
1.308 +void test_g_return_if_fail_warning()
1.309 +{
1.310 + //currently not exported
1.311 + //g_return_if_fail_warning (NULL,"extra_tests::main","1== 1");
1.312 +}
1.313 +
1.314 +void test_g_slist_alloc()
1.315 +{
1.316 + GSList* pList = g_slist_alloc();
1.317 + g_assert(pList != NULL);
1.318 + g_slist_free_1 (pList);
1.319 +}
1.320 +
1.321 +void test_g_string_insert_c()
1.322 +{
1.323 + GString* string1 = g_string_new ("firstlast");
1.324 + g_string_insert_c (string1, 5, '_');
1.325 + g_assert (strcmp (string1->str, "first_last") == 0);
1.326 + g_string_free (string1, TRUE);
1.327 +}
1.328 +
1.329 +void test_g_strsignal()
1.330 +{
1.331 + const gchar* errmsg = g_strsignal(0);
1.332 + g_assert(strcmp(errmsg, "unknown signal (0)")==0);
1.333 +}
1.334 +
1.335 +
1.336 +
1.337 +void test_g_generictype_get_type()
1.338 +{
1.339 + GType type_id, type_id2;
1.340 + int i;
1.341 +
1.342 +
1.343 + GType (*fnArray[])() =
1.344 + {
1.345 + g_closure_get_type,
1.346 + g_date_get_type,
1.347 + g_gstring_get_type,
1.348 + g_hash_table_get_type,
1.349 + g_io_channel_get_type,
1.350 + g_io_condition_get_type,
1.351 + g_strv_get_type,
1.352 + g_value_get_type,
1.353 + };
1.354 +
1.355 +
1.356 +
1.357 +#define NumFns sizeof(fnArray)/sizeof(GType (*)())
1.358 +
1.359 + for(i =0; i<NumFns;i++)
1.360 + {
1.361 + type_id = fnArray[i]();
1.362 + g_assert(type_id != 0);
1.363 + type_id2 = fnArray[i]();
1.364 + g_assert(type_id == type_id2);
1.365 + //pInstance = g_type_create_instance(type_id);
1.366 + //g_assert(g_type_name(type_id) == g_type_name_from_instance(pInstance) );
1.367 + }
1.368 +
1.369 +}
1.370 +
1.371 +void test_enumClass()
1.372 +{
1.373 + GType type_id = 0;
1.374 + GEnumClass* pPointer = NULL;
1.375 + GEnumValue* retrievedValue;
1.376 + static GEnumValue enum_array[] =
1.377 + {
1.378 + { 0, "EZero", "zero"},
1.379 + { 1, "EOne", "One"},
1.380 + { 2, "ETwo", "Two"},
1.381 + { 0, NULL, NULL},
1.382 + };
1.383 +
1.384 + //g_type_init();
1.385 +
1.386 + type_id = g_enum_register_static("egEnum",enum_array);
1.387 + pPointer = g_type_class_ref(type_id);
1.388 + if(pPointer)
1.389 + {
1.390 +
1.391 + retrievedValue = g_enum_get_value(pPointer,1);
1.392 + g_assert(retrievedValue && retrievedValue->value == 1);
1.393 + retrievedValue = g_enum_get_value(pPointer,5);
1.394 + g_assert(retrievedValue == NULL);
1.395 +
1.396 + retrievedValue = g_enum_get_value_by_name(pPointer,"EOne");
1.397 + g_assert(retrievedValue && retrievedValue->value == 1);
1.398 + retrievedValue = g_enum_get_value_by_name(pPointer,"EFive");
1.399 + g_assert(retrievedValue == NULL);
1.400 +
1.401 + retrievedValue = g_enum_get_value_by_nick(pPointer,"One");
1.402 + g_assert(retrievedValue && retrievedValue->value == 1);
1.403 + retrievedValue = g_enum_get_value_by_nick(pPointer,"Five");
1.404 + g_assert(retrievedValue == NULL);
1.405 +
1.406 + }
1.407 +}//fn
1.408 +
1.409 +
1.410 +void test_flagsClass()
1.411 +{
1.412 + GType type_id = 0;
1.413 + GFlagsClass* pPointer = NULL;
1.414 + GFlagsValue* retrievedValue;
1.415 + static GFlagsValue flags_array[] =
1.416 + {
1.417 + { 1, "EOne", "One"},
1.418 + { 2, "ETwo", "Two"},
1.419 + { 4, "EFour", "Four"},
1.420 + { 0, NULL, NULL},
1.421 + };
1.422 +
1.423 + //g_type_init();
1.424 +
1.425 + type_id = g_flags_register_static("egFlags",flags_array);
1.426 + pPointer = g_type_class_ref(type_id);
1.427 + if(pPointer)
1.428 + {
1.429 +
1.430 + retrievedValue = g_flags_get_value_by_name(pPointer,"EOne");
1.431 + g_assert(retrievedValue && retrievedValue->value == 1);
1.432 + retrievedValue = g_flags_get_value_by_name(pPointer,"EFive");
1.433 + g_assert(retrievedValue == NULL);
1.434 +
1.435 + retrievedValue = g_flags_get_value_by_nick(pPointer,"One");
1.436 + g_assert(retrievedValue && retrievedValue->value == 1);
1.437 + retrievedValue = g_flags_get_value_by_nick(pPointer,"Five");
1.438 + g_assert(retrievedValue == NULL);
1.439 + }
1.440 +}//fn
1.441 +
1.442 +
1.443 +
1.444 +int main (int argc,
1.445 + char *argv[])
1.446 +{
1.447 + #ifdef SYMBIAN
1.448 + 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);
1.449 + #endif /*SYMBIAN*/
1.450 +
1.451 + g_thread_init(NULL);
1.452 + g_type_init();
1.453 +
1.454 + g_ascii_strdown_test();
1.455 + test_g_atomic_int_set();
1.456 + test_g_atomic_pointer_set();
1.457 +
1.458 + //test_g_get_codeset();
1.459 + test_g_blow_chunks();
1.460 + test_g_date_set_time_val();
1.461 + hook_test();
1.462 +
1.463 + test_g_mem_chunk_alloc0();
1.464 + test_gonce();
1.465 + //test_g_return_if_fail_warning ();
1.466 + test_g_slist_alloc();
1.467 + test_g_string_insert_c();
1.468 + test_g_strsignal();
1.469 +
1.470 + test_g_generictype_get_type();
1.471 +
1.472 + test_enumClass();
1.473 + test_flagsClass();
1.474 +
1.475 +
1.476 +
1.477 + //test
1.478 + #ifdef SYMBIAN
1.479 + g_log_remove_handler (NULL, handler);
1.480 + g_warning("This test message should have been printed on console\n");
1.481 + 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);
1.482 + #endif /*SYMBIAN*/
1.483 +
1.484 +
1.485 +
1.486 + //g_assert_warning_test();
1.487 +
1.488 + #ifdef SYMBIAN
1.489 + testResultXml("extra_tests");
1.490 + #endif /* EMULATOR */
1.491 +
1.492 + return 0;
1.493 +}
1.494 \ No newline at end of file