[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mono / eglib / test / string-util.c
blobfb447229ef4046deefb2054f790a705cc415223e
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 static RESULT
8 test_strfreev (void)
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 static RESULT
23 test_concat (void)
25 gchar *x = g_strconcat ("Hello", ", ", "world", (const char*)NULL);
26 if (strcmp (x, "Hello, world") != 0)
27 return FAILED("concat failed, got: %s", x);
28 g_free (x);
29 return OK;
32 static RESULT
33 test_split (void)
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 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]);
182 if (v [2] != NULL)
183 return FAILED ("Expected only 2 elements (6)");
185 g_strfreev (v);
187 return OK;
190 static RESULT
191 test_split_set (void)
193 gchar **v;
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");
214 if (v [6] != NULL)
215 return FAILED ("Expected only 6 elements (1)");
217 g_strfreev (v);
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");
229 if (v [3] != NULL)
230 return FAILED ("Expected only 3 elements (2)");
232 g_strfreev (v);
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");
250 if (v [5] != NULL)
251 return FAILED ("Expected only 5 elements (5)");
253 g_strfreev (v);
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");
274 if (v [6] != NULL)
275 return FAILED ("Expected only 6 elements (4)");
277 g_strfreev (v);
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");
298 if (v [6] != NULL)
299 return FAILED ("Expected only 6 elements (5)");
301 g_strfreev (v);
303 return OK;
306 static RESULT
307 test_strreverse (void)
309 RESULT res = OK;
310 gchar *a = g_strdup ("onetwothree");
311 gchar *a_target = (char*)"eerhtowteno";
312 gchar *b = g_strdup ("onetwothre");
313 gchar *b_target = (char*)"erhtowteno";
314 gchar *c = g_strdup ("");
315 gchar *c_target = (char*)"";
317 g_strreverse (a);
318 if (strcmp (a, a_target)) {
319 res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
320 goto cleanup;
323 g_strreverse (b);
324 if (strcmp (b, b_target)) {
325 res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
326 goto cleanup;
329 g_strreverse (c);
330 if (strcmp (c, c_target)) {
331 res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
332 goto cleanup;
335 cleanup:
336 g_free (c);
337 g_free (b);
338 g_free (a);
339 return res;
342 static RESULT
343 test_strjoin (void)
345 char *s;
347 s = g_strjoin (NULL, "a", "b", (const char*)NULL);
348 if (strcmp (s, "ab") != 0)
349 return FAILED ("Join of two strings with no separator fails");
350 g_free (s);
352 s = g_strjoin ("", "a", "b", (const char*)NULL);
353 if (strcmp (s, "ab") != 0)
354 return FAILED ("Join of two strings with empty separator fails");
355 g_free (s);
357 s = g_strjoin ("-", "a", "b", (const char*)NULL);
358 if (strcmp (s, "a-b") != 0)
359 return FAILED ("Join of two strings with separator fails");
360 g_free (s);
362 s = g_strjoin ("-", "aaaa", "bbbb", "cccc", "dddd", (const char*)NULL);
363 if (strcmp (s, "aaaa-bbbb-cccc-dddd") != 0)
364 return FAILED ("Join of multiple strings fails");
365 g_free (s);
367 s = g_strjoin ("-", (const char*)NULL);
368 if (s == NULL || (strcmp (s, "") != 0))
369 return FAILED ("Failed to join empty arguments");
370 g_free (s);
372 return OK;
375 static RESULT
376 test_strchug (void)
378 char *str = g_strdup (" \t\n hola");
380 g_strchug (str);
381 if (strcmp ("hola", str)) {
382 fprintf (stderr, "%s\n", str);
383 g_free (str);
384 return FAILED ("Failed.");
386 g_free (str);
387 return OK;
390 static RESULT
391 test_strchomp (void)
393 char *str = g_strdup ("hola \t");
395 g_strchomp (str);
396 if (strcmp ("hola", str)) {
397 fprintf (stderr, "%s\n", str);
398 g_free (str);
399 return FAILED ("Failed.");
401 g_free (str);
402 return OK;
405 static RESULT
406 test_strstrip (void)
408 char *str = g_strdup (" \t hola ");
410 g_strstrip (str);
411 if (strcmp ("hola", str)) {
412 fprintf (stderr, "%s\n", str);
413 g_free (str);
414 return FAILED ("Failed.");
416 g_free (str);
417 return OK;
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);
424 static RESULT
425 test_filename_to_uri (void)
427 #ifdef G_OS_WIN32
428 #else
429 char *s;
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");
445 errit ("a");
446 errit ("./hola");
447 #endif
449 return OK;
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);
456 static RESULT
457 test_filename_from_uri (void)
459 #ifndef G_OS_WIN32
460 char *s;
462 fileit ("file:///a", "/a");
463 fileit ("file:///%41", "/A");
464 fileit ("file:///home/miguel", "/home/miguel");
465 fileit ("file:///home/mig%20uel", "/home/mig uel");
466 fileit ("file:///home/c%2B%2B", "/home/c++");
467 fileit ("file:///home/c%2b%2b", "/home/c++");
468 ferrit ("/a");
469 ferrit ("a");
470 ferrit ("file://a");
471 ferrit ("file:a");
472 ferrit ("file:///%");
473 ferrit ("file:///%0");
474 ferrit ("file:///%jj");
475 #endif
477 return OK;
480 static RESULT
481 test_ascii_xdigit_value (void)
483 int i;
484 gchar j;
486 i = g_ascii_xdigit_value ('9' + 1);
487 if (i != -1)
488 return FAILED ("'9' + 1");
489 i = g_ascii_xdigit_value ('0' - 1);
490 if (i != -1)
491 return FAILED ("'0' - 1");
492 i = g_ascii_xdigit_value ('a' - 1);
493 if (i != -1)
494 return FAILED ("'a' - 1");
495 i = g_ascii_xdigit_value ('f' + 1);
496 if (i != -1)
497 return FAILED ("'f' + 1");
498 i = g_ascii_xdigit_value ('A' - 1);
499 if (i != -1)
500 return FAILED ("'A' - 1");
501 i = g_ascii_xdigit_value ('F' + 1);
502 if (i != -1)
503 return FAILED ("'F' + 1");
505 for (j = '0'; j < '9'; j++) {
506 int c = g_ascii_xdigit_value (j);
507 if (c != (j - '0'))
508 return FAILED ("Digits %c -> %d", j, c);
510 for (j = 'a'; j < 'f'; j++) {
511 int c = g_ascii_xdigit_value (j);
512 if (c != (j - 'a' + 10))
513 return FAILED ("Lower %c -> %d", j, c);
515 for (j = 'A'; j < 'F'; j++) {
516 int c = g_ascii_xdigit_value (j);
517 if (c != (j - 'A' + 10))
518 return FAILED ("Upper %c -> %d", j, c);
520 return OK;
523 #define G_STR_DELIMITERS "_-|> <."
525 static void
526 g_strdelimits (char *a, const char *old, char new)
528 old = old ? old : G_STR_DELIMITERS;
529 while (*old)
530 g_strdelimit (a, *old++, new);
533 static RESULT
534 test_strdelimit (void)
536 gchar *str;
538 str = g_strdup (G_STR_DELIMITERS);
539 g_strdelimits (str, NULL, 'a');
540 if (0 != strcmp ("aaaaaaa", str))
541 return FAILED ("All delimiters: '%s'", str);
542 g_free (str);
543 str = g_strdup ("hola");
544 g_strdelimits (str, "ha", '+');
545 if (0 != strcmp ("+ol+", str))
546 return FAILED ("2 delimiters: '%s'", str);
547 g_free (str);
548 return OK;
551 #define NUMBERS "0123456789"
553 static RESULT
554 test_strlcpy (void)
556 const gchar *src = "onetwothree";
557 gchar *dest;
558 gsize i;
560 dest = g_malloc (strlen (src) + 1);
561 memset (dest, 0, strlen (src) + 1);
562 i = g_strlcpy (dest, src, (gsize)-1);
563 if (i != strlen (src))
564 return FAILED ("Test1 got %d", i);
566 if (0 != strcmp (dest, src))
567 return FAILED ("Src and dest not equal");
569 i = g_strlcpy (dest, src, 3);
570 if (i != strlen (src))
571 return FAILED ("Test1 got %d", i);
572 if (0 != strcmp (dest, "on"))
573 return FAILED ("Test2");
575 i = g_strlcpy (dest, src, 1);
576 if (i != strlen (src))
577 return FAILED ("Test3 got %d", i);
578 if (*dest != '\0')
579 return FAILED ("Test4");
581 i = g_strlcpy (dest, src, 12345);
582 if (i != strlen (src))
583 return FAILED ("Test4 got %d", i);
584 if (0 != strcmp (dest, src))
585 return FAILED ("Src and dest not equal 2");
586 g_free (dest);
588 /* This is a test for g_filename_from_utf8, even if it does not look like it */
589 dest = g_filename_from_utf8 (NUMBERS, strlen (NUMBERS), NULL, NULL, NULL);
590 if (0 != strcmp (dest, NUMBERS))
591 return FAILED ("problem [%s] and [%s]", dest, NUMBERS);
592 g_free (dest);
594 return OK;
597 static RESULT
598 test_strescape (void)
600 gchar *str;
602 str = g_strescape ("abc", NULL);
603 if (strcmp ("abc", str))
604 return FAILED ("#1");
605 str = g_strescape ("\t\b\f\n\r\\\"abc", NULL);
606 if (strcmp ("\\t\\b\\f\\n\\r\\\\\\\"abc", str))
607 return FAILED ("#2 %s", str);
608 str = g_strescape ("\001abc", NULL);
609 if (strcmp ("\\001abc", str))
610 return FAILED ("#3 %s", str);
611 str = g_strescape ("\001abc", "\001");
612 if (strcmp ("\001abc", str))
613 return FAILED ("#3 %s", str);
614 return OK;
617 static RESULT
618 test_ascii_strncasecmp (void)
620 int n;
622 n = g_ascii_strncasecmp ("123", "123", 1);
623 if (n != 0)
624 return FAILED ("Should have been 0");
626 n = g_ascii_strncasecmp ("423", "123", 1);
627 if (n <= 0)
628 return FAILED ("Should have been > 0, got %d", n);
630 n = g_ascii_strncasecmp ("123", "423", 1);
631 if (n >= 0)
632 return FAILED ("Should have been < 0, got %d", n);
634 n = g_ascii_strncasecmp ("1", "1", 10);
635 if (n != 0)
636 return FAILED ("Should have been 0, got %d", n);
637 return OK;
640 static RESULT
641 test_ascii_strdown (void)
643 const gchar *a = "~09+AaBcDeFzZ$0909EmPAbCdEEEEEZZZZAAA";
644 const gchar *b = "~09+aabcdefzz$0909empabcdeeeeezzzzaaa";
645 gchar *c;
646 gint n, l;
648 l = (gint)strlen (b);
649 c = g_ascii_strdown (a, l);
650 n = g_ascii_strncasecmp (b, c, l);
652 if (n != 0) {
653 g_free (c);
654 return FAILED ("Should have been 0, got %d", n);
657 g_free (c);
658 return OK;
661 static RESULT
662 test_strdupv (void)
664 gchar **one;
665 gchar **two;
666 gint len;
668 one = g_strdupv (NULL);
669 if (one)
670 return FAILED ("Should have been NULL");
672 one = g_malloc (sizeof (gchar *));
673 *one = NULL;
674 two = g_strdupv (one);
675 if (!two)
676 FAILED ("Should have been not NULL");
677 len = g_strv_length (two);
678 if (len)
679 FAILED ("Should have been 0");
680 g_strfreev (two);
681 g_strfreev (one);
682 return NULL;
685 static Test strutil_tests [] = {
686 {"g_strfreev", test_strfreev},
687 {"g_strconcat", test_concat},
688 {"g_strsplit", test_split},
689 {"g_strsplit_set", test_split_set},
690 {"g_strreverse", test_strreverse},
691 {"g_strjoin", test_strjoin},
692 {"g_strchug", test_strchug},
693 {"g_strchomp", test_strchomp},
694 {"g_strstrip", test_strstrip},
695 {"g_filename_to_uri", test_filename_to_uri},
696 {"g_filename_from_uri", test_filename_from_uri},
697 {"g_ascii_xdigit_value", test_ascii_xdigit_value},
698 {"g_strdelimit", test_strdelimit},
699 {"g_strlcpy", test_strlcpy},
700 {"g_strescape", test_strescape},
701 {"g_ascii_strncasecmp", test_ascii_strncasecmp },
702 {"g_ascii_strdown", test_ascii_strdown },
703 {"g_strdupv", test_strdupv },
704 {NULL, NULL}
707 DEFINE_TEST_GROUP_INIT(strutil_tests_init, strutil_tests)