1 /* Unit tests for gstring
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 /* We are testing some deprecated APIs here */
23 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
33 test_string_chunks (void)
35 GStringChunk *string_chunk;
36 gchar *tmp_string, *tmp_string_2;
39 string_chunk = g_string_chunk_new (1024);
41 for (i = 0; i < 100000; i ++)
43 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
44 g_assert_cmpstr ("hi pete", ==, tmp_string);
47 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
48 g_assert (tmp_string_2 != tmp_string);
49 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
51 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
52 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
54 g_string_chunk_clear (string_chunk);
55 g_string_chunk_free (string_chunk);
59 test_string_chunk_insert (void)
61 const gchar s0[] = "Testing GStringChunk";
62 const gchar s1[] = "a\0b\0c\0d\0";
63 const gchar s2[] = "Hello, world";
67 chunk = g_string_chunk_new (512);
69 str[0] = g_string_chunk_insert (chunk, s0);
70 str[1] = g_string_chunk_insert_len (chunk, s1, 8);
71 str[2] = g_string_chunk_insert (chunk, s2);
73 g_assert (memcmp (s0, str[0], sizeof s0) == 0);
74 g_assert (memcmp (s1, str[1], sizeof s1) == 0);
75 g_assert (memcmp (s2, str[2], sizeof s2) == 0);
77 g_string_chunk_free (chunk);
81 test_string_new (void)
83 GString *string1, *string2;
85 string1 = g_string_new ("hi pete!");
86 string2 = g_string_new (NULL);
88 g_assert (string1 != NULL);
89 g_assert (string2 != NULL);
90 g_assert (strlen (string1->str) == string1->len);
91 g_assert (strlen (string2->str) == string2->len);
92 g_assert (string2->len == 0);
93 g_assert_cmpstr ("hi pete!", ==, string1->str);
94 g_assert_cmpstr ("", ==, string2->str);
96 g_string_free (string1, TRUE);
97 g_string_free (string2, TRUE);
99 string1 = g_string_new_len ("foo", -1);
100 string2 = g_string_new_len ("foobar", 3);
102 g_assert_cmpstr (string1->str, ==, "foo");
103 g_assert_cmpint (string1->len, ==, 3);
104 g_assert_cmpstr (string2->str, ==, "foo");
105 g_assert_cmpint (string2->len, ==, 3);
107 g_string_free (string1, TRUE);
108 g_string_free (string2, TRUE);
113 my_string_printf (GString *string,
119 va_start (args, format);
120 g_string_vprintf (string, format, args);
125 test_string_printf (void)
129 string = g_string_new (NULL);
132 /* MSVC and mingw32 use the same run-time C library, which doesn't like
133 the %10000.10000f format... */
134 g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%10000.10000f",
135 "this pete guy sure is a wuss, like he's the number ",
137 " wuss. everyone agrees.\n",
138 10, 666, 15, 15, 666.666666666, 666.666666666);
140 g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%100.100f",
141 "this pete guy sure is a wuss, like he's the number ",
143 " wuss. everyone agrees.\n",
144 10, 666, 15, 15, 666.666666666, 666.666666666);
147 g_string_free (string, TRUE);
149 string = g_string_new (NULL);
150 g_string_printf (string, "bla %s %d", "foo", 99);
151 g_assert_cmpstr (string->str, ==, "bla foo 99");
152 my_string_printf (string, "%d,%s,%d", 1, "two", 3);
153 g_assert_cmpstr (string->str, ==, "1,two,3");
155 g_string_free (string, TRUE);
159 test_string_assign (void)
163 string = g_string_new (NULL);
164 g_string_assign (string, "boring text");
165 g_assert_cmpstr (string->str, ==, "boring text");
166 g_string_free (string, TRUE);
168 /* assign with string overlap */
169 string = g_string_new ("textbeforetextafter");
170 g_string_assign (string, string->str + 10);
171 g_assert_cmpstr (string->str, ==, "textafter");
172 g_string_free (string, TRUE);
174 string = g_string_new ("boring text");
175 g_string_assign (string, string->str);
176 g_assert_cmpstr (string->str, ==, "boring text");
177 g_string_free (string, TRUE);
181 test_string_append_c (void)
186 string = g_string_new ("hi pete!");
188 for (i = 0; i < 10000; i++)
190 g_string_append_c (string, 'a'+(i%26));
192 (g_string_append_c) (string, 'a'+(i%26));
194 g_assert((strlen("hi pete!") + 10000) == string->len);
195 g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
197 g_string_free (string, TRUE);
201 test_string_append (void)
206 string = g_string_new ("firsthalf");
207 g_string_append (string, "lasthalf");
208 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
209 g_string_free (string, TRUE);
212 string = g_string_new ("firsthalf");
213 g_string_append_len (string, "lasthalfjunkjunk", strlen ("lasthalf"));
214 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
215 g_string_free (string, TRUE);
219 test_string_prepend_c (void)
224 string = g_string_new ("hi pete!");
226 for (i = 0; i < 10000; i++)
227 g_string_prepend_c (string, 'a'+(i%26));
229 g_assert((strlen("hi pete!") + 10000) == string->len);
230 g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
232 g_string_free (string, TRUE);
236 test_string_prepend (void)
241 string = g_string_new ("lasthalf");
242 g_string_prepend (string, "firsthalf");
243 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
244 g_string_free (string, TRUE);
247 string = g_string_new ("lasthalf");
248 g_string_prepend_len (string, "firsthalfjunkjunk", strlen ("firsthalf"));
249 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
250 g_string_free (string, TRUE);
254 test_string_insert (void)
259 string = g_string_new ("firstlast");
260 g_string_insert (string, 5, "middle");
261 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
262 g_string_free (string, TRUE);
264 /* insert with pos == end of the string */
265 string = g_string_new ("firstmiddle");
266 g_string_insert (string, strlen ("firstmiddle"), "last");
267 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
268 g_string_free (string, TRUE);
271 string = g_string_new ("firstlast");
272 g_string_insert_len (string, 5, "middlejunkjunk", strlen ("middle"));
273 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
274 g_string_free (string, TRUE);
276 /* insert_len with magic -1 pos for append */
277 string = g_string_new ("first");
278 g_string_insert_len (string, -1, "lastjunkjunk", strlen ("last"));
279 g_assert_cmpstr (string->str, ==, "firstlast");
280 g_string_free (string, TRUE);
282 /* insert_len with magic -1 len for strlen-the-string */
283 string = g_string_new ("first");
284 g_string_insert_len (string, 5, "last", -1);
285 g_assert_cmpstr (string->str, ==, "firstlast");
286 g_string_free (string, TRUE);
288 /* insert_len with string overlap */
289 string = g_string_new ("textbeforetextafter");
290 g_string_insert_len (string, 10, string->str + 8, 5);
291 g_assert_cmpstr (string->str, ==, "textbeforeretextextafter");
292 g_string_free (string, TRUE);
296 test_string_insert_unichar (void)
300 /* insert_unichar with insertion in middle */
301 string = g_string_new ("firsthalf");
302 g_string_insert_unichar (string, 5, 0x0041);
303 g_assert_cmpstr (string->str, ==, "first\x41half");
304 g_string_free (string, TRUE);
306 string = g_string_new ("firsthalf");
307 g_string_insert_unichar (string, 5, 0x0298);
308 g_assert_cmpstr (string->str, ==, "first\xCA\x98half");
309 g_string_free (string, TRUE);
311 string = g_string_new ("firsthalf");
312 g_string_insert_unichar (string, 5, 0xFFFD);
313 g_assert_cmpstr (string->str, ==, "first\xEF\xBF\xBDhalf");
314 g_string_free (string, TRUE);
316 string = g_string_new ("firsthalf");
317 g_string_insert_unichar (string, 5, 0x1D100);
318 g_assert_cmpstr (string->str, ==, "first\xF0\x9D\x84\x80half");
319 g_string_free (string, TRUE);
321 /* insert_unichar with insertion at end */
322 string = g_string_new ("start");
323 g_string_insert_unichar (string, -1, 0x0041);
324 g_assert_cmpstr (string->str, ==, "start\x41");
325 g_string_free (string, TRUE);
327 string = g_string_new ("start");
328 g_string_insert_unichar (string, -1, 0x0298);
329 g_assert_cmpstr (string->str, ==, "start\xCA\x98");
330 g_string_free (string, TRUE);
332 string = g_string_new ("start");
333 g_string_insert_unichar (string, -1, 0xFFFD);
334 g_assert_cmpstr (string->str, ==, "start\xEF\xBF\xBD");
335 g_string_free (string, TRUE);
337 string = g_string_new ("start");
338 g_string_insert_unichar (string, -1, 0x1D100);
339 g_assert_cmpstr (string->str, ==, "start\xF0\x9D\x84\x80");
340 g_string_free (string, TRUE);
344 test_string_equal (void)
346 GString *string1, *string2;
348 string1 = g_string_new ("test");
349 string2 = g_string_new ("te");
350 g_assert (!g_string_equal(string1, string2));
351 g_string_append (string2, "st");
352 g_assert (g_string_equal(string1, string2));
353 g_string_free (string1, TRUE);
354 g_string_free (string2, TRUE);
358 test_string_truncate (void)
362 string = g_string_new ("testing");
364 g_string_truncate (string, 1000);
365 g_assert (string->len == strlen("testing"));
366 g_assert_cmpstr (string->str, ==, "testing");
368 g_string_truncate (string, 4);
369 g_assert (string->len == 4);
370 g_assert_cmpstr (string->str, ==, "test");
372 g_string_truncate (string, 0);
373 g_assert (string->len == 0);
374 g_assert_cmpstr (string->str, ==, "");
376 g_string_free (string, TRUE);
380 test_string_overwrite (void)
384 /* overwriting functions */
385 string = g_string_new ("testing");
387 g_string_overwrite (string, 4, " and expand");
388 g_assert (15 == string->len);
389 g_assert ('\0' == string->str[15]);
390 g_assert (g_str_equal ("test and expand", string->str));
392 g_string_overwrite (string, 5, "NOT-");
393 g_assert (15 == string->len);
394 g_assert ('\0' == string->str[15]);
395 g_assert (g_str_equal ("test NOT-expand", string->str));
397 g_string_overwrite_len (string, 9, "blablabla", 6);
398 g_assert (15 == string->len);
399 g_assert ('\0' == string->str[15]);
400 g_assert (g_str_equal ("test NOT-blabla", string->str));
402 g_string_overwrite_len (string, 4, "BLABL", 0);
403 g_assert (g_str_equal ("test NOT-blabla", string->str));
404 g_string_overwrite_len (string, 4, "BLABL", -1);
405 g_assert (g_str_equal ("testBLABLblabla", string->str));
407 g_string_free (string, TRUE);
411 test_string_nul_handling (void)
413 GString *string1, *string2;
415 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
416 string1 = g_string_new ("fiddle");
417 string2 = g_string_new ("fiddle");
418 g_assert (g_string_equal (string1, string2));
419 g_string_append_c (string1, '\0');
420 g_assert (!g_string_equal (string1, string2));
421 g_string_append_c (string2, '\0');
422 g_assert (g_string_equal (string1, string2));
423 g_string_append_c (string1, 'x');
424 g_string_append_c (string2, 'y');
425 g_assert (!g_string_equal (string1, string2));
426 g_assert (string1->len == 8);
427 g_string_append (string1, "yzzy");
428 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
429 g_string_insert (string1, 1, "QED");
430 g_assert_cmpmem (string1->str, string1->len + 1, "fQEDiddle\0xyzzy", 16);
431 g_string_printf (string1, "fiddle%cxyzzy", '\0');
432 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
434 g_string_free (string1, TRUE);
435 g_string_free (string2, TRUE);
439 test_string_up_down (void)
443 s = g_string_new ("Mixed Case String !?");
444 g_string_ascii_down (s);
445 g_assert_cmpstr (s->str, ==, "mixed case string !?");
447 g_string_assign (s, "Mixed Case String !?");
449 g_assert_cmpstr (s->str, ==, "mixed case string !?");
451 g_string_assign (s, "Mixed Case String !?");
452 g_string_ascii_up (s);
453 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
455 g_string_assign (s, "Mixed Case String !?");
457 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
459 g_string_free (s, TRUE);
463 test_string_set_size (void)
467 s = g_string_new ("foo");
468 g_string_set_size (s, 30);
470 g_assert_cmpstr (s->str, ==, "foo");
471 g_assert_cmpint (s->len, ==, 30);
473 g_string_free (s, TRUE);
477 test_string_to_bytes (void)
481 gconstpointer byte_data;
484 s = g_string_new ("foo");
485 g_string_append (s, "-bar");
487 bytes = g_string_free_to_bytes (s);
489 byte_data = g_bytes_get_data (bytes, &byte_len);
491 g_assert_cmpint (byte_len, ==, 7);
493 g_assert_cmpmem (byte_data, byte_len, "foo-bar", 7);
495 g_bytes_unref (bytes);
499 test_string_replace (void)
504 s = g_string_new ("foo bar foo baz foo bar foobarbaz");
506 n = g_string_replace (s, "bar", "baz", 0);
507 g_assert_cmpstr ("foo baz foo baz foo baz foobazbaz", ==, s->str);
508 g_assert_cmpint (n, ==, 3);
510 n = g_string_replace (s, "baz", "bar", 3);
511 g_assert_cmpstr ("foo bar foo bar foo bar foobazbaz", ==, s->str);
512 g_assert_cmpint (n, ==, 3);
514 n = g_string_replace (s, "foobar", "bar", 1);
515 g_assert_cmpstr ("foo bar foo bar foo bar foobazbaz", ==, s->str);
516 g_assert_cmpint (n, ==, 0);
518 s = g_string_assign (s, "aaaaaaaa");
519 n = g_string_replace (s, "a", "abcdefghijkl", 0);
520 g_assert_cmpstr ("abcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijkl",
523 g_string_free (s, TRUE);
530 g_test_init (&argc, &argv, NULL);
532 g_test_add_func ("/string/test-string-chunks", test_string_chunks);
533 g_test_add_func ("/string/test-string-chunk-insert", test_string_chunk_insert);
534 g_test_add_func ("/string/test-string-new", test_string_new);
535 g_test_add_func ("/string/test-string-printf", test_string_printf);
536 g_test_add_func ("/string/test-string-assign", test_string_assign);
537 g_test_add_func ("/string/test-string-append-c", test_string_append_c);
538 g_test_add_func ("/string/test-string-append", test_string_append);
539 g_test_add_func ("/string/test-string-prepend-c", test_string_prepend_c);
540 g_test_add_func ("/string/test-string-prepend", test_string_prepend);
541 g_test_add_func ("/string/test-string-insert", test_string_insert);
542 g_test_add_func ("/string/test-string-insert-unichar", test_string_insert_unichar);
543 g_test_add_func ("/string/test-string-equal", test_string_equal);
544 g_test_add_func ("/string/test-string-truncate", test_string_truncate);
545 g_test_add_func ("/string/test-string-overwrite", test_string_overwrite);
546 g_test_add_func ("/string/test-string-nul-handling", test_string_nul_handling);
547 g_test_add_func ("/string/test-string-up-down", test_string_up_down);
548 g_test_add_func ("/string/test-string-set-size", test_string_set_size);
549 g_test_add_func ("/string/test-string-to-bytes", test_string_to_bytes);
550 g_test_add_func ("/string/test-string-replace", test_string_replace);