glib/tsrc/glib_nonstif/src/misc_test.c
branchRCL_3
changeset 57 2efc27d87e1c
parent 0 e4d67989cc36
equal deleted inserted replaced
56:acd3cd4aaceb 57:2efc27d87e1c
       
     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 <string.h>
       
    26 #include "glib.h"
       
    27 #include <stdlib.h>
       
    28 
       
    29 
       
    30 #ifdef __SYMBIAN32__
       
    31 #include "mrt2_glib2_test.h"
       
    32 #endif /*__SYMBIAN32__*/
       
    33 
       
    34 void array_test()
       
    35 {
       
    36 	GArray *garray;
       
    37 	gint i;
       
    38 	int a = 1;
       
    39 	int b[3] = 
       
    40 	{
       
    41 		4,5,6
       
    42 	};
       
    43 		
       
    44 	garray = g_array_new (FALSE,FALSE,sizeof(gint));
       
    45 	
       
    46 	g_array_append_val(garray,a);
       
    47 	g_array_append_val(garray,a);
       
    48 	g_array_append_val(garray,a);
       
    49 	
       
    50 	g_array_insert_vals(garray,0,b,3);
       
    51 	
       
    52     g_assert (g_array_index (garray, gint, 0) == 4);
       
    53     g_assert (g_array_index (garray, gint, 1) == 5);
       
    54     g_assert (g_array_index (garray, gint, 2) == 6);
       
    55 	
       
    56 	g_array_free(garray,TRUE);
       
    57 	
       
    58 }
       
    59 
       
    60 void g_ascii_strdown_test()
       
    61 {
       
    62 	gchar str[] = "ABCd";
       
    63 	gchar *str1;
       
    64 	str1 = g_ascii_strdown(str,4);
       
    65 	g_assert(str1[0] == 'a');
       
    66 	g_assert(str1[1] == 'b');
       
    67 	g_assert(str1[2] == 'c');
       
    68 	g_assert(str1[3] == 'd');	
       
    69 }
       
    70 
       
    71 void g_ascii_strup_test()
       
    72 {
       
    73 	gchar str[] = "ABCd";
       
    74 	gchar *str1;
       
    75 	str1 = g_ascii_strup(str,4);
       
    76 	g_assert(str1[0] == 'A');
       
    77 	g_assert(str1[1] == 'B');
       
    78 	g_assert(str1[2] == 'C');
       
    79 	g_assert(str1[3] == 'D');	
       
    80 }
       
    81 
       
    82 void g_fprintf_test()
       
    83 {
       
    84 	FILE *fp;
       
    85 	char *teststring = "testing";
       
    86 	int retVal;
       
    87 	fp = fopen("c:\\test.txt","w");
       
    88 	if(fp)
       
    89 	{
       
    90 		g_assert(g_fprintf(fp,"%s",teststring) == strlen(teststring));
       
    91 		fclose(fp);
       
    92 	}
       
    93 }
       
    94 
       
    95 void g_application_name_test()
       
    96 {
       
    97 	gchar *app_name = "Test App";
       
    98 	g_set_application_name(app_name);
       
    99 	g_assert(!strcmp(g_get_application_name(),app_name));
       
   100 }
       
   101 
       
   102 void g_listenv_test()
       
   103 {
       
   104 	gchar **e = NULL;
       
   105 	int i;
       
   106 	e = g_listenv();
       
   107 	g_assert(e!=NULL);
       
   108 	g_strfreev(e);
       
   109 }
       
   110 
       
   111 void g_direct_equal_test()
       
   112 {
       
   113 	int *i,a=1,*j;
       
   114 	i = &a;
       
   115 	j=i;
       
   116 	g_assert(g_direct_equal(i,j));
       
   117 	j++;
       
   118 	g_assert(!g_direct_equal(i,j));
       
   119 }
       
   120 
       
   121 void g_direct_hash_test()
       
   122 {
       
   123 	int *i,a=1,j;
       
   124 	int hash_value;
       
   125 	i = &a;	
       
   126 	j = (gint)i;
       
   127 	g_assert(g_direct_hash(i) == j);
       
   128 }
       
   129 
       
   130 void g_bit_nth_lsf_test()
       
   131 {
       
   132 	gulong mask = 15;
       
   133 	
       
   134 	// 15 is 00000000 ........ 1111. Therefore the position of first 1 should be 0
       
   135 	g_assert(g_bit_nth_lsf(mask,-1) == 0);
       
   136 }
       
   137 
       
   138 void g_bit_nth_msf_test()
       
   139 {
       
   140 	gulong mask = 15;
       
   141 	
       
   142 	// 15 is 00000000 ........ 1111. Therefore the position of last 1 should be 3
       
   143 	g_assert(g_bit_nth_msf(mask,-1) == 3);
       
   144 }
       
   145 
       
   146 void g_basename_test()
       
   147 {
       
   148 	const gchar *filename;
       
   149 	filename = g_basename("c:\\test\\test.txt");
       
   150 	g_assert(!strcmp(filename,"test.txt"));
       
   151 }
       
   152 
       
   153 
       
   154 void function()
       
   155 {
       
   156 	return;
       
   157 }
       
   158 
       
   159 void g_atexit_test()
       
   160 {
       
   161 	g_atexit(function);
       
   162 }
       
   163 
       
   164 void g_bit_storage_test()
       
   165 {
       
   166 	g_assert(g_bit_storage(8) == 4);
       
   167 }
       
   168 
       
   169 void g_filename_display_basename_test()
       
   170 {
       
   171 	g_assert(!strcmp(g_filename_display_basename("c:/test/test.txt"),"test.txt"));
       
   172 }
       
   173 
       
   174 void g_find_program_in_path_test()
       
   175 {
       
   176 	char *program_name = g_find_program_in_path("misc_test.exe");
       
   177 }
       
   178 
       
   179 void g_atomic_test()
       
   180 {
       
   181 	int i = 5;
       
   182 	int j;
       
   183 	char *p,*q;
       
   184 	
       
   185 	j = g_atomic_int_get(&i);
       
   186 	g_assert(j == i);
       
   187 	
       
   188 	p = g_malloc(1);
       
   189 	q = g_atomic_pointer_get(&p);
       
   190 	g_assert(p == q);
       
   191 	
       
   192 	g_free(p);
       
   193 }
       
   194 
       
   195 void g_error_test()
       
   196 {
       
   197 	GError err,*err_copy = NULL;
       
   198 	err.domain = 1;
       
   199 	err.code = 5;
       
   200 	err.message = "test";
       
   201 	err_copy = g_error_copy(&err);
       
   202 	g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test"));
       
   203 	g_free(err_copy);
       
   204 	err_copy = NULL;
       
   205 	err_copy = g_error_new_literal(err.domain,err.code,"test is %s");
       
   206 	g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test is %s"));
       
   207 	g_free(err_copy);
       
   208 	err_copy = NULL;
       
   209 }
       
   210 
       
   211 static guint
       
   212 my_hash (gconstpointer key)
       
   213 {
       
   214   return (guint) *((const gint*) key);
       
   215 }
       
   216 
       
   217 static gboolean
       
   218 my_hash_equal (gconstpointer a,
       
   219 	       gconstpointer b)
       
   220 {
       
   221   return *((const gint*) a) == *((const gint*) b);
       
   222 }
       
   223 
       
   224 static gboolean find_first     (gpointer key, 
       
   225 				gpointer value, 
       
   226 				gpointer user_data)
       
   227 {
       
   228   gint *v = value; 
       
   229   gint *test = user_data;
       
   230   return (*v == *test);
       
   231 }
       
   232 
       
   233 gboolean func(gpointer key,gpointer value,gpointer user_data)
       
   234 {
       
   235 	gint *key1 = (int *)key;
       
   236 	gint *value1 = (int *)value;
       
   237 	gint *user_data1 = (int *)user_data;
       
   238 	if(*key1 == *user_data1 && *value1 == *user_data1)
       
   239 		return TRUE;
       
   240 	else
       
   241 		return FALSE;
       
   242 }
       
   243 
       
   244 void hash_test()
       
   245 {
       
   246 	GHashTable *hash_table;
       
   247 	gint i;
       
   248 	gint value = 4;
       
   249 	gint *pvalue; 
       
   250 	int array[10];
       
   251 	int count = 0;
       
   252 
       
   253 	hash_table = g_hash_table_new (my_hash, my_hash_equal);
       
   254 	for (i = 0; i < 10; i++)
       
   255 	{
       
   256 	  array[i] = i;
       
   257 	  g_hash_table_insert (hash_table, &array[i], &array[i]);
       
   258 	}
       
   259 	
       
   260 	g_assert(g_hash_table_steal(hash_table,&value));
       
   261 	
       
   262 	pvalue = g_hash_table_find (hash_table, find_first, &value);
       
   263 	
       
   264 	//checks if g_hash_table_steal worked or not
       
   265 	g_assert(pvalue == NULL);
       
   266 	
       
   267 	value = 5;
       
   268 	
       
   269 	pvalue = g_hash_table_find (hash_table, find_first, &value);
       
   270 	
       
   271 	count = g_hash_table_foreach_steal(hash_table,func,&value);
       
   272 	
       
   273 	pvalue = g_hash_table_find (hash_table, find_first, &value);
       
   274 	
       
   275 	g_assert(count == 1 && pvalue == NULL);
       
   276 
       
   277 }
       
   278 
       
   279 void check_version_test()
       
   280 {
       
   281     const char *x = glib_check_version(2,8,3);
       
   282 	g_assert(x == NULL);
       
   283 }
       
   284 
       
   285 int main (int   argc,
       
   286       char *argv[])
       
   287 {
       
   288 	#ifdef __SYMBIAN32__
       
   289 	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);
       
   290 	#endif /*__SYMBIAN32__*/
       
   291 	
       
   292 	array_test();
       
   293 	g_ascii_strup_test();
       
   294 	g_ascii_strdown_test();
       
   295 	g_fprintf_test();
       
   296 	g_application_name_test();
       
   297 	g_listenv_test();
       
   298 	g_direct_equal_test();
       
   299 	g_direct_hash_test();
       
   300 	g_bit_nth_lsf_test();
       
   301 	g_bit_nth_msf_test();
       
   302 	g_basename_test();
       
   303 	g_atexit_test();
       
   304 	g_bit_storage_test();
       
   305 	g_filename_display_basename_test();
       
   306 	g_find_program_in_path_test();
       
   307 	g_atomic_test();
       
   308 	g_error_test();
       
   309 	hash_test();
       
   310 	check_version_test();
       
   311 	
       
   312 	#ifdef __SYMBIAN32__
       
   313   	testResultXml("misc_test");
       
   314   	#endif /* EMULATOR */
       
   315 	
       
   316 	return 0;
       
   317 }