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