os/ossrv/glib/tsrc/BC/src/keyfile_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/keyfile_test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,483 @@
     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 <locale.h>
    1.34 +#include <string.h>
    1.35 +#include <stdlib.h>
    1.36 +
    1.37 +#ifdef SYMBIAN
    1.38 +#include "mrt2_glib2_test.h"
    1.39 +#endif /*SYMBIAN*/
    1.40 +
    1.41 +
    1.42 +static GKeyFile *
    1.43 +load_data (const gchar   *data, 
    1.44 +	   GKeyFileFlags  flags)
    1.45 +{
    1.46 +  GKeyFile *keyfile;
    1.47 +  GError *error = NULL;
    1.48 +
    1.49 +  keyfile = g_key_file_new ();
    1.50 +  g_key_file_load_from_data (keyfile, data, -1, flags, &error);
    1.51 +  if (error)
    1.52 +    {
    1.53 +      g_print ("Could not load data: %s\n", error->message);
    1.54 +      
    1.55 +      g_assert(FALSE && "keyfile_test failed");
    1.56 +      
    1.57 +      #ifdef SYMBIAN
    1.58 +  	  testResultXml("keyfile_test");
    1.59 +  	  #endif /* EMULATOR */
    1.60 +      
    1.61 +      exit (1);
    1.62 +    }
    1.63 +  
    1.64 +  return keyfile;
    1.65 +}
    1.66 +
    1.67 +static void
    1.68 +check_error (GError **error,
    1.69 +	     GQuark   domain,
    1.70 +	     gint     code)
    1.71 +{
    1.72 +  if (*error == NULL)
    1.73 +    {
    1.74 +      g_print ("Missing an error\n");
    1.75 +      exit (1);
    1.76 +    }
    1.77 +  
    1.78 +  if ((*error)->domain != domain)
    1.79 +    {
    1.80 +      g_print ("Wrong error domain: got %s, expected %s\n",
    1.81 +	       g_quark_to_string ((*error)->domain),
    1.82 +	       g_quark_to_string (domain));
    1.83 +	  
    1.84 +	  g_assert(FALSE && "keyfile_test failed");
    1.85 +      
    1.86 +      #ifdef SYMBIAN
    1.87 +  	  testResultXml("keyfile_test");
    1.88 +  	  #endif /* EMULATOR */
    1.89 +  	  
    1.90 +      exit (1);
    1.91 +    }
    1.92 +  
    1.93 +  if ((*error)->code != code)
    1.94 +    {
    1.95 +      g_print ("Wrong error code: got %d, expected %d\n",
    1.96 +	       (*error)->code, code);
    1.97 +	       
    1.98 +	  g_assert(FALSE && "keyfile_test failed");
    1.99 +      
   1.100 +      #ifdef SYMBIAN
   1.101 +  	  testResultXml("keyfile_test");
   1.102 +  	  #endif /* EMULATOR */
   1.103 +      
   1.104 +      exit (1);
   1.105 +    }
   1.106 +
   1.107 +  g_error_free (*error);
   1.108 +  *error = NULL;
   1.109 +}
   1.110 +
   1.111 +static void 
   1.112 +check_no_error (GError **error)
   1.113 +{
   1.114 +  if (*error != NULL)
   1.115 +    {
   1.116 +      g_print ("Unexpected error: (%s, %d) %s\n",
   1.117 +	       g_quark_to_string ((*error)->domain),
   1.118 +	       (*error)->code, (*error)->message);
   1.119 +	  g_assert(FALSE && "keyfile_test failed");
   1.120 +      
   1.121 +      #ifdef SYMBIAN
   1.122 +  	  testResultXml("keyfile_test");
   1.123 +  	  #endif /* EMULATOR */
   1.124 +      
   1.125 +      exit (1);
   1.126 +    }
   1.127 +}
   1.128 +
   1.129 +static void
   1.130 +check_string_value (GKeyFile    *keyfile,
   1.131 +		    const gchar *group,
   1.132 +		    const gchar *key,
   1.133 +		    const gchar *expected) 
   1.134 +{
   1.135 +  GError *error = NULL;
   1.136 +  gchar *value;
   1.137 +
   1.138 +  value = g_key_file_get_string (keyfile, group, key, &error);
   1.139 +  check_no_error (&error);
   1.140 +  g_assert (value != NULL);
   1.141 +
   1.142 +  if (strcmp (value, expected) != 0)
   1.143 +    {
   1.144 +      g_print ("Group %s key %s: "
   1.145 +	       "expected string value '%s', actual value '%s'\n",
   1.146 +	       group, key, expected, value);      
   1.147 +	  
   1.148 +	  g_assert(FALSE && "keyfile_test failed");
   1.149 +      
   1.150 +      #ifdef SYMBIAN
   1.151 +  	  testResultXml("keyfile_test");
   1.152 +  	  #endif /* EMULATOR */
   1.153 +      
   1.154 +      exit (1);
   1.155 +    }
   1.156 +
   1.157 +  g_free (value);
   1.158 +}
   1.159 +
   1.160 +static void
   1.161 +check_locale_string_value (GKeyFile    *keyfile,
   1.162 +			   const gchar *group,
   1.163 +			   const gchar *key,
   1.164 +			   const gchar *locale,
   1.165 +			   const gchar *expected) 
   1.166 +{
   1.167 +  GError *error = NULL;
   1.168 +  gchar *value;
   1.169 +
   1.170 +  value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
   1.171 +  check_no_error (&error);
   1.172 +  g_assert (value != NULL);
   1.173 +
   1.174 +  if (strcmp (value, expected) != 0)
   1.175 +    {
   1.176 +      g_print ("Group %s key %s locale %s: "
   1.177 +	       "expected string value '%s', actual value '%s'\n",
   1.178 +	       group, key, locale, expected, value);      
   1.179 +	  
   1.180 +	  g_assert(FALSE && "keyfile_test failed");
   1.181 +      
   1.182 +      #ifdef SYMBIAN
   1.183 +  	  testResultXml("keyfile_test");
   1.184 +  	  #endif /* EMULATOR */
   1.185 +  	  
   1.186 +      exit (1);
   1.187 +    }
   1.188 +
   1.189 +  g_free (value);
   1.190 +}
   1.191 +
   1.192 +static void
   1.193 +check_boolean_value (GKeyFile    *keyfile,
   1.194 +		     const gchar *group,
   1.195 +		     const gchar *key,
   1.196 +		     gboolean     expected) 
   1.197 +{
   1.198 +  GError *error = NULL;
   1.199 +  gboolean value;
   1.200 +
   1.201 +  value = g_key_file_get_boolean (keyfile, group, key, &error);
   1.202 +  check_no_error (&error);
   1.203 +
   1.204 +  if (value != expected)
   1.205 +    {
   1.206 +      g_print ("Group %s key %s: "
   1.207 +	       "expected boolean value '%s', actual value '%s'\n",
   1.208 +	       group, key, 
   1.209 +	       expected ? "true" : "false", 
   1.210 +	       value ? "true" : "false");      
   1.211 +	       
   1.212 +	       
   1.213 +	  g_assert(FALSE && "keyfile_test failed");
   1.214 +      
   1.215 +      #ifdef SYMBIAN
   1.216 +  	  testResultXml("keyfile_test");
   1.217 +  	  #endif /* EMULATOR */
   1.218 +  	  
   1.219 +      exit (1);
   1.220 +    }
   1.221 +}
   1.222 +
   1.223 +static void
   1.224 +check_integer_value (GKeyFile    *keyfile,
   1.225 +		     const gchar *group,
   1.226 +		     const gchar *key,
   1.227 +		     gint         expected) 
   1.228 +{
   1.229 +  GError *error = NULL;
   1.230 +  gint value;
   1.231 +
   1.232 +  value = g_key_file_get_integer (keyfile, group, key, &error);
   1.233 +  check_no_error (&error);
   1.234 +
   1.235 +  if (value != expected)
   1.236 +    {
   1.237 +      g_print ("Group %s key %s: "
   1.238 +	       "expected integer value %d, actual value %d\n",
   1.239 +	       group, key, expected, value);      
   1.240 +	  
   1.241 +	  g_assert(FALSE && "keyfile_test failed");
   1.242 +      
   1.243 +      #ifdef SYMBIAN
   1.244 +  	  testResultXml("keyfile_test");
   1.245 +  	  #endif /* EMULATOR */
   1.246 +  	  
   1.247 +      exit (1);
   1.248 +    }
   1.249 +}
   1.250 +
   1.251 +int create_file(char *path)
   1.252 +{
   1.253 +	FILE *fp = fopen(path,"w");
   1.254 +	if(fp)
   1.255 +	{
   1.256 +		fprintf(fp,"[group1]\n");
   1.257 +		fprintf(fp,"key1=123\n");
   1.258 +		fprintf(fp,"key2=456\n");
   1.259 +		fclose(fp);
   1.260 +		return TRUE;
   1.261 +	}
   1.262 +	return FALSE;
   1.263 +}
   1.264 +
   1.265 +void test_loadfromfile()
   1.266 +{
   1.267 +	
   1.268 +	GKeyFile *keyfile = g_key_file_new(),*keyfile1=g_key_file_new();
   1.269 +	
   1.270 +	gchar *full_path = NULL;	
   1.271 +
   1.272 +	int ret;
   1.273 +	if(create_file("c:\\tempfile.txt"))
   1.274 +	{
   1.275 +		g_assert(g_key_file_load_from_file(keyfile,"c:\\tempfile.txt",0,NULL));
   1.276 +		check_string_value (keyfile, "group1", "key1", "123");
   1.277 +		check_string_value (keyfile, "group1", "key2", "456");
   1.278 +	}
   1.279 +	else
   1.280 +		g_assert(FALSE);
   1.281 +	
   1.282 +	g_key_file_free(keyfile);
   1.283 +	
   1.284 +	if(create_file("tempfile.txt"))
   1.285 +		g_assert(g_key_file_load_from_data_dirs(keyfile1,"tempfile.txt",&full_path,0,NULL));
   1.286 +	
   1.287 +	
   1.288 +}
   1.289 +
   1.290 +void test_setstring()
   1.291 +{
   1.292 +	GKeyFile *keyfile;
   1.293 +	gchar **value;
   1.294 +	gsize n = 2;
   1.295 +	
   1.296 +	const gchar *list[2] = 
   1.297 +	{
   1.298 +		"test1","test2"	
   1.299 +	};
   1.300 +
   1.301 +  	const gchar *data = 
   1.302 +	    "[1]\n"
   1.303 +	    "key1=123\n"
   1.304 +	    "[2]\n"
   1.305 +	    "key2=456\n";
   1.306 +
   1.307 +	keyfile = load_data (data, 0);
   1.308 +	
   1.309 +	check_string_value (keyfile, "1", "key1", "123");
   1.310 +	g_key_file_set_string(keyfile,"1","key1","789");
   1.311 +	check_string_value (keyfile, "1", "key1", "789");
   1.312 +	
   1.313 +	g_key_file_set_string_list(keyfile,"1","key1",list,2);
   1.314 +	
   1.315 +	value = g_key_file_get_string_list(keyfile, "1", "key1",&n, NULL);
   1.316 +	
   1.317 +	g_assert(!strcmp(value[0],"test1"));
   1.318 +	g_assert(!strcmp(value[1],"test2"));
   1.319 +	g_strfreev(value);
   1.320 +}
   1.321 +
   1.322 +void test_setboolean()
   1.323 +{
   1.324 +	GKeyFile *keyfile;
   1.325 +	gboolean *value;
   1.326 +	gsize n = 2;
   1.327 +	
   1.328 +	gboolean list[2] = {TRUE,FALSE};
   1.329 +	
   1.330 +	const gchar *data = 
   1.331 +	    "[1]\n"
   1.332 +	    "key1=true\n"
   1.333 +	    "key2=false\n";
   1.334 +
   1.335 +	keyfile = load_data (data, 0);
   1.336 +	
   1.337 +	check_boolean_value (keyfile, "1", "key1", TRUE);
   1.338 +	g_key_file_set_boolean(keyfile,"1","key1",FALSE);
   1.339 +	check_boolean_value (keyfile, "1", "key1", FALSE);
   1.340 +	
   1.341 +	g_key_file_set_boolean_list(keyfile,"1","key1",list,2);
   1.342 +	
   1.343 +	value = g_key_file_get_boolean_list(keyfile, "1", "key1",&n, NULL);
   1.344 +	
   1.345 +	g_assert(value[0] == TRUE);
   1.346 +	g_assert(value[1] == FALSE);
   1.347 +	
   1.348 +}
   1.349 +
   1.350 +void test_setinteger()
   1.351 +{
   1.352 +	GKeyFile *keyfile;
   1.353 +	gint *value;
   1.354 +	gsize n = 2;
   1.355 +	
   1.356 +	gint list[2] = {111,222};
   1.357 +	
   1.358 +	const gchar *data = 
   1.359 +	    "[1]\n"
   1.360 +	    "key1=123\n"
   1.361 +	    "key2=456\n";
   1.362 +
   1.363 +	keyfile = load_data (data, 0);
   1.364 +	
   1.365 +	check_integer_value (keyfile, "1", "key1", 123);
   1.366 +	g_key_file_set_integer(keyfile,"1","key1",789);
   1.367 +	check_integer_value (keyfile, "1", "key1", 789);
   1.368 +	
   1.369 +	g_key_file_set_integer_list(keyfile,"1","key1",list,2);
   1.370 +	
   1.371 +	value = g_key_file_get_integer_list(keyfile, "1", "key1",&n, NULL);
   1.372 +	
   1.373 +	g_assert(value[0] == 111);
   1.374 +	g_assert(value[1] == 222);
   1.375 +	
   1.376 +}
   1.377 +
   1.378 +void test_keyfiletodata()
   1.379 +{
   1.380 +	GKeyFile *keyfile;
   1.381 +	
   1.382 +	gchar *retVal = NULL;
   1.383 +	gsize length = 0;
   1.384 +	
   1.385 +	const gchar *data = 
   1.386 +	    "[1]\n"
   1.387 +	    "key1=123\n"
   1.388 +	    "key2=456\n";
   1.389 +	
   1.390 +	keyfile = load_data (data, 0);
   1.391 +	
   1.392 +	retVal = g_key_file_to_data(keyfile,&length,NULL);
   1.393 +	
   1.394 +	g_assert(retVal != NULL && length > 0);
   1.395 +}
   1.396 +
   1.397 +void test_setcomment()
   1.398 +{
   1.399 +	GKeyFile *keyfile;
   1.400 +	gchar *comment;
   1.401 +
   1.402 +	const gchar *data = 
   1.403 +	    "[1]\n"
   1.404 +	    "key1=123\n"
   1.405 +	    "key2=456\n";
   1.406 +	
   1.407 +	keyfile = load_data (data, 0);
   1.408 +	
   1.409 +	g_key_file_set_comment(keyfile,"1","key1","comment1",NULL);
   1.410 +	
   1.411 +	comment = g_key_file_get_comment(keyfile,"1","key1",NULL);
   1.412 +	
   1.413 +	g_assert(!strcmp(comment,"comment1\n"));
   1.414 +	
   1.415 +	g_key_file_remove_comment(keyfile,"1","key1",NULL);
   1.416 +	
   1.417 +	g_free(comment);
   1.418 +	
   1.419 +	comment = g_key_file_get_comment(keyfile,"1","key1",NULL);
   1.420 +	
   1.421 +	g_assert(comment == NULL);
   1.422 +	
   1.423 +}
   1.424 +
   1.425 +void test_setlocale()
   1.426 +{
   1.427 +	GKeyFile *keyfile;
   1.428 +	gchar *value;
   1.429 +	gchar **list;
   1.430 +	gchar *input_list[2] = {"test2","test3"};
   1.431 +	gsize length = 2;
   1.432 +	
   1.433 +	const gchar *data = 
   1.434 +	    "[1]\n"
   1.435 +	    "key1=123\n"
   1.436 +	    "key2=456\n";
   1.437 +	
   1.438 +	keyfile = load_data (data, 0);
   1.439 +	
   1.440 +	g_key_file_set_locale_string(keyfile,"1","key3","C","test");
   1.441 +	
   1.442 +	value = g_key_file_get_locale_string(keyfile,"1","key3","C",NULL);
   1.443 +	
   1.444 +	g_assert(!strcmp(value,"test"));
   1.445 +	
   1.446 +	g_free(value);
   1.447 +	
   1.448 +	value = g_key_file_get_locale_string(keyfile,"1","key3","de",NULL);
   1.449 +	
   1.450 +	g_assert(value == NULL);
   1.451 +	
   1.452 +	g_key_file_set_locale_string_list(keyfile,"1","key4","C",input_list,2);
   1.453 +	
   1.454 +	list = g_key_file_get_locale_string_list(keyfile,"1","key4","C",&length,NULL);
   1.455 +	
   1.456 +	g_assert(!strcmp(list[0],"test2"));
   1.457 +	g_assert(!strcmp(list[1],"test3"));
   1.458 +	
   1.459 +}
   1.460 +
   1.461 +int
   1.462 +main (int argc, char *argv[])
   1.463 +{
   1.464 +  	#ifdef SYMBIAN
   1.465 +	
   1.466 +	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.467 +	#endif /*SYMBIAN*/
   1.468 +  
   1.469 +  test_loadfromfile();
   1.470 +  test_setstring();
   1.471 +  test_setboolean();
   1.472 +  test_setinteger();
   1.473 +  test_keyfiletodata();
   1.474 +  test_setcomment();
   1.475 +  test_setlocale();
   1.476 +  
   1.477 +  #ifdef SYMBIAN
   1.478 +  testResultXml("keyfile_test");
   1.479 +  #endif /* EMULATOR */
   1.480 +   
   1.481 +  #ifdef SYMBIAN
   1.482 +  testResultXml("keyfile_test");
   1.483 +  #endif /* EMULATOR */
   1.484 +  
   1.485 +  return 0;
   1.486 +}