1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/src/tgstring.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,352 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 +*
1.7 +* This library is free software; you can redistribute it and/or
1.8 +* modify it under the terms of the GNU Lesser General Public
1.9 +* License as published by the Free Software Foundation; either
1.10 +* version 2 of the License, or (at your option) any later version.
1.11 +*
1.12 +* This library is distributed in the hope that it will be useful,
1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 +* Lesser General Public License for more details.
1.16 +*
1.17 +* You should have received a copy of the GNU Lesser General Public
1.18 +* License along with this library; if not, write to the
1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 +* Boston, MA 02111-1307, USA.
1.21 +*
1.22 +* Description: ?Description
1.23 +*
1.24 +*/
1.25 +
1.26 +
1.27 +#undef G_DISABLE_ASSERT
1.28 +#undef G_LOG_DOMAIN
1.29 +
1.30 +
1.31 +#include <stdio.h>
1.32 +#include <string.h>
1.33 +#include <glib.h>
1.34 +#include <fcntl.h>
1.35 +#include <goption.h>
1.36 +#include <glib/gprintf.h>
1.37 +#include <stdlib.h>
1.38 +
1.39 +#ifdef SYMBIAN
1.40 +#include "mrt2_glib2_test.h"
1.41 +#endif /*SYMBIAN*/
1.42 +
1.43 +#define C2P(c) ((gpointer) ((long) (c)))
1.44 +#define GINT_TO_POINTER(i) ((gpointer) (i))
1.45 +#define GPOINTER_TO_INT(p) ((gint) (p))
1.46 +#define TESTPASS 1
1.47 +#define TESTFAIL 0
1.48 +
1.49 +
1.50 +//Test for g_strcanon
1.51 +void tg_strcanon()
1.52 +{
1.53 + gchar string1[]="aabbccdd";
1.54 + gchar string2[]="aabbccdd";
1.55 + gchar* valid;
1.56 + gchar* teststring1;
1.57 + gchar* teststring2;
1.58 +
1.59 + //Testing for a valid char available in the string
1.60 + //The invalid chars must be replaced
1.61 + valid="cc";
1.62 + teststring1=g_strcanon (string1, valid, 'x');
1.63 + g_assert(!strcmp(teststring1,"xxxxccxx"));
1.64 +
1.65 + //Testing for a valid char not-available in the string
1.66 + //All the chars must be replaced
1.67 + valid="nn";
1.68 + teststring2=g_strcanon(string2,valid,'x');
1.69 + g_assert(!strcmp(teststring2,"xxxxxxxx"));
1.70 +}
1.71 +
1.72 +//Test for g_strcasecmp
1.73 +void tg_strcasecmp()
1.74 +{
1.75 + gint strcasecmp_eq,strcasecmp_gr8,strcasecmp_less;
1.76 + //Testing for equal strings,zero must be returned
1.77 + strcasecmp_eq=g_strcasecmp ("abcd123","abcd123");
1.78 + g_assert(strcasecmp_eq==0);
1.79 +
1.80 + //Testing for un-equal strings,left greater,positive value must be returned
1.81 + strcasecmp_gr8=g_strcasecmp ("abcd123","abcd");
1.82 + g_assert(strcasecmp_gr8>0);
1.83 +
1.84 + //Testing for un-equal strings,right greater,negative value must be returned
1.85 + strcasecmp_less=g_strcasecmp ("abcd","abcd123");
1.86 + g_assert(strcasecmp_less<0);
1.87 +
1.88 + }
1.89 +
1.90 + //Test for g_strdown
1.91 + void tg_strdown()
1.92 + {
1.93 + gchar input[]="ABCDef";
1.94 + gchar* upperToLower=g_strdown(input);
1.95 + g_assert(!strcmp(upperToLower,"abcdef"));
1.96 + }
1.97 +
1.98 + //Test for g_string_append_c
1.99 + void tg_string_append_c()
1.100 + {
1.101 + GString* obj=(GString*)malloc(sizeof(GString));
1.102 + gchar ip[]="abcd";
1.103 + obj->str=ip;
1.104 + obj->len=strlen(ip);
1.105 + obj->allocated_len=10;
1.106 + obj=g_string_append_c(obj,'e');
1.107 + g_assert(!strcmp((obj->str),"abcde"));
1.108 + }
1.109 +
1.110 + //Test for g_string_ascii_down
1.111 + void tg_string_ascii_down()
1.112 + {
1.113 + GString* obj=(GString*)malloc(sizeof(GString));
1.114 + gchar ip[]="ABc12DeF";
1.115 + obj->str=ip;
1.116 + obj->len=strlen(ip);
1.117 + obj->allocated_len=10;
1.118 + obj=g_string_ascii_down(obj);
1.119 + g_assert(!strcmp((obj->str),"abc12def"));
1.120 + }
1.121 +
1.122 + //Test for g_string_ascii_up
1.123 + void tg_string_ascii_up()
1.124 + {
1.125 + GString* obj=(GString*)malloc(sizeof(GString));
1.126 + gchar ip[]="ABc12DeF";
1.127 + obj->str=ip;
1.128 + obj->len=strlen(ip);
1.129 + obj->allocated_len=10;
1.130 + obj=g_string_ascii_up(obj);
1.131 + g_assert(!strcmp((obj->str),"ABC12DEF"));
1.132 + }
1.133 +
1.134 + //Test for g_string_down
1.135 + void tg_string_down()
1.136 + {
1.137 + GString* obj=(GString*)malloc(sizeof(GString));
1.138 + gchar ip[]="ABc12DeF";
1.139 + obj->str=ip;
1.140 + obj->len=strlen(ip);
1.141 + obj->allocated_len=10;
1.142 + obj=g_string_down(obj);
1.143 + g_assert(!strcmp((obj->str),"abc12def"));
1.144 + }
1.145 +
1.146 + //Test for g_string_hash
1.147 + void tg_string_hash()
1.148 + {
1.149 + GString* obj=(GString*)malloc(sizeof(GString));
1.150 + guint g_string_hash_result1,g_string_hash_result2;
1.151 + guint g_string_hash_result3,g_string_hash_result4;
1.152 + gchar ip1[]="ABC12";
1.153 + gchar ip2[]="abc12";
1.154 + obj->allocated_len=10;
1.155 +
1.156 + obj->str=ip1;
1.157 + obj->len=strlen(ip1);
1.158 + g_string_hash_result1=g_string_hash(obj);
1.159 + g_string_hash_result2=g_string_hash(obj);
1.160 + g_assert(g_string_hash_result1==g_string_hash_result2);
1.161 +
1.162 + obj->str=ip2;
1.163 + obj->len=strlen(ip2);
1.164 + g_string_hash_result3=g_string_hash(obj);
1.165 + g_string_hash_result4=g_string_hash(obj);
1.166 + g_assert(g_string_hash_result3==g_string_hash_result4);
1.167 +
1.168 + g_assert(g_string_hash_result1 != g_string_hash_result3);
1.169 +
1.170 + }
1.171 +
1.172 +
1.173 + //Test for g_string_prepend_c
1.174 + void tg_string_prepend_c()
1.175 + {
1.176 + GString* obj=(GString*)malloc(sizeof(GString));
1.177 + gchar ip[]="abcd";
1.178 + obj->str=ip;
1.179 + obj->len=strlen(ip);
1.180 + obj->allocated_len=10;
1.181 + obj=g_string_prepend_c(obj,'e');
1.182 + g_assert(!strcmp((obj->str),"eabcd"));
1.183 + }
1.184 +
1.185 +
1.186 + //Test for g_string_up
1.187 + void tg_string_up()
1.188 + {
1.189 + GString* obj=(GString*)malloc(sizeof(GString));
1.190 + gchar ip[]="ABc12DeF";
1.191 + obj->str=ip;
1.192 + obj->len=strlen(ip);
1.193 + obj->allocated_len=10;
1.194 + obj=g_string_up(obj);
1.195 + g_assert(!strcmp((obj->str),"ABC12DEF"));
1.196 + }
1.197 +
1.198 +
1.199 + //Test for g_string_prepend_unichar
1.200 + void tg_string_prepend_unichar()
1.201 + {
1.202 + GString* obj=(GString*)malloc(sizeof(GString));
1.203 + gchar ip[]="abcd";
1.204 + obj->str=ip;
1.205 + obj->len=strlen(ip);
1.206 + obj->allocated_len=10;
1.207 + obj=g_string_prepend_unichar(obj,'e');
1.208 + g_assert(!strcmp((obj->str),"eabcd"));
1.209 + }
1.210 +
1.211 + //Test for g_strip_context
1.212 + void tg_strip_context()
1.213 + {
1.214 + gchar msgid[]="abc|defgh";
1.215 + gchar msgval[]="abc|defgh";
1.216 + const gchar* op;
1.217 + op=g_strip_context(msgid,msgid);
1.218 + g_assert(!strcmp(op,"defgh"));
1.219 + }
1.220 +
1.221 + //Test for g_strjoin
1.222 + void tg_strjoin()
1.223 + {
1.224 + gchar sep[]="#&#";
1.225 + gchar a[]="abc";
1.226 + gchar b[]="def";
1.227 + gchar* op=g_strjoin(sep,a,b,NULL);
1.228 + g_assert(!strcmp(op,"abc#&#def"));
1.229 + }
1.230 +
1.231 +
1.232 + //Test for g_strncasecmp
1.233 +void tg_strncasecmp()
1.234 +{
1.235 + gint strncasecmp_eq,strncasecmp_gr8,strncasecmp_less;
1.236 + //Testing for equal strings,zero must be returned
1.237 + strncasecmp_eq=g_strncasecmp ("abcd123","abcd123",10);
1.238 + g_assert(strncasecmp_eq==0);
1.239 +
1.240 + //Testing for un-equal strings,left greater,positive value must be returned
1.241 + strncasecmp_gr8=g_strncasecmp ("abcd123","abcd",4);
1.242 + g_assert(strncasecmp_gr8==0);
1.243 +
1.244 + //Testing for un-equal strings,right greater,negative value must be returned
1.245 + strncasecmp_less=g_strncasecmp ("abcd","abcd123",6);
1.246 + g_assert(strncasecmp_less<0);
1.247 +
1.248 + }
1.249 +
1.250 +
1.251 + //Test for g_strnfill
1.252 + void tg_strnfill()
1.253 + {
1.254 + gchar fill='x';
1.255 + gsize size=10;
1.256 + gchar* strnfill_buf_null;
1.257 + gchar* strnfill_buf=g_strnfill(size,fill);
1.258 + g_assert(!strcmp(strnfill_buf,"xxxxxxxxxx"));
1.259 +
1.260 + size=0;
1.261 + strnfill_buf_null=g_strnfill(size,fill);
1.262 + g_assert(!strcmp(strnfill_buf_null,""));
1.263 +
1.264 + }
1.265 +
1.266 + //Test for g_strreverse
1.267 + void tg_strreverse()
1.268 + {
1.269 + gchar ip[]="abCdeF123";
1.270 + gchar* strreverse_op=g_strreverse(ip);
1.271 + g_assert(!strcmp(strreverse_op,"321FedCba"));
1.272 +
1.273 + }
1.274 +
1.275 +
1.276 + //Test for g_strup
1.277 + void tg_strup()
1.278 + {
1.279 + gchar ip[]="Abc12deF";
1.280 + gchar* strup=g_strup(ip);
1.281 + g_assert(!strcmp(strup,"ABC12DEF"));
1.282 + }
1.283 +
1.284 +
1.285 + //Test for g_pattern_match_string
1.286 + void tg_pattern_match_string()
1.287 + {
1.288 + gchar pattern_str[]="abcdefghijklmnopqrstuvwxyz";
1.289 + gchar match_str_pos[]="abcdefghijklmnopqrstuvwxyz"; //Proper a-z
1.290 + gchar match_str_neg[]="abcdefghjiklmnopqrstuvwxyz"; //i and j interchanged
1.291 +
1.292 + GPatternSpec* spec=g_pattern_spec_new(pattern_str);
1.293 + g_assert(g_pattern_match_string(spec,match_str_pos));
1.294 + g_assert(!g_pattern_match_string(spec,match_str_neg));
1.295 +
1.296 + }
1.297 +
1.298 + //Called by g_printf_string_upper_bound for a va-list
1.299 + void test_arg_string_upper_bound(gchar* fmt,...)
1.300 + {
1.301 + va_list ap;
1.302 + va_start(ap,fmt);
1.303 + g_assert(g_printf_string_upper_bound(fmt,ap)==27);
1.304 + va_end(ap);
1.305 + }
1.306 + //Test for g_printf_string_upper_bound
1.307 + void tg_printf_string_upper_bound()
1.308 + {
1.309 + test_arg_string_upper_bound("%d\n%s\t%f",9999999,"Abcd#%@",999.999999);
1.310 + }
1.311 +
1.312 +
1.313 + //Test for g_sprintf
1.314 + void tg_sprintf()
1.315 + {
1.316 + gchar sprintf_converted[100];
1.317 + gint sprintf_return_val=g_sprintf(sprintf_converted,"%d%s%f",9999999,"Abcd#%@",999.999999);
1.318 + g_assert(sprintf_return_val==24);
1.319 + }
1.320 +
1.321 +
1.322 +
1.323 +int main (int argc,char *argv[])
1.324 +{
1.325 +
1.326 + #ifdef SYMBIAN
1.327 + 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);
1.328 + #endif /*SYMBIAN*/
1.329 +
1.330 + tg_strcanon();
1.331 + tg_strcasecmp();
1.332 + tg_strdown();
1.333 + tg_string_append_c();
1.334 + tg_string_ascii_down();
1.335 + tg_string_ascii_up();
1.336 + tg_string_down();
1.337 + tg_string_hash();
1.338 + tg_string_prepend_c();
1.339 + tg_string_prepend_unichar();
1.340 + tg_string_up();
1.341 + tg_strip_context();
1.342 + tg_strjoin();
1.343 + tg_strncasecmp();
1.344 + tg_strnfill();
1.345 + tg_strreverse();
1.346 + tg_strup();
1.347 + tg_pattern_match_string();
1.348 + tg_printf_string_upper_bound();
1.349 + tg_sprintf();
1.350 +
1.351 + #ifdef SYMBIAN
1.352 + testResultXml("tgstring");
1.353 +#endif /* EMULATOR */
1.354 + return 0;
1.355 +}
1.356 \ No newline at end of file