1 /* Unit tests for gstring
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * SPDX-License-Identifier: LicenseRef-old-glib-tests
5 *
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
9 *
10 * This work is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
22 */
23
24 /* We are testing some deprecated APIs here */
25 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include "glib.h"
32
33
34 static void
35 test_string_chunks (void)
36 {
37 GStringChunk *string_chunk;
38 gchar *tmp_string, *tmp_string_2;
39 gint i;
40
41 string_chunk = g_string_chunk_new (1024);
42
43 for (i = 0; i < 100000; i ++)
44 {
45 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
46 g_assert_cmpstr ("hi pete", ==, tmp_string);
47 }
48
49 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
50 g_assert_true (tmp_string_2 != tmp_string);
51 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
52
53 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
54 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
55
56 g_string_chunk_clear (string_chunk);
57 g_string_chunk_free (string_chunk);
58 }
59
60 static void
61 test_string_chunk_insert (void)
62 {
63 const gchar s0[] = "Testing GStringChunk";
64 const gchar s1[] = "a\0b\0c\0d\0";
65 const gchar s2[] = "Hello, world";
66 GStringChunk *chunk;
67 gchar *str[3];
68
69 chunk = g_string_chunk_new (512);
70
71 str[0] = g_string_chunk_insert (chunk, s0);
72 str[1] = g_string_chunk_insert_len (chunk, s1, 8);
73 str[2] = g_string_chunk_insert (chunk, s2);
74
75 g_assert_cmpmem (s0, sizeof s0, str[0], sizeof s0);
76 g_assert_cmpmem (s1, sizeof s1, str[1], sizeof s1);
77 g_assert_cmpmem (s2, sizeof s2, str[2], sizeof s2);
78
79 g_string_chunk_free (chunk);
80 }
81
82 static void
83 test_string_new (void)
84 {
85 GString *string1, *string2;
86
87 string1 = g_string_new ("hi pete!");
88 string2 = g_string_new (NULL);
89
90 g_assert_nonnull (string1);
91 g_assert_nonnull (string2);
92 g_assert_cmpuint (strlen (string1->str), ==, string1->len);
93 g_assert_cmpuint (strlen (string2->str), ==, string2->len);
94 g_assert_cmpuint (string2->len, ==, 0);
95 g_assert_cmpstr ("hi pete!", ==, string1->str);
96 g_assert_cmpstr ("", ==, string2->str);
97
98 g_string_free (string1, TRUE);
99 g_string_free (string2, TRUE);
100
101 string1 = g_string_new_len ("foo", -1);
102 string2 = g_string_new_len ("foobar", 3);
103
104 g_assert_cmpstr (string1->str, ==, "foo");
105 g_assert_cmpuint (string1->len, ==, 3);
106 g_assert_cmpstr (string2->str, ==, "foo");
107 g_assert_cmpuint (string2->len, ==, 3);
108
109 g_string_free (string1, TRUE);
110 g_string_free (string2, TRUE);
111 }
112
113 G_GNUC_PRINTF(2, 3)
114 static void
115 my_string_printf (GString *string,
116 const gchar *format,
117 ...)
118 {
119 va_list args;
120
121 va_start (args, format);
122 g_string_vprintf (string, format, args);
123 va_end (args);
124 }
125
126 static void
127 test_string_printf (void)
128 {
129 GString *string;
130
131 string = g_string_new (NULL);
132
133 #ifndef G_OS_WIN32
134 /* MSVC and mingw32 use the same run-time C library, which doesn't like
135 the %10000.10000f format... */
136 g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%10000.10000f",
137 "this pete guy sure is a wuss, like he's the number ",
138 1,
139 " wuss. everyone agrees.\n",
140 10, 666, 15, 15, 666.666666666, 666.666666666);
141 #else
142 g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%100.100f",
143 "this pete guy sure is a wuss, like he's the number ",
144 1,
145 " wuss. everyone agrees.\n",
146 10, 666, 15, 15, 666.666666666, 666.666666666);
147 #endif
148
149 g_string_free (string, TRUE);
150
151 string = g_string_new (NULL);
152 g_string_printf (string, "bla %s %d", "foo", 99);
153 g_assert_cmpstr (string->str, ==, "bla foo 99");
154 my_string_printf (string, "%d,%s,%d", 1, "two", 3);
155 g_assert_cmpstr (string->str, ==, "1,two,3");
156
157 g_string_free (string, TRUE);
158 }
159
160 static void
161 test_string_assign (void)
162 {
163 GString *string;
164
165 string = g_string_new (NULL);
166 g_string_assign (string, "boring text");
167 g_assert_cmpstr (string->str, ==, "boring text");
168 g_string_free (string, TRUE);
169
170 /* assign with string overlap */
171 string = g_string_new ("textbeforetextafter");
172 g_string_assign (string, string->str + 10);
173 g_assert_cmpstr (string->str, ==, "textafter");
174 g_string_free (string, TRUE);
175
176 string = g_string_new ("boring text");
177 g_string_assign (string, string->str);
178 g_assert_cmpstr (string->str, ==, "boring text");
179 g_string_free (string, TRUE);
180 }
181
182 static void
183 test_string_append_c (void)
184 {
185 GString *string;
186 guint i;
187
188 string = g_string_new ("hi pete!");
189
190 for (i = 0; i < 10000; i++)
191 if (i % 2)
192 g_string_append_c (string, 'a'+(i%26));
193 else
194 (g_string_append_c) (string, 'a'+(i%26));
195
196 g_assert_true ((strlen("hi pete!") + 10000) == string->len);
197 g_assert_true ((strlen("hi pete!") + 10000) == strlen(string->str));
198
199 for (i = 0; i < 10000; i++)
200 g_assert_true (string->str[strlen ("Hi pete!") + i] == 'a' + (gchar) (i%26));
201
202 g_string_free (string, TRUE);
203 }
204
205 static void
206 test_string_append (void)
207 {
208 GString *string;
209 char *tmp;
210 int i;
211
212 tmp = g_strdup ("more");
213
214 /* append */
215 string = g_string_new ("firsthalf");
216 g_string_append (string, "last");
217 (g_string_append) (string, "half");
218
219 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
220
221 i = 0;
222 g_string_append (string, &tmp[i++]);
223 (g_string_append) (string, &tmp[i++]);
224 g_assert_true (i == 2);
225
226 g_assert_cmpstr (string->str, ==, "firsthalflasthalfmoreore");
227
228 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
229 "*assertion*string != NULL*failed*");
230 g_assert_null (g_string_append (NULL, NULL));
231 g_test_assert_expected_messages ();
232
233 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
234 "*assertion*string != NULL*failed*");
235 g_assert_null ((g_string_append) (NULL, NULL));
236 g_test_assert_expected_messages ();
237
238 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
239 "*assertion*val != NULL*failed*");
240 g_assert_true (g_string_append (string, NULL) == string);
241 g_test_assert_expected_messages ();
242
243 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
244 "*assertion*val != NULL*failed*");
245 g_assert_true ((g_string_append) (string, NULL) == string);
246 g_test_assert_expected_messages ();
247
248 g_string_free (string, TRUE);
249 g_free (tmp);
250
251 /* append_len */
252 string = g_string_new ("firsthalf");
253 g_string_append_len (string, "lasthalfjunkjunk", strlen ("last"));
254 (g_string_append_len) (string, "halfjunkjunk", strlen ("half"));
255 g_string_append_len (string, "more", -1);
256 (g_string_append_len) (string, "ore", -1);
257
258 g_assert_true (g_string_append_len (string, NULL, 0) == string);
259 g_assert_true ((g_string_append_len) (string, NULL, 0) == string);
260
261 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
262 "*assertion*string != NULL*failed*");
263 g_assert_null (g_string_append_len (NULL, NULL, -1));
264 g_test_assert_expected_messages ();
265
266 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
267 "*assertion*string != NULL*failed*");
268 g_assert_null ((g_string_append_len) (NULL, NULL, -1));
269 g_test_assert_expected_messages ();
270
271 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
272 "*assertion*val != NULL*failed*");
273 g_assert_true (g_string_append_len (string, NULL, -1) == string);
274 g_test_assert_expected_messages ();
275
276 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
277 "*assertion*val != NULL*failed*");
278 g_assert_true ((g_string_append_len) (string, NULL, -1) == string);
279 g_test_assert_expected_messages ();
280
281 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
282 "*assertion*val != NULL*failed*");
283 g_assert_true (g_string_append_len (string, NULL, 1) == string);
284 g_test_assert_expected_messages ();
285
286 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
287 "*assertion*val != NULL*failed*");
288 g_assert_true ((g_string_append_len) (string, NULL, 1) == string);
289 g_test_assert_expected_messages ();
290
291 g_assert_cmpstr (string->str, ==, "firsthalflasthalfmoreore");
292 g_string_free (string, TRUE);
293 }
294
295 static void string_append_vprintf_va (GString *string,
296 const gchar *format,
297 ...) G_GNUC_PRINTF (2, 3);
298
299 /* Wrapper around g_string_append_vprintf() which takes varargs */
300 static void
301 string_append_vprintf_va (GString *string,
302 const gchar *format,
303 ...)
304 {
305 va_list args;
306
307 va_start (args, format);
308 g_string_append_vprintf (string, format, args);
309 va_end (args);
310 }
311
312 static void
313 test_string_append_vprintf (void)
314 {
315 GString *string;
316
317 /* append */
318 string = g_string_new ("firsthalf");
319
320 string_append_vprintf_va (string, "some %s placeholders", "format");
321
322 /* vasprintf() placeholder checks on BSDs are less strict, so skip these checks if so */
323 #if !defined(__APPLE__) && !defined(__FreeBSD__)
324 if (g_test_undefined ())
325 {
326 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
327 "Failed to append to string*");
328 #pragma GCC diagnostic push
329 #pragma GCC diagnostic ignored "-Wformat"
330 #pragma GCC diagnostic ignored "-Wformat-extra-args"
331 string_append_vprintf_va (string, "%l", "invalid");
332 #pragma GCC diagnostic pop
333 g_test_assert_expected_messages ();
334 }
335 #endif
336
337 g_assert_cmpstr (string->str, ==, "firsthalfsome format placeholders");
338
339 g_string_free (string, TRUE);
340 }
341
342 static void
343 test_string_prepend_c (void)
344 {
345 GString *string;
346 gint i;
347
348 string = g_string_new ("hi pete!");
349
350 for (i = 0; i < 10000; i++)
351 g_string_prepend_c (string, 'a'+(i%26));
352
353 g_assert((strlen("hi pete!") + 10000) == string->len);
354 g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
355
356 g_string_free (string, TRUE);
357 }
358
359 static void
360 test_string_prepend (void)
361 {
362 GString *string;
363
364 /* prepend */
365 string = g_string_new ("lasthalf");
366 g_string_prepend (string, "firsthalf");
367 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
368 g_string_free (string, TRUE);
369
370 /* prepend_len */
371 string = g_string_new ("lasthalf");
372 g_string_prepend_len (string, "firsthalfjunkjunk", strlen ("firsthalf"));
373 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
374 g_string_free (string, TRUE);
375 }
376
377 static void
378 test_string_insert (void)
379 {
380 GString *string;
381
382 /* insert */
383 string = g_string_new ("firstlast");
384 g_string_insert (string, 5, "middle");
385 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
386 g_string_free (string, TRUE);
387
388 /* insert with pos == end of the string */
389 string = g_string_new ("firstmiddle");
390 g_string_insert (string, strlen ("firstmiddle"), "last");
391 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
392 g_string_free (string, TRUE);
393
394 /* insert_len */
395 string = g_string_new ("firstlast");
396 g_string_insert_len (string, 5, "middlejunkjunk", strlen ("middle"));
397 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
398 g_string_free (string, TRUE);
399
400 /* insert_len with magic -1 pos for append */
401 string = g_string_new ("first");
402 g_string_insert_len (string, -1, "lastjunkjunk", strlen ("last"));
403 g_assert_cmpstr (string->str, ==, "firstlast");
404 g_string_free (string, TRUE);
405
406 /* insert_len with magic -1 len for strlen-the-string */
407 string = g_string_new ("first");
408 g_string_insert_len (string, 5, "last", -1);
409 g_assert_cmpstr (string->str, ==, "firstlast");
410 g_string_free (string, TRUE);
411
412 /* insert_len with string overlap */
413 string = g_string_new ("textbeforetextafter");
414 g_string_insert_len (string, 10, string->str + 8, 5);
415 g_assert_cmpstr (string->str, ==, "textbeforeretextextafter");
416 g_string_free (string, TRUE);
417 }
418
419 static void
420 test_string_insert_unichar (void)
421 {
422 GString *string;
423
424 /* insert_unichar with insertion in middle */
425 string = g_string_new ("firsthalf");
426 g_string_insert_unichar (string, 5, 0x0041);
427 g_assert_cmpstr (string->str, ==, "first\x41half");
428 g_string_free (string, TRUE);
429
430 string = g_string_new ("firsthalf");
431 g_string_insert_unichar (string, 5, 0x0298);
432 g_assert_cmpstr (string->str, ==, "first\xCA\x98half");
433 g_string_free (string, TRUE);
434
435 string = g_string_new ("firsthalf");
436 g_string_insert_unichar (string, 5, 0xFFFD);
437 g_assert_cmpstr (string->str, ==, "first\xEF\xBF\xBDhalf");
438 g_string_free (string, TRUE);
439
440 string = g_string_new ("firsthalf");
441 g_string_insert_unichar (string, 5, 0x1D100);
442 g_assert_cmpstr (string->str, ==, "first\xF0\x9D\x84\x80half");
443 g_string_free (string, TRUE);
444
445 /* insert_unichar with insertion at end */
446 string = g_string_new ("start");
447 g_string_insert_unichar (string, -1, 0x0041);
448 g_assert_cmpstr (string->str, ==, "start\x41");
449 g_string_free (string, TRUE);
450
451 string = g_string_new ("start");
452 g_string_insert_unichar (string, -1, 0x0298);
453 g_assert_cmpstr (string->str, ==, "start\xCA\x98");
454 g_string_free (string, TRUE);
455
456 string = g_string_new ("start");
457 g_string_insert_unichar (string, -1, 0xFFFD);
458 g_assert_cmpstr (string->str, ==, "start\xEF\xBF\xBD");
459 g_string_free (string, TRUE);
460
461 string = g_string_new ("start");
462 g_string_insert_unichar (string, -1, 0x1D100);
463 g_assert_cmpstr (string->str, ==, "start\xF0\x9D\x84\x80");
464 g_string_free (string, TRUE);
465
466 string = g_string_new ("start");
467 g_string_insert_unichar (string, -1, 0xFFD0);
468 g_assert_cmpstr (string->str, ==, "start\xEF\xBF\x90");
469 g_string_free (string, TRUE);
470
471 string = g_string_new ("start");
472 g_string_insert_unichar (string, -1, 0xFDD0);
473 g_assert_cmpstr (string->str, ==, "start\xEF\xB7\x90");
474 g_string_free (string, TRUE);
475 }
476
477 static void
478 test_string_equal (void)
479 {
480 GString *string1, *string2;
481
482 string1 = g_string_new ("test");
483 string2 = g_string_new ("te");
484 g_assert_false (g_string_equal (string1, string2));
485 g_string_append (string2, "st");
486 g_assert_true (g_string_equal (string1, string2));
487 g_string_free (string1, TRUE);
488 g_string_free (string2, TRUE);
489 }
490
491 static void
492 test_string_truncate (void)
493 {
494 GString *string;
495
496 string = g_string_new ("testing");
497
498 g_string_truncate (string, 1000);
499 g_assert_cmpuint (string->len, ==, strlen("testing"));
500 g_assert_cmpstr (string->str, ==, "testing");
501
502 (g_string_truncate) (string, 4);
503 g_assert_cmpuint (string->len, ==, 4);
504 g_assert_cmpstr (string->str, ==, "test");
505
506 g_string_truncate (string, 0);
507 g_assert_cmpuint (string->len, ==, 0);
508 g_assert_cmpstr (string->str, ==, "");
509
510 g_string_free (string, TRUE);
511 }
512
513 static void
514 test_string_overwrite (void)
515 {
516 GString *string;
517
518 /* overwriting functions */
519 string = g_string_new ("testing");
520
521 g_string_overwrite (string, 4, " and expand");
522 g_assert_cmpuint (15, ==, string->len);
523 g_assert_true ('\0' == string->str[15]);
524 g_assert_true (g_str_equal ("test and expand", string->str));
525
526 g_string_overwrite (string, 5, "NOT-");
527 g_assert_cmpuint (15, ==, string->len);
528 g_assert_true ('\0' == string->str[15]);
529 g_assert_true (g_str_equal ("test NOT-expand", string->str));
530
531 g_string_overwrite_len (string, 9, "blablabla", 6);
532 g_assert_cmpuint (15, ==, string->len);
533 g_assert_true ('\0' == string->str[15]);
534 g_assert_true (g_str_equal ("test NOT-blabla", string->str));
535
536 g_string_overwrite_len (string, 4, "BLABL", 0);
537 g_assert_true (g_str_equal ("test NOT-blabla", string->str));
538 g_string_overwrite_len (string, 4, "BLABL", -1);
539 g_assert_true (g_str_equal ("testBLABLblabla", string->str));
540
541 g_string_free (string, TRUE);
542 }
543
544 static void
545 test_string_nul_handling (void)
546 {
547 GString *string1, *string2;
548
549 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
550 string1 = g_string_new ("fiddle");
551 string2 = g_string_new ("fiddle");
552 g_assert_true (g_string_equal (string1, string2));
553 g_string_append_c (string1, '\0');
554 g_assert_false (g_string_equal (string1, string2));
555 g_string_append_c (string2, '\0');
556 g_assert_true (g_string_equal (string1, string2));
557 g_string_append_c (string1, 'x');
558 g_string_append_c (string2, 'y');
559 g_assert_false (g_string_equal (string1, string2));
560 g_assert_cmpuint (string1->len, ==, 8);
561 g_string_append (string1, "yzzy");
562 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
563 g_string_insert (string1, 1, "QED");
564 g_assert_cmpmem (string1->str, string1->len + 1, "fQEDiddle\0xyzzy", 16);
565 g_string_printf (string1, "fiddle%cxyzzy", '\0');
566 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
567
568 g_string_free (string1, TRUE);
569 g_string_free (string2, TRUE);
570 }
571
572 static void
573 test_string_up_down (void)
574 {
575 GString *s;
576
577 s = g_string_new ("Mixed Case String !?");
578 g_string_ascii_down (s);
579 g_assert_cmpstr (s->str, ==, "mixed case string !?");
580
581 g_string_assign (s, "Mixed Case String !?");
582 g_string_down (s);
583 g_assert_cmpstr (s->str, ==, "mixed case string !?");
584
585 g_string_assign (s, "Mixed Case String !?");
586 g_string_ascii_up (s);
587 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
588
589 g_string_assign (s, "Mixed Case String !?");
590 g_string_up (s);
591 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
592
593 g_string_free (s, TRUE);
594 }
595
596 static void
597 test_string_set_size (void)
598 {
599 GString *s;
600
601 s = g_string_new ("foo");
602 g_string_set_size (s, 30);
603
604 g_assert_cmpstr (s->str, ==, "foo");
605 g_assert_cmpuint (s->len, ==, 30);
606
607 g_string_free (s, TRUE);
608 }
609
610 static void
611 test_string_to_bytes (void)
612 {
613 GString *s;
614 GBytes *bytes;
615 gconstpointer byte_data;
616 gsize byte_len;
617
618 s = g_string_new ("foo");
619 g_string_append (s, "-bar");
620
621 bytes = g_string_free_to_bytes (s);
622
623 byte_data = g_bytes_get_data (bytes, &byte_len);
624
625 g_assert_cmpuint (byte_len, ==, 7);
626
627 g_assert_cmpmem (byte_data, byte_len, "foo-bar", 7);
628
629 g_bytes_unref (bytes);
630 }
631
632 static void
633 test_string_replace (void)
634 {
635 static const struct
636 {
637 const char *string;
638 const char *original;
639 const char *replacement;
640 guint limit;
641 const char *expected;
642 guint expected_n;
643 }
644 tests[] =
645 {
646 { "foo bar foo baz foo bar foobarbaz", "bar", "baz", 0,
647 "foo baz foo baz foo baz foobazbaz", 3 },
648 { "foo baz foo baz foo baz foobazbaz", "baz", "bar", 3,
649 "foo bar foo bar foo bar foobazbaz", 3 },
650 { "foo bar foo bar foo bar foobazbaz", "foobar", "bar", 1,
651 "foo bar foo bar foo bar foobazbaz", 0 },
652 { "aaaaaaaa", "a", "abcdefghijkl", 0,
653 "abcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijkl",
654 8 },
655 { "/usr/$LIB/libMangoHud.so", "$LIB", "lib32", 0,
656 "/usr/lib32/libMangoHud.so", 1 },
657 { "food for foals", "o", "", 0,
658 "fd fr fals", 4 },
659 { "aaa", "a", "aaa", 0,
660 "aaaaaaaaa", 3 },
661 { "aaa", "a", "", 0,
662 "", 3 },
663 { "aaa", "aa", "bb", 0,
664 "bba", 1 },
665 { "foo", "", "bar", 0,
666 "barfbarobarobar", 4 },
667 { "", "", "x", 0,
668 "x", 1 },
669 { "", "", "", 0,
670 "", 1 },
671 };
672 gsize i;
673
674 for (i = 0; i < G_N_ELEMENTS (tests); i++)
675 {
676 GString *s;
677 guint n;
678
679 s = g_string_new (tests[i].string);
680 g_test_message ("%" G_GSIZE_FORMAT ": Replacing \"%s\" with \"%s\" (limit %u) in \"%s\"",
681 i, tests[i].original, tests[i].replacement,
682 tests[i].limit, tests[i].string);
683 n = g_string_replace (s, tests[i].original, tests[i].replacement,
684 tests[i].limit);
685 g_test_message ("-> %u replacements, \"%s\"",
686 n, s->str);
687 g_assert_cmpstr (tests[i].expected, ==, s->str);
688 g_assert_cmpuint (strlen (tests[i].expected), ==, s->len);
689 g_assert_cmpuint (strlen (tests[i].expected) + 1, <=, s->allocated_len);
690 g_assert_cmpuint (tests[i].expected_n, ==, n);
691 g_string_free (s, TRUE);
692 }
693 }
694
695 static void
696 test_string_steal (void)
697 {
698 GString *string;
699 char *str;
700
701 string = g_string_new ("One");
702 g_string_append (string, ", two");
703 g_string_append (string, ", three");
704 g_string_append_c (string, '.');
705
706 str = g_string_free (string, FALSE);
707
708 g_assert_cmpstr (str, ==, "One, two, three.");
709 g_free (str);
710
711 string = g_string_new ("1");
712 g_string_append (string, " 2");
713 g_string_append (string, " 3");
714
715 str = g_string_free_and_steal (string);
716
717 g_assert_cmpstr (str, ==, "1 2 3");
718 g_free (str);
719 }
720
721 static void
722 test_string_new_take (void)
723 {
724 const char *test_str_const = "test_test";
725 const char *replaced_str_const = "test__test";
726 char *test_str = malloc (10 * sizeof (*test_str_const));
727 GString *string;
728
729 strcpy (test_str, test_str_const);
730 g_assert_cmpstr (test_str, ==, test_str_const);
731
732 string = g_string_new_take (g_steal_pointer (&test_str));
733 g_assert_null (test_str);
734 g_assert_nonnull (string);
735
736 g_string_replace (string, "_", "__", 0);
737 g_assert_cmpstr (string->str, ==, replaced_str_const);
738
739 test_str = g_string_free_and_steal (g_steal_pointer (&string));
740 g_assert_cmpstr (test_str, ==, replaced_str_const);
741
742 g_free (test_str);
743 }
744
745 static void
746 test_string_new_take_null (void)
747 {
748 GString *string = g_string_new_take (NULL);
749
750 g_assert_cmpstr (string->str, ==, "");
751
752 g_string_free (g_steal_pointer (&string), TRUE);
753 }
754
755 int
756 main (int argc,
757 char *argv[])
758 {
759 g_test_init (&argc, &argv, NULL);
760
761 g_test_add_func ("/string/test-string-chunks", test_string_chunks);
762 g_test_add_func ("/string/test-string-chunk-insert", test_string_chunk_insert);
763 g_test_add_func ("/string/test-string-new", test_string_new);
764 g_test_add_func ("/string/test-string-printf", test_string_printf);
765 g_test_add_func ("/string/test-string-assign", test_string_assign);
766 g_test_add_func ("/string/test-string-append-c", test_string_append_c);
767 g_test_add_func ("/string/test-string-append", test_string_append);
768 g_test_add_func ("/string/test-string-append-vprintf", test_string_append_vprintf);
769 g_test_add_func ("/string/test-string-prepend-c", test_string_prepend_c);
770 g_test_add_func ("/string/test-string-prepend", test_string_prepend);
771 g_test_add_func ("/string/test-string-insert", test_string_insert);
772 g_test_add_func ("/string/test-string-insert-unichar", test_string_insert_unichar);
773 g_test_add_func ("/string/test-string-equal", test_string_equal);
774 g_test_add_func ("/string/test-string-truncate", test_string_truncate);
775 g_test_add_func ("/string/test-string-overwrite", test_string_overwrite);
776 g_test_add_func ("/string/test-string-nul-handling", test_string_nul_handling);
777 g_test_add_func ("/string/test-string-up-down", test_string_up_down);
778 g_test_add_func ("/string/test-string-set-size", test_string_set_size);
779 g_test_add_func ("/string/test-string-to-bytes", test_string_to_bytes);
780 g_test_add_func ("/string/test-string-replace", test_string_replace);
781 g_test_add_func ("/string/test-string-steal", test_string_steal);
782 g_test_add_func ("/string/test-string-new-take", test_string_new_take);
783 g_test_add_func ("/string/test-string-new-take/null", test_string_new_take_null);
784
785 return g_test_run();
786 }