Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / glib / tests / string.c
blob017fdff53a04771767a65c6c442d789ee2b4122d
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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
25 #include <stdio.h>
26 #include <string.h>
27 #include "glib.h"
30 static void
31 test_string_chunks (void)
33 GStringChunk *string_chunk;
34 gchar *tmp_string, *tmp_string_2;
35 gint i;
37 string_chunk = g_string_chunk_new (1024);
39 for (i = 0; i < 100000; i ++)
41 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
42 g_assert_cmpstr ("hi pete", ==, tmp_string);
45 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
46 g_assert (tmp_string_2 != tmp_string);
47 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
49 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
50 g_assert_cmpstr (tmp_string_2, ==, tmp_string);
52 g_string_chunk_clear (string_chunk);
53 g_string_chunk_free (string_chunk);
56 static void
57 test_string_chunk_insert (void)
59 const gchar s0[] = "Testing GStringChunk";
60 const gchar s1[] = "a\0b\0c\0d\0";
61 const gchar s2[] = "Hello, world";
62 GStringChunk *chunk;
63 gchar *str[3];
65 chunk = g_string_chunk_new (512);
67 str[0] = g_string_chunk_insert (chunk, s0);
68 str[1] = g_string_chunk_insert_len (chunk, s1, 8);
69 str[2] = g_string_chunk_insert (chunk, s2);
71 g_assert (memcmp (s0, str[0], sizeof s0) == 0);
72 g_assert (memcmp (s1, str[1], sizeof s1) == 0);
73 g_assert (memcmp (s2, str[2], sizeof s2) == 0);
75 g_string_chunk_free (chunk);
78 static void
79 test_string_new (void)
81 GString *string1, *string2;
83 string1 = g_string_new ("hi pete!");
84 string2 = g_string_new (NULL);
86 g_assert (string1 != NULL);
87 g_assert (string2 != NULL);
88 g_assert (strlen (string1->str) == string1->len);
89 g_assert (strlen (string2->str) == string2->len);
90 g_assert (string2->len == 0);
91 g_assert_cmpstr ("hi pete!", ==, string1->str);
92 g_assert_cmpstr ("", ==, string2->str);
94 g_string_free (string1, TRUE);
95 g_string_free (string2, TRUE);
97 string1 = g_string_new_len ("foo", -1);
98 string2 = g_string_new_len ("foobar", 3);
100 g_assert_cmpstr (string1->str, ==, "foo");
101 g_assert_cmpint (string1->len, ==, 3);
102 g_assert_cmpstr (string2->str, ==, "foo");
103 g_assert_cmpint (string2->len, ==, 3);
105 g_string_free (string1, TRUE);
106 g_string_free (string2, TRUE);
109 G_GNUC_PRINTF(2, 3)
110 static void
111 my_string_printf (GString *string,
112 const gchar *format,
113 ...)
115 va_list args;
117 va_start (args, format);
118 g_string_vprintf (string, format, args);
119 va_end (args);
122 static void
123 test_string_printf (void)
125 GString *string;
127 string = g_string_new (NULL);
129 #ifndef G_OS_WIN32
130 /* MSVC and mingw32 use the same run-time C library, which doesn't like
131 the %10000.10000f format... */
132 g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%10000.10000f",
133 "this pete guy sure is a wuss, like he's the number ",
135 " wuss. everyone agrees.\n",
136 10, 666, 15, 15, 666.666666666, 666.666666666);
137 #else
138 g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%100.100f",
139 "this pete guy sure is a wuss, like he's the number ",
141 " wuss. everyone agrees.\n",
142 10, 666, 15, 15, 666.666666666, 666.666666666);
143 #endif
145 g_string_free (string, TRUE);
147 string = g_string_new (NULL);
148 g_string_printf (string, "bla %s %d", "foo", 99);
149 g_assert_cmpstr (string->str, ==, "bla foo 99");
150 my_string_printf (string, "%d,%s,%d", 1, "two", 3);
151 g_assert_cmpstr (string->str, ==, "1,two,3");
153 g_string_free (string, TRUE);
156 static void
157 test_string_assign (void)
159 GString *string;
161 string = g_string_new (NULL);
162 g_string_assign (string, "boring text");
163 g_assert_cmpstr (string->str, ==, "boring text");
164 g_string_free (string, TRUE);
166 /* assign with string overlap */
167 string = g_string_new ("textbeforetextafter");
168 g_string_assign (string, string->str + 10);
169 g_assert_cmpstr (string->str, ==, "textafter");
170 g_string_free (string, TRUE);
172 string = g_string_new ("boring text");
173 g_string_assign (string, string->str);
174 g_assert_cmpstr (string->str, ==, "boring text");
175 g_string_free (string, TRUE);
178 static void
179 test_string_append_c (void)
181 GString *string;
182 gint i;
184 string = g_string_new ("hi pete!");
186 for (i = 0; i < 10000; i++)
187 if (i % 2)
188 g_string_append_c (string, 'a'+(i%26));
189 else
190 (g_string_append_c) (string, 'a'+(i%26));
192 g_assert((strlen("hi pete!") + 10000) == string->len);
193 g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
195 g_string_free (string, TRUE);
198 static void
199 test_string_append (void)
201 GString *string;
203 /* append */
204 string = g_string_new ("firsthalf");
205 g_string_append (string, "lasthalf");
206 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
207 g_string_free (string, TRUE);
209 /* append_len */
210 string = g_string_new ("firsthalf");
211 g_string_append_len (string, "lasthalfjunkjunk", strlen ("lasthalf"));
212 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
213 g_string_free (string, TRUE);
216 static void
217 test_string_prepend_c (void)
219 GString *string;
220 gint i;
222 string = g_string_new ("hi pete!");
224 for (i = 0; i < 10000; i++)
225 g_string_prepend_c (string, 'a'+(i%26));
227 g_assert((strlen("hi pete!") + 10000) == string->len);
228 g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
230 g_string_free (string, TRUE);
233 static void
234 test_string_prepend (void)
236 GString *string;
238 /* prepend */
239 string = g_string_new ("lasthalf");
240 g_string_prepend (string, "firsthalf");
241 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
242 g_string_free (string, TRUE);
244 /* prepend_len */
245 string = g_string_new ("lasthalf");
246 g_string_prepend_len (string, "firsthalfjunkjunk", strlen ("firsthalf"));
247 g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
248 g_string_free (string, TRUE);
251 static void
252 test_string_insert (void)
254 GString *string;
256 /* insert */
257 string = g_string_new ("firstlast");
258 g_string_insert (string, 5, "middle");
259 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
260 g_string_free (string, TRUE);
262 /* insert with pos == end of the string */
263 string = g_string_new ("firstmiddle");
264 g_string_insert (string, strlen ("firstmiddle"), "last");
265 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
266 g_string_free (string, TRUE);
268 /* insert_len */
269 string = g_string_new ("firstlast");
270 g_string_insert_len (string, 5, "middlejunkjunk", strlen ("middle"));
271 g_assert_cmpstr (string->str, ==, "firstmiddlelast");
272 g_string_free (string, TRUE);
274 /* insert_len with magic -1 pos for append */
275 string = g_string_new ("first");
276 g_string_insert_len (string, -1, "lastjunkjunk", strlen ("last"));
277 g_assert_cmpstr (string->str, ==, "firstlast");
278 g_string_free (string, TRUE);
280 /* insert_len with magic -1 len for strlen-the-string */
281 string = g_string_new ("first");
282 g_string_insert_len (string, 5, "last", -1);
283 g_assert_cmpstr (string->str, ==, "firstlast");
284 g_string_free (string, TRUE);
286 /* insert_len with string overlap */
287 string = g_string_new ("textbeforetextafter");
288 g_string_insert_len (string, 10, string->str + 8, 5);
289 g_assert_cmpstr (string->str, ==, "textbeforeretextextafter");
290 g_string_free (string, TRUE);
293 static void
294 test_string_insert_unichar (void)
296 GString *string;
298 /* insert_unichar with insertion in middle */
299 string = g_string_new ("firsthalf");
300 g_string_insert_unichar (string, 5, 0x0041);
301 g_assert_cmpstr (string->str, ==, "first\x41half");
302 g_string_free (string, TRUE);
304 string = g_string_new ("firsthalf");
305 g_string_insert_unichar (string, 5, 0x0298);
306 g_assert_cmpstr (string->str, ==, "first\xCA\x98half");
307 g_string_free (string, TRUE);
309 string = g_string_new ("firsthalf");
310 g_string_insert_unichar (string, 5, 0xFFFD);
311 g_assert_cmpstr (string->str, ==, "first\xEF\xBF\xBDhalf");
312 g_string_free (string, TRUE);
314 string = g_string_new ("firsthalf");
315 g_string_insert_unichar (string, 5, 0x1D100);
316 g_assert_cmpstr (string->str, ==, "first\xF0\x9D\x84\x80half");
317 g_string_free (string, TRUE);
319 /* insert_unichar with insertion at end */
320 string = g_string_new ("start");
321 g_string_insert_unichar (string, -1, 0x0041);
322 g_assert_cmpstr (string->str, ==, "start\x41");
323 g_string_free (string, TRUE);
325 string = g_string_new ("start");
326 g_string_insert_unichar (string, -1, 0x0298);
327 g_assert_cmpstr (string->str, ==, "start\xCA\x98");
328 g_string_free (string, TRUE);
330 string = g_string_new ("start");
331 g_string_insert_unichar (string, -1, 0xFFFD);
332 g_assert_cmpstr (string->str, ==, "start\xEF\xBF\xBD");
333 g_string_free (string, TRUE);
335 string = g_string_new ("start");
336 g_string_insert_unichar (string, -1, 0x1D100);
337 g_assert_cmpstr (string->str, ==, "start\xF0\x9D\x84\x80");
338 g_string_free (string, TRUE);
341 static void
342 test_string_equal (void)
344 GString *string1, *string2;
346 string1 = g_string_new ("test");
347 string2 = g_string_new ("te");
348 g_assert (!g_string_equal(string1, string2));
349 g_string_append (string2, "st");
350 g_assert (g_string_equal(string1, string2));
351 g_string_free (string1, TRUE);
352 g_string_free (string2, TRUE);
355 static void
356 test_string_truncate (void)
358 GString *string;
360 string = g_string_new ("testing");
362 g_string_truncate (string, 1000);
363 g_assert (string->len == strlen("testing"));
364 g_assert_cmpstr (string->str, ==, "testing");
366 g_string_truncate (string, 4);
367 g_assert (string->len == 4);
368 g_assert_cmpstr (string->str, ==, "test");
370 g_string_truncate (string, 0);
371 g_assert (string->len == 0);
372 g_assert_cmpstr (string->str, ==, "");
374 g_string_free (string, TRUE);
377 static void
378 test_string_overwrite (void)
380 GString *string;
382 /* overwriting functions */
383 string = g_string_new ("testing");
385 g_string_overwrite (string, 4, " and expand");
386 g_assert (15 == string->len);
387 g_assert ('\0' == string->str[15]);
388 g_assert (g_str_equal ("test and expand", string->str));
390 g_string_overwrite (string, 5, "NOT-");
391 g_assert (15 == string->len);
392 g_assert ('\0' == string->str[15]);
393 g_assert (g_str_equal ("test NOT-expand", string->str));
395 g_string_overwrite_len (string, 9, "blablabla", 6);
396 g_assert (15 == string->len);
397 g_assert ('\0' == string->str[15]);
398 g_assert (g_str_equal ("test NOT-blabla", string->str));
400 g_string_overwrite_len (string, 4, "BLABL", 0);
401 g_assert (g_str_equal ("test NOT-blabla", string->str));
402 g_string_overwrite_len (string, 4, "BLABL", -1);
403 g_assert (g_str_equal ("testBLABLblabla", string->str));
405 g_string_free (string, TRUE);
408 static void
409 test_string_nul_handling (void)
411 GString *string1, *string2;
413 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
414 string1 = g_string_new ("fiddle");
415 string2 = g_string_new ("fiddle");
416 g_assert (g_string_equal (string1, string2));
417 g_string_append_c (string1, '\0');
418 g_assert (!g_string_equal (string1, string2));
419 g_string_append_c (string2, '\0');
420 g_assert (g_string_equal (string1, string2));
421 g_string_append_c (string1, 'x');
422 g_string_append_c (string2, 'y');
423 g_assert (!g_string_equal (string1, string2));
424 g_assert (string1->len == 8);
425 g_string_append (string1, "yzzy");
426 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
427 g_string_insert (string1, 1, "QED");
428 g_assert_cmpmem (string1->str, string1->len + 1, "fQEDiddle\0xyzzy", 16);
429 g_string_printf (string1, "fiddle%cxyzzy", '\0');
430 g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
432 g_string_free (string1, TRUE);
433 g_string_free (string2, TRUE);
436 static void
437 test_string_up_down (void)
439 GString *s;
441 s = g_string_new ("Mixed Case String !?");
442 g_string_ascii_down (s);
443 g_assert_cmpstr (s->str, ==, "mixed case string !?");
445 g_string_assign (s, "Mixed Case String !?");
446 g_string_down (s);
447 g_assert_cmpstr (s->str, ==, "mixed case string !?");
449 g_string_assign (s, "Mixed Case String !?");
450 g_string_ascii_up (s);
451 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
453 g_string_assign (s, "Mixed Case String !?");
454 g_string_up (s);
455 g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
457 g_string_free (s, TRUE);
460 static void
461 test_string_set_size (void)
463 GString *s;
465 s = g_string_new ("foo");
466 g_string_set_size (s, 30);
468 g_assert_cmpstr (s->str, ==, "foo");
469 g_assert_cmpint (s->len, ==, 30);
471 g_string_free (s, TRUE);
474 static void
475 test_string_to_bytes (void)
477 GString *s;
478 GBytes *bytes;
479 gconstpointer byte_data;
480 gsize byte_len;
482 s = g_string_new ("foo");
483 g_string_append (s, "-bar");
485 bytes = g_string_free_to_bytes (s);
487 byte_data = g_bytes_get_data (bytes, &byte_len);
489 g_assert_cmpint (byte_len, ==, 7);
491 g_assert_cmpmem (byte_data, byte_len, "foo-bar", 7);
493 g_bytes_unref (bytes);
497 main (int argc,
498 char *argv[])
500 g_test_init (&argc, &argv, NULL);
502 g_test_add_func ("/string/test-string-chunks", test_string_chunks);
503 g_test_add_func ("/string/test-string-chunk-insert", test_string_chunk_insert);
504 g_test_add_func ("/string/test-string-new", test_string_new);
505 g_test_add_func ("/string/test-string-printf", test_string_printf);
506 g_test_add_func ("/string/test-string-assign", test_string_assign);
507 g_test_add_func ("/string/test-string-append-c", test_string_append_c);
508 g_test_add_func ("/string/test-string-append", test_string_append);
509 g_test_add_func ("/string/test-string-prepend-c", test_string_prepend_c);
510 g_test_add_func ("/string/test-string-prepend", test_string_prepend);
511 g_test_add_func ("/string/test-string-insert", test_string_insert);
512 g_test_add_func ("/string/test-string-insert-unichar", test_string_insert_unichar);
513 g_test_add_func ("/string/test-string-equal", test_string_equal);
514 g_test_add_func ("/string/test-string-truncate", test_string_truncate);
515 g_test_add_func ("/string/test-string-overwrite", test_string_overwrite);
516 g_test_add_func ("/string/test-string-nul-handling", test_string_nul_handling);
517 g_test_add_func ("/string/test-string-up-down", test_string_up_down);
518 g_test_add_func ("/string/test-string-set-size", test_string_set_size);
519 g_test_add_func ("/string/test-string-to-bytes", test_string_to_bytes);
521 return g_test_run();