Wrap always_inline and noinline attributes in compiler checks and use MSVC equivalent.
[mono.git] / eglib / test / utf8.c
1 #include <stdlib.h>
2
3 #include "test.h"
4
5 /*
6  * g_utf16_to_utf8
7  */
8
9 glong
10 compare_strings_utf8_pos (const gchar *expected, const gchar *actual, glong size)
11 {
12         int i;
13         for (i = 0; i < size; i++)
14                 if (expected [i] != actual [i])
15                         return i;
16         return -1;
17 }
18
19 RESULT
20 compare_strings_utf8_RESULT (const gchar *expected, const gchar *actual, glong size)
21 {
22         glong ret;
23
24         ret = compare_strings_utf8_pos (expected, actual, size);
25         if (ret < 0)
26                 return OK;
27         return FAILED ("Incorrect output: expected '%s' but was '%s', differ at %d\n", expected, actual, ret);
28 }
29
30 void
31 gchar_to_gunichar2 (gunichar2 ret[], const gchar *src)
32 {
33         int i;
34
35         for (i = 0; src [i]; i++)
36                 ret [i] = src [i];
37         ret [i] = 0;
38 }
39
40 RESULT
41 compare_utf16_to_utf8_explicit (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out, glong size_spec)
42 {
43         GError *error;
44         gchar* ret;
45         RESULT result;
46         glong in_read, out_read;
47
48         result = NULL;
49
50         error = NULL;
51         ret = g_utf16_to_utf8 (utf16, size_spec, &in_read, &out_read, &error);
52         if (error) {
53                 result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
54                 g_error_free (error);
55                 if (ret)
56                         g_free (ret);
57                 return result;
58         }
59         if (in_read != len_in)
60                 result = FAILED ("Read size is incorrect: expected %d but was %d\n", len_in, in_read);
61         else if (out_read != len_out)
62                 result = FAILED ("Converted size is incorrect: expected %d but was %d\n", len_out, out_read);
63         else
64                 result = compare_strings_utf8_RESULT (expected, ret, len_out);
65
66         g_free (ret);
67         if (result)
68                 return result;
69
70         return OK;
71 }
72
73 RESULT
74 compare_utf16_to_utf8 (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out)
75 {
76         RESULT result;
77
78         result = compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, -1);
79         if (result != OK)
80                 return result;
81         return compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, len_in);
82 }
83
84 RESULT
85 test_utf16_to_utf8 ()
86 {
87         const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27", *src3 = "\xEF\xBC\xA1", *src4 = "\xEF\xBD\x81", *src5 = "\xF0\x90\x90\x80";
88         gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0}, str3 [] = {0xFF21, 0}, str4 [] = {0xFF41, 0}, str5 [] = {0xD801, 0xDC00, 0};
89         RESULT result;
90
91         gchar_to_gunichar2 (str1, src1);
92
93         /* empty string */
94         result = compare_utf16_to_utf8 (src0, str0, 0, 0);
95         if (result != OK)
96                 return result;
97
98         result = compare_utf16_to_utf8 (src1, str1, 5, 5);
99         if (result != OK)
100                 return result;
101         result = compare_utf16_to_utf8 (src2, str2, 2, 4);
102         if (result != OK)
103                 return result;
104         result = compare_utf16_to_utf8 (src3, str3, 1, 3);
105         if (result != OK)
106                 return result;
107         result = compare_utf16_to_utf8 (src4, str4, 1, 3);
108         if (result != OK)
109                 return result;
110         result = compare_utf16_to_utf8 (src5, str5, 2, 4);
111         if (result != OK)
112                 return result;
113
114         return OK;
115 }
116
117 /*
118  * g_utf8_to_utf16 
119  */
120
121 glong
122 compare_strings_utf16_pos (const gunichar2 *expected, const gunichar2 *actual, glong size)
123 {
124         int i;
125         for (i = 0; i < size; i++)
126                 if (expected [i] != actual [i])
127                         return i;
128         return -1;
129 }
130
131 RESULT
132 compare_strings_utf16_RESULT (const gunichar2 *expected, const gunichar2 *actual, glong size)
133 {
134         glong ret;
135
136         ret = compare_strings_utf16_pos (expected, actual, size);
137         if (ret < 0)
138                 return OK;
139         return FAILED ("Incorrect output: expected '%s' but was '%s'\n", expected, actual);
140 }
141
142 RESULT
143 compare_utf8_to_utf16_explicit (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out, glong size_spec)
144 {
145         GError *error;
146         gunichar2* ret;
147         RESULT result;
148         glong in_read, out_read;
149
150         result = NULL;
151
152         error = NULL;
153         ret = g_utf8_to_utf16 (utf8, size_spec, &in_read, &out_read, &error);
154         if (error) {
155                 result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
156                 g_error_free (error);
157                 if (ret)
158                         g_free (ret);
159                 return result;
160         }
161         if (in_read != len_in)
162                 result = FAILED ("Read size is incorrect: expected %d but was %d\n", len_in, in_read);
163         else if (out_read != len_out)
164                 result = FAILED ("Converted size is incorrect: expected %d but was %d\n", len_out, out_read);
165         else
166                 result = compare_strings_utf16_RESULT (expected, ret, len_out);
167
168         g_free (ret);
169         if (result)
170                 return result;
171
172         return OK;
173 }
174
175
176 RESULT
177 compare_utf8_to_utf16 (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out)
178 {
179         RESULT result;
180
181         result = compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, -1);
182         if (result != OK)
183                 return result;
184         return compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, len_in);
185 }
186
187 RESULT
188 test_utf8_seq ()
189 {
190         const gchar *src = "\xE5\xB9\xB4\x27";
191         glong in_read, out_read;
192         //gunichar2 expected [6];
193         GError *error = NULL;
194         gunichar2 *dst;
195
196         //printf ("got: %s\n", src);
197         dst = g_utf8_to_utf16 (src, (glong)strlen (src), &in_read, &out_read, &error);
198         if (error != NULL){
199                 return error->message;
200         }
201
202         if (in_read != 4) {
203                 return FAILED ("in_read is expected to be 4 but was %d\n", in_read);
204         }
205         if (out_read != 2) {
206                 return FAILED ("out_read is expected to be 2 but was %d\n", out_read);
207         }
208         g_free (dst);
209
210         return OK;
211 }
212
213 RESULT
214 test_utf8_to_utf16 ()
215 {
216         const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27", *src3 = "\xEF\xBC\xA1", *src4 = "\xEF\xBD\x81";
217         gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0}, str3 [] = {0xFF21, 0}, str4 [] = {0xFF41, 0};
218         RESULT result;
219
220         gchar_to_gunichar2 (str1, src1);
221
222         /* empty string */
223         result = compare_utf8_to_utf16 (str0, src0, 0, 0);
224         if (result != OK)
225                 return result;
226
227         result = compare_utf8_to_utf16 (str1, src1, 5, 5);
228         if (result != OK)
229                 return result;
230         result = compare_utf8_to_utf16 (str2, src2, 4, 2);
231         if (result != OK)
232                 return result;
233         result = compare_utf8_to_utf16 (str3, src3, 3, 1);
234         if (result != OK)
235                 return result;
236         result = compare_utf8_to_utf16 (str4, src4, 3, 1);
237         if (result != OK)
238                 return result;
239
240         return OK;
241 }
242
243 typedef struct {
244         char *content;
245         size_t length;
246 } convert_result_t;
247
248 RESULT
249 test_convert ()
250 {
251         static const char *charsets[] = { "UTF-8", "UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE" };
252         gsize length, converted_length, n;
253         char *content, *converted, *path;
254         convert_result_t **expected;
255         GError *err = NULL;
256         const char *srcdir;
257         gboolean loaded;
258         guint i, j, k;
259         char c;
260         
261         if (!(srcdir = getenv ("srcdir")) && !(srcdir = getenv ("PWD")))
262                 return FAILED ("srcdir not defined!");
263         
264         expected = g_malloc (sizeof (convert_result_t *) * G_N_ELEMENTS (charsets));
265         
266         /* first load all our test samples... */
267         for (i = 0; i < G_N_ELEMENTS (charsets); i++) {
268                 path = g_strdup_printf ("%s%c%s.txt", srcdir, G_DIR_SEPARATOR, charsets[i]);
269                 loaded = g_file_get_contents (path, &content, &length, &err);
270                 g_free (path);
271                 
272                 if (!loaded) {
273                         for (j = 0; j < i; j++) {
274                                 g_free (expected[j]->content);
275                                 g_free (expected[j]);
276                         }
277                         
278                         g_free (expected);
279                         
280                         return FAILED ("Failed to load content for %s: %s", charsets[i], err->message);
281                 }
282                 
283                 expected[i] = g_malloc (sizeof (convert_result_t));
284                 expected[i]->content = content;
285                 expected[i]->length = length;
286         }
287         
288         /* test conversion from every charset to every other charset */
289         for (i = 0; i < G_N_ELEMENTS (charsets); i++) {
290                 for (j = 0; j < G_N_ELEMENTS (charsets); j++) {
291                         converted = g_convert (expected[i]->content, expected[i]->length, charsets[j],
292                                                charsets[i], NULL, &converted_length, NULL);
293                         
294                         if (converted == NULL) {
295                                 for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
296                                         g_free (expected[k]->content);
297                                         g_free (expected[k]);
298                                 }
299                                 
300                                 g_free (expected);
301                                 
302                                 return FAILED ("Failed to convert from %s to %s: NULL", charsets[i], charsets[j]);
303                         }
304                         
305                         if (converted_length != expected[j]->length) {
306                                 length = expected[j]->length;
307                                 
308                                 for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
309                                         g_free (expected[k]->content);
310                                         g_free (expected[k]);
311                                 }
312                                 
313                                 g_free (converted);
314                                 g_free (expected);
315                                 
316                                 return FAILED ("Failed to convert from %s to %s: expected %u bytes, got %u",
317                                                charsets[i], charsets[j], length, converted_length);
318                         }
319                         
320                         for (n = 0; n < converted_length; n++) {
321                                 if (converted[n] != expected[j]->content[n]) {
322                                         c = expected[j]->content[n];
323                                         
324                                         for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
325                                                 g_free (expected[k]->content);
326                                                 g_free (expected[k]);
327                                         }
328                                         
329                                         g_free (converted);
330                                         g_free (expected);
331                                         
332                                         return FAILED ("Failed to convert from %s to %s: expected 0x%x at offset %u, got 0x%x",
333                                                        charsets[i], charsets[j], c, n, converted[n]);
334                                 }
335                         }
336                         
337                         g_free (converted);
338                 }
339         }
340         
341         for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
342                 g_free (expected[k]->content);
343                 g_free (expected[k]);
344         }
345         
346         g_free (expected);
347         
348         return OK;
349 }
350
351
352 RESULT
353 test_xdigit ()
354 {
355         static char test_chars[] = {
356                 '0', '1', '2', '3', '4', 
357                 '5', '6', '7', '8', '9', 
358                 'a', 'b', 'c', 'd', 'e', 'f', 'g',
359                 'A', 'B', 'C', 'D', 'E', 'F', 'G'};
360         static gint32 test_values[] = {
361                 0, 1, 2, 3, 4, 
362                 5, 6, 7, 8, 9, 
363                 10, 11, 12, 13, 14, 15, -1,
364                 10, 11, 12, 13, 14, 15, -1};
365
366                 int i =0;
367
368                 for (i = 0; i < sizeof(test_chars); i++)
369                         if (g_unichar_xdigit_value ((gunichar)test_chars[i]) != test_values[i])
370                                 return FAILED("Incorrect value %d at index %d", test_values[i], i);
371
372                 return OK;
373 }
374
375 static RESULT
376 ucs4_to_utf16_check_result (const gunichar2 *result_str, const gunichar2 *expected_str,
377                             glong result_items_read, glong expected_items_read,
378                             glong result_items_written, glong expected_items_written,
379                             GError* result_error, gboolean expect_error)
380 {
381         glong i;
382         if (result_items_read != expected_items_read)
383                 return FAILED("Incorrect number of items read; expected %d, got %d", expected_items_read, result_items_read);
384         if (result_items_written != expected_items_written)
385                 return FAILED("Incorrect number of items written; expected %d, got %d", expected_items_written, result_items_written);
386         if (result_error && !expect_error)
387                 return FAILED("There should not be an error code.");
388         if (!result_error && expect_error)
389                 return FAILED("Unexpected error object.");
390         if (expect_error && result_str)
391                 return FAILED("NULL should be returned when an error occurs.");
392         if (!expect_error && !result_str)
393                 return FAILED("When no error occurs NULL should not be returned.");
394         for (i=0; i<expected_items_written;i++) {
395                 if (result_str [i] != expected_str [i])
396                         return FAILED("Incorrect value %d at index %d", result_str [i], i);
397         }
398         if (result_str && result_str[expected_items_written] != '\0') 
399                 return FAILED("Null termination not found at the end of the string.");
400         
401         return OK;
402 }
403
404 RESULT
405 test_ucs4_to_utf16 ()
406 {
407         static gunichar str1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
408         static gunichar2 exp1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
409         static gunichar str2[3] = {'h',0x80000000,'\0'};
410         static gunichar2 exp2[2] = {'h','\0'};
411         static gunichar str3[3] = {'h',0xDA00,'\0'};
412         static gunichar str4[3] = {'h',0x10FFFF,'\0'};
413         static gunichar2 exp4[4] = {'h',0xdbff,0xdfff,'\0'};
414         static gunichar str5[7] = {0xD7FF,0xD800,0xDFFF,0xE000,0x110000,0x10FFFF,'\0'};
415         static gunichar2 exp5[5] = {0xD7FF,0xE000,0xdbff,0xdfff,'\0'};
416         static gunichar str6[2] = {0x10400, '\0'};
417         static gunichar2 exp6[3] = {0xD801, 0xDC00, '\0'};
418         static glong read_write[12] = {1,1,0,0,0,0,1,1,0,0,1,2};
419         gunichar2* res;
420         glong items_read, items_written, current_write_index;
421         GError* err=0;
422         RESULT check_result;
423         glong i;
424         
425         res = g_ucs4_to_utf16 (str1, 12, &items_read, &items_written, &err);
426         check_result = ucs4_to_utf16_check_result (res, exp1, items_read, 11, items_written, 11, err, FALSE);
427         if (check_result) return check_result;
428         g_free (res);
429
430         items_read = items_written = 0;
431         res = g_ucs4_to_utf16 (str2, 0, &items_read, &items_written, &err);
432         check_result = ucs4_to_utf16_check_result (res, exp2, items_read, 0, items_written, 0, err, FALSE);
433         if (check_result) return check_result;
434         g_free (res);
435
436         items_read = items_written = 0;
437         res = g_ucs4_to_utf16 (str2, 1, &items_read, &items_written, &err);
438         check_result = ucs4_to_utf16_check_result (res, exp2, items_read, 1, items_written, 1, err, FALSE);
439         if (check_result) return check_result;
440         g_free (res);
441
442         items_read = items_written = 0;
443         res = g_ucs4_to_utf16 (str2, 2, &items_read, &items_written, &err);
444         check_result = ucs4_to_utf16_check_result (res, 0, items_read, 1, items_written, 0, err, TRUE);
445         g_free (res);
446         if (check_result) return check_result;
447
448         items_read = items_written = 0;
449         err = 0;
450         res = g_ucs4_to_utf16 (str3, 2, &items_read, &items_written, &err);
451         check_result = ucs4_to_utf16_check_result (res, 0, items_read, 1, items_written, 0, err, TRUE);
452         if (check_result) return check_result;
453         g_free (res);
454
455         items_read = items_written = 0;
456         err = 0;
457         res = g_ucs4_to_utf16 (str4, 5, &items_read, &items_written, &err);
458         check_result = ucs4_to_utf16_check_result (res, exp4, items_read, 2, items_written, 3, err, FALSE);
459         if (check_result) return check_result;
460         g_free (res);
461
462         // This loop tests the bounds of the conversion algorithm
463         current_write_index = 0;
464         for (i=0;i<6;i++) {
465                 items_read = items_written = 0;
466                 err = 0;
467                 res = g_ucs4_to_utf16 (&str5[i], 1, &items_read, &items_written, &err);
468                 check_result = ucs4_to_utf16_check_result (res, &exp5[current_write_index], 
469                                         items_read, read_write[i*2], items_written, read_write[(i*2)+1], err, !read_write[(i*2)+1]);
470                 if (check_result) return check_result;
471                 g_free (res);
472                 current_write_index += items_written;
473         }
474
475         items_read = items_written = 0;
476         err = 0;
477         res = g_ucs4_to_utf16 (str6, 1, &items_read, &items_written, &err);
478         check_result = ucs4_to_utf16_check_result (res, exp6, items_read, 1, items_written, 2, err, FALSE);
479         if (check_result) return check_result;
480         g_free (res);
481
482         return OK;
483 }
484
485 static RESULT
486 utf16_to_ucs4_check_result (const gunichar *result_str, const gunichar *expected_str,
487                             glong result_items_read, glong expected_items_read,
488                             glong result_items_written, glong expected_items_written,
489                             GError* result_error, gboolean expect_error)
490 {
491         glong i;
492         if (result_items_read != expected_items_read)
493                 return FAILED("Incorrect number of items read; expected %d, got %d", expected_items_read, result_items_read);
494         if (result_items_written != expected_items_written)
495                 return FAILED("Incorrect number of items written; expected %d, got %d", expected_items_written, result_items_written);
496         if (result_error && !expect_error)
497                 return FAILED("There should not be an error code.");
498         if (!result_error && expect_error)
499                 return FAILED("Unexpected error object.");
500         if (expect_error && result_str)
501                 return FAILED("NULL should be returned when an error occurs.");
502         if (!expect_error && !result_str)
503                 return FAILED("When no error occurs NULL should not be returned.");
504         for (i=0; i<expected_items_written;i++) {
505                 if (result_str [i] != expected_str [i])
506                         return FAILED("Incorrect value %d at index %d", result_str [i], i);
507         }
508         if (result_str && result_str[expected_items_written] != '\0') 
509                 return FAILED("Null termination not found at the end of the string.");
510         
511         return OK;
512 }
513
514 RESULT
515 test_utf16_to_ucs4 ()
516 {
517         static gunichar2 str1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
518         static gunichar exp1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
519         static gunichar2 str2[7] = {'H', 0xD800, 0xDC01,0xD800,0xDBFF,'l','\0'};
520         static gunichar exp2[3] = {'H',0x00010001,'\0'};
521         static gunichar2 str3[4] = {'H', 0xDC00 ,'l','\0'};
522         static gunichar exp3[2] = {'H','\0'};
523         static gunichar2 str4[20] = {0xDC00,0xDFFF,0xDFF,0xD800,0xDBFF,0xD800,0xDC00,0xD800,0xDFFF,
524                                      0xD800,0xE000,0xDBFF,0xDBFF,0xDBFF,0xDC00,0xDBFF,0xDFFF,0xDBFF,0xE000,'\0'};
525         static gunichar exp4[6] = {0xDFF,0x10000,0x103ff,0x10fc00,0x10FFFF,'\0'};
526         static gunichar2 str5[3] = {0xD801, 0xDC00, 0};
527         static gunichar exp5[2] = {0x10400, 0};
528         static glong read_write[33] = {1,0,0,1,0,0,1,1,1,2,1,0,2,2,1,2,2,1,2,1,0,2,1,0,2,2,1,2,2,1,2,1,0};
529         gunichar* res;
530         glong items_read, items_written, current_read_index,current_write_index;
531         GError* err=0;
532         RESULT check_result;
533         glong i;
534         
535         res = g_utf16_to_ucs4 (str1, 12, &items_read, &items_written, &err);
536         check_result = utf16_to_ucs4_check_result (res, exp1, items_read, 11, items_written, 11, err, FALSE);
537         if (check_result) return check_result;
538         g_free (res);
539         
540         items_read = items_written = 0;
541         res = g_utf16_to_ucs4 (str2, 0, &items_read, &items_written, &err);
542         check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 0, items_written, 0, err, FALSE);
543         if (check_result) return check_result;
544         g_free (res);
545         
546         items_read = items_written = 0;
547         res = g_utf16_to_ucs4 (str2, 1, &items_read, &items_written, &err);
548         check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 1, items_written, 1, err, FALSE);
549         if (check_result) return check_result;
550         g_free (res);
551         
552         items_read = items_written = 0;
553         res = g_utf16_to_ucs4 (str2, 2, &items_read, &items_written, &err);
554         check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 1, items_written, 1, err, FALSE);
555         if (check_result) return check_result;
556         g_free (res);
557         
558         items_read = items_written = 0;
559         res = g_utf16_to_ucs4 (str2, 3, &items_read, &items_written, &err);
560         check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 3, items_written, 2, err, FALSE);
561         if (check_result) return check_result;
562         g_free (res);
563         
564         items_read = items_written = 0;
565         res = g_utf16_to_ucs4 (str2, 4, &items_read, &items_written, &err);
566         check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 3, items_written, 2, err, FALSE);
567         if (check_result) return check_result;
568         g_free (res);
569         
570         items_read = items_written = 0;
571         res = g_utf16_to_ucs4 (str2, 5, &items_read, &items_written, &err);
572         check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 4, items_written, 0, err, TRUE);
573         if (check_result) return check_result;
574         g_free (res);
575         
576         items_read = items_written = 0;
577         err = 0;
578         res = g_utf16_to_ucs4 (str3, 5, &items_read, &items_written, &err);
579         check_result = utf16_to_ucs4_check_result (res, exp3, items_read, 1, items_written, 0, err, TRUE);
580         if (check_result) return check_result;
581         g_free (res);
582         
583         // This loop tests the bounds of the conversion algorithm
584         current_read_index = current_write_index = 0;
585         for (i=0;i<11;i++) {
586                 items_read = items_written = 0;
587                 err = 0;
588                 res = g_utf16_to_ucs4 (&str4[current_read_index], read_write[i*3], &items_read, &items_written, &err);
589                 check_result = utf16_to_ucs4_check_result (res, &exp4[current_write_index], items_read, 
590                                              read_write[(i*3)+1], items_written, read_write[(i*3)+2], err, 
591                                              !read_write[(i*3)+2]);
592                 if (check_result) return check_result;
593                 g_free (res);
594                 current_read_index += read_write[i*3];
595                 current_write_index += items_written;
596         }
597
598         items_read = items_written = 0;
599         err = 0;
600         res = g_utf16_to_ucs4 (str5, 2, &items_read, &items_written, &err);
601         check_result = utf16_to_ucs4_check_result (res, exp5, items_read, 2, items_written, 1, err, FALSE);
602         if (check_result) return check_result;
603         g_free (res);
604
605         return OK;
606 }
607 RESULT
608 test_utf8_strlen ()
609 {
610         gchar word1 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'};//Valid, len = 5
611         gchar word2 [] = {0xF1, 0x82, 0x82, 0x82,0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,'\0'};//Valid, len = 5
612         gchar word3 [] = {'h','e',0xC2, 0x82,0x45,'\0'};                                                                                //Valid, len = 4
613         gchar word4 [] = {0x62,0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,'\0'};                                     //Valid, len = 5
614         
615         glong len = 0;
616         
617         //Test word1
618         len = g_utf8_strlen (word1,-1);
619         if (len != 5)
620                 return FAILED ("Word1 expected length of 5, but was %i", len);
621         //Do tests with different values for max parameter.
622         len = g_utf8_strlen (word1,1);
623         if (len != 0)
624                 return FAILED ("Word1, max = 1, expected length of 0, but was %i", len);
625         len = g_utf8_strlen (word1,2);
626         if (len != 1)
627                 return FAILED ("Word1, max = 1, expected length of 1, but was %i", len);
628         len = g_utf8_strlen (word1,3);
629         if (len != 2)
630                 return FAILED ("Word1, max = 2, expected length of 2, but was %i", len);
631                 
632         //Test word2
633         len = g_utf8_strlen (word2,-1);
634         if (len != 5)
635                 return FAILED ("Word2 expected length of 5, but was %i", len);
636                 
637         //Test word3
638         len = g_utf8_strlen (word3,-1);
639         if (len != 4)
640                 return FAILED ("Word3 expected length of 4, but was %i", len);
641                 
642         //Test word4
643         len = g_utf8_strlen (word4,-1);
644         if (len != 5)
645                 return FAILED ("Word4 expected length of 5, but was %i", len);
646                 
647         //Test null case
648         len = g_utf8_strlen(NULL,0);
649         if (len != 0)
650                 return FAILED ("Expected passing null to result in a length of 0");
651         return OK;
652 }
653
654 RESULT
655 test_utf8_get_char()
656 {
657         gchar word1 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'}; //Valid, len = 5
658
659         gunichar value = g_utf8_get_char (&word1 [0]);
660         if (value != 0x82UL)
661                 return FAILED ("Expected value of 0x82, but was %x", value);
662         value = g_utf8_get_char (&word1 [2]);
663         if (value != 0x45UL)
664                 return FAILED ("Expected value of 0x45, but was %x", value);
665         value = g_utf8_get_char (&word1 [3]);
666         if (value != 0x1043UL)
667                 return FAILED ("Expected value of 0x1043, but was %x", value);
668         value = g_utf8_get_char (&word1 [6]);
669         if (value != 0x58UL)
670                 return FAILED ("Expected value of 0x58, but was %x", value);
671         value = g_utf8_get_char (&word1 [7]);
672         if (value != 0x42082UL)
673                 return FAILED ("Expected value of 0x42082, but was %x", value);
674
675         return OK;
676 }
677
678 RESULT
679 test_utf8_next_char()
680 {
681         gchar word1 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'}; //Valid, len = 5
682         gchar word2 [] = {0xF1, 0x82, 0x82, 0x82,0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,'\0'}; //Valid, len = 5
683         gchar word1ExpectedValues [] = {0xC2, 0x45,0xE1, 0x58, 0xF1};
684         gchar word2ExpectedValues [] = {0xF1, 0xC2, 0x45, 0xE1, 0x58};
685         
686         gchar* ptr = word1;
687         gint count = 0;
688         //Test word1
689         while (*ptr != 0) {
690                 if (count > 4)
691                         return FAILED ("Word1 has gone past its expected length");
692                 if (*ptr != word1ExpectedValues[count])
693                         return FAILED ("Word1 has an incorrect next_char at index %i", count);
694                 ptr = g_utf8_next_char (ptr);
695                 count++;
696         }
697         
698         //Test word2
699         count = 0;
700         ptr = word2;
701         while (*ptr != 0) {
702                 if (count > 4)
703                         return FAILED ("Word2 has gone past its expected length");
704                 if (*ptr != word2ExpectedValues[count])
705                         return FAILED ("Word2 has an incorrect next_char at index %i", count);
706                 ptr = g_utf8_next_char (ptr);
707                 count++;
708         }
709         
710         return OK;
711 }
712
713 RESULT
714 test_utf8_validate()
715 {
716         gchar invalidWord1 [] = {0xC3, 0x82, 0xC1,0x90,'\0'}; //Invalid, 1nd oct Can't be 0xC0 or 0xC1
717         gchar invalidWord2 [] = {0xC1, 0x89, 0x60, '\0'}; //Invalid, 1st oct can not be 0xC1
718         gchar invalidWord3 [] = {0xC2, 0x45,0xE1, 0x81, 0x83,0x58,'\0'}; //Invalid, oct after 0xC2 must be > 0x80
719
720         gchar validWord1 [] = {0xC2, 0x82, 0xC3,0xA0,'\0'}; //Valid
721         gchar validWord2 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'}; //Valid
722         
723         const gchar* end;
724         gboolean retVal = g_utf8_validate (invalidWord1, -1, &end);
725         if (retVal != FALSE)
726                 return FAILED ("Expected invalidWord1 to be invalid");
727         if (end != &invalidWord1 [2])
728                 return FAILED ("Expected end parameter to be pointing to invalidWord1[2]");
729
730         end = NULL;
731         retVal = g_utf8_validate (invalidWord2, -1, &end);
732         if (retVal != FALSE)
733                 return FAILED ("Expected invalidWord2 to be invalid");
734         if (end != &invalidWord2 [0])
735                 return FAILED ("Expected end parameter to be pointing to invalidWord2[0]");
736
737         end = NULL;
738         retVal = g_utf8_validate (invalidWord3, -1, &end);
739         if (retVal != FALSE)
740                 return FAILED ("Expected invalidWord3 to be invalid");
741         if (end != &invalidWord3 [0])
742                 return FAILED ("Expected end parameter to be pointing to invalidWord3[1]");
743
744         end = NULL;
745         retVal = g_utf8_validate (validWord1, -1, &end);
746         if (retVal != TRUE)
747                 return FAILED ("Expected validWord1 to be valid");
748         if (end != &validWord1 [4])
749                 return FAILED ("Expected end parameter to be pointing to validWord1[4]");
750
751         end = NULL;
752         retVal = g_utf8_validate (validWord2, -1, &end);
753         if (retVal != TRUE)
754                 return FAILED ("Expected validWord2 to be valid");
755         if (end != &validWord2 [11])
756                 return FAILED ("Expected end parameter to be pointing to validWord2[11]");
757         return OK;
758 }
759
760 glong
761 utf8_byteslen (const gchar *src)
762 {
763         int i = 0;
764         do {
765                 if (src [i] == '\0')
766                         return i;
767                 i++;
768         } while (TRUE);
769 }
770
771 RESULT
772 test_utf8_strcase_each (const gchar *src, const gchar *expected, gboolean strup)
773 {
774         gchar *tmp;
775         glong len, len2;
776         RESULT r;
777
778         len = utf8_byteslen (src);
779         tmp = strup ? g_utf8_strup (src, len) : g_utf8_strdown (src, len);
780         len2 = utf8_byteslen (tmp);
781         r = compare_strings_utf8_RESULT (expected, tmp, len < len2 ? len2 : len);
782         g_free (tmp);
783         return r;
784 }
785
786 RESULT
787 test_utf8_strup_each (const gchar *src, const gchar *expected)
788 {
789         return test_utf8_strcase_each (src, expected, TRUE);
790 }
791
792 RESULT
793 test_utf8_strdown_each (const gchar *src, const gchar *expected)
794 {
795         return test_utf8_strcase_each (src, expected, FALSE);
796 }
797
798 /*
799  * g_utf8_strup
800  */
801 RESULT
802 test_utf8_strup ()
803 {
804         RESULT r;
805
806         if ((r = test_utf8_strup_each ("aBc", "ABC")) != OK)
807                 return r;
808         if ((r = test_utf8_strup_each ("x86-64", "X86-64")) != OK)
809                 return r;
810         // U+3B1 U+392 -> U+391 U+392
811         if ((r = test_utf8_strup_each ("\xCE\xB1\xCE\x92", "\xCE\x91\xCE\x92")) != OK)
812                 return r;
813         // U+FF21 -> U+FF21
814         if ((r = test_utf8_strup_each ("\xEF\xBC\xA1", "\xEF\xBC\xA1")) != OK)
815                 return r;
816         // U+FF41 -> U+FF21
817         if ((r = test_utf8_strup_each ("\xEF\xBD\x81", "\xEF\xBC\xA1")) != OK)
818                 return r;
819         // U+10428 -> U+10400
820         if ((r = test_utf8_strup_each ("\xF0\x90\x90\xA8", "\xF0\x90\x90\x80")) != OK)
821                 return r;
822
823         return OK;
824 }
825
826 /*
827  * g_utf8_strdown
828  */
829 RESULT
830 test_utf8_strdown ()
831 {
832         RESULT r;
833
834         if ((r = test_utf8_strdown_each ("aBc", "abc")) != OK)
835                 return r;
836         if ((r = test_utf8_strdown_each ("X86-64", "x86-64")) != OK)
837                 return r;
838         // U+391 U+3B2 -> U+3B1 U+3B2
839         if ((r = test_utf8_strdown_each ("\xCE\x91\xCE\xB2", "\xCE\xB1\xCE\xB2")) != OK)
840                 return r;
841 /*
842         // U+FF41 -> U+FF41
843         if ((r = test_utf8_strdown_each ("\xEF\xBC\x81", "\xEF\xBC\x81")) != OK)
844                 return r;
845         // U+FF21 -> U+FF41
846         if ((r = test_utf8_strdown_each ("\xEF\xBC\xA1", "\xEF\xBD\x81")) != OK)
847                 return r;
848         // U+10400 -> U+10428
849         if ((r = test_utf8_strdown_each ("\xF0\x90\x90\x80", "\xF0\x90\x90\xA8")) != OK)
850                 return r;
851 */
852         return OK;
853 }
854
855 /*
856  * test initialization
857  */
858
859 static Test utf8_tests [] = {
860         {"g_utf16_to_utf8", test_utf16_to_utf8},
861         {"g_utf8_to_utf16", test_utf8_to_utf16},
862         {"g_utf8_seq", test_utf8_seq},
863         {"g_convert", test_convert },
864         {"g_unichar_xdigit_value", test_xdigit },
865         {"g_ucs4_to_utf16", test_ucs4_to_utf16 },
866         {"g_utf16_to_ucs4", test_utf16_to_ucs4 },
867         {"g_utf8_strlen", test_utf8_strlen },
868         {"g_utf8_get_char", test_utf8_get_char },
869         {"g_utf8_next_char", test_utf8_next_char },
870         {"g_utf8_validate", test_utf8_validate },
871         {"g_utf8_strup", test_utf8_strup},
872         {"g_utf8_strdown", test_utf8_strdown},
873         {NULL, NULL}
874 };
875
876 DEFINE_TEST_GROUP_INIT(utf8_tests_init, utf8_tests)
877
878