1/* Unit tests for gstring
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
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.
7 *
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.
11 *
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.
20 */
21
22/* We are testing some deprecated APIs here */
23#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
24#define GLIB_DISABLE_DEPRECATION_WARNINGS
25#endif
26
27#include <stdio.h>
28#include <string.h>
29#include "glib.h"
30
31
32static void
33test_string_chunks (void)
34{
35 GStringChunk *string_chunk;
36 gchar *tmp_string, *tmp_string_2;
37 gint i;
38
39 string_chunk = g_string_chunk_new (size: 1024);
40
41 for (i = 0; i < 100000; i ++)
42 {
43 tmp_string = g_string_chunk_insert (chunk: string_chunk, string: "hi pete");
44 g_assert_cmpstr ("hi pete", ==, tmp_string);
45 }
46
47 tmp_string_2 = g_string_chunk_insert_const (chunk: string_chunk, string: tmp_string);
48 g_assert (tmp_string_2 != tmp_string);
49 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
50
51 tmp_string = g_string_chunk_insert_const (chunk: string_chunk, string: tmp_string);
52 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
53
54 g_string_chunk_clear (chunk: string_chunk);
55 g_string_chunk_free (chunk: string_chunk);
56}
57
58static void
59test_string_chunk_insert (void)
60{
61 const gchar s0[] = "Testing GStringChunk";
62 const gchar s1[] = "a\0b\0c\0d\0";
63 const gchar s2[] = "Hello, world";
64 GStringChunk *chunk;
65 gchar *str[3];
66
67 chunk = g_string_chunk_new (size: 512);
68
69 str[0] = g_string_chunk_insert (chunk, string: s0);
70 str[1] = g_string_chunk_insert_len (chunk, string: s1, len: 8);
71 str[2] = g_string_chunk_insert (chunk, string: s2);
72
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);
76
77 g_string_chunk_free (chunk);
78}
79
80static void
81test_string_new (void)
82{
83 GString *string1, *string2;
84
85 string1 = g_string_new (init: "hi pete!");
86 string2 = g_string_new (NULL);
87
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);
95
96 g_string_free (string: string1, TRUE);
97 g_string_free (string: string2, TRUE);
98
99 string1 = g_string_new_len (init: "foo", len: -1);
100 string2 = g_string_new_len (init: "foobar", len: 3);
101
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);
106
107 g_string_free (string: string1, TRUE);
108 g_string_free (string: string2, TRUE);
109}
110
111G_GNUC_PRINTF(2, 3)
112static void
113my_string_printf (GString *string,
114 const gchar *format,
115 ...)
116{
117 va_list args;
118
119 va_start (args, format);
120 g_string_vprintf (string, format, args);
121 va_end (args);
122}
123
124static void
125test_string_printf (void)
126{
127 GString *string;
128
129 string = g_string_new (NULL);
130
131#ifndef G_OS_WIN32
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, format: "%s|%0100d|%s|%0*d|%*.*f|%10000.10000f",
135 "this pete guy sure is a wuss, like he's the number ",
136 1,
137 " wuss. everyone agrees.\n",
138 10, 666, 15, 15, 666.666666666, 666.666666666);
139#else
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 ",
142 1,
143 " wuss. everyone agrees.\n",
144 10, 666, 15, 15, 666.666666666, 666.666666666);
145#endif
146
147 g_string_free (string, TRUE);
148
149 string = g_string_new (NULL);
150 g_string_printf (string, format: "bla %s %d", "foo", 99);
151 g_assert_cmpstr (string->str, ==, "bla foo 99");
152 my_string_printf (string, format: "%d,%s,%d", 1, "two", 3);
153 g_assert_cmpstr (string->str, ==, "1,two,3");
154
155 g_string_free (string, TRUE);
156}
157
158static void
159test_string_assign (void)
160{
161 GString *string;
162
163 string = g_string_new (NULL);
164 g_string_assign (string, rval: "boring text");
165 g_assert_cmpstr (string->str, ==, "boring text");
166 g_string_free (string, TRUE);
167
168 /* assign with string overlap */
169 string = g_string_new (init: "textbeforetextafter");
170 g_string_assign (string, rval: string->str + 10);
171 g_assert_cmpstr (string->str, ==, "textafter");
172 g_string_free (string, TRUE);
173
174 string = g_string_new (init: "boring text");
175 g_string_assign (string, rval: string->str);
176 g_assert_cmpstr (string->str, ==, "boring text");
177 g_string_free (string, TRUE);
178}
179
180static void
181test_string_append_c (void)
182{
183 GString *string;
184 gint i;
185
186 string = g_string_new (init: "hi pete!");
187
188 for (i = 0; i < 10000; i++)
189 if (i % 2)
190 g_string_append_c (string, 'a'+(i%26));
191 else
192 (g_string_append_c) (string, c: 'a'+(i%26));
193
194 g_assert((strlen("hi pete!") + 10000) == string->len);
195 g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
196
197 g_string_free (string, TRUE);
198}
199
200static void
201test_string_append (void)
202{
203 GString *string;
204
205 /* append */
206 string = g_string_new (init: "firsthalf");
207 g_string_append (string, val: "lasthalf");
208 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
209 g_string_free (string, TRUE);
210
211 /* append_len */
212 string = g_string_new (init: "firsthalf");
213 g_string_append_len (string, val: "lasthalfjunkjunk", len: strlen (s: "lasthalf"));
214 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
215 g_string_free (string, TRUE);
216}
217
218static void
219test_string_prepend_c (void)
220{
221 GString *string;
222 gint i;
223
224 string = g_string_new (init: "hi pete!");
225
226 for (i = 0; i < 10000; i++)
227 g_string_prepend_c (string, c: 'a'+(i%26));
228
229 g_assert((strlen("hi pete!") + 10000) == string->len);
230 g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
231
232 g_string_free (string, TRUE);
233}
234
235static void
236test_string_prepend (void)
237{
238 GString *string;
239
240 /* prepend */
241 string = g_string_new (init: "lasthalf");
242 g_string_prepend (string, val: "firsthalf");
243 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
244 g_string_free (string, TRUE);
245
246 /* prepend_len */
247 string = g_string_new (init: "lasthalf");
248 g_string_prepend_len (string, val: "firsthalfjunkjunk", len: strlen (s: "firsthalf"));
249 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
250 g_string_free (string, TRUE);
251}
252
253static void
254test_string_insert (void)
255{
256 GString *string;
257
258 /* insert */
259 string = g_string_new (init: "firstlast");
260 g_string_insert (string, pos: 5, val: "middle");
261 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
262 g_string_free (string, TRUE);
263
264 /* insert with pos == end of the string */
265 string = g_string_new (init: "firstmiddle");
266 g_string_insert (string, pos: strlen (s: "firstmiddle"), val: "last");
267 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
268 g_string_free (string, TRUE);
269
270 /* insert_len */
271 string = g_string_new (init: "firstlast");
272 g_string_insert_len (string, pos: 5, val: "middlejunkjunk", len: strlen (s: "middle"));
273 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
274 g_string_free (string, TRUE);
275
276 /* insert_len with magic -1 pos for append */
277 string = g_string_new (init: "first");
278 g_string_insert_len (string, pos: -1, val: "lastjunkjunk", len: strlen (s: "last"));
279 g_assert_cmpstr (string->str, ==, "firstlast");
280 g_string_free (string, TRUE);
281
282 /* insert_len with magic -1 len for strlen-the-string */
283 string = g_string_new (init: "first");
284 g_string_insert_len (string, pos: 5, val: "last", len: -1);
285 g_assert_cmpstr (string->str, ==, "firstlast");
286 g_string_free (string, TRUE);
287
288 /* insert_len with string overlap */
289 string = g_string_new (init: "textbeforetextafter");
290 g_string_insert_len (string, pos: 10, val: string->str + 8, len: 5);
291 g_assert_cmpstr (string->str, ==, "textbeforeretextextafter");
292 g_string_free (string, TRUE);
293}
294
295static void
296test_string_insert_unichar (void)
297{
298 GString *string;
299
300 /* insert_unichar with insertion in middle */
301 string = g_string_new (init: "firsthalf");
302 g_string_insert_unichar (string, pos: 5, wc: 0x0041);
303 g_assert_cmpstr (string->str, ==, "first\x41half");
304 g_string_free (string, TRUE);
305
306 string = g_string_new (init: "firsthalf");
307 g_string_insert_unichar (string, pos: 5, wc: 0x0298);
308 g_assert_cmpstr (string->str, ==, "first\xCA\x98half");
309 g_string_free (string, TRUE);
310
311 string = g_string_new (init: "firsthalf");
312 g_string_insert_unichar (string, pos: 5, wc: 0xFFFD);
313 g_assert_cmpstr (string->str, ==, "first\xEF\xBF\xBDhalf");
314 g_string_free (string, TRUE);
315
316 string = g_string_new (init: "firsthalf");
317 g_string_insert_unichar (string, pos: 5, wc: 0x1D100);
318 g_assert_cmpstr (string->str, ==, "first\xF0\x9D\x84\x80half");
319 g_string_free (string, TRUE);
320
321 /* insert_unichar with insertion at end */
322 string = g_string_new (init: "start");
323 g_string_insert_unichar (string, pos: -1, wc: 0x0041);
324 g_assert_cmpstr (string->str, ==, "start\x41");
325 g_string_free (string, TRUE);
326
327 string = g_string_new (init: "start");
328 g_string_insert_unichar (string, pos: -1, wc: 0x0298);
329 g_assert_cmpstr (string->str, ==, "start\xCA\x98");
330 g_string_free (string, TRUE);
331
332 string = g_string_new (init: "start");
333 g_string_insert_unichar (string, pos: -1, wc: 0xFFFD);
334 g_assert_cmpstr (string->str, ==, "start\xEF\xBF\xBD");
335 g_string_free (string, TRUE);
336
337 string = g_string_new (init: "start");
338 g_string_insert_unichar (string, pos: -1, wc: 0x1D100);
339 g_assert_cmpstr (string->str, ==, "start\xF0\x9D\x84\x80");
340 g_string_free (string, TRUE);
341}
342
343static void
344test_string_equal (void)
345{
346 GString *string1, *string2;
347
348 string1 = g_string_new (init: "test");
349 string2 = g_string_new (init: "te");
350 g_assert (!g_string_equal(string1, string2));
351 g_string_append (string: string2, val: "st");
352 g_assert (g_string_equal(string1, string2));
353 g_string_free (string: string1, TRUE);
354 g_string_free (string: string2, TRUE);
355}
356
357static void
358test_string_truncate (void)
359{
360 GString *string;
361
362 string = g_string_new (init: "testing");
363
364 g_string_truncate (string, len: 1000);
365 g_assert (string->len == strlen("testing"));
366 g_assert_cmpstr (string->str, ==, "testing");
367
368 g_string_truncate (string, len: 4);
369 g_assert (string->len == 4);
370 g_assert_cmpstr (string->str, ==, "test");
371
372 g_string_truncate (string, len: 0);
373 g_assert (string->len == 0);
374 g_assert_cmpstr (string->str, ==, "");
375
376 g_string_free (string, TRUE);
377}
378
379static void
380test_string_overwrite (void)
381{
382 GString *string;
383
384 /* overwriting functions */
385 string = g_string_new (init: "testing");
386
387 g_string_overwrite (string, pos: 4, val: " 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));
391
392 g_string_overwrite (string, pos: 5, val: "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));
396
397 g_string_overwrite_len (string, pos: 9, val: "blablabla", len: 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));
401
402 g_string_overwrite_len (string, pos: 4, val: "BLABL", len: 0);
403 g_assert (g_str_equal ("test NOT-blabla", string->str));
404 g_string_overwrite_len (string, pos: 4, val: "BLABL", len: -1);
405 g_assert (g_str_equal ("testBLABLblabla", string->str));
406
407 g_string_free (string, TRUE);
408}
409
410static void
411test_string_nul_handling (void)
412{
413 GString *string1, *string2;
414
415 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
416 string1 = g_string_new (init: "fiddle");
417 string2 = g_string_new (init: "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 (string: string1, val: "yzzy");
428 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
429 g_string_insert (string: string1, pos: 1, val: "QED");
430 g_assert_cmpmem (string1->str, string1->len + 1, "fQEDiddle\0xyzzy", 16);
431 g_string_printf (string: string1, format: "fiddle%cxyzzy", '\0');
432 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
433
434 g_string_free (string: string1, TRUE);
435 g_string_free (string: string2, TRUE);
436}
437
438static void
439test_string_up_down (void)
440{
441 GString *s;
442
443 s = g_string_new (init: "Mixed Case String !?");
444 g_string_ascii_down (string: s);
445 g_assert_cmpstr (s->str, ==, "mixed case string !?");
446
447 g_string_assign (string: s, rval: "Mixed Case String !?");
448 g_string_down (string: s);
449 g_assert_cmpstr (s->str, ==, "mixed case string !?");
450
451 g_string_assign (string: s, rval: "Mixed Case String !?");
452 g_string_ascii_up (string: s);
453 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
454
455 g_string_assign (string: s, rval: "Mixed Case String !?");
456 g_string_up (string: s);
457 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
458
459 g_string_free (string: s, TRUE);
460}
461
462static void
463test_string_set_size (void)
464{
465 GString *s;
466
467 s = g_string_new (init: "foo");
468 g_string_set_size (string: s, len: 30);
469
470 g_assert_cmpstr (s->str, ==, "foo");
471 g_assert_cmpint (s->len, ==, 30);
472
473 g_string_free (string: s, TRUE);
474}
475
476static void
477test_string_to_bytes (void)
478{
479 GString *s;
480 GBytes *bytes;
481 gconstpointer byte_data;
482 gsize byte_len;
483
484 s = g_string_new (init: "foo");
485 g_string_append (string: s, val: "-bar");
486
487 bytes = g_string_free_to_bytes (string: s);
488
489 byte_data = g_bytes_get_data (bytes, size: &byte_len);
490
491 g_assert_cmpint (byte_len, ==, 7);
492
493 g_assert_cmpmem (byte_data, byte_len, "foo-bar", 7);
494
495 g_bytes_unref (bytes);
496}
497
498static void
499test_string_replace (void)
500{
501 static const struct
502 {
503 const char *string;
504 const char *original;
505 const char *replacement;
506 guint limit;
507 const char *expected;
508 guint expected_n;
509 }
510 tests[] =
511 {
512 { "foo bar foo baz foo bar foobarbaz", "bar", "baz", 0,
513 "foo baz foo baz foo baz foobazbaz", 3 },
514 { "foo baz foo baz foo baz foobazbaz", "baz", "bar", 3,
515 "foo bar foo bar foo bar foobazbaz", 3 },
516 { "foo bar foo bar foo bar foobazbaz", "foobar", "bar", 1,
517 "foo bar foo bar foo bar foobazbaz", 0 },
518 { "aaaaaaaa", "a", "abcdefghijkl", 0,
519 "abcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijkl",
520 8 },
521 { "/usr/$LIB/libMangoHud.so", "$LIB", "lib32", 0,
522 "/usr/lib32/libMangoHud.so", 1 },
523 { "food for foals", "o", "", 0,
524 "fd fr fals", 4 },
525 { "aaa", "a", "aaa", 0,
526 "aaaaaaaaa", 3 },
527 { "aaa", "a", "", 0,
528 "", 3 },
529 { "aaa", "aa", "bb", 0,
530 "bba", 1 },
531 { "foo", "", "bar", 0,
532 "barfbarobarobar", 4 },
533 { "", "", "x", 0,
534 "x", 1 },
535 { "", "", "", 0,
536 "", 1 },
537 };
538 gsize i;
539
540 for (i = 0; i < G_N_ELEMENTS (tests); i++)
541 {
542 GString *s;
543 guint n;
544
545 s = g_string_new (init: tests[i].string);
546 g_test_message (format: "%" G_GSIZE_FORMAT ": Replacing \"%s\" with \"%s\" (limit %u) in \"%s\"",
547 i, tests[i].original, tests[i].replacement,
548 tests[i].limit, tests[i].string);
549 n = g_string_replace (string: s, find: tests[i].original, replace: tests[i].replacement,
550 limit: tests[i].limit);
551 g_test_message (format: "-> %u replacements, \"%s\"",
552 n, s->str);
553 g_assert_cmpstr (tests[i].expected, ==, s->str);
554 g_assert_cmpuint (strlen (tests[i].expected), ==, s->len);
555 g_assert_cmpuint (strlen (tests[i].expected) + 1, <=, s->allocated_len);
556 g_assert_cmpuint (tests[i].expected_n, ==, n);
557 g_string_free (string: s, TRUE);
558 }
559}
560
561int
562main (int argc,
563 char *argv[])
564{
565 g_test_init (argc: &argc, argv: &argv, NULL);
566
567 g_test_add_func (testpath: "/string/test-string-chunks", test_func: test_string_chunks);
568 g_test_add_func (testpath: "/string/test-string-chunk-insert", test_func: test_string_chunk_insert);
569 g_test_add_func (testpath: "/string/test-string-new", test_func: test_string_new);
570 g_test_add_func (testpath: "/string/test-string-printf", test_func: test_string_printf);
571 g_test_add_func (testpath: "/string/test-string-assign", test_func: test_string_assign);
572 g_test_add_func (testpath: "/string/test-string-append-c", test_func: test_string_append_c);
573 g_test_add_func (testpath: "/string/test-string-append", test_func: test_string_append);
574 g_test_add_func (testpath: "/string/test-string-prepend-c", test_func: test_string_prepend_c);
575 g_test_add_func (testpath: "/string/test-string-prepend", test_func: test_string_prepend);
576 g_test_add_func (testpath: "/string/test-string-insert", test_func: test_string_insert);
577 g_test_add_func (testpath: "/string/test-string-insert-unichar", test_func: test_string_insert_unichar);
578 g_test_add_func (testpath: "/string/test-string-equal", test_func: test_string_equal);
579 g_test_add_func (testpath: "/string/test-string-truncate", test_func: test_string_truncate);
580 g_test_add_func (testpath: "/string/test-string-overwrite", test_func: test_string_overwrite);
581 g_test_add_func (testpath: "/string/test-string-nul-handling", test_func: test_string_nul_handling);
582 g_test_add_func (testpath: "/string/test-string-up-down", test_func: test_string_up_down);
583 g_test_add_func (testpath: "/string/test-string-set-size", test_func: test_string_set_size);
584 g_test_add_func (testpath: "/string/test-string-to-bytes", test_func: test_string_to_bytes);
585 g_test_add_func (testpath: "/string/test-string-replace", test_func: test_string_replace);
586
587 return g_test_run();
588}
589

source code of gtk/subprojects/glib/glib/tests/string.c