glib/tsrc/BC/src/tgstring.c
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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:  ?Description
       
    20 *
       
    21 */
       
    22 
       
    23 
       
    24 #undef G_DISABLE_ASSERT
       
    25 #undef G_LOG_DOMAIN
       
    26 
       
    27 
       
    28 #include <stdio.h>
       
    29 #include <string.h>
       
    30 #include <glib.h>
       
    31 #include <fcntl.h>
       
    32 #include <goption.h>
       
    33 
       
    34 #ifdef SYMBIAN
       
    35 #include "mrt2_glib2_test.h"
       
    36 #endif /*SYMBIAN*/
       
    37 
       
    38 #define	C2P(c)		((gpointer) ((long) (c)))
       
    39 #define GINT_TO_POINTER(i)	((gpointer)  (i))
       
    40 #define GPOINTER_TO_INT(p)	((gint)   (p))
       
    41 #define TESTPASS	1
       
    42 #define TESTFAIL	0
       
    43 
       
    44 
       
    45 //Test for g_strcanon
       
    46 void tg_strcanon()
       
    47 {
       
    48 	gchar string1[]="aabbccdd";
       
    49 	gchar string2[]="aabbccdd";
       
    50 	gchar* valid;
       
    51 	gchar* teststring1;
       
    52 	gchar* teststring2;
       
    53   
       
    54  	//Testing for a valid char available in the string
       
    55  	//The invalid chars must be replaced
       
    56  	valid="cc";
       
    57  	teststring1=g_strcanon (string1, valid, 'x');
       
    58  	g_assert(!strcmp(teststring1,"xxxxccxx"));
       
    59 
       
    60 	//Testing for a valid char not-available in the string
       
    61 	//All the chars must be replaced
       
    62 	valid="nn";
       
    63 	teststring2=g_strcanon(string2,valid,'x');
       
    64 	g_assert(!strcmp(teststring2,"xxxxxxxx"));
       
    65 }
       
    66 
       
    67 //Test for g_strcasecmp
       
    68 void tg_strcasecmp()
       
    69 {
       
    70  	gint strcasecmp_eq,strcasecmp_gr8,strcasecmp_less;
       
    71  	//Testing for equal strings,zero must be returned
       
    72  	strcasecmp_eq=g_strcasecmp ("abcd123","abcd123");
       
    73  	g_assert(strcasecmp_eq==0);
       
    74 
       
    75  	//Testing for un-equal strings,left greater,positive value must be returned
       
    76  	strcasecmp_gr8=g_strcasecmp ("abcd123","abcd");
       
    77  	g_assert(strcasecmp_gr8>0);
       
    78  	
       
    79  	//Testing for un-equal strings,right greater,negative value must be returned
       
    80  	strcasecmp_less=g_strcasecmp ("abcd","abcd123");
       
    81  	g_assert(strcasecmp_less<0);
       
    82  	
       
    83  }
       
    84  
       
    85  //Test for g_strdown
       
    86  void tg_strdown()
       
    87  {
       
    88  	gchar input[]="ABCDef";
       
    89  	gchar* upperToLower=g_strdown(input);
       
    90  	g_assert(!strcmp(upperToLower,"abcdef"));	
       
    91  }
       
    92  
       
    93  //Test for g_string_append_c
       
    94  void tg_string_append_c()
       
    95  {
       
    96 	GString* obj=(GString*)malloc(sizeof(GString));
       
    97 	gchar ip[]="abcd";
       
    98   	obj->str=ip;
       
    99  	obj->len=strlen(ip);
       
   100  	obj->allocated_len=10;
       
   101  	obj=g_string_append_c(obj,'e');
       
   102  	g_assert(!strcmp((obj->str),"abcde"));
       
   103  }
       
   104  
       
   105  //Test for g_string_ascii_down
       
   106  void tg_string_ascii_down()
       
   107  {
       
   108  	GString* obj=(GString*)malloc(sizeof(GString));
       
   109  	gchar ip[]="ABc12DeF";
       
   110  	obj->str=ip;
       
   111  	obj->len=strlen(ip);
       
   112  	obj->allocated_len=10;
       
   113  	obj=g_string_ascii_down(obj);
       
   114  	g_assert(!strcmp((obj->str),"abc12def"));
       
   115  }
       
   116  
       
   117  //Test for g_string_ascii_up
       
   118  void tg_string_ascii_up()
       
   119  {
       
   120  	GString* obj=(GString*)malloc(sizeof(GString));
       
   121  	gchar ip[]="ABc12DeF";
       
   122  	obj->str=ip;
       
   123  	obj->len=strlen(ip);
       
   124  	obj->allocated_len=10;
       
   125  	obj=g_string_ascii_up(obj);
       
   126  	g_assert(!strcmp((obj->str),"ABC12DEF"));
       
   127  }
       
   128  
       
   129  //Test for g_string_down
       
   130  void tg_string_down()
       
   131  {
       
   132  	GString* obj=(GString*)malloc(sizeof(GString));
       
   133  	gchar ip[]="ABc12DeF";
       
   134  	obj->str=ip;
       
   135  	obj->len=strlen(ip);
       
   136  	obj->allocated_len=10;
       
   137  	obj=g_string_down(obj);
       
   138  	g_assert(!strcmp((obj->str),"abc12def"));
       
   139  }
       
   140 
       
   141  //Test for g_string_hash
       
   142  void tg_string_hash()
       
   143  {
       
   144  	GString* obj=(GString*)malloc(sizeof(GString));
       
   145  	guint g_string_hash_result1,g_string_hash_result2;
       
   146  	guint g_string_hash_result3,g_string_hash_result4;
       
   147  	gchar ip1[]="ABC12";
       
   148  	gchar ip2[]="abc12";
       
   149  	obj->allocated_len=10;
       
   150  	
       
   151  	obj->str=ip1;
       
   152  	obj->len=strlen(ip1);
       
   153  	g_string_hash_result1=g_string_hash(obj);
       
   154  	g_string_hash_result2=g_string_hash(obj);
       
   155  	g_assert(g_string_hash_result1==g_string_hash_result2);
       
   156  	
       
   157  	obj->str=ip2;
       
   158  	obj->len=strlen(ip2);
       
   159  	g_string_hash_result3=g_string_hash(obj);
       
   160  	g_string_hash_result4=g_string_hash(obj);
       
   161  	g_assert(g_string_hash_result3==g_string_hash_result4);
       
   162  	
       
   163  	g_assert(g_string_hash_result1 != g_string_hash_result3);
       
   164  	
       
   165  }
       
   166  
       
   167  
       
   168  //Test for g_string_prepend_c
       
   169   void tg_string_prepend_c()
       
   170  {
       
   171 	GString* obj=(GString*)malloc(sizeof(GString));
       
   172 	gchar ip[]="abcd";
       
   173   	obj->str=ip;
       
   174  	obj->len=strlen(ip);
       
   175  	obj->allocated_len=10;
       
   176  	obj=g_string_prepend_c(obj,'e');
       
   177  	g_assert(!strcmp((obj->str),"eabcd"));
       
   178  }
       
   179  
       
   180  
       
   181  //Test for g_string_up
       
   182  void tg_string_up()
       
   183  {
       
   184  	GString* obj=(GString*)malloc(sizeof(GString));
       
   185  	gchar ip[]="ABc12DeF";
       
   186  	obj->str=ip;
       
   187  	obj->len=strlen(ip);
       
   188  	obj->allocated_len=10;
       
   189  	obj=g_string_up(obj);
       
   190  	g_assert(!strcmp((obj->str),"ABC12DEF"));
       
   191  }
       
   192  
       
   193  
       
   194  //Test for g_string_prepend_unichar
       
   195  void tg_string_prepend_unichar()
       
   196  {
       
   197 	GString* obj=(GString*)malloc(sizeof(GString));
       
   198 	gchar ip[]="abcd";
       
   199   	obj->str=ip;
       
   200  	obj->len=strlen(ip);
       
   201  	obj->allocated_len=10;
       
   202  	obj=g_string_prepend_unichar(obj,'e');
       
   203  	g_assert(!strcmp((obj->str),"eabcd"));
       
   204  }
       
   205  
       
   206  //Test for g_strip_context
       
   207  void tg_strip_context()
       
   208  {
       
   209  	gchar msgid[]="abc|defgh";
       
   210  	gchar msgval[]="abc|defgh";
       
   211  	const gchar* op;
       
   212  	op=g_strip_context(msgid,msgid);
       
   213  	g_assert(!strcmp(op,"defgh"));	
       
   214  }
       
   215  
       
   216  //Test for g_strjoin
       
   217  void tg_strjoin()
       
   218  {
       
   219  	gchar sep[]="#&#";
       
   220 	gchar a[]="abc";
       
   221 	gchar b[]="def";
       
   222  	gchar* op=g_strjoin(sep,a,b,NULL);
       
   223  	g_assert(!strcmp(op,"abc#&#def"));
       
   224  }
       
   225  
       
   226  
       
   227  //Test for g_strncasecmp
       
   228 void tg_strncasecmp()
       
   229 {
       
   230  	gint strncasecmp_eq,strncasecmp_gr8,strncasecmp_less;
       
   231  	//Testing for equal strings,zero must be returned
       
   232  	strncasecmp_eq=g_strncasecmp ("abcd123","abcd123",10);
       
   233  	g_assert(strncasecmp_eq==0);
       
   234 
       
   235  	//Testing for un-equal strings,left greater,positive value must be returned
       
   236  	strncasecmp_gr8=g_strncasecmp ("abcd123","abcd",4);
       
   237  	g_assert(strncasecmp_gr8==0);
       
   238  	
       
   239  	//Testing for un-equal strings,right greater,negative value must be returned
       
   240  	strncasecmp_less=g_strncasecmp ("abcd","abcd123",6);
       
   241  	g_assert(strncasecmp_less<0);
       
   242  	
       
   243  }
       
   244  
       
   245  
       
   246  //Test for g_strnfill
       
   247  void tg_strnfill()
       
   248  {
       
   249  	gchar fill='x';
       
   250 	gsize size=10;
       
   251 	gchar* strnfill_buf_null;
       
   252 	gchar* strnfill_buf=g_strnfill(size,fill);
       
   253  	g_assert(!strcmp(strnfill_buf,"xxxxxxxxxx"));
       
   254  
       
   255  	size=0;
       
   256 	strnfill_buf_null=g_strnfill(size,fill);
       
   257  	g_assert(!strcmp(strnfill_buf_null,""));
       
   258  
       
   259  }
       
   260  
       
   261  //Test for g_strreverse
       
   262  void tg_strreverse()
       
   263  {
       
   264  	gchar ip[]="abCdeF123";
       
   265 	gchar* strreverse_op=g_strreverse(ip);
       
   266  	g_assert(!strcmp(strreverse_op,"321FedCba"));
       
   267  
       
   268  }
       
   269  
       
   270  
       
   271  //Test for g_strup
       
   272  void tg_strup()
       
   273  {
       
   274  	gchar ip[]="Abc12deF";
       
   275  	gchar* strup=g_strup(ip);
       
   276  	g_assert(!strcmp(strup,"ABC12DEF"));
       
   277  }
       
   278  
       
   279  
       
   280  //Test for g_pattern_match_string
       
   281  void tg_pattern_match_string()
       
   282  {
       
   283  	gchar pattern_str[]="abcdefghijklmnopqrstuvwxyz";
       
   284  	gchar match_str_pos[]="abcdefghijklmnopqrstuvwxyz"; //Proper a-z
       
   285  	gchar match_str_neg[]="abcdefghjiklmnopqrstuvwxyz";	//i and j interchanged
       
   286  	
       
   287  	GPatternSpec* spec=g_pattern_spec_new(pattern_str);
       
   288  	g_assert(g_pattern_match_string(spec,match_str_pos));
       
   289  	g_assert(!g_pattern_match_string(spec,match_str_neg));
       
   290  	
       
   291  }
       
   292 
       
   293  //Called by g_printf_string_upper_bound for a va-list
       
   294  void test_arg_string_upper_bound(gchar* fmt,...)
       
   295  {
       
   296  	va_list ap;
       
   297 	va_start(ap,fmt);
       
   298 	g_assert(g_printf_string_upper_bound(fmt,ap)==27);
       
   299 	va_end(ap);
       
   300  }
       
   301  //Test for g_printf_string_upper_bound
       
   302  void tg_printf_string_upper_bound()
       
   303  {
       
   304  	test_arg_string_upper_bound("%d\n%s\t%f",9999999,"Abcd#%@",999.999999);
       
   305  }
       
   306 
       
   307 
       
   308  //Test for g_sprintf
       
   309  void tg_sprintf()
       
   310  {
       
   311  	gchar sprintf_converted[100];
       
   312  	gint sprintf_return_val=g_sprintf(sprintf_converted,"%d%s%f",9999999,"Abcd#%@",999.999999);
       
   313  	g_assert(sprintf_return_val==24);
       
   314  }
       
   315 
       
   316 
       
   317 
       
   318 int main (int argc,char *argv[])
       
   319 {
       
   320 
       
   321 	#ifdef SYMBIAN
       
   322   	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);
       
   323  	#endif /*SYMBIAN*/
       
   324  	
       
   325  	tg_strcanon();
       
   326 	tg_strcasecmp();
       
   327 	tg_strdown();
       
   328 	tg_string_append_c();
       
   329 	tg_string_ascii_down();
       
   330 	tg_string_ascii_up();
       
   331 	tg_string_down();
       
   332 	tg_string_hash();
       
   333 	tg_string_prepend_c();
       
   334 	tg_string_prepend_unichar();
       
   335 	tg_string_up();
       
   336 	tg_strip_context();
       
   337 	tg_strjoin();
       
   338 	tg_strncasecmp();
       
   339 	tg_strnfill();
       
   340 	tg_strreverse();
       
   341 	tg_strup();
       
   342 	tg_pattern_match_string();
       
   343 	tg_printf_string_upper_bound();
       
   344 	tg_sprintf();
       
   345 	
       
   346 	#ifdef SYMBIAN
       
   347   testResultXml("tgstring");
       
   348 #endif /* EMULATOR */
       
   349  	return 0;
       
   350 }