Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / eglib / test / string-util.c
index 833e52a19cf3b3bff80d4095e1fb8b9d720653dd..73efd13f4de91ecfbbe8a8139f139d2e56cc795e 100644 (file)
@@ -64,34 +64,279 @@ test_split ()
                        to_split, v[0]);
        }
        g_strfreev(v);
+
+       v = g_strsplit ("", ":", 0);
+       if (v == NULL)
+               return FAILED ("g_strsplit returned NULL");
+       g_strfreev (v);
+
+       v = g_strsplit ("/home/miguel/dingus", "/", 0);
+       if (v [0][0] != 0)
+               return FAILED ("Got a non-empty first element");
+       g_strfreev (v);
+
+       v = g_strsplit ("appdomain1, Version=0.0.0.0, Culture=neutral", ",", 4);
+       if (strcmp (v [0], "appdomain1") != 0)
+               return FAILED ("Invalid value");
+       
+       if (strcmp (v [1], " Version=0.0.0.0") != 0)
+               return FAILED ("Invalid value");
+       
+       if (strcmp (v [2], " Culture=neutral") != 0)
+               return FAILED ("Invalid value");
+
+       if (v [3] != NULL)
+               return FAILED ("Expected only 3 elements");
+       
+       g_strfreev (v);
+
+       v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 4);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 0");
+       
+       if (strcmp (v [1], "defXghi") != 0)
+               return FAILED ("Invalid value 1");
+
+       if (strcmp (v [2], "jklYmno") != 0)
+               return FAILED ("Invalid value 2");
+
+       if (v [3] != NULL)
+               return FAILED ("Expected only 3 elements (1)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 2);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 3");
+       
+       if (strcmp (v [1], "defXghiXYjklYmno") != 0)
+               return FAILED ("Invalid value 4");
+
+       if (v [2] != NULL)
+               return FAILED ("Expected only 2 elements (2)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit ("abcXYdefXghiXYjklYmnoXY", "XY", 3);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 5");
+       
+       if (strcmp (v [1], "defXghi") != 0)
+               return FAILED ("Invalid value 6");
+
+       if (strcmp (v [2], "jklYmnoXY") != 0)
+               return FAILED ("Invalid value 7");
+
+       if (v [3] != NULL)
+               return FAILED ("Expected only 3 elements (3)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit ("abcXYXYXYdefXY", "XY", -1);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 8");
+
+       if (strcmp (v [1], "") != 0)
+               return FAILED ("Invalid value 9");
+
+       if (strcmp (v [2], "") != 0)
+               return FAILED ("Invalid value 10");
+       
+       if (strcmp (v [3], "def") != 0)
+               return FAILED ("Invalid value 11");
+
+       if (strcmp (v [4], "") != 0)
+               return FAILED ("Invalid value 12");
+
+       if (v [5] != NULL)
+               return FAILED ("Expected only 5 elements (4)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit ("XYXYXYabcXYdef", "XY", -1);
+       if (strcmp (v [0], "") != 0)
+               return FAILED ("Invalid value 13");
+       
+       if (strcmp (v [1], "") != 0)
+               return FAILED ("Invalid value 14");
        
+       if (strcmp (v [2], "") != 0)
+               return FAILED ("Invalid value 15");
+       
+       if (strcmp (v [3], "abc") != 0)
+               return FAILED ("Invalid value 16");
+       
+       if (strcmp (v [4], "def") != 0)
+               return FAILED ("Invalid value 17");
+
+       if (v [5] != NULL)
+               return FAILED ("Expected only 5 elements (5)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit ("value=", "=", 2);
+       if (strcmp (v [0], "value") != 0)
+               return FAILED ("Invalid value 18; expected 'value', got '%s'", v [0]);
+       if (strcmp (v [1], "") != 0)
+               return FAILED ("Invalid value 19; expected '', got '%s'", v [1]);
+       if (v [2] != NULL)
+               return FAILED ("Expected only 2 elements (6)");
+
+       g_strfreev (v);
+
+       return OK;
+}
+
+RESULT
+test_split_set ()
+{
+       gchar **v;
+       
+       v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 6);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 0");
+
+       if (strcmp (v [1], "") != 0)
+               return FAILED ("Invalid value 1");
+       
+       if (strcmp (v [2], "def") != 0)
+               return FAILED ("Invalid value 2");
+
+       if (strcmp (v [3], "ghi") != 0)
+               return FAILED ("Invalid value 3");
+
+       if (strcmp (v [4], "") != 0)
+               return FAILED ("Invalid value 4");
+
+       if (strcmp (v [5], "jklYmno") != 0)
+               return FAILED ("Invalid value 5");
+
+       if (v [6] != NULL)
+               return FAILED ("Expected only 6 elements (1)");
+
+       g_strfreev (v);
+
+       v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 3);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 6");
+
+       if (strcmp (v [1], "") != 0)
+               return FAILED ("Invalid value 7");
+       
+       if (strcmp (v [2], "defXghiXYjklYmno") != 0)
+               return FAILED ("Invalid value 8");
+
+       if (v [3] != NULL)
+               return FAILED ("Expected only 3 elements (2)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit_set ("abcXdefYghiXjklYmnoX", "XY", 5);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 9");
+       
+       if (strcmp (v [1], "def") != 0)
+               return FAILED ("Invalid value 10");
+
+       if (strcmp (v [2], "ghi") != 0)
+               return FAILED ("Invalid value 11");
+
+       if (strcmp (v [3], "jkl") != 0)
+               return FAILED ("Invalid value 12");
+
+       if (strcmp (v [4], "mnoX") != 0)
+               return FAILED ("Invalid value 13");
+
+       if (v [5] != NULL)
+               return FAILED ("Expected only 5 elements (5)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit_set ("abcXYXdefXY", "XY", -1);
+       if (strcmp (v [0], "abc") != 0)
+               return FAILED ("Invalid value 14");
+
+       if (strcmp (v [1], "") != 0)
+               return FAILED ("Invalid value 15");
+
+       if (strcmp (v [2], "") != 0)
+               return FAILED ("Invalid value 16");
+       
+       if (strcmp (v [3], "def") != 0)
+               return FAILED ("Invalid value 17");
+
+       if (strcmp (v [4], "") != 0)
+               return FAILED ("Invalid value 18");
+
+       if (strcmp (v [5], "") != 0)
+               return FAILED ("Invalid value 19");
+
+       if (v [6] != NULL)
+               return FAILED ("Expected only 6 elements (4)");
+       
+       g_strfreev (v);
+
+       v = g_strsplit_set ("XYXabcXYdef", "XY", -1);
+       if (strcmp (v [0], "") != 0)
+               return FAILED ("Invalid value 20");
+       
+       if (strcmp (v [1], "") != 0)
+               return FAILED ("Invalid value 21");
+       
+       if (strcmp (v [2], "") != 0)
+               return FAILED ("Invalid value 22");
+       
+       if (strcmp (v [3], "abc") != 0)
+               return FAILED ("Invalid value 23");
+
+       if (strcmp (v [4], "") != 0)
+               return FAILED ("Invalid value 24");
+       
+       if (strcmp (v [5], "def") != 0)
+               return FAILED ("Invalid value 25");
+
+       if (v [6] != NULL)
+               return FAILED ("Expected only 6 elements (5)");
+       
+       g_strfreev (v);
+
        return OK;
 }
 
 RESULT
 test_strreverse ()
 {
+       RESULT res = OK;
        gchar *a = g_strdup ("onetwothree");
        gchar *a_target = "eerhtowteno";
        gchar *b = g_strdup ("onetwothre");
        gchar *b_target = "erhtowteno";
+       gchar *c = g_strdup ("");
+       gchar *c_target = "";
 
        g_strreverse (a);
        if (strcmp (a, a_target)) {
-               g_free (b);
-               g_free (a);
-               return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
+               res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
+               goto cleanup;
        }
 
        g_strreverse (b);
        if (strcmp (b, b_target)) {
-               g_free (b);
-               g_free (a);
-               return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               goto cleanup;
+       }
+
+       g_strreverse (c);
+       if (strcmp (c, c_target)) {
+               res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               goto cleanup;
        }
+
+cleanup:
+       g_free (c);
        g_free (b);
        g_free (a);
-       return OK;
+       return res;
 }
 
 RESULT
@@ -179,6 +424,8 @@ test_strstrip ()
 RESULT
 test_filename_to_uri ()
 {
+#ifdef G_OS_WIN32
+#else
        char *s;
 
        urit ("/a", "file:///a");
@@ -197,6 +444,7 @@ test_filename_to_uri ()
        urit ("/@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "file:///@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
        errit ("a");
        errit ("./hola");
+#endif
        
        return OK;
 }
@@ -208,6 +456,8 @@ test_filename_to_uri ()
 RESULT
 test_filename_from_uri ()
 {
+#ifdef G_OS_WIN32
+#else
        char *s;
 
        fileit ("file:///a", "/a");
@@ -221,6 +471,7 @@ test_filename_from_uri ()
        ferrit ("file:///%");
        ferrit ("file:///%0");
        ferrit ("file:///%jj");
+#endif
        
        return OK;
 }
@@ -229,6 +480,7 @@ RESULT
 test_ascii_xdigit_value ()
 {
        int i;
+       gchar j;
 
        i = g_ascii_xdigit_value ('9' + 1);
        if (i != -1)
@@ -249,20 +501,20 @@ test_ascii_xdigit_value ()
        if (i != -1)
                return FAILED ("'F' + 1");
 
-       for (i = '0'; i < '9'; i++) {
-               int c = g_ascii_xdigit_value (i);
-               if (c  != (i - '0'))
-                       return FAILED ("Digits %c -> %d", i, c);
+       for (j = '0'; j < '9'; j++) {
+               int c = g_ascii_xdigit_value (j);
+               if (c  != (j - '0'))
+                       return FAILED ("Digits %c -> %d", j, c);
        }
-       for (i = 'a'; i < 'f'; i++) {
-               int c = g_ascii_xdigit_value (i);
-               if (c  != (i - 'a' + 10))
-                       return FAILED ("Lower %c -> %d", i, c);
+       for (j = 'a'; j < 'f'; j++) {
+               int c = g_ascii_xdigit_value (j);
+               if (c  != (j - 'a' + 10))
+                       return FAILED ("Lower %c -> %d", j, c);
        }
-       for (i = 'A'; i < 'F'; i++) {
-               int c = g_ascii_xdigit_value (i);
-               if (c  != (i - 'A' + 10))
-                       return FAILED ("Upper %c -> %d", i, c);
+       for (j = 'A'; j < 'F'; j++) {
+               int c = g_ascii_xdigit_value (j);
+               if (c  != (j - 'A' + 10))
+                       return FAILED ("Upper %c -> %d", j, c);
        }
        return OK;
 }
@@ -285,10 +537,145 @@ test_strdelimit ()
        return OK;
 }
 
+#define NUMBERS "0123456789"
+
+RESULT
+test_strlcpy ()
+{
+       const gchar *src = "onetwothree";
+       gchar *dest;
+       gsize i;
+
+       dest = g_malloc (strlen (src) + 1);
+       memset (dest, 0, strlen (src) + 1);
+       i = g_strlcpy (dest, src, (gsize)-1);
+       if (i != strlen (src))
+               return FAILED ("Test1 got %d", i);
+
+       if (0 != strcmp (dest, src))
+               return FAILED ("Src and dest not equal");
+
+       i = g_strlcpy (dest, src, 3);
+       if (i != strlen (src))
+               return FAILED ("Test1 got %d", i);
+       if (0 != strcmp (dest, "on"))
+               return FAILED ("Test2");
+
+       i = g_strlcpy (dest, src, 1);
+       if (i != strlen (src))
+               return FAILED ("Test3 got %d", i);
+       if (*dest != '\0')
+               return FAILED ("Test4");
+
+       i = g_strlcpy (dest, src, 12345);
+       if (i != strlen (src))
+               return FAILED ("Test4 got %d", i);
+       if (0 != strcmp (dest, src))
+               return FAILED ("Src and dest not equal 2");
+       g_free (dest);
+
+       /* This is a test for g_filename_from_utf8, even if it does not look like it */
+       dest = g_filename_from_utf8 (NUMBERS, strlen (NUMBERS), NULL, NULL, NULL);
+       if (0 != strcmp (dest, NUMBERS))
+               return FAILED ("problem [%s] and [%s]", dest, NUMBERS);
+       g_free (dest);
+       
+       return OK;
+}
+
+RESULT
+test_strescape ()
+{
+       gchar *str;
+
+       str = g_strescape ("abc", NULL);
+       if (strcmp ("abc", str))
+               return FAILED ("#1");
+       str = g_strescape ("\t\b\f\n\r\\\"abc", NULL);
+       if (strcmp ("\\t\\b\\f\\n\\r\\\\\\\"abc", str))
+               return FAILED ("#2 %s", str);
+       str = g_strescape ("\001abc", NULL);
+       if (strcmp ("\\001abc", str))
+               return FAILED ("#3 %s", str);
+       str = g_strescape ("\001abc", "\001");
+       if (strcmp ("\001abc", str))
+               return FAILED ("#3 %s", str);
+       return OK;
+}
+
+RESULT
+test_ascii_strncasecmp ()
+{
+       int n;
+
+       n = g_ascii_strncasecmp ("123", "123", 1);
+       if (n != 0)
+               return FAILED ("Should have been 0");
+       
+       n = g_ascii_strncasecmp ("423", "123", 1);
+       if (n != 3)
+               return FAILED ("Should have been 3, got %d", n);
+
+       n = g_ascii_strncasecmp ("123", "423", 1);
+       if (n != -3)
+               return FAILED ("Should have been -3, got %d", n);
+
+       n = g_ascii_strncasecmp ("1", "1", 10);
+       if (n != 0)
+               return FAILED ("Should have been 0, got %d", n);
+       return OK;
+}
+
+RESULT
+test_ascii_strdown ()
+{
+       const gchar *a = "~09+AaBcDeFzZ$0909EmPAbCdEEEEEZZZZAAA";
+       const gchar *b = "~09+aabcdefzz$0909empabcdeeeeezzzzaaa";
+       gchar *c;
+       gint n, l;
+
+       l = (gint)strlen (b);
+       c = g_ascii_strdown (a, l);
+       n = g_ascii_strncasecmp (b, c, l);
+
+       if (n != 0) {
+               g_free (c);
+               return FAILED ("Should have been 0, got %d", n);
+       }
+
+       g_free (c);
+       return OK;
+}
+
+RESULT
+test_strdupv ()
+{
+       gchar **one;
+       gchar **two;
+       gint len;
+
+       one = g_strdupv (NULL);
+       if (one)
+               return FAILED ("Should have been NULL");
+
+       one = g_malloc (sizeof (gchar *));
+       *one = NULL;
+       two = g_strdupv (one);
+       if (!two)
+               FAILED ("Should have been not NULL");
+       len = g_strv_length (two);
+       if (len)
+               FAILED ("Should have been 0");
+       g_strfreev (two);
+       g_strfreev (one);
+       return NULL;
+}
+
 static Test strutil_tests [] = {
        {"g_strfreev", test_strfreev},
        {"g_strconcat", test_concat},
        {"g_strsplit", test_split},
+       {"g_strsplit_set", test_split_set},
        {"g_strreverse", test_strreverse},
        {"g_strjoin", test_strjoin},
        {"g_strchug", test_strchug},
@@ -298,8 +685,14 @@ static Test strutil_tests [] = {
        {"g_filename_from_uri", test_filename_from_uri},
        {"g_ascii_xdigit_value", test_ascii_xdigit_value},
        {"g_strdelimit", test_strdelimit},
+       {"g_strlcpy", test_strlcpy},
+       {"g_strescape", test_strescape},
+       {"g_ascii_strncasecmp", test_ascii_strncasecmp },
+       {"g_ascii_strdown", test_ascii_strdown },
+       {"g_strdupv", test_strdupv },
        {NULL, NULL}
 };
 
 DEFINE_TEST_GROUP_INIT(strutil_tests_init, strutil_tests)
 
+