3dbd738aba7fc7de580460d2a1942b9b573eab87
[mono.git] / eglib / test / string-util.c
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "test.h"
5
6 /* This test is just to be used with valgrind */
7 RESULT
8 test_strfreev ()
9 {
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;
15         
16         g_strfreev (array);
17         g_strfreev (NULL);
18
19         return OK;
20 }
21
22 RESULT
23 test_concat ()
24 {
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;
30 }
31
32 RESULT
33 test_split ()
34 {
35         const gchar *to_split = "Hello world, how are we doing today?";
36         gint i;
37         gchar **v;
38         
39         v= g_strsplit(to_split, " ", 0);
40         
41         if(v == NULL) {
42                 return FAILED("split failed, got NULL vector (1)");
43         }
44         
45         for(i = 0; v[i] != NULL; i++);
46         if(i != 7) {
47                 return FAILED("split failed, expected 7 tokens, got %d", i);
48         }
49         
50         g_strfreev(v);
51
52         v = g_strsplit(to_split, ":", -1);
53         if(v == NULL) {
54                 return FAILED("split failed, got NULL vector (2)");
55         }
56
57         for(i = 0; v[i] != NULL; i++);
58         if(i != 1) {
59                 return FAILED("split failed, expected 1 token, got %d", i);
60         }
61
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]);
65         }
66         g_strfreev(v);
67
68         v = g_strsplit ("", ":", 0);
69         if (v == NULL)
70                 return FAILED ("g_strsplit returned NULL");
71         g_strfreev (v);
72
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);
77
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");
81         
82         if (strcmp (v [1], " Version=0.0.0.0") != 0)
83                 return FAILED ("Invalid value");
84         
85         if (strcmp (v [2], " Culture=neutral") != 0)
86                 return FAILED ("Invalid value");
87
88         if (v [3] != NULL)
89                 return FAILED ("Expected only 3 elements");
90         
91         g_strfreev (v);
92
93         v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 4);
94         if (strcmp (v [0], "abc") != 0)
95                 return FAILED ("Invalid value 0");
96         
97         if (strcmp (v [1], "defXghi") != 0)
98                 return FAILED ("Invalid value 1");
99
100         if (strcmp (v [2], "jklYmno") != 0)
101                 return FAILED ("Invalid value 2");
102
103         if (v [3] != NULL)
104                 return FAILED ("Expected only 3 elements (1)");
105         
106         g_strfreev (v);
107
108         v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 2);
109         if (strcmp (v [0], "abc") != 0)
110                 return FAILED ("Invalid value 3");
111         
112         if (strcmp (v [1], "defXghiXYjklYmno") != 0)
113                 return FAILED ("Invalid value 4");
114
115         if (v [2] != NULL)
116                 return FAILED ("Expected only 2 elements (2)");
117         
118         g_strfreev (v);
119
120         v = g_strsplit ("abcXYdefXghiXYjklYmnoXY", "XY", 3);
121         if (strcmp (v [0], "abc") != 0)
122                 return FAILED ("Invalid value 5");
123         
124         if (strcmp (v [1], "defXghi") != 0)
125                 return FAILED ("Invalid value 6");
126
127         if (strcmp (v [2], "jklYmnoXY") != 0)
128                 return FAILED ("Invalid value 7");
129
130         if (v [3] != NULL)
131                 return FAILED ("Expected only 3 elements (3)");
132         
133         g_strfreev (v);
134
135         v = g_strsplit ("abcXYXYXYdefXY", "XY", -1);
136         if (strcmp (v [0], "abc") != 0)
137                 return FAILED ("Invalid value 8");
138
139         if (strcmp (v [1], "") != 0)
140                 return FAILED ("Invalid value 9");
141
142         if (strcmp (v [2], "") != 0)
143                 return FAILED ("Invalid value 10");
144         
145         if (strcmp (v [3], "def") != 0)
146                 return FAILED ("Invalid value 11");
147
148         if (strcmp (v [4], "") != 0)
149                 return FAILED ("Invalid value 12");
150
151         if (v [5] != NULL)
152                 return FAILED ("Expected only 5 elements (4)");
153         
154         g_strfreev (v);
155
156         v = g_strsplit ("XYXYXYabcXYdef", "XY", -1);
157         if (strcmp (v [0], "") != 0)
158                 return FAILED ("Invalid value 13");
159         
160         if (strcmp (v [1], "") != 0)
161                 return FAILED ("Invalid value 14");
162         
163         if (strcmp (v [2], "") != 0)
164                 return FAILED ("Invalid value 15");
165         
166         if (strcmp (v [3], "abc") != 0)
167                 return FAILED ("Invalid value 16");
168         
169         if (strcmp (v [4], "def") != 0)
170                 return FAILED ("Invalid value 17");
171
172         if (v [5] != NULL)
173                 return FAILED ("Expected only 5 elements (5)");
174         
175         g_strfreev (v);
176
177         return OK;
178 }
179
180 RESULT
181 test_split_set ()
182 {
183         gchar **v;
184         
185         v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 6);
186         if (strcmp (v [0], "abc") != 0)
187                 return FAILED ("Invalid value 0");
188
189         if (strcmp (v [1], "") != 0)
190                 return FAILED ("Invalid value 1");
191         
192         if (strcmp (v [2], "def") != 0)
193                 return FAILED ("Invalid value 2");
194
195         if (strcmp (v [3], "ghi") != 0)
196                 return FAILED ("Invalid value 3");
197
198         if (strcmp (v [4], "") != 0)
199                 return FAILED ("Invalid value 4");
200
201         if (strcmp (v [5], "jklYmno") != 0)
202                 return FAILED ("Invalid value 5");
203
204         if (v [6] != NULL)
205                 return FAILED ("Expected only 6 elements (1)");
206
207         g_strfreev (v);
208
209         v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 3);
210         if (strcmp (v [0], "abc") != 0)
211                 return FAILED ("Invalid value 6");
212
213         if (strcmp (v [1], "") != 0)
214                 return FAILED ("Invalid value 7");
215         
216         if (strcmp (v [2], "defXghiXYjklYmno") != 0)
217                 return FAILED ("Invalid value 8");
218
219         if (v [3] != NULL)
220                 return FAILED ("Expected only 3 elements (2)");
221         
222         g_strfreev (v);
223
224         v = g_strsplit_set ("abcXdefYghiXjklYmnoX", "XY", 5);
225         if (strcmp (v [0], "abc") != 0)
226                 return FAILED ("Invalid value 9");
227         
228         if (strcmp (v [1], "def") != 0)
229                 return FAILED ("Invalid value 10");
230
231         if (strcmp (v [2], "ghi") != 0)
232                 return FAILED ("Invalid value 11");
233
234         if (strcmp (v [3], "jkl") != 0)
235                 return FAILED ("Invalid value 12");
236
237         if (strcmp (v [4], "mnoX") != 0)
238                 return FAILED ("Invalid value 13");
239
240         if (v [5] != NULL)
241                 return FAILED ("Expected only 5 elements (5)");
242         
243         g_strfreev (v);
244
245         v = g_strsplit_set ("abcXYXdefXY", "XY", -1);
246         if (strcmp (v [0], "abc") != 0)
247                 return FAILED ("Invalid value 14");
248
249         if (strcmp (v [1], "") != 0)
250                 return FAILED ("Invalid value 15");
251
252         if (strcmp (v [2], "") != 0)
253                 return FAILED ("Invalid value 16");
254         
255         if (strcmp (v [3], "def") != 0)
256                 return FAILED ("Invalid value 17");
257
258         if (strcmp (v [4], "") != 0)
259                 return FAILED ("Invalid value 18");
260
261         if (strcmp (v [5], "") != 0)
262                 return FAILED ("Invalid value 19");
263
264         if (v [6] != NULL)
265                 return FAILED ("Expected only 6 elements (4)");
266         
267         g_strfreev (v);
268
269         v = g_strsplit_set ("XYXabcXYdef", "XY", -1);
270         if (strcmp (v [0], "") != 0)
271                 return FAILED ("Invalid value 20");
272         
273         if (strcmp (v [1], "") != 0)
274                 return FAILED ("Invalid value 21");
275         
276         if (strcmp (v [2], "") != 0)
277                 return FAILED ("Invalid value 22");
278         
279         if (strcmp (v [3], "abc") != 0)
280                 return FAILED ("Invalid value 23");
281
282         if (strcmp (v [4], "") != 0)
283                 return FAILED ("Invalid value 24");
284         
285         if (strcmp (v [5], "def") != 0)
286                 return FAILED ("Invalid value 25");
287
288         if (v [6] != NULL)
289                 return FAILED ("Expected only 6 elements (5)");
290         
291         g_strfreev (v);
292
293         return OK;
294 }
295
296 RESULT
297 test_strreverse ()
298 {
299         RESULT res = OK;
300         gchar *a = g_strdup ("onetwothree");
301         gchar *a_target = "eerhtowteno";
302         gchar *b = g_strdup ("onetwothre");
303         gchar *b_target = "erhtowteno";
304         gchar *c = g_strdup ("");
305         gchar *c_target = "";
306
307         g_strreverse (a);
308         if (strcmp (a, a_target)) {
309                 res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
310                 goto cleanup;
311         }
312
313         g_strreverse (b);
314         if (strcmp (b, b_target)) {
315                 res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
316                 goto cleanup;
317         }
318
319         g_strreverse (c);
320         if (strcmp (c, c_target)) {
321                 res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
322                 goto cleanup;
323         }
324
325 cleanup:
326         g_free (c);
327         g_free (b);
328         g_free (a);
329         return res;
330 }
331
332 RESULT
333 test_strjoin ()
334 {
335         char *s;
336         
337         s = g_strjoin (NULL, "a", "b", NULL);
338         if (strcmp (s, "ab") != 0)
339                 return FAILED ("Join of two strings with no separator fails");
340         g_free (s);
341
342         s = g_strjoin ("", "a", "b", NULL);
343         if (strcmp (s, "ab") != 0)
344                 return FAILED ("Join of two strings with empty separator fails");
345         g_free (s);
346
347         s = g_strjoin ("-", "a", "b", NULL);
348         if (strcmp (s, "a-b") != 0)
349                 return FAILED ("Join of two strings with separator fails");
350         g_free (s);
351
352         s = g_strjoin ("-", "aaaa", "bbbb", "cccc", "dddd", NULL);
353         if (strcmp (s, "aaaa-bbbb-cccc-dddd") != 0)
354                 return FAILED ("Join of multiple strings fails");
355         g_free (s);
356
357         s = g_strjoin ("-", NULL);
358         if (s == NULL || (strcmp (s, "") != 0))
359                 return FAILED ("Failed to join empty arguments");
360         g_free (s);
361
362         return OK;
363 }
364
365 RESULT
366 test_strchug ()
367 {
368         char *str = g_strdup (" \t\n hola");
369
370         g_strchug (str);
371         if (strcmp ("hola", str)) {
372                 fprintf (stderr, "%s\n", str);
373                 g_free (str);
374                 return FAILED ("Failed.");
375         }
376         g_free (str);
377         return OK;
378 }
379
380 RESULT
381 test_strchomp ()
382 {
383         char *str = g_strdup ("hola  \t");
384
385         g_strchomp (str);
386         if (strcmp ("hola", str)) {
387                 fprintf (stderr, "%s\n", str);
388                 g_free (str);
389                 return FAILED ("Failed.");
390         }
391         g_free (str);
392         return OK;
393 }
394
395 RESULT
396 test_strstrip ()
397 {
398         char *str = g_strdup (" \t hola   ");
399
400         g_strstrip (str);
401         if (strcmp ("hola", str)) {
402                 fprintf (stderr, "%s\n", str);
403                 g_free (str);
404                 return FAILED ("Failed.");
405         }
406         g_free (str);
407         return OK;
408 }
409
410 #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);
411
412 #define errit(so) do { s = g_filename_to_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
413
414 RESULT
415 test_filename_to_uri ()
416 {
417         char *s;
418
419         urit ("/a", "file:///a");
420         urit ("/home/miguel", "file:///home/miguel");
421         urit ("/home/mig uel", "file:///home/mig%20uel");
422         urit ("/\303\241", "file:///%C3%A1");
423         urit ("/\303\241/octal", "file:///%C3%A1/octal");
424         urit ("/%", "file:///%25");
425         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");
426         urit ("/!$&'()*+,-./", "file:///!$&'()*+,-./");
427         urit ("/\042\043\045", "file:///%22%23%25");
428         urit ("/0123456789:=", "file:///0123456789:=");
429         urit ("/\073\074\076\077", "file:///%3B%3C%3E%3F");
430         urit ("/\133\134\135\136_\140\173\174\175", "file:///%5B%5C%5D%5E_%60%7B%7C%7D");
431         urit ("/\173\174\175\176\177\200", "file:///%7B%7C%7D~%7F%80");
432         urit ("/@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "file:///@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
433         errit ("a");
434         errit ("./hola");
435         
436         return OK;
437 }
438
439 #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);
440
441 #define ferrit(so) do { s = g_filename_from_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
442
443 RESULT
444 test_filename_from_uri ()
445 {
446         char *s;
447
448         fileit ("file:///a", "/a");
449         fileit ("file:///%41", "/A");
450         fileit ("file:///home/miguel", "/home/miguel");
451         fileit ("file:///home/mig%20uel", "/home/mig uel");
452         ferrit ("/a");
453         ferrit ("a");
454         ferrit ("file://a");
455         ferrit ("file:a");
456         ferrit ("file:///%");
457         ferrit ("file:///%0");
458         ferrit ("file:///%jj");
459         
460         return OK;
461 }
462
463 RESULT
464 test_ascii_xdigit_value ()
465 {
466         int i;
467         gchar j;
468
469         i = g_ascii_xdigit_value ('9' + 1);
470         if (i != -1)
471                 return FAILED ("'9' + 1");
472         i = g_ascii_xdigit_value ('0' - 1);
473         if (i != -1)
474                 return FAILED ("'0' - 1");
475         i = g_ascii_xdigit_value ('a' - 1);
476         if (i != -1)
477                 return FAILED ("'a' - 1");
478         i = g_ascii_xdigit_value ('f' + 1);
479         if (i != -1)
480                 return FAILED ("'f' + 1");
481         i = g_ascii_xdigit_value ('A' - 1);
482         if (i != -1)
483                 return FAILED ("'A' - 1");
484         i = g_ascii_xdigit_value ('F' + 1);
485         if (i != -1)
486                 return FAILED ("'F' + 1");
487
488         for (j = '0'; j < '9'; j++) {
489                 int c = g_ascii_xdigit_value (j);
490                 if (c  != (j - '0'))
491                         return FAILED ("Digits %c -> %d", j, c);
492         }
493         for (j = 'a'; j < 'f'; j++) {
494                 int c = g_ascii_xdigit_value (j);
495                 if (c  != (j - 'a' + 10))
496                         return FAILED ("Lower %c -> %d", j, c);
497         }
498         for (j = 'A'; j < 'F'; j++) {
499                 int c = g_ascii_xdigit_value (j);
500                 if (c  != (j - 'A' + 10))
501                         return FAILED ("Upper %c -> %d", j, c);
502         }
503         return OK;
504 }
505
506 RESULT
507 test_strdelimit ()
508 {
509         gchar *str;
510
511         str = g_strdup (G_STR_DELIMITERS);
512         str = g_strdelimit (str, NULL, 'a');
513         if (0 != strcmp ("aaaaaaa", str))
514                 return FAILED ("All delimiters: '%s'", str);
515         g_free (str);
516         str = g_strdup ("hola");
517         str = g_strdelimit (str, "ha", '+');
518         if (0 != strcmp ("+ol+", str))
519                 return FAILED ("2 delimiters: '%s'", str);
520         g_free (str);
521         return OK;
522 }
523
524 #define NUMBERS "0123456789"
525
526 RESULT
527 test_strlcpy ()
528 {
529         const gchar *src = "onetwothree";
530         gchar *dest;
531         gsize i;
532
533         dest = g_malloc (strlen (src) + 1);
534         memset (dest, 0, strlen (src) + 1);
535         i = g_strlcpy (dest, src, (gsize)-1);
536         if (i != strlen (src))
537                 return FAILED ("Test1 got %d", i);
538
539         if (0 != strcmp (dest, src))
540                 return FAILED ("Src and dest not equal");
541
542         i = g_strlcpy (dest, src, 3);
543         if (i != strlen (src))
544                 return FAILED ("Test1 got %d", i);
545         if (0 != strcmp (dest, "on"))
546                 return FAILED ("Test2");
547
548         i = g_strlcpy (dest, src, 1);
549         if (i != strlen (src))
550                 return FAILED ("Test3 got %d", i);
551         if (*dest != '\0')
552                 return FAILED ("Test4");
553
554         i = g_strlcpy (dest, src, 12345);
555         if (i != strlen (src))
556                 return FAILED ("Test4 got %d", i);
557         if (0 != strcmp (dest, src))
558                 return FAILED ("Src and dest not equal 2");
559         g_free (dest);
560
561         /* This is a test for g_filename_from_utf8, even if it does not look like it */
562         dest = g_filename_from_utf8 (NUMBERS, strlen (NUMBERS), NULL, NULL, NULL);
563         if (0 != strcmp (dest, NUMBERS))
564                 return FAILED ("problem [%s] and [%s]", dest, NUMBERS);
565         g_free (dest);
566         
567         return OK;
568 }
569
570 RESULT
571 test_strescape ()
572 {
573         gchar *str;
574
575         str = g_strescape ("abc", NULL);
576         if (strcmp ("abc", str))
577                 return FAILED ("#1");
578         str = g_strescape ("\t\b\f\n\r\\\"abc", NULL);
579         if (strcmp ("\\t\\b\\f\\n\\r\\\\\\\"abc", str))
580                 return FAILED ("#2 %s", str);
581         str = g_strescape ("\001abc", NULL);
582         if (strcmp ("\\001abc", str))
583                 return FAILED ("#3 %s", str);
584         str = g_strescape ("\001abc", "\001");
585         if (strcmp ("\001abc", str))
586                 return FAILED ("#3 %s", str);
587         return OK;
588 }
589
590 RESULT
591 test_ascii_strncasecmp ()
592 {
593         int n;
594
595         n = g_ascii_strncasecmp ("123", "123", 1);
596         if (n != 0)
597                 return FAILED ("Should have been 0");
598         
599         n = g_ascii_strncasecmp ("423", "123", 1);
600         if (n != 3)
601                 return FAILED ("Should have been 3, got %d", n);
602
603         n = g_ascii_strncasecmp ("123", "423", 1);
604         if (n != -3)
605                 return FAILED ("Should have been -3, got %d", n);
606
607         n = g_ascii_strncasecmp ("1", "1", 10);
608         if (n != 0)
609                 return FAILED ("Should have been 0, got %d", n);
610         return OK;
611 }
612
613 RESULT
614 test_ascii_strdown ()
615 {
616         const gchar *a = "~09+AaBcDeFzZ$0909EmPAbCdEEEEEZZZZAAA";
617         const gchar *b = "~09+aabcdefzz$0909empabcdeeeeezzzzaaa";
618         gchar *c;
619         gint n, l;
620
621         l = (gint)strlen (b);
622         c = g_ascii_strdown (a, l);
623         n = g_ascii_strncasecmp (b, c, l);
624
625         if (n != 0) {
626                 g_free (c);
627                 return FAILED ("Should have been 0, got %d", n);
628         }
629
630         g_free (c);
631         return OK;
632 }
633
634 RESULT
635 test_strdupv ()
636 {
637         gchar **one;
638         gchar **two;
639         gint len;
640
641         one = g_strdupv (NULL);
642         if (one)
643                 return FAILED ("Should have been NULL");
644
645         one = g_malloc (sizeof (gchar *));
646         *one = NULL;
647         two = g_strdupv (one);
648         if (!two)
649                 FAILED ("Should have been not NULL");
650         len = g_strv_length (two);
651         if (len)
652                 FAILED ("Should have been 0");
653         g_strfreev (two);
654         g_strfreev (one);
655         return NULL;
656 }
657
658 static Test strutil_tests [] = {
659         {"g_strfreev", test_strfreev},
660         {"g_strconcat", test_concat},
661         {"g_strsplit", test_split},
662         {"g_strsplit_set", test_split_set},
663         {"g_strreverse", test_strreverse},
664         {"g_strjoin", test_strjoin},
665         {"g_strchug", test_strchug},
666         {"g_strchomp", test_strchomp},
667         {"g_strstrip", test_strstrip},
668         {"g_filename_to_uri", test_filename_to_uri},
669         {"g_filename_from_uri", test_filename_from_uri},
670         {"g_ascii_xdigit_value", test_ascii_xdigit_value},
671         {"g_strdelimit", test_strdelimit},
672         {"g_strlcpy", test_strlcpy},
673         {"g_strescape", test_strescape},
674         {"g_ascii_strncasecmp", test_ascii_strncasecmp },
675         {"g_ascii_strdown", test_ascii_strdown },
676         {"g_strdupv", test_strdupv },
677         {NULL, NULL}
678 };
679
680 DEFINE_TEST_GROUP_INIT(strutil_tests_init, strutil_tests)
681
682