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