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