2010-05-14 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / eglib / test / string-util.c
blob83f711516c975a7a55187de5eecba684e31165b7
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "test.h"
6 /* This test is just to be used with valgrind */
7 RESULT
8 test_strfreev ()
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");
14 array [3] = NULL;
16 g_strfreev (array);
17 g_strfreev (NULL);
19 return OK;
22 RESULT
23 test_concat ()
25 gchar *x = g_strconcat ("Hello", ", ", "world", NULL);
26 if (strcmp (x, "Hello, world") != 0)
27 return FAILED("concat failed, got: %s", x);
28 g_free (x);
29 return OK;
32 RESULT
33 test_split ()
35 const gchar *to_split = "Hello world, how are we doing today?";
36 gint i;
37 gchar **v;
39 v= g_strsplit(to_split, " ", 0);
41 if(v == NULL) {
42 return FAILED("split failed, got NULL vector (1)");
45 for(i = 0; v[i] != NULL; i++);
46 if(i != 7) {
47 return FAILED("split failed, expected 7 tokens, got %d", i);
50 g_strfreev(v);
52 v = g_strsplit(to_split, ":", -1);
53 if(v == NULL) {
54 return FAILED("split failed, got NULL vector (2)");
57 for(i = 0; v[i] != NULL; i++);
58 if(i != 1) {
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'",
64 to_split, v[0]);
66 g_strfreev(v);
68 v = g_strsplit ("", ":", 0);
69 if (v == NULL)
70 return FAILED ("g_strsplit returned NULL");
71 g_strfreev (v);
73 v = g_strsplit ("/home/miguel/dingus", "/", 0);
74 if (v [0][0] != 0)
75 return FAILED ("Got a non-empty first element");
76 g_strfreev (v);
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");
88 if (v [3] != NULL)
89 return FAILED ("Expected only 3 elements");
91 g_strfreev (v);
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");
103 if (v [3] != NULL)
104 return FAILED ("Expected only 3 elements (1)");
106 g_strfreev (v);
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");
115 if (v [2] != NULL)
116 return FAILED ("Expected only 2 elements (2)");
118 g_strfreev (v);
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");
130 if (v [3] != NULL)
131 return FAILED ("Expected only 3 elements (3)");
133 g_strfreev (v);
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");
151 if (v [5] != NULL)
152 return FAILED ("Expected only 5 elements (4)");
154 g_strfreev (v);
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");
172 if (v [5] != NULL)
173 return FAILED ("Expected only 5 elements (5)");
175 g_strfreev (v);
177 return OK;
180 RESULT
181 test_split_set ()
183 gchar **v;
185 v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 6);
186 if (strcmp (v [0], "abc") != 0)
187 return FAILED ("Invalid value 0");
189 if (strcmp (v [1], "") != 0)
190 return FAILED ("Invalid value 1");
192 if (strcmp (v [2], "def") != 0)
193 return FAILED ("Invalid value 2");
195 if (strcmp (v [3], "ghi") != 0)
196 return FAILED ("Invalid value 3");
198 if (strcmp (v [4], "") != 0)
199 return FAILED ("Invalid value 4");
201 if (strcmp (v [5], "jklYmno") != 0)
202 return FAILED ("Invalid value 5");
204 if (v [6] != NULL)
205 return FAILED ("Expected only 6 elements (1)");
207 g_strfreev (v);
209 v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 3);
210 if (strcmp (v [0], "abc") != 0)
211 return FAILED ("Invalid value 6");
213 if (strcmp (v [1], "") != 0)
214 return FAILED ("Invalid value 7");
216 if (strcmp (v [2], "defXghiXYjklYmno") != 0)
217 return FAILED ("Invalid value 8");
219 if (v [3] != NULL)
220 return FAILED ("Expected only 3 elements (2)");
222 g_strfreev (v);
224 v = g_strsplit_set ("abcXdefYghiXjklYmnoX", "XY", 5);
225 if (strcmp (v [0], "abc") != 0)
226 return FAILED ("Invalid value 9");
228 if (strcmp (v [1], "def") != 0)
229 return FAILED ("Invalid value 10");
231 if (strcmp (v [2], "ghi") != 0)
232 return FAILED ("Invalid value 11");
234 if (strcmp (v [3], "jkl") != 0)
235 return FAILED ("Invalid value 12");
237 if (strcmp (v [4], "mnoX") != 0)
238 return FAILED ("Invalid value 13");
240 if (v [5] != NULL)
241 return FAILED ("Expected only 5 elements (5)");
243 g_strfreev (v);
245 v = g_strsplit_set ("abcXYXdefXY", "XY", -1);
246 if (strcmp (v [0], "abc") != 0)
247 return FAILED ("Invalid value 14");
249 if (strcmp (v [1], "") != 0)
250 return FAILED ("Invalid value 15");
252 if (strcmp (v [2], "") != 0)
253 return FAILED ("Invalid value 16");
255 if (strcmp (v [3], "def") != 0)
256 return FAILED ("Invalid value 17");
258 if (strcmp (v [4], "") != 0)
259 return FAILED ("Invalid value 18");
261 if (strcmp (v [5], "") != 0)
262 return FAILED ("Invalid value 19");
264 if (v [6] != NULL)
265 return FAILED ("Expected only 6 elements (4)");
267 g_strfreev (v);
269 v = g_strsplit_set ("XYXabcXYdef", "XY", -1);
270 if (strcmp (v [0], "") != 0)
271 return FAILED ("Invalid value 20");
273 if (strcmp (v [1], "") != 0)
274 return FAILED ("Invalid value 21");
276 if (strcmp (v [2], "") != 0)
277 return FAILED ("Invalid value 22");
279 if (strcmp (v [3], "abc") != 0)
280 return FAILED ("Invalid value 23");
282 if (strcmp (v [4], "") != 0)
283 return FAILED ("Invalid value 24");
285 if (strcmp (v [5], "def") != 0)
286 return FAILED ("Invalid value 25");
288 if (v [6] != NULL)
289 return FAILED ("Expected only 6 elements (5)");
291 g_strfreev (v);
293 return OK;
296 RESULT
297 test_strreverse ()
299 gchar *a = g_strdup ("onetwothree");
300 gchar *a_target = "eerhtowteno";
301 gchar *b = g_strdup ("onetwothre");
302 gchar *b_target = "erhtowteno";
304 g_strreverse (a);
305 if (strcmp (a, a_target)) {
306 g_free (b);
307 g_free (a);
308 return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
311 g_strreverse (b);
312 if (strcmp (b, b_target)) {
313 g_free (b);
314 g_free (a);
315 return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
317 g_free (b);
318 g_free (a);
319 return OK;
322 RESULT
323 test_strjoin ()
325 char *s;
327 s = g_strjoin (NULL, "a", "b", NULL);
328 if (strcmp (s, "ab") != 0)
329 return FAILED ("Join of two strings with no separator fails");
330 g_free (s);
332 s = g_strjoin ("", "a", "b", NULL);
333 if (strcmp (s, "ab") != 0)
334 return FAILED ("Join of two strings with empty separator fails");
335 g_free (s);
337 s = g_strjoin ("-", "a", "b", NULL);
338 if (strcmp (s, "a-b") != 0)
339 return FAILED ("Join of two strings with separator fails");
340 g_free (s);
342 s = g_strjoin ("-", "aaaa", "bbbb", "cccc", "dddd", NULL);
343 if (strcmp (s, "aaaa-bbbb-cccc-dddd") != 0)
344 return FAILED ("Join of multiple strings fails");
345 g_free (s);
347 s = g_strjoin ("-", NULL);
348 if (s == NULL || (strcmp (s, "") != 0))
349 return FAILED ("Failed to join empty arguments");
350 g_free (s);
352 return OK;
355 RESULT
356 test_strchug ()
358 char *str = g_strdup (" \t\n hola");
360 g_strchug (str);
361 if (strcmp ("hola", str)) {
362 fprintf (stderr, "%s\n", str);
363 g_free (str);
364 return FAILED ("Failed.");
366 g_free (str);
367 return OK;
370 RESULT
371 test_strchomp ()
373 char *str = g_strdup ("hola \t");
375 g_strchomp (str);
376 if (strcmp ("hola", str)) {
377 fprintf (stderr, "%s\n", str);
378 g_free (str);
379 return FAILED ("Failed.");
381 g_free (str);
382 return OK;
385 RESULT
386 test_strstrip ()
388 char *str = g_strdup (" \t hola ");
390 g_strstrip (str);
391 if (strcmp ("hola", str)) {
392 fprintf (stderr, "%s\n", str);
393 g_free (str);
394 return FAILED ("Failed.");
396 g_free (str);
397 return OK;
400 #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);
402 #define errit(so) do { s = g_filename_to_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
404 RESULT
405 test_filename_to_uri ()
407 char *s;
409 urit ("/a", "file:///a");
410 urit ("/home/miguel", "file:///home/miguel");
411 urit ("/home/mig uel", "file:///home/mig%20uel");
412 urit ("/\303\241", "file:///%C3%A1");
413 urit ("/\303\241/octal", "file:///%C3%A1/octal");
414 urit ("/%", "file:///%25");
415 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");
416 urit ("/!$&'()*+,-./", "file:///!$&'()*+,-./");
417 urit ("/\042\043\045", "file:///%22%23%25");
418 urit ("/0123456789:=", "file:///0123456789:=");
419 urit ("/\073\074\076\077", "file:///%3B%3C%3E%3F");
420 urit ("/\133\134\135\136_\140\173\174\175", "file:///%5B%5C%5D%5E_%60%7B%7C%7D");
421 urit ("/\173\174\175\176\177\200", "file:///%7B%7C%7D~%7F%80");
422 urit ("/@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "file:///@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
423 errit ("a");
424 errit ("./hola");
426 return OK;
429 #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);
431 #define ferrit(so) do { s = g_filename_from_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
433 RESULT
434 test_filename_from_uri ()
436 char *s;
438 fileit ("file:///a", "/a");
439 fileit ("file:///%41", "/A");
440 fileit ("file:///home/miguel", "/home/miguel");
441 fileit ("file:///home/mig%20uel", "/home/mig uel");
442 ferrit ("/a");
443 ferrit ("a");
444 ferrit ("file://a");
445 ferrit ("file:a");
446 ferrit ("file:///%");
447 ferrit ("file:///%0");
448 ferrit ("file:///%jj");
450 return OK;
453 RESULT
454 test_ascii_xdigit_value ()
456 int i;
457 gchar j;
459 i = g_ascii_xdigit_value ('9' + 1);
460 if (i != -1)
461 return FAILED ("'9' + 1");
462 i = g_ascii_xdigit_value ('0' - 1);
463 if (i != -1)
464 return FAILED ("'0' - 1");
465 i = g_ascii_xdigit_value ('a' - 1);
466 if (i != -1)
467 return FAILED ("'a' - 1");
468 i = g_ascii_xdigit_value ('f' + 1);
469 if (i != -1)
470 return FAILED ("'f' + 1");
471 i = g_ascii_xdigit_value ('A' - 1);
472 if (i != -1)
473 return FAILED ("'A' - 1");
474 i = g_ascii_xdigit_value ('F' + 1);
475 if (i != -1)
476 return FAILED ("'F' + 1");
478 for (j = '0'; j < '9'; j++) {
479 int c = g_ascii_xdigit_value (j);
480 if (c != (j - '0'))
481 return FAILED ("Digits %c -> %d", j, c);
483 for (j = 'a'; j < 'f'; j++) {
484 int c = g_ascii_xdigit_value (j);
485 if (c != (j - 'a' + 10))
486 return FAILED ("Lower %c -> %d", j, c);
488 for (j = 'A'; j < 'F'; j++) {
489 int c = g_ascii_xdigit_value (j);
490 if (c != (j - 'A' + 10))
491 return FAILED ("Upper %c -> %d", j, c);
493 return OK;
496 RESULT
497 test_strdelimit ()
499 gchar *str;
501 str = g_strdup (G_STR_DELIMITERS);
502 str = g_strdelimit (str, NULL, 'a');
503 if (0 != strcmp ("aaaaaaa", str))
504 return FAILED ("All delimiters: '%s'", str);
505 g_free (str);
506 str = g_strdup ("hola");
507 str = g_strdelimit (str, "ha", '+');
508 if (0 != strcmp ("+ol+", str))
509 return FAILED ("2 delimiters: '%s'", str);
510 g_free (str);
511 return OK;
514 #define NUMBERS "0123456789"
516 RESULT
517 test_strlcpy ()
519 const gchar *src = "onetwothree";
520 gchar *dest;
521 gsize i;
523 dest = g_malloc (strlen (src) + 1);
524 memset (dest, 0, strlen (src) + 1);
525 i = g_strlcpy (dest, src, (gsize)-1);
526 if (i != strlen (src))
527 return FAILED ("Test1 got %d", i);
529 if (0 != strcmp (dest, src))
530 return FAILED ("Src and dest not equal");
532 i = g_strlcpy (dest, src, 3);
533 if (i != strlen (src))
534 return FAILED ("Test1 got %d", i);
535 if (0 != strcmp (dest, "on"))
536 return FAILED ("Test2");
538 i = g_strlcpy (dest, src, 1);
539 if (i != strlen (src))
540 return FAILED ("Test3 got %d", i);
541 if (*dest != '\0')
542 return FAILED ("Test4");
544 i = g_strlcpy (dest, src, 12345);
545 if (i != strlen (src))
546 return FAILED ("Test4 got %d", i);
547 if (0 != strcmp (dest, src))
548 return FAILED ("Src and dest not equal 2");
549 g_free (dest);
551 /* This is a test for g_filename_from_utf8, even if it does not look like it */
552 dest = g_filename_from_utf8 (NUMBERS, strlen (NUMBERS), NULL, NULL, NULL);
553 if (0 != strcmp (dest, NUMBERS))
554 return FAILED ("problem [%s] and [%s]", dest, NUMBERS);
555 g_free (dest);
557 return OK;
560 RESULT
561 test_strescape ()
563 gchar *str;
565 str = g_strescape ("abc", NULL);
566 if (strcmp ("abc", str))
567 return FAILED ("#1");
568 str = g_strescape ("\t\b\f\n\r\\\"abc", NULL);
569 if (strcmp ("\\t\\b\\f\\n\\r\\\\\\\"abc", str))
570 return FAILED ("#2 %s", str);
571 str = g_strescape ("\001abc", NULL);
572 if (strcmp ("\\001abc", str))
573 return FAILED ("#3 %s", str);
574 str = g_strescape ("\001abc", "\001");
575 if (strcmp ("\001abc", str))
576 return FAILED ("#3 %s", str);
577 return OK;
580 RESULT
581 test_ascii_strncasecmp ()
583 int n;
585 n = g_ascii_strncasecmp ("123", "123", 1);
586 if (n != 0)
587 return FAILED ("Should have been 0");
589 n = g_ascii_strncasecmp ("423", "123", 1);
590 if (n != 3)
591 return FAILED ("Should have been 3, got %d", n);
593 n = g_ascii_strncasecmp ("123", "423", 1);
594 if (n != -3)
595 return FAILED ("Should have been -3, got %d", n);
597 n = g_ascii_strncasecmp ("1", "1", 10);
598 if (n != 0)
599 return FAILED ("Should have been 0, got %d", n);
600 return OK;
603 RESULT
604 test_ascii_strdown ()
606 const gchar *a = "~09+AaBcDeFzZ$0909EmPAbCdEEEEEZZZZAAA";
607 const gchar *b = "~09+aabcdefzz$0909empabcdeeeeezzzzaaa";
608 gchar *c;
609 gint n, l;
611 l = (gint)strlen (b);
612 c = g_ascii_strdown (a, l);
613 n = g_ascii_strncasecmp (b, c, l);
615 if (n != 0) {
616 g_free (c);
617 return FAILED ("Should have been 0, got %d", n);
620 g_free (c);
621 return OK;
624 static Test strutil_tests [] = {
625 {"g_strfreev", test_strfreev},
626 {"g_strconcat", test_concat},
627 {"g_strsplit", test_split},
628 {"g_strsplit_set", test_split_set},
629 {"g_strreverse", test_strreverse},
630 {"g_strjoin", test_strjoin},
631 {"g_strchug", test_strchug},
632 {"g_strchomp", test_strchomp},
633 {"g_strstrip", test_strstrip},
634 {"g_filename_to_uri", test_filename_to_uri},
635 {"g_filename_from_uri", test_filename_from_uri},
636 {"g_ascii_xdigit_value", test_ascii_xdigit_value},
637 {"g_strdelimit", test_strdelimit},
638 {"g_strlcpy", test_strlcpy},
639 {"g_strescape", test_strescape},
640 {"g_ascii_strncasecmp", test_ascii_strncasecmp },
641 {"g_ascii_strdown", test_ascii_strdown },
642 {NULL, NULL}
645 DEFINE_TEST_GROUP_INIT(strutil_tests_init, strutil_tests)