os/ossrv/glib/tsrc/BC/src/misc_test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/src/misc_test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,321 @@
     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 <string.h>
    1.33 +#include "glib.h"
    1.34 +#include <stdlib.h>
    1.35 +#include <glib/gprintf.h>
    1.36 +
    1.37 +#ifdef SYMBIAN
    1.38 +#include "mrt2_glib2_test.h"
    1.39 +#endif /*SYMBIAN*/
    1.40 +
    1.41 +void array_test()
    1.42 +{
    1.43 +	GArray *garray;
    1.44 +	gint i;
    1.45 +	int a = 1;
    1.46 +	int b[3] = 
    1.47 +	{
    1.48 +		4,5,6
    1.49 +	};
    1.50 +		
    1.51 +	garray = g_array_new (FALSE,FALSE,sizeof(gint));
    1.52 +	
    1.53 +	g_array_append_val(garray,a);
    1.54 +	g_array_append_val(garray,a);
    1.55 +	g_array_append_val(garray,a);
    1.56 +	
    1.57 +	g_array_insert_vals(garray,0,b,3);
    1.58 +	
    1.59 +    g_assert (g_array_index (garray, gint, 0) == 4);
    1.60 +    g_assert (g_array_index (garray, gint, 1) == 5);
    1.61 +    g_assert (g_array_index (garray, gint, 2) == 6);
    1.62 +	
    1.63 +	g_array_free(garray,TRUE);
    1.64 +	
    1.65 +}
    1.66 +
    1.67 +void g_ascii_strdown_test()
    1.68 +{
    1.69 +	gchar str[] = "ABCd";
    1.70 +	gchar *str1;
    1.71 +	str1 = g_ascii_strdown(str,4);
    1.72 +	g_assert(str1[0] == 'a');
    1.73 +	g_assert(str1[1] == 'b');
    1.74 +	g_assert(str1[2] == 'c');
    1.75 +	g_assert(str1[3] == 'd');	
    1.76 +}
    1.77 +
    1.78 +void g_ascii_strup_test()
    1.79 +{
    1.80 +	gchar str[] = "ABCd";
    1.81 +	gchar *str1;
    1.82 +	str1 = g_ascii_strup(str,4);
    1.83 +	g_assert(str1[0] == 'A');
    1.84 +	g_assert(str1[1] == 'B');
    1.85 +	g_assert(str1[2] == 'C');
    1.86 +	g_assert(str1[3] == 'D');	
    1.87 +}
    1.88 +
    1.89 +void g_fprintf_test()
    1.90 +{
    1.91 +	FILE *fp;
    1.92 +	char *teststring = "testing";
    1.93 +	int retVal;
    1.94 +	fp = fopen("c:\\test.txt","w");
    1.95 +	if(fp)
    1.96 +	{
    1.97 +		g_assert(g_fprintf(fp,"%s",teststring) == strlen(teststring));
    1.98 +		fclose(fp);
    1.99 +	}
   1.100 +}
   1.101 +
   1.102 +void g_application_name_test()
   1.103 +{
   1.104 +	gchar *app_name = "Test App";
   1.105 +	g_set_application_name(app_name);
   1.106 +	g_assert(!strcmp(g_get_application_name(),app_name));
   1.107 +}
   1.108 +
   1.109 +void g_listenv_test()
   1.110 +{
   1.111 +	gchar **e = NULL;
   1.112 +	int i;
   1.113 +	e = g_listenv();
   1.114 +	g_assert(e!=NULL);
   1.115 +	g_strfreev(e);
   1.116 +}
   1.117 +
   1.118 +void g_direct_equal_test()
   1.119 +{
   1.120 +	int *i,a=1,*j;
   1.121 +	i = &a;
   1.122 +	j=i;
   1.123 +	g_assert(g_direct_equal(i,j));
   1.124 +	j++;
   1.125 +	g_assert(!g_direct_equal(i,j));
   1.126 +}
   1.127 +
   1.128 +void g_direct_hash_test()
   1.129 +{
   1.130 +	int *i,a=1,j;
   1.131 +	int hash_value;
   1.132 +	i = &a;	
   1.133 +	j = (gint)i;
   1.134 +	g_assert(g_direct_hash(i) == j);
   1.135 +}
   1.136 +
   1.137 +void g_bit_nth_lsf_test()
   1.138 +{
   1.139 +	gulong mask = 15;
   1.140 +	
   1.141 +	// 15 is 00000000 ........ 1111. Therefore the position of first 1 should be 0
   1.142 +	g_assert(g_bit_nth_lsf(mask,-1) == 0);
   1.143 +}
   1.144 +
   1.145 +void g_bit_nth_msf_test()
   1.146 +{
   1.147 +	gulong mask = 15;
   1.148 +	
   1.149 +	// 15 is 00000000 ........ 1111. Therefore the position of last 1 should be 3
   1.150 +	g_assert(g_bit_nth_msf(mask,-1) == 3);
   1.151 +}
   1.152 +
   1.153 +void g_basename_test()
   1.154 +{
   1.155 +	const gchar *filename;
   1.156 +	filename = g_basename("c:\\test\\test.txt");
   1.157 +	g_assert(!strcmp(filename,"test.txt"));
   1.158 +}
   1.159 +
   1.160 +
   1.161 +void function()
   1.162 +{
   1.163 +	return;
   1.164 +}
   1.165 +
   1.166 +void g_atexit_test()
   1.167 +{
   1.168 +	g_atexit(function);
   1.169 +}
   1.170 +
   1.171 +void g_bit_storage_test()
   1.172 +{
   1.173 +	g_assert(g_bit_storage(8) == 4);
   1.174 +}
   1.175 +
   1.176 +void g_filename_display_basename_test()
   1.177 +{
   1.178 +	g_assert(!strcmp(g_filename_display_basename("c:/test/test.txt"),"test.txt"));
   1.179 +}
   1.180 +
   1.181 +void g_find_program_in_path_test()
   1.182 +{
   1.183 +	char *program_name = g_find_program_in_path("misc_test.exe");
   1.184 +}
   1.185 +
   1.186 +void g_atomic_test()
   1.187 +{
   1.188 +	int i = 5;
   1.189 +	int j;
   1.190 +	char *p,*q;
   1.191 +	
   1.192 +	j = g_atomic_int_get(&i);
   1.193 +	g_assert(j == i);
   1.194 +	
   1.195 +	p = g_malloc(1);
   1.196 +	q = g_atomic_pointer_get(&p);
   1.197 +	g_assert(p == q);
   1.198 +	
   1.199 +	g_free(p);
   1.200 +}
   1.201 +
   1.202 +void g_error_test()
   1.203 +{
   1.204 +	GError err,*err_copy = NULL;
   1.205 +	err.domain = 1;
   1.206 +	err.code = 5;
   1.207 +	err.message = "test";
   1.208 +	err_copy = g_error_copy(&err);
   1.209 +	g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test"));
   1.210 +	g_error_free(err_copy);
   1.211 +	err_copy = NULL;
   1.212 +	err_copy = g_error_new_literal(err.domain,err.code,"test is %s");
   1.213 +	g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test is %s"));
   1.214 +	g_error_free(err_copy);
   1.215 +	err_copy = NULL;
   1.216 +}
   1.217 +
   1.218 +static guint
   1.219 +my_hash (gconstpointer key)
   1.220 +{
   1.221 +  return (guint) *((const gint*) key);
   1.222 +}
   1.223 +
   1.224 +static gboolean
   1.225 +my_hash_equal (gconstpointer a,
   1.226 +	       gconstpointer b)
   1.227 +{
   1.228 +  return *((const gint*) a) == *((const gint*) b);
   1.229 +}
   1.230 +
   1.231 +static gboolean find_first     (gpointer key, 
   1.232 +				gpointer value, 
   1.233 +				gpointer user_data)
   1.234 +{
   1.235 +  gint *v = value; 
   1.236 +  gint *test = user_data;
   1.237 +  return (*v == *test);
   1.238 +}
   1.239 +
   1.240 +gboolean func(gpointer key,gpointer value,gpointer user_data)
   1.241 +{
   1.242 +	gint *key1 = (int *)key;
   1.243 +	gint *value1 = (int *)value;
   1.244 +	gint *user_data1 = (int *)user_data;
   1.245 +	if(*key1 == *user_data1 && *value1 == *user_data1)
   1.246 +		return TRUE;
   1.247 +	else
   1.248 +		return FALSE;
   1.249 +}
   1.250 +
   1.251 +void hash_test()
   1.252 +{
   1.253 +	GHashTable *hash_table;
   1.254 +	gint i;
   1.255 +	gint value = 4;
   1.256 +	gint *pvalue; 
   1.257 +	int array[10];
   1.258 +	int count = 0;
   1.259 +
   1.260 +	hash_table = g_hash_table_new (my_hash, my_hash_equal);
   1.261 +	for (i = 0; i < 10; i++)
   1.262 +	{
   1.263 +	  array[i] = i;
   1.264 +	  g_hash_table_insert (hash_table, &array[i], &array[i]);
   1.265 +	}
   1.266 +	
   1.267 +	g_assert(g_hash_table_steal(hash_table,&value));
   1.268 +	
   1.269 +	pvalue = g_hash_table_find (hash_table, find_first, &value);
   1.270 +	
   1.271 +	//checks if g_hash_table_steal worked or not
   1.272 +	g_assert(pvalue == NULL);
   1.273 +	
   1.274 +	value = 5;
   1.275 +	
   1.276 +	pvalue = g_hash_table_find (hash_table, find_first, &value);
   1.277 +	
   1.278 +	count = g_hash_table_foreach_steal(hash_table,func,&value);
   1.279 +	
   1.280 +	pvalue = g_hash_table_find (hash_table, find_first, &value);
   1.281 +	
   1.282 +	g_assert(count == 1 && pvalue == NULL);
   1.283 +
   1.284 +}
   1.285 +
   1.286 +void check_version_test()
   1.287 +{
   1.288 +    const char *x = glib_check_version(2,8,3);
   1.289 +	g_assert(x == NULL);
   1.290 +}
   1.291 +
   1.292 +int main (int   argc,
   1.293 +      char *argv[])
   1.294 +{
   1.295 +	#ifdef SYMBIAN
   1.296 +	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.297 +	#endif /*SYMBIAN*/
   1.298 +	
   1.299 +	array_test();
   1.300 +	g_ascii_strup_test();
   1.301 +	g_ascii_strdown_test();
   1.302 +	g_fprintf_test();
   1.303 +	g_application_name_test();
   1.304 +	g_listenv_test();
   1.305 +	g_direct_equal_test();
   1.306 +	g_direct_hash_test();
   1.307 +	g_bit_nth_lsf_test();
   1.308 +	g_bit_nth_msf_test();
   1.309 +	g_basename_test();
   1.310 +	g_atexit_test();
   1.311 +	g_bit_storage_test();
   1.312 +	g_filename_display_basename_test();
   1.313 +	g_find_program_in_path_test();
   1.314 +	g_atomic_test();
   1.315 +	g_error_test();
   1.316 +	hash_test();
   1.317 +	check_version_test();
   1.318 +	
   1.319 +	#ifdef SYMBIAN
   1.320 +  	testResultXml("misc_test");
   1.321 +  	#endif /* EMULATOR */
   1.322 +	
   1.323 +	return 0;
   1.324 +}