6 /* This test is just to be used with valgrind */
10 gchar
**array
= g_new (gchar
*, 4);
11 array
[0] = g_strdup ("one");
12 array
[1] = g_strdup ("two");
13 array
[2] = g_strdup ("three");
25 gchar
*x
= g_strconcat ("Hello", ", ", "world", NULL
);
26 if (strcmp (x
, "Hello, world") != 0)
27 return FAILED("concat failed, got: %s", x
);
35 const gchar
*to_split
= "Hello world, how are we doing today?";
39 v
= g_strsplit(to_split
, " ", 0);
42 return FAILED("split failed, got NULL vector (1)");
45 for(i
= 0; v
[i
] != NULL
; i
++);
47 return FAILED("split failed, expected 7 tokens, got %d", i
);
52 v
= g_strsplit(to_split
, ":", -1);
54 return FAILED("split failed, got NULL vector (2)");
57 for(i
= 0; v
[i
] != NULL
; i
++);
59 return FAILED("split failed, expected 1 token, got %d", i
);
62 if(strcmp(v
[0], to_split
) != 0) {
63 return FAILED("expected vector[0] to be '%s' but it was '%s'",
68 v
= g_strsplit ("", ":", 0);
70 return FAILED ("g_strsplit returned NULL");
73 v
= g_strsplit ("/home/miguel/dingus", "/", 0);
75 return FAILED ("Got a non-empty first element");
78 v
= g_strsplit ("appdomain1, Version=0.0.0.0, Culture=neutral", ",", 4);
79 if (strcmp (v
[0], "appdomain1") != 0)
80 return FAILED ("Invalid value");
82 if (strcmp (v
[1], " Version=0.0.0.0") != 0)
83 return FAILED ("Invalid value");
85 if (strcmp (v
[2], " Culture=neutral") != 0)
86 return FAILED ("Invalid value");
89 return FAILED ("Expected only 3 elements");
93 v
= g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 4);
94 if (strcmp (v
[0], "abc") != 0)
95 return FAILED ("Invalid value 0");
97 if (strcmp (v
[1], "defXghi") != 0)
98 return FAILED ("Invalid value 1");
100 if (strcmp (v
[2], "jklYmno") != 0)
101 return FAILED ("Invalid value 2");
104 return FAILED ("Expected only 3 elements (1)");
108 v
= g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 2);
109 if (strcmp (v
[0], "abc") != 0)
110 return FAILED ("Invalid value 3");
112 if (strcmp (v
[1], "defXghiXYjklYmno") != 0)
113 return FAILED ("Invalid value 4");
116 return FAILED ("Expected only 2 elements (2)");
120 v
= g_strsplit ("abcXYdefXghiXYjklYmnoXY", "XY", 3);
121 if (strcmp (v
[0], "abc") != 0)
122 return FAILED ("Invalid value 5");
124 if (strcmp (v
[1], "defXghi") != 0)
125 return FAILED ("Invalid value 6");
127 if (strcmp (v
[2], "jklYmnoXY") != 0)
128 return FAILED ("Invalid value 7");
131 return FAILED ("Expected only 3 elements (3)");
135 v
= g_strsplit ("abcXYXYXYdefXY", "XY", -1);
136 if (strcmp (v
[0], "abc") != 0)
137 return FAILED ("Invalid value 8");
139 if (strcmp (v
[1], "") != 0)
140 return FAILED ("Invalid value 9");
142 if (strcmp (v
[2], "") != 0)
143 return FAILED ("Invalid value 10");
145 if (strcmp (v
[3], "def") != 0)
146 return FAILED ("Invalid value 11");
148 if (strcmp (v
[4], "") != 0)
149 return FAILED ("Invalid value 12");
152 return FAILED ("Expected only 5 elements (4)");
156 v
= g_strsplit ("XYXYXYabcXYdef", "XY", -1);
157 if (strcmp (v
[0], "") != 0)
158 return FAILED ("Invalid value 13");
160 if (strcmp (v
[1], "") != 0)
161 return FAILED ("Invalid value 14");
163 if (strcmp (v
[2], "") != 0)
164 return FAILED ("Invalid value 15");
166 if (strcmp (v
[3], "abc") != 0)
167 return FAILED ("Invalid value 16");
169 if (strcmp (v
[4], "def") != 0)
170 return FAILED ("Invalid value 17");
173 return FAILED ("Expected only 5 elements (5)");
177 v
= g_strsplit ("value=", "=", 2);
178 if (strcmp (v
[0], "value") != 0)
179 return FAILED ("Invalid value 18; expected 'value', got '%s'", v
[0]);
180 if (strcmp (v
[1], "") != 0)
181 return FAILED ("Invalid value 19; expected '', got '%s'", v
[1]);
183 return FAILED ("Expected only 2 elements (6)");
195 v
= g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 6);
196 if (strcmp (v
[0], "abc") != 0)
197 return FAILED ("Invalid value 0");
199 if (strcmp (v
[1], "") != 0)
200 return FAILED ("Invalid value 1");
202 if (strcmp (v
[2], "def") != 0)
203 return FAILED ("Invalid value 2");
205 if (strcmp (v
[3], "ghi") != 0)
206 return FAILED ("Invalid value 3");
208 if (strcmp (v
[4], "") != 0)
209 return FAILED ("Invalid value 4");
211 if (strcmp (v
[5], "jklYmno") != 0)
212 return FAILED ("Invalid value 5");
215 return FAILED ("Expected only 6 elements (1)");
219 v
= g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 3);
220 if (strcmp (v
[0], "abc") != 0)
221 return FAILED ("Invalid value 6");
223 if (strcmp (v
[1], "") != 0)
224 return FAILED ("Invalid value 7");
226 if (strcmp (v
[2], "defXghiXYjklYmno") != 0)
227 return FAILED ("Invalid value 8");
230 return FAILED ("Expected only 3 elements (2)");
234 v
= g_strsplit_set ("abcXdefYghiXjklYmnoX", "XY", 5);
235 if (strcmp (v
[0], "abc") != 0)
236 return FAILED ("Invalid value 9");
238 if (strcmp (v
[1], "def") != 0)
239 return FAILED ("Invalid value 10");
241 if (strcmp (v
[2], "ghi") != 0)
242 return FAILED ("Invalid value 11");
244 if (strcmp (v
[3], "jkl") != 0)
245 return FAILED ("Invalid value 12");
247 if (strcmp (v
[4], "mnoX") != 0)
248 return FAILED ("Invalid value 13");
251 return FAILED ("Expected only 5 elements (5)");
255 v
= g_strsplit_set ("abcXYXdefXY", "XY", -1);
256 if (strcmp (v
[0], "abc") != 0)
257 return FAILED ("Invalid value 14");
259 if (strcmp (v
[1], "") != 0)
260 return FAILED ("Invalid value 15");
262 if (strcmp (v
[2], "") != 0)
263 return FAILED ("Invalid value 16");
265 if (strcmp (v
[3], "def") != 0)
266 return FAILED ("Invalid value 17");
268 if (strcmp (v
[4], "") != 0)
269 return FAILED ("Invalid value 18");
271 if (strcmp (v
[5], "") != 0)
272 return FAILED ("Invalid value 19");
275 return FAILED ("Expected only 6 elements (4)");
279 v
= g_strsplit_set ("XYXabcXYdef", "XY", -1);
280 if (strcmp (v
[0], "") != 0)
281 return FAILED ("Invalid value 20");
283 if (strcmp (v
[1], "") != 0)
284 return FAILED ("Invalid value 21");
286 if (strcmp (v
[2], "") != 0)
287 return FAILED ("Invalid value 22");
289 if (strcmp (v
[3], "abc") != 0)
290 return FAILED ("Invalid value 23");
292 if (strcmp (v
[4], "") != 0)
293 return FAILED ("Invalid value 24");
295 if (strcmp (v
[5], "def") != 0)
296 return FAILED ("Invalid value 25");
299 return FAILED ("Expected only 6 elements (5)");
310 gchar
*a
= g_strdup ("onetwothree");
311 gchar
*a_target
= "eerhtowteno";
312 gchar
*b
= g_strdup ("onetwothre");
313 gchar
*b_target
= "erhtowteno";
314 gchar
*c
= g_strdup ("");
315 gchar
*c_target
= "";
318 if (strcmp (a
, a_target
)) {
319 res
= FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a
, a_target
);
324 if (strcmp (b
, b_target
)) {
325 res
= FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b
, b_target
);
330 if (strcmp (c
, c_target
)) {
331 res
= FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b
, b_target
);
347 s
= g_strjoin (NULL
, "a", "b", NULL
);
348 if (strcmp (s
, "ab") != 0)
349 return FAILED ("Join of two strings with no separator fails");
352 s
= g_strjoin ("", "a", "b", NULL
);
353 if (strcmp (s
, "ab") != 0)
354 return FAILED ("Join of two strings with empty separator fails");
357 s
= g_strjoin ("-", "a", "b", NULL
);
358 if (strcmp (s
, "a-b") != 0)
359 return FAILED ("Join of two strings with separator fails");
362 s
= g_strjoin ("-", "aaaa", "bbbb", "cccc", "dddd", NULL
);
363 if (strcmp (s
, "aaaa-bbbb-cccc-dddd") != 0)
364 return FAILED ("Join of multiple strings fails");
367 s
= g_strjoin ("-", NULL
);
368 if (s
== NULL
|| (strcmp (s
, "") != 0))
369 return FAILED ("Failed to join empty arguments");
378 char *str
= g_strdup (" \t\n hola");
381 if (strcmp ("hola", str
)) {
382 fprintf (stderr
, "%s\n", str
);
384 return FAILED ("Failed.");
393 char *str
= g_strdup ("hola \t");
396 if (strcmp ("hola", str
)) {
397 fprintf (stderr
, "%s\n", str
);
399 return FAILED ("Failed.");
408 char *str
= g_strdup (" \t hola ");
411 if (strcmp ("hola", str
)) {
412 fprintf (stderr
, "%s\n", str
);
414 return FAILED ("Failed.");
420 #define urit(so,j) do { s = g_filename_to_uri (so, NULL, NULL); if (strcmp (s, j) != 0) return FAILED("Got %s expected %s", s, j); g_free (s); } while (0);
422 #define errit(so) do { s = g_filename_to_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
425 test_filename_to_uri ()
431 urit ("/a", "file:///a");
432 urit ("/home/miguel", "file:///home/miguel");
433 urit ("/home/mig uel", "file:///home/mig%20uel");
434 urit ("/\303\241", "file:///%C3%A1");
435 urit ("/\303\241/octal", "file:///%C3%A1/octal");
436 urit ("/%", "file:///%25");
437 urit ("/\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040", "file:///%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20");
438 urit ("/!$&'()*+,-./", "file:///!$&'()*+,-./");
439 urit ("/\042\043\045", "file:///%22%23%25");
440 urit ("/0123456789:=", "file:///0123456789:=");
441 urit ("/\073\074\076\077", "file:///%3B%3C%3E%3F");
442 urit ("/\133\134\135\136_\140\173\174\175", "file:///%5B%5C%5D%5E_%60%7B%7C%7D");
443 urit ("/\173\174\175\176\177\200", "file:///%7B%7C%7D~%7F%80");
444 urit ("/@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "file:///@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
452 #define fileit(so,j) do { s = g_filename_from_uri (so, NULL, NULL); if (strcmp (s, j) != 0) return FAILED("Got %s expected %s", s, j); g_free (s); } while (0);
454 #define ferrit(so) do { s = g_filename_from_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
457 test_filename_from_uri ()
463 fileit ("file:///a", "/a");
464 fileit ("file:///%41", "/A");
465 fileit ("file:///home/miguel", "/home/miguel");
466 fileit ("file:///home/mig%20uel", "/home/mig uel");
471 ferrit ("file:///%");
472 ferrit ("file:///%0");
473 ferrit ("file:///%jj");
480 test_ascii_xdigit_value ()
485 i
= g_ascii_xdigit_value ('9' + 1);
487 return FAILED ("'9' + 1");
488 i
= g_ascii_xdigit_value ('0' - 1);
490 return FAILED ("'0' - 1");
491 i
= g_ascii_xdigit_value ('a' - 1);
493 return FAILED ("'a' - 1");
494 i
= g_ascii_xdigit_value ('f' + 1);
496 return FAILED ("'f' + 1");
497 i
= g_ascii_xdigit_value ('A' - 1);
499 return FAILED ("'A' - 1");
500 i
= g_ascii_xdigit_value ('F' + 1);
502 return FAILED ("'F' + 1");
504 for (j
= '0'; j
< '9'; j
++) {
505 int c
= g_ascii_xdigit_value (j
);
507 return FAILED ("Digits %c -> %d", j
, c
);
509 for (j
= 'a'; j
< 'f'; j
++) {
510 int c
= g_ascii_xdigit_value (j
);
511 if (c
!= (j
- 'a' + 10))
512 return FAILED ("Lower %c -> %d", j
, c
);
514 for (j
= 'A'; j
< 'F'; j
++) {
515 int c
= g_ascii_xdigit_value (j
);
516 if (c
!= (j
- 'A' + 10))
517 return FAILED ("Upper %c -> %d", j
, c
);
527 str
= g_strdup (G_STR_DELIMITERS
);
528 str
= g_strdelimit (str
, NULL
, 'a');
529 if (0 != strcmp ("aaaaaaa", str
))
530 return FAILED ("All delimiters: '%s'", str
);
532 str
= g_strdup ("hola");
533 str
= g_strdelimit (str
, "ha", '+');
534 if (0 != strcmp ("+ol+", str
))
535 return FAILED ("2 delimiters: '%s'", str
);
540 #define NUMBERS "0123456789"
545 const gchar
*src
= "onetwothree";
549 dest
= g_malloc (strlen (src
) + 1);
550 memset (dest
, 0, strlen (src
) + 1);
551 i
= g_strlcpy (dest
, src
, (gsize
)-1);
552 if (i
!= strlen (src
))
553 return FAILED ("Test1 got %d", i
);
555 if (0 != strcmp (dest
, src
))
556 return FAILED ("Src and dest not equal");
558 i
= g_strlcpy (dest
, src
, 3);
559 if (i
!= strlen (src
))
560 return FAILED ("Test1 got %d", i
);
561 if (0 != strcmp (dest
, "on"))
562 return FAILED ("Test2");
564 i
= g_strlcpy (dest
, src
, 1);
565 if (i
!= strlen (src
))
566 return FAILED ("Test3 got %d", i
);
568 return FAILED ("Test4");
570 i
= g_strlcpy (dest
, src
, 12345);
571 if (i
!= strlen (src
))
572 return FAILED ("Test4 got %d", i
);
573 if (0 != strcmp (dest
, src
))
574 return FAILED ("Src and dest not equal 2");
577 /* This is a test for g_filename_from_utf8, even if it does not look like it */
578 dest
= g_filename_from_utf8 (NUMBERS
, strlen (NUMBERS
), NULL
, NULL
, NULL
);
579 if (0 != strcmp (dest
, NUMBERS
))
580 return FAILED ("problem [%s] and [%s]", dest
, NUMBERS
);
591 str
= g_strescape ("abc", NULL
);
592 if (strcmp ("abc", str
))
593 return FAILED ("#1");
594 str
= g_strescape ("\t\b\f\n\r\\\"abc", NULL
);
595 if (strcmp ("\\t\\b\\f\\n\\r\\\\\\\"abc", str
))
596 return FAILED ("#2 %s", str
);
597 str
= g_strescape ("\001abc", NULL
);
598 if (strcmp ("\\001abc", str
))
599 return FAILED ("#3 %s", str
);
600 str
= g_strescape ("\001abc", "\001");
601 if (strcmp ("\001abc", str
))
602 return FAILED ("#3 %s", str
);
607 test_ascii_strncasecmp ()
611 n
= g_ascii_strncasecmp ("123", "123", 1);
613 return FAILED ("Should have been 0");
615 n
= g_ascii_strncasecmp ("423", "123", 1);
617 return FAILED ("Should have been 3, got %d", n
);
619 n
= g_ascii_strncasecmp ("123", "423", 1);
621 return FAILED ("Should have been -3, got %d", n
);
623 n
= g_ascii_strncasecmp ("1", "1", 10);
625 return FAILED ("Should have been 0, got %d", n
);
630 test_ascii_strdown ()
632 const gchar
*a
= "~09+AaBcDeFzZ$0909EmPAbCdEEEEEZZZZAAA";
633 const gchar
*b
= "~09+aabcdefzz$0909empabcdeeeeezzzzaaa";
637 l
= (gint
)strlen (b
);
638 c
= g_ascii_strdown (a
, l
);
639 n
= g_ascii_strncasecmp (b
, c
, l
);
643 return FAILED ("Should have been 0, got %d", n
);
657 one
= g_strdupv (NULL
);
659 return FAILED ("Should have been NULL");
661 one
= g_malloc (sizeof (gchar
*));
663 two
= g_strdupv (one
);
665 FAILED ("Should have been not NULL");
666 len
= g_strv_length (two
);
668 FAILED ("Should have been 0");
674 static Test strutil_tests
[] = {
675 {"g_strfreev", test_strfreev
},
676 {"g_strconcat", test_concat
},
677 {"g_strsplit", test_split
},
678 {"g_strsplit_set", test_split_set
},
679 {"g_strreverse", test_strreverse
},
680 {"g_strjoin", test_strjoin
},
681 {"g_strchug", test_strchug
},
682 {"g_strchomp", test_strchomp
},
683 {"g_strstrip", test_strstrip
},
684 {"g_filename_to_uri", test_filename_to_uri
},
685 {"g_filename_from_uri", test_filename_from_uri
},
686 {"g_ascii_xdigit_value", test_ascii_xdigit_value
},
687 {"g_strdelimit", test_strdelimit
},
688 {"g_strlcpy", test_strlcpy
},
689 {"g_strescape", test_strescape
},
690 {"g_ascii_strncasecmp", test_ascii_strncasecmp
},
691 {"g_ascii_strdown", test_ascii_strdown
},
692 {"g_strdupv", test_strdupv
},
696 DEFINE_TEST_GROUP_INIT(strutil_tests_init
, strutil_tests
)