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