Fix problems with overlong directory names: phase #1
[mono.git] / eglib / test / utf8.c
1 #include "test.h"
2
3 /*
4  * g_utf16_to_utf8
5  */
6
7 glong
8 compare_strings_utf8_pos (const gchar *expected, const gchar *actual, glong size)
9 {
10         int i;
11         for (i = 0; i < size; i++)
12                 if (expected [i] != actual [i])
13                         return i;
14         return -1;
15 }
16
17 RESULT
18 compare_strings_utf8_RESULT (const gchar *expected, const gchar *actual, glong size)
19 {
20         glong ret;
21
22         ret = compare_strings_utf8_pos (expected, actual, size);
23         if (ret < 0)
24                 return OK;
25         return FAILED ("Incorrect output: expected '%s' but was '%s', differ at %d\n", expected, actual, ret);
26 }
27
28 void
29 gchar_to_gunichar2 (gunichar2 ret[], const gchar *src)
30 {
31         int i;
32
33         for (i = 0; src [i]; i++)
34                 ret [i] = src [i];
35         ret [i] = 0;
36 }
37
38 RESULT
39 compare_utf16_to_utf8_explicit (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out, glong size_spec)
40 {
41         GError *error;
42         gchar* ret;
43         RESULT result;
44         glong in_read, out_read;
45
46         result = NULL;
47
48         error = NULL;
49         ret = g_utf16_to_utf8 (utf16, size_spec, &in_read, &out_read, &error);
50         if (error) {
51                 result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
52                 g_error_free (error);
53                 if (ret)
54                         g_free (ret);
55                 return result;
56         }
57         if (in_read != len_in)
58                 result = FAILED ("Read size is incorrect: expected %d but was %d\n", len_in, in_read);
59         else if (out_read != len_out)
60                 result = FAILED ("Converted size is incorrect: expected %d but was %d\n", len_out, out_read);
61         else
62                 result = compare_strings_utf8_RESULT (expected, ret, len_out);
63
64         g_free (ret);
65         if (result)
66                 return result;
67
68         return OK;
69 }
70
71 RESULT
72 compare_utf16_to_utf8 (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out)
73 {
74         RESULT result;
75
76         result = compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, -1);
77         if (result != OK)
78                 return result;
79         return compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, len_in);
80 }
81
82 RESULT
83 test_utf16_to_utf8 ()
84 {
85         const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27";
86         gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0};
87         RESULT result;
88
89         gchar_to_gunichar2 (str1, src1);
90
91         /* empty string */
92         result = compare_utf16_to_utf8 (src0, str0, 0, 0);
93         if (result != OK)
94                 return result;
95
96         result = compare_utf16_to_utf8 (src1, str1, 5, 5);
97         if (result != OK)
98                 return result;
99         result = compare_utf16_to_utf8 (src2, str2, 2, 4);
100         if (result != OK)
101                 return result;
102
103         return OK;
104 }
105
106 /*
107  * g_utf8_to_utf16 
108  */
109
110 glong
111 compare_strings_utf16_pos (const gunichar2 *expected, const gunichar2 *actual, glong size)
112 {
113         int i;
114         for (i = 0; i < size; i++)
115                 if (expected [i] != actual [i])
116                         return i;
117         return -1;
118 }
119
120 RESULT
121 compare_strings_utf16_RESULT (const gunichar2 *expected, const gunichar2 *actual, glong size)
122 {
123         glong ret;
124
125         ret = compare_strings_utf16_pos (expected, actual, size);
126         if (ret < 0)
127                 return OK;
128         return FAILED ("Incorrect output: expected '%s' but was '%s'\n", expected, actual);
129 }
130
131 RESULT
132 compare_utf8_to_utf16_explicit (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out, glong size_spec)
133 {
134         GError *error;
135         gunichar2* ret;
136         RESULT result;
137         glong in_read, out_read;
138
139         result = NULL;
140
141         error = NULL;
142         ret = g_utf8_to_utf16 (utf8, size_spec, &in_read, &out_read, &error);
143         if (error) {
144                 result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
145                 g_error_free (error);
146                 if (ret)
147                         g_free (ret);
148                 return result;
149         }
150         if (in_read != len_in)
151                 result = FAILED ("Read size is incorrect: expected %d but was %d\n", len_in, in_read);
152         else if (out_read != len_out)
153                 result = FAILED ("Converted size is incorrect: expected %d but was %d\n", len_out, out_read);
154         else
155                 result = compare_strings_utf16_RESULT (expected, ret, len_out);
156
157         g_free (ret);
158         if (result)
159                 return result;
160
161         return OK;
162 }
163
164
165 RESULT
166 compare_utf8_to_utf16 (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out)
167 {
168         RESULT result;
169
170         result = compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, -1);
171         if (result != OK)
172                 return result;
173         return compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, len_in);
174 }
175
176 RESULT
177 test_utf8_seq ()
178 {
179         const gchar *src = "\xE5\xB9\xB4\x27";
180         glong in_read, out_read;
181         //gunichar2 expected [6];
182         GError *error = NULL;
183         gunichar2 *dst;
184
185         printf ("got: %s\n", src);
186         dst = g_utf8_to_utf16 (src, strlen (src), &in_read, &out_read, &error);
187         if (error != NULL){
188                 return error->message;
189         }
190
191         if (in_read != 4) {
192                 return FAILED ("in_read is expected to be 4 but was %d\n", in_read);
193         }
194         if (out_read != 2) {
195                 return FAILED ("out_read is expected to be 2 but was %d\n", out_read);
196         }
197
198         return OK;
199 }
200
201 RESULT
202 test_utf8_to_utf16 ()
203 {
204         const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27";
205         gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0};
206         RESULT result;
207
208         gchar_to_gunichar2 (str1, src1);
209
210         /* empty string */
211         result = compare_utf8_to_utf16 (str0, src0, 0, 0);
212         if (result != OK)
213                 return result;
214
215         result = compare_utf8_to_utf16 (str1, src1, 5, 5);
216         if (result != OK)
217                 return result;
218         result = compare_utf8_to_utf16 (str2, src2, 4, 2);
219         if (result != OK)
220                 return result;
221
222         return OK;
223 }
224
225 RESULT
226 test_convert ()
227 {
228         gsize n;
229         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);
230         guchar *u = (guchar *) s;
231         
232         if (strlen (s) != 24)
233                 return FAILED ("Expected 24 bytes, got: %d", strlen (s));
234
235         if (u [1] != 162 || u [2] != 194 ||
236             u [3] != 161 || u [4] != 194 ||
237             u [5] != 163 || u [6] != 194)
238                 return FAILED ("Incorrect conversion");
239         
240         g_free (s);
241         
242         return OK;
243 }
244
245 /*
246  * test initialization
247  */
248
249 static Test utf8_tests [] = {
250         {"g_utf16_to_utf8", test_utf16_to_utf8},
251         {"g_utf8_to_utf16", test_utf8_to_utf16},
252         {"g_utf8_seq", test_utf8_seq},
253         {"g_convert", test_convert },
254         {NULL, NULL}
255 };
256
257 DEFINE_TEST_GROUP_INIT(utf8_tests_init, utf8_tests)
258