2008-01-26 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / eglib / test / utf8.c
index 171c0de8f06370d9ccbbf09a8ec87d9c837b17d3..4076cc8ca558ffadfe29e47e2d005887bf2a42bc 100644 (file)
@@ -82,11 +82,10 @@ compare_utf16_to_utf8 (const gchar *expected, const gunichar2 *utf16, glong len_
 RESULT
 test_utf16_to_utf8 ()
 {
-       const gchar *src0 = "", *src1 = "ABCDE";
-       gunichar2 str0 [1], str1 [6];
+       const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27";
+       gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0};
        RESULT result;
 
-       str0 [0] = 0;
        gchar_to_gunichar2 (str1, src1);
 
        /* empty string */
@@ -95,6 +94,9 @@ test_utf16_to_utf8 ()
                return result;
 
        result = compare_utf16_to_utf8 (src1, str1, 5, 5);
+       if (result != OK)
+               return result;
+       result = compare_utf16_to_utf8 (src2, str2, 2, 4);
        if (result != OK)
                return result;
 
@@ -171,14 +173,38 @@ compare_utf8_to_utf16 (const gunichar2 *expected, const gchar *utf8, glong len_i
        return compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, len_in);
 }
 
+RESULT
+test_utf8_seq ()
+{
+       const gchar *src = "\xE5\xB9\xB4\x27";
+       glong in_read, out_read;
+       //gunichar2 expected [6];
+       GError *error = NULL;
+       gunichar2 *dst;
+
+       printf ("got: %s\n", src);
+       dst = g_utf8_to_utf16 (src, (glong)strlen (src), &in_read, &out_read, &error);
+       if (error != NULL){
+               return error->message;
+       }
+
+       if (in_read != 4) {
+               return FAILED ("in_read is expected to be 4 but was %d\n", in_read);
+       }
+       if (out_read != 2) {
+               return FAILED ("out_read is expected to be 2 but was %d\n", out_read);
+       }
+
+       return OK;
+}
+
 RESULT
 test_utf8_to_utf16 ()
 {
-       const gchar *src0 = "", *src1 = "ABCDE";
-       gunichar2 str0 [1], str1 [6];
+       const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27";
+       gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0};
        RESULT result;
 
-       str0 [0] = 0;
        gchar_to_gunichar2 (str1, src1);
 
        /* empty string */
@@ -187,12 +213,61 @@ test_utf8_to_utf16 ()
                return result;
 
        result = compare_utf8_to_utf16 (str1, src1, 5, 5);
+       if (result != OK)
+               return result;
+       result = compare_utf8_to_utf16 (str2, src2, 4, 2);
        if (result != OK)
                return result;
 
        return OK;
 }
 
+RESULT
+test_convert ()
+{
+       gsize n;
+       char *s = g_convert ("\242\241\243\242\241\243\242\241\243\242\241\243", -1, "UTF-8", "ISO-8859-1", NULL, &n, NULL);
+       guchar *u = (guchar *) s;
+       
+       if (!s)
+               return FAILED ("Expected 24 bytes, got: NULL");
+
+       if (strlen (s) != 24)
+               return FAILED ("Expected 24 bytes, got: %d", strlen (s));
+
+       if (u [1] != 162 || u [2] != 194 ||
+           u [3] != 161 || u [4] != 194 ||
+           u [5] != 163 || u [6] != 194)
+               return FAILED ("Incorrect conversion");
+       
+       g_free (s);
+       
+       return OK;
+}
+
+
+RESULT
+test_xdigit ()
+{
+       static char test_chars[] = {
+               '0', '1', '2', '3', '4', 
+               '5', '6', '7', '8', '9', 
+               'a', 'b', 'c', 'd', 'e', 'f', 'g',
+               'A', 'B', 'C', 'D', 'E', 'F', 'G'};
+       static gint32 test_values[] = {
+               0, 1, 2, 3, 4, 
+               5, 6, 7, 8, 9, 
+               10, 11, 12, 13, 14, 15, -1,
+               10, 11, 12, 13, 14, 15, -1};
+
+               int i =0;
+
+               for (i = 0; i < sizeof(test_chars); i++)
+                       if (g_unichar_xdigit_value ((gunichar)test_chars[i]) != test_values[i])
+                               return FAILED("Incorrect value %d at index %d", test_values[i], i);
+
+               return OK;
+}
 
 /*
  * test initialization
@@ -201,8 +276,12 @@ test_utf8_to_utf16 ()
 static Test utf8_tests [] = {
        {"g_utf16_to_utf8", test_utf16_to_utf8},
        {"g_utf8_to_utf16", test_utf8_to_utf16},
+       {"g_utf8_seq", test_utf8_seq},
+       {"g_convert", test_convert },
+       {"g_unichar_xdigit_value", test_xdigit },
        {NULL, NULL}
 };
 
 DEFINE_TEST_GROUP_INIT(utf8_tests_init, utf8_tests)
 
+