glib/tsrc/glib_nonstif/src/keyfile_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 <glib.h>
       
    26 #include <locale.h>
       
    27 #include <string.h>
       
    28 #include <stdlib.h>
       
    29 
       
    30 #ifdef __SYMBIAN32__
       
    31 #include "mrt2_glib2_test.h"
       
    32 #endif /*__SYMBIAN32__*/
       
    33 
       
    34 
       
    35 static GKeyFile *
       
    36 load_data (const gchar   *data, 
       
    37 	   GKeyFileFlags  flags)
       
    38 {
       
    39   GKeyFile *keyfile;
       
    40   GError *error = NULL;
       
    41 
       
    42   keyfile = g_key_file_new ();
       
    43   g_key_file_load_from_data (keyfile, data, -1, flags, &error);
       
    44   if (error)
       
    45     {
       
    46       g_print ("Could not load data: %s\n", error->message);
       
    47       
       
    48       g_assert(FALSE && "keyfile_test failed");
       
    49       
       
    50       #ifdef __SYMBIAN32__
       
    51   	  testResultXml("keyfile_test");
       
    52   	  #endif /* EMULATOR */
       
    53       
       
    54       exit (1);
       
    55     }
       
    56   
       
    57   return keyfile;
       
    58 }
       
    59 
       
    60 static void
       
    61 check_error (GError **error,
       
    62 	     GQuark   domain,
       
    63 	     gint     code)
       
    64 {
       
    65   if (*error == NULL)
       
    66     {
       
    67       g_print ("Missing an error\n");
       
    68       exit (1);
       
    69     }
       
    70   
       
    71   if ((*error)->domain != domain)
       
    72     {
       
    73       g_print ("Wrong error domain: got %s, expected %s\n",
       
    74 	       g_quark_to_string ((*error)->domain),
       
    75 	       g_quark_to_string (domain));
       
    76 	  
       
    77 	  g_assert(FALSE && "keyfile_test failed");
       
    78       
       
    79       #ifdef __SYMBIAN32__
       
    80   	  testResultXml("keyfile_test");
       
    81   	  #endif /* EMULATOR */
       
    82   	  
       
    83       exit (1);
       
    84     }
       
    85   
       
    86   if ((*error)->code != code)
       
    87     {
       
    88       g_print ("Wrong error code: got %d, expected %d\n",
       
    89 	       (*error)->code, code);
       
    90 	       
       
    91 	  g_assert(FALSE && "keyfile_test failed");
       
    92       
       
    93       #ifdef __SYMBIAN32__
       
    94   	  testResultXml("keyfile_test");
       
    95   	  #endif /* EMULATOR */
       
    96       
       
    97       exit (1);
       
    98     }
       
    99 
       
   100   g_error_free (*error);
       
   101   *error = NULL;
       
   102 }
       
   103 
       
   104 static void 
       
   105 check_no_error (GError **error)
       
   106 {
       
   107   if (*error != NULL)
       
   108     {
       
   109       g_print ("Unexpected error: (%s, %d) %s\n",
       
   110 	       g_quark_to_string ((*error)->domain),
       
   111 	       (*error)->code, (*error)->message);
       
   112 	  g_assert(FALSE && "keyfile_test failed");
       
   113       
       
   114       #ifdef __SYMBIAN32__
       
   115   	  testResultXml("keyfile_test");
       
   116   	  #endif /* EMULATOR */
       
   117       
       
   118       exit (1);
       
   119     }
       
   120 }
       
   121 
       
   122 static void
       
   123 check_string_value (GKeyFile    *keyfile,
       
   124 		    const gchar *group,
       
   125 		    const gchar *key,
       
   126 		    const gchar *expected) 
       
   127 {
       
   128   GError *error = NULL;
       
   129   gchar *value;
       
   130 
       
   131   value = g_key_file_get_string (keyfile, group, key, &error);
       
   132   check_no_error (&error);
       
   133   g_assert (value != NULL);
       
   134 
       
   135   if (strcmp (value, expected) != 0)
       
   136     {
       
   137       g_print ("Group %s key %s: "
       
   138 	       "expected string value '%s', actual value '%s'\n",
       
   139 	       group, key, expected, value);      
       
   140 	  
       
   141 	  g_assert(FALSE && "keyfile_test failed");
       
   142       
       
   143       #ifdef __SYMBIAN32__
       
   144   	  testResultXml("keyfile_test");
       
   145   	  #endif /* EMULATOR */
       
   146       
       
   147       exit (1);
       
   148     }
       
   149 
       
   150   g_free (value);
       
   151 }
       
   152 
       
   153 static void
       
   154 check_locale_string_value (GKeyFile    *keyfile,
       
   155 			   const gchar *group,
       
   156 			   const gchar *key,
       
   157 			   const gchar *locale,
       
   158 			   const gchar *expected) 
       
   159 {
       
   160   GError *error = NULL;
       
   161   gchar *value;
       
   162 
       
   163   value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
       
   164   check_no_error (&error);
       
   165   g_assert (value != NULL);
       
   166 
       
   167   if (strcmp (value, expected) != 0)
       
   168     {
       
   169       g_print ("Group %s key %s locale %s: "
       
   170 	       "expected string value '%s', actual value '%s'\n",
       
   171 	       group, key, locale, expected, value);      
       
   172 	  
       
   173 	  g_assert(FALSE && "keyfile_test failed");
       
   174       
       
   175       #ifdef __SYMBIAN32__
       
   176   	  testResultXml("keyfile_test");
       
   177   	  #endif /* EMULATOR */
       
   178   	  
       
   179       exit (1);
       
   180     }
       
   181 
       
   182   g_free (value);
       
   183 }
       
   184 
       
   185 static void
       
   186 check_boolean_value (GKeyFile    *keyfile,
       
   187 		     const gchar *group,
       
   188 		     const gchar *key,
       
   189 		     gboolean     expected) 
       
   190 {
       
   191   GError *error = NULL;
       
   192   gboolean value;
       
   193 
       
   194   value = g_key_file_get_boolean (keyfile, group, key, &error);
       
   195   check_no_error (&error);
       
   196 
       
   197   if (value != expected)
       
   198     {
       
   199       g_print ("Group %s key %s: "
       
   200 	       "expected boolean value '%s', actual value '%s'\n",
       
   201 	       group, key, 
       
   202 	       expected ? "true" : "false", 
       
   203 	       value ? "true" : "false");      
       
   204 	       
       
   205 	       
       
   206 	  g_assert(FALSE && "keyfile_test failed");
       
   207       
       
   208       #ifdef __SYMBIAN32__
       
   209   	  testResultXml("keyfile_test");
       
   210   	  #endif /* EMULATOR */
       
   211   	  
       
   212       exit (1);
       
   213     }
       
   214 }
       
   215 
       
   216 static void
       
   217 check_integer_value (GKeyFile    *keyfile,
       
   218 		     const gchar *group,
       
   219 		     const gchar *key,
       
   220 		     gint         expected) 
       
   221 {
       
   222   GError *error = NULL;
       
   223   gint value;
       
   224 
       
   225   value = g_key_file_get_integer (keyfile, group, key, &error);
       
   226   check_no_error (&error);
       
   227 
       
   228   if (value != expected)
       
   229     {
       
   230       g_print ("Group %s key %s: "
       
   231 	       "expected integer value %d, actual value %d\n",
       
   232 	       group, key, expected, value);      
       
   233 	  
       
   234 	  g_assert(FALSE && "keyfile_test failed");
       
   235       
       
   236       #ifdef __SYMBIAN32__
       
   237   	  testResultXml("keyfile_test");
       
   238   	  #endif /* EMULATOR */
       
   239   	  
       
   240       exit (1);
       
   241     }
       
   242 }
       
   243 
       
   244 int create_file(char *path)
       
   245 {
       
   246 	FILE *fp = fopen(path,"w");
       
   247 	if(fp)
       
   248 	{
       
   249 		fprintf(fp,"[group1]\n");
       
   250 		fprintf(fp,"key1=123\n");
       
   251 		fprintf(fp,"key2=456\n");
       
   252 		fclose(fp);
       
   253 		return TRUE;
       
   254 	}
       
   255 	return FALSE;
       
   256 }
       
   257 
       
   258 void test_loadfromfile()
       
   259 {
       
   260 	
       
   261 	GKeyFile *keyfile = g_key_file_new(),*keyfile1=g_key_file_new();
       
   262 	
       
   263 	gchar *full_path = NULL;	
       
   264 
       
   265 	int ret;
       
   266 	if(create_file("c:\\tempfile.txt"))
       
   267 	{
       
   268 		g_assert(g_key_file_load_from_file(keyfile,"c:\\tempfile.txt",0,NULL));
       
   269 		check_string_value (keyfile, "group1", "key1", "123");
       
   270 		check_string_value (keyfile, "group1", "key2", "456");
       
   271 	}
       
   272 	else
       
   273 		g_assert(FALSE);
       
   274 	
       
   275 	g_key_file_free(keyfile);
       
   276 	mkdir(g_get_user_data_dir () , 0666);
       
   277 	chdir(g_get_user_data_dir ());
       
   278 	
       
   279 	if(create_file("tempfile.txt"))
       
   280 		g_assert(g_key_file_load_from_data_dirs(keyfile1,"tempfile.txt",&full_path,0,NULL));
       
   281 	
       
   282 	
       
   283 }
       
   284 
       
   285 void test_setstring()
       
   286 {
       
   287 	GKeyFile *keyfile;
       
   288 	gchar **value;
       
   289 	gsize n = 2;
       
   290 	
       
   291 	const gchar *list[2] = 
       
   292 	{
       
   293 		"test1","test2"	
       
   294 	};
       
   295 
       
   296   	const gchar *data = 
       
   297 	    "[1]\n"
       
   298 	    "key1=123\n"
       
   299 	    "[2]\n"
       
   300 	    "key2=456\n";
       
   301 
       
   302 	keyfile = load_data (data, 0);
       
   303 	
       
   304 	check_string_value (keyfile, "1", "key1", "123");
       
   305 	g_key_file_set_string(keyfile,"1","key1","789");
       
   306 	check_string_value (keyfile, "1", "key1", "789");
       
   307 	
       
   308 	g_key_file_set_string_list(keyfile,"1","key1",list,2);
       
   309 	
       
   310 	value = g_key_file_get_string_list(keyfile, "1", "key1",&n, NULL);
       
   311 	
       
   312 	g_assert(!strcmp(value[0],"test1"));
       
   313 	g_assert(!strcmp(value[1],"test2"));
       
   314 	g_strfreev(value);
       
   315 }
       
   316 
       
   317 void test_setboolean()
       
   318 {
       
   319 	GKeyFile *keyfile;
       
   320 	gboolean *value;
       
   321 	gsize n = 2;
       
   322 	
       
   323 	gboolean list[2] = {TRUE,FALSE};
       
   324 	
       
   325 	const gchar *data = 
       
   326 	    "[1]\n"
       
   327 	    "key1=true\n"
       
   328 	    "key2=false\n";
       
   329 
       
   330 	keyfile = load_data (data, 0);
       
   331 	
       
   332 	check_boolean_value (keyfile, "1", "key1", TRUE);
       
   333 	g_key_file_set_boolean(keyfile,"1","key1",FALSE);
       
   334 	check_boolean_value (keyfile, "1", "key1", FALSE);
       
   335 	
       
   336 	g_key_file_set_boolean_list(keyfile,"1","key1",list,2);
       
   337 	
       
   338 	value = g_key_file_get_boolean_list(keyfile, "1", "key1",&n, NULL);
       
   339 	
       
   340 	g_assert(value[0] == TRUE);
       
   341 	g_assert(value[1] == FALSE);
       
   342 	
       
   343 }
       
   344 
       
   345 void test_setinteger()
       
   346 {
       
   347 	GKeyFile *keyfile;
       
   348 	gint *value;
       
   349 	gsize n = 2;
       
   350 	
       
   351 	gint list[2] = {111,222};
       
   352 	
       
   353 	const gchar *data = 
       
   354 	    "[1]\n"
       
   355 	    "key1=123\n"
       
   356 	    "key2=456\n";
       
   357 
       
   358 	keyfile = load_data (data, 0);
       
   359 	
       
   360 	check_integer_value (keyfile, "1", "key1", 123);
       
   361 	g_key_file_set_integer(keyfile,"1","key1",789);
       
   362 	check_integer_value (keyfile, "1", "key1", 789);
       
   363 	
       
   364 	g_key_file_set_integer_list(keyfile,"1","key1",list,2);
       
   365 	
       
   366 	value = g_key_file_get_integer_list(keyfile, "1", "key1",&n, NULL);
       
   367 	
       
   368 	g_assert(value[0] == 111);
       
   369 	g_assert(value[1] == 222);
       
   370 	
       
   371 }
       
   372 
       
   373 void test_keyfiletodata()
       
   374 {
       
   375 	GKeyFile *keyfile;
       
   376 	
       
   377 	gchar *retVal = NULL;
       
   378 	gsize length = 0;
       
   379 	
       
   380 	const gchar *data = 
       
   381 	    "[1]\n"
       
   382 	    "key1=123\n"
       
   383 	    "key2=456\n";
       
   384 	
       
   385 	keyfile = load_data (data, 0);
       
   386 	
       
   387 	retVal = g_key_file_to_data(keyfile,&length,NULL);
       
   388 	
       
   389 	g_assert(retVal != NULL && length > 0);
       
   390 }
       
   391 
       
   392 void test_setcomment()
       
   393 {
       
   394 	GKeyFile *keyfile;
       
   395 	gchar *comment;
       
   396 
       
   397 	const gchar *data = 
       
   398 	    "[1]\n"
       
   399 	    "key1=123\n"
       
   400 	    "key2=456\n";
       
   401 	
       
   402 	keyfile = load_data (data, 0);
       
   403 	
       
   404 	g_key_file_set_comment(keyfile,"1","key1","comment1",NULL);
       
   405 	
       
   406 	comment = g_key_file_get_comment(keyfile,"1","key1",NULL);
       
   407 	
       
   408 	g_assert(!strcmp(comment,"comment1\n"));
       
   409 	
       
   410 	g_key_file_remove_comment(keyfile,"1","key1",NULL);
       
   411 	
       
   412 	g_free(comment);
       
   413 	
       
   414 	comment = g_key_file_get_comment(keyfile,"1","key1",NULL);
       
   415 	
       
   416 	g_assert(comment == NULL);
       
   417 	
       
   418 }
       
   419 
       
   420 void test_setlocale()
       
   421 {
       
   422 	GKeyFile *keyfile;
       
   423 	gchar *value;
       
   424 	gchar **list;
       
   425 	gchar *input_list[2] = {"test2","test3"};
       
   426 	gsize length = 2;
       
   427 	
       
   428 	const gchar *data = 
       
   429 	    "[1]\n"
       
   430 	    "key1=123\n"
       
   431 	    "key2=456\n";
       
   432 	
       
   433 	keyfile = load_data (data, 0);
       
   434 	
       
   435 	g_key_file_set_locale_string(keyfile,"1","key3","C","test");
       
   436 	
       
   437 	value = g_key_file_get_locale_string(keyfile,"1","key3","C",NULL);
       
   438 	
       
   439 	g_assert(!strcmp(value,"test"));
       
   440 	
       
   441 	g_free(value);
       
   442 	
       
   443 	value = g_key_file_get_locale_string(keyfile,"1","key3","de",NULL);
       
   444 	
       
   445 	g_assert(value == NULL);
       
   446 	
       
   447 	g_key_file_set_locale_string_list(keyfile,"1","key4","C",input_list,2);
       
   448 	
       
   449 	list = g_key_file_get_locale_string_list(keyfile,"1","key4","C",&length,NULL);
       
   450 	
       
   451 	g_assert(!strcmp(list[0],"test2"));
       
   452 	g_assert(!strcmp(list[1],"test3"));
       
   453 	
       
   454 }
       
   455 
       
   456 int
       
   457 main (int argc, char *argv[])
       
   458 {
       
   459   	#ifdef __SYMBIAN32__
       
   460 	
       
   461 	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);
       
   462 	#endif /*__SYMBIAN32__*/
       
   463   
       
   464   test_loadfromfile();
       
   465   test_setstring();
       
   466   test_setboolean();
       
   467   test_setinteger();
       
   468   test_keyfiletodata();
       
   469   test_setcomment();
       
   470   test_setlocale();
       
   471   
       
   472   #ifdef __SYMBIAN32__
       
   473   testResultXml("keyfile_test");
       
   474   #endif /* EMULATOR */
       
   475    
       
   476   #ifdef __SYMBIAN32__
       
   477   testResultXml("keyfile_test");
       
   478   #endif /* EMULATOR */
       
   479   
       
   480   return 0;
       
   481 }