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