|
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
|
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 |
|
20 /* |
|
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
22 * file for a list of people on the GLib Team. See the ChangeLog |
|
23 * files for a list of changes. These files are distributed with |
|
24 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
25 */ |
|
26 |
|
27 #undef G_DISABLE_ASSERT |
|
28 #undef G_LOG_DOMAIN |
|
29 |
|
30 #include <stdio.h> |
|
31 #include <string.h> |
|
32 #include "glib.h" |
|
33 #include "glib/gprintf.h" |
|
34 |
|
35 #ifdef SYMBIAN |
|
36 #include "mrt2_glib2_test.h" |
|
37 #endif /*SYMBIAN*/ |
|
38 |
|
39 |
|
40 int array[10000]; |
|
41 gboolean failed = FALSE; |
|
42 |
|
43 #define TEST(m,cond) G_STMT_START { failed = !(cond); \ |
|
44 if (failed) \ |
|
45 { assert_failed = TRUE; \ |
|
46 if (!m) \ |
|
47 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ |
|
48 else \ |
|
49 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ |
|
50 } \ |
|
51 else \ |
|
52 g_print ("."); fflush (stdout); \ |
|
53 } G_STMT_END |
|
54 |
|
55 #define C2P(c) ((gpointer) ((long) (c))) |
|
56 #define P2C(p) ((gchar) ((long) (p))) |
|
57 |
|
58 #define GLIB_TEST_STRING "el dorado " |
|
59 #define GLIB_TEST_STRING_5 "el do" |
|
60 |
|
61 typedef struct { |
|
62 guint age; |
|
63 gchar name[40]; |
|
64 } GlibTestInfo; |
|
65 |
|
66 int |
|
67 main (int argc, |
|
68 char *argv[]) |
|
69 { |
|
70 GStringChunk *string_chunk; |
|
71 |
|
72 gchar *tmp_string = NULL, *tmp_string_2; |
|
73 gint i; |
|
74 GString *string1, *string2; |
|
75 |
|
76 #ifdef SYMBIAN |
|
77 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); |
|
78 g_set_print_handler(mrtPrintHandler); |
|
79 #endif /*SYMBIAN*/ |
|
80 |
|
81 |
|
82 string_chunk = g_string_chunk_new (1024); |
|
83 |
|
84 for (i = 0; i < 1000; i ++) |
|
85 { |
|
86 tmp_string = g_string_chunk_insert (string_chunk, "hi pete"); |
|
87 |
|
88 if (strcmp ("hi pete", tmp_string) != 0) |
|
89 g_error ("string chunks are broken.\n"); |
|
90 } |
|
91 |
|
92 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string); |
|
93 |
|
94 g_assert (tmp_string_2 != tmp_string && |
|
95 strcmp(tmp_string_2, tmp_string) == 0); |
|
96 |
|
97 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string); |
|
98 |
|
99 g_assert (tmp_string_2 == tmp_string); |
|
100 |
|
101 g_string_chunk_free (string_chunk); |
|
102 |
|
103 string1 = g_string_new ("hi pete!"); |
|
104 string2 = g_string_new (NULL); |
|
105 |
|
106 g_assert (string1 != NULL); |
|
107 g_assert (string2 != NULL); |
|
108 g_assert (strlen (string1->str) == string1->len); |
|
109 g_assert (strlen (string2->str) == string2->len); |
|
110 g_assert (string2->len == 0); |
|
111 g_assert (strcmp ("hi pete!", string1->str) == 0); |
|
112 g_assert (strcmp ("", string2->str) == 0); |
|
113 |
|
114 for (i = 0; i < 10000; i++) |
|
115 g_string_append_c (string1, 'a'+(i%26)); |
|
116 |
|
117 g_assert((strlen("hi pete!") + 10000) == string1->len); |
|
118 g_assert((strlen("hi pete!") + 10000) == strlen(string1->str)); |
|
119 |
|
120 #ifndef G_OS_WIN32 |
|
121 /* MSVC and mingw32 use the same run-time C library, which doesn't like |
|
122 the %10000.10000f format... */ |
|
123 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f", |
|
124 "this pete guy sure is a wuss, like he's the number ", |
|
125 1, |
|
126 " wuss. everyone agrees.\n", |
|
127 string1->str, |
|
128 10, 666, 15, 15, 666.666666666, 666.666666666); |
|
129 #else |
|
130 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f", |
|
131 "this pete guy sure is a wuss, like he's the number ", |
|
132 1, |
|
133 " wuss. everyone agrees.\n", |
|
134 string1->str, |
|
135 10, 666, 15, 15, 666.666666666, 666.666666666); |
|
136 #endif |
|
137 |
|
138 g_string_free (string1, TRUE); |
|
139 g_string_free (string2, TRUE); |
|
140 |
|
141 /* append */ |
|
142 string1 = g_string_new ("firsthalf"); |
|
143 g_string_append (string1, "lasthalf"); |
|
144 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); |
|
145 g_string_free (string1, TRUE); |
|
146 |
|
147 /* append_len */ |
|
148 string1 = g_string_new ("firsthalf"); |
|
149 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf")); |
|
150 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); |
|
151 g_string_free (string1, TRUE); |
|
152 |
|
153 /* prepend */ |
|
154 string1 = g_string_new ("lasthalf"); |
|
155 g_string_prepend (string1, "firsthalf"); |
|
156 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); |
|
157 g_string_free (string1, TRUE); |
|
158 |
|
159 /* prepend_len */ |
|
160 string1 = g_string_new ("lasthalf"); |
|
161 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf")); |
|
162 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); |
|
163 g_string_free (string1, TRUE); |
|
164 |
|
165 /* insert */ |
|
166 string1 = g_string_new ("firstlast"); |
|
167 g_string_insert (string1, 5, "middle"); |
|
168 g_assert (strcmp (string1->str, "firstmiddlelast") == 0); |
|
169 g_string_free (string1, TRUE); |
|
170 |
|
171 /* insert with pos == end of the string */ |
|
172 string1 = g_string_new ("firstmiddle"); |
|
173 g_string_insert (string1, strlen ("firstmiddle"), "last"); |
|
174 g_assert (strcmp (string1->str, "firstmiddlelast") == 0); |
|
175 g_string_free (string1, TRUE); |
|
176 |
|
177 /* insert_len */ |
|
178 string1 = g_string_new ("firstlast"); |
|
179 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle")); |
|
180 g_assert (strcmp (string1->str, "firstmiddlelast") == 0); |
|
181 g_string_free (string1, TRUE); |
|
182 |
|
183 /* insert_len with magic -1 pos for append */ |
|
184 string1 = g_string_new ("first"); |
|
185 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last")); |
|
186 g_assert (strcmp (string1->str, "firstlast") == 0); |
|
187 g_string_free (string1, TRUE); |
|
188 |
|
189 /* insert_len with magic -1 len for strlen-the-string */ |
|
190 string1 = g_string_new ("first"); |
|
191 g_string_insert_len (string1, 5, "last", -1); |
|
192 g_assert (strcmp (string1->str, "firstlast") == 0); |
|
193 g_string_free (string1, TRUE); |
|
194 |
|
195 /* insert_len with string overlap */ |
|
196 string1 = g_string_new ("textbeforetextafter"); |
|
197 g_string_insert_len (string1, 10, string1->str + 8, 5); |
|
198 g_assert (strcmp (string1->str, "textbeforeretextextafter") == 0); |
|
199 g_string_free (string1, TRUE); |
|
200 |
|
201 string1 = g_string_new ("boring text"); |
|
202 g_string_insert_len (string1, 7, string1->str + 2, 4); |
|
203 g_assert (strcmp (string1->str, "boring ringtext") == 0); |
|
204 g_string_free (string1, TRUE); |
|
205 |
|
206 string1 = g_string_new ("boring text"); |
|
207 g_string_insert_len (string1, 6, string1->str + 7, 4); |
|
208 g_assert (strcmp (string1->str, "boringtext text") == 0); |
|
209 g_string_free (string1, TRUE); |
|
210 |
|
211 /* assign_len with string overlap */ |
|
212 string1 = g_string_new ("textbeforetextafter"); |
|
213 g_string_assign (string1, string1->str + 10); |
|
214 g_assert (strcmp (string1->str, "textafter") == 0); |
|
215 g_string_free (string1, TRUE); |
|
216 |
|
217 string1 = g_string_new ("boring text"); |
|
218 g_string_assign (string1, string1->str); |
|
219 g_assert (strcmp (string1->str, "boring text") == 0); |
|
220 g_string_free (string1, TRUE); |
|
221 |
|
222 /* insert_unichar with insertion in middle */ |
|
223 string1 = g_string_new ("firsthalf"); |
|
224 g_string_insert_unichar (string1, 5, 0x0041); |
|
225 g_assert (strcmp (string1->str, "first\x41half") == 0); |
|
226 g_string_free (string1, TRUE); |
|
227 |
|
228 string1 = g_string_new ("firsthalf"); |
|
229 g_string_insert_unichar (string1, 5, 0x0298); |
|
230 g_assert (strcmp (string1->str, "first\xCA\x98half") == 0); |
|
231 g_string_free (string1, TRUE); |
|
232 |
|
233 string1 = g_string_new ("firsthalf"); |
|
234 g_string_insert_unichar (string1, 5, 0xFFFD); |
|
235 g_assert (strcmp (string1->str, "first\xEF\xBF\xBDhalf") == 0); |
|
236 g_string_free (string1, TRUE); |
|
237 |
|
238 string1 = g_string_new ("firsthalf"); |
|
239 g_string_insert_unichar (string1, 5, 0x1D100); |
|
240 g_assert (strcmp (string1->str, "first\xF0\x9D\x84\x80half") == 0); |
|
241 g_string_free (string1, TRUE); |
|
242 |
|
243 /* insert_unichar with insertion at end */ |
|
244 string1 = g_string_new ("start"); |
|
245 g_string_insert_unichar (string1, -1, 0x0041); |
|
246 g_assert (strcmp (string1->str, "start\x41") == 0); |
|
247 g_string_free (string1, TRUE); |
|
248 |
|
249 string1 = g_string_new ("start"); |
|
250 g_string_insert_unichar (string1, -1, 0x0298); |
|
251 g_assert (strcmp (string1->str, "start\xCA\x98") == 0); |
|
252 g_string_free (string1, TRUE); |
|
253 |
|
254 string1 = g_string_new ("start"); |
|
255 g_string_insert_unichar (string1, -1, 0xFFFD); |
|
256 g_assert (strcmp (string1->str, "start\xEF\xBF\xBD") == 0); |
|
257 g_string_free (string1, TRUE); |
|
258 |
|
259 string1 = g_string_new ("start"); |
|
260 g_string_insert_unichar (string1, -1, 0x1D100); |
|
261 g_assert (strcmp (string1->str, "start\xF0\x9D\x84\x80") == 0); |
|
262 g_string_free (string1, TRUE); |
|
263 |
|
264 /* g_string_equal */ |
|
265 string1 = g_string_new ("test"); |
|
266 string2 = g_string_new ("te"); |
|
267 g_assert (! g_string_equal(string1, string2)); |
|
268 g_string_append (string2, "st"); |
|
269 g_assert (g_string_equal(string1, string2)); |
|
270 g_string_free (string1, TRUE); |
|
271 g_string_free (string2, TRUE); |
|
272 |
|
273 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */ |
|
274 string1 = g_string_new ("fiddle"); |
|
275 string2 = g_string_new ("fiddle"); |
|
276 g_assert (g_string_equal(string1, string2)); |
|
277 g_string_append_c(string1, '\0'); |
|
278 g_assert (! g_string_equal(string1, string2)); |
|
279 g_string_append_c(string2, '\0'); |
|
280 g_assert (g_string_equal(string1, string2)); |
|
281 g_string_append_c(string1, 'x'); |
|
282 g_string_append_c(string2, 'y'); |
|
283 g_assert (! g_string_equal(string1, string2)); |
|
284 g_assert (string1->len == 8); |
|
285 g_string_append(string1, "yzzy"); |
|
286 g_assert (string1->len == 12); |
|
287 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0); |
|
288 g_string_insert(string1, 1, "QED"); |
|
289 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0); |
|
290 g_string_printf (string1, "fiddle%cxyzzy", '\0'); |
|
291 g_assert (string1->len == 12); |
|
292 g_assert (memcmp (string1->str, "fiddle\0xyzzy", 13) == 0); |
|
293 |
|
294 g_string_free (string1, TRUE); |
|
295 g_string_free (string2, TRUE); |
|
296 |
|
297 g_assert (g_str_has_prefix("foobar", "gazonk") == FALSE); |
|
298 g_assert (g_str_has_prefix("xyzzy", "xyzzy") == TRUE); |
|
299 g_assert (g_str_has_prefix("xyzzy", "xy") == TRUE); |
|
300 g_assert (g_str_has_prefix("xyzzy", "") == TRUE); |
|
301 g_assert (g_str_has_prefix("xyz", "xyzzy") == FALSE); |
|
302 g_assert (g_str_has_prefix("", "xyzzy") == FALSE); |
|
303 g_assert (g_str_has_prefix("", "") == TRUE); |
|
304 |
|
305 g_assert (g_str_has_suffix("foobar", "gazonk") == FALSE); |
|
306 g_assert (g_str_has_suffix("xyzzy", "xyzzy") == TRUE); |
|
307 g_assert (g_str_has_suffix("xyzzy", "zy") == TRUE); |
|
308 g_assert (g_str_has_suffix("xyzzy", "") == TRUE); |
|
309 g_assert (g_str_has_suffix("zzy", "xyzzy") == FALSE); |
|
310 g_assert (g_str_has_suffix("", "xyzzy") == FALSE); |
|
311 g_assert (g_str_has_suffix("", "") == TRUE); |
|
312 |
|
313 tmp_string = (gchar *) g_malloc (10); |
|
314 g_snprintf (tmp_string, 10, "%2$s %1$s", "a", "b"); |
|
315 g_assert (strcmp (tmp_string, "b a") == 0); |
|
316 g_free (tmp_string); |
|
317 #ifdef SYMBIAN |
|
318 testResultXml("string-test"); |
|
319 #endif /* EMULATOR */ |
|
320 return 0; |
|
321 } |
|
322 |
|
323 |