File: utf8.c

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (935 lines) | stat: -rw-r--r-- 28,632 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
#include <stdlib.h>

#include "test.h"

/*
 * g_utf16_to_utf8
 */

glong
compare_strings_utf8_pos (const gchar *expected, const gchar *actual, glong size)
{
	int i;
	for (i = 0; i < size; i++)
		if (expected [i] != actual [i])
			return i;
	return -1;
}

RESULT
compare_strings_utf8_RESULT (const gchar *expected, const gchar *actual, glong size)
{
	glong ret;

	ret = compare_strings_utf8_pos (expected, actual, size);
	if (ret < 0)
		return OK;
	return FAILED ("Incorrect output: expected '%s' but was '%s', differ at %d\n", expected, actual, ret);
}

void
gchar_to_gunichar2 (gunichar2 ret[], const gchar *src)
{
	int i;

	for (i = 0; src [i]; i++)
		ret [i] = src [i];
	ret [i] = 0;
}

RESULT
compare_utf16_to_utf8_explicit (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out, glong size_spec)
{
	GError *error;
	gchar* ret;
	RESULT result;
	glong in_read, out_read;

	result = NULL;

	error = NULL;
	ret = g_utf16_to_utf8 (utf16, size_spec, &in_read, &out_read, &error);
	if (error) {
		result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
		g_error_free (error);
		if (ret)
			g_free (ret);
		return result;
	}
	if (in_read != len_in)
		result = FAILED ("Read size is incorrect: expected %d but was %d\n", len_in, in_read);
	else if (out_read != len_out)
		result = FAILED ("Converted size is incorrect: expected %d but was %d\n", len_out, out_read);
	else
		result = compare_strings_utf8_RESULT (expected, ret, len_out);

	g_free (ret);
	if (result)
		return result;

	return OK;
}

RESULT
compare_utf16_to_utf8 (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out)
{
	RESULT result;

	result = compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, -1);
	if (result != OK)
		return result;
	return compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, len_in);
}

RESULT
test_utf16_to_utf8 ()
{
	const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27", *src3 = "\xEF\xBC\xA1", *src4 = "\xEF\xBD\x81", *src5 = "\xF0\x90\x90\x80";
	gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0}, str3 [] = {0xFF21, 0}, str4 [] = {0xFF41, 0}, str5 [] = {0xD801, 0xDC00, 0};
	RESULT result;

	gchar_to_gunichar2 (str1, src1);

	/* empty string */
	result = compare_utf16_to_utf8 (src0, str0, 0, 0);
	if (result != OK)
		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;
	result = compare_utf16_to_utf8 (src3, str3, 1, 3);
	if (result != OK)
		return result;
	result = compare_utf16_to_utf8 (src4, str4, 1, 3);
	if (result != OK)
		return result;
	result = compare_utf16_to_utf8 (src5, str5, 2, 4);
	if (result != OK)
		return result;

	return OK;
}

/*
 * g_utf8_to_utf16 
 */

glong
compare_strings_utf16_pos (const gunichar2 *expected, const gunichar2 *actual, glong size)
{
	int i;
	for (i = 0; i < size; i++)
		if (expected [i] != actual [i])
			return i;
	return -1;
}

RESULT
compare_strings_utf16_RESULT (const gunichar2 *expected, const gunichar2 *actual, glong size)
{
	glong ret;

	ret = compare_strings_utf16_pos (expected, actual, size);
	if (ret < 0)
		return OK;
	return FAILED ("Incorrect output: expected '%s' but was '%s', differ at %d ('%c' x '%c')\n", expected, actual, ret, expected [ret], actual [ret]);
}

#if !defined(EGLIB_TESTS)
#define eg_utf8_to_utf16_with_nuls g_utf8_to_utf16
#endif

RESULT
compare_utf8_to_utf16_explicit (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out, glong size_spec, gboolean include_nuls)
{
	GError *error;
	gunichar2* ret;
	RESULT result;
	glong in_read, out_read;

	result = NULL;

	error = NULL;
	if (include_nuls)
		ret = eg_utf8_to_utf16_with_nuls (utf8, size_spec, &in_read, &out_read, &error);
	else
		ret = g_utf8_to_utf16 (utf8, size_spec, &in_read, &out_read, &error);

	if (error) {
		result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
		g_error_free (error);
		if (ret)
			g_free (ret);
		return result;
	}
	if (in_read != len_in)
		result = FAILED ("Read size is incorrect: expected %d but was %d\n", len_in, in_read);
	else if (out_read != len_out)
		result = FAILED ("Converted size is incorrect: expected %d but was %d\n", len_out, out_read);
	else
		result = compare_strings_utf16_RESULT (expected, ret, len_out);

	g_free (ret);
	if (result)
		return result;

	return OK;
}

RESULT
compare_utf8_to_utf16_general (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out, gboolean include_nuls)
{
	RESULT result;

	result = compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, -1, include_nuls);
	if (result != OK)
		return result;
	return compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, len_in, include_nuls);
}

RESULT
compare_utf8_to_utf16 (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out)
{
	return compare_utf8_to_utf16_general (expected, utf8, len_in, len_out, FALSE);
}

RESULT
compare_utf8_to_utf16_with_nuls (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out)
{
	return compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, len_in, TRUE);
}


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);
	}
	g_free (dst);

	return OK;
}

RESULT
test_utf8_to_utf16 ()
{
	const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27", *src3 = "\xEF\xBC\xA1", *src4 = "\xEF\xBD\x81";
	gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0}, str3 [] = {0xFF21, 0}, str4 [] = {0xFF41, 0};
	RESULT result;

	gchar_to_gunichar2 (str1, src1);

	/* empty string */
	result = compare_utf8_to_utf16 (str0, src0, 0, 0);
	if (result != OK)
		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;
	result = compare_utf8_to_utf16 (str3, src3, 3, 1);
	if (result != OK)
		return result;
	result = compare_utf8_to_utf16 (str4, src4, 3, 1);
	if (result != OK)
		return result;

	return OK;
}

RESULT
test_utf8_to_utf16_with_nuls ()
{
	const gchar *src0 = "", *src1 = "AB\0DE", *src2 = "\xE5\xB9\xB4\x27", *src3 = "\xEF\xBC\xA1", *src4 = "\xEF\xBD\x81";
	gunichar2 str0 [] = {0}, str1 [] = {'A', 'B', 0, 'D', 'E', 0}, str2 [] = {0x5E74, 39, 0}, str3 [] = {0xFF21, 0}, str4 [] = {0xFF41, 0};
	RESULT result;

#if !defined(EGLIB_TESTS)
	return OK;
#endif

	/* implicit length is forbidden */
		if (eg_utf8_to_utf16_with_nuls (src1, -1, NULL, NULL, NULL) != NULL)
		return FAILED ("explicit nulls must fail with -1 length\n");

	/* empty string */
	result = compare_utf8_to_utf16_with_nuls (str0, src0, 0, 0);
	if (result != OK)
		return result;

	result = compare_utf8_to_utf16_with_nuls  (str1, src1, 5, 5);
	if (result != OK)
		return result;
	result = compare_utf8_to_utf16_with_nuls  (str2, src2, 4, 2);
	if (result != OK)
		return result;
	result = compare_utf8_to_utf16_with_nuls  (str3, src3, 3, 1);
	if (result != OK)
		return result;
	result = compare_utf8_to_utf16_with_nuls  (str4, src4, 3, 1);
	if (result != OK)
		return result;

	return OK;
}

typedef struct {
	char *content;
	size_t length;
} convert_result_t;

RESULT
test_convert ()
{
	static const char *charsets[] = { "UTF-8", "UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE" };
	gsize length, converted_length, n;
	char *content, *converted, *path;
	convert_result_t **expected;
	GError *err = NULL;
	const char *srcdir;
	gboolean loaded;
	guint i, j, k;
	char c;
	
	if (!(srcdir = getenv ("srcdir")) && !(srcdir = getenv ("PWD")))
		return FAILED ("srcdir not defined!");
	
	expected = g_malloc (sizeof (convert_result_t *) * G_N_ELEMENTS (charsets));
	
	/* first load all our test samples... */
	for (i = 0; i < G_N_ELEMENTS (charsets); i++) {
		path = g_strdup_printf ("%s%c%s.txt", srcdir, G_DIR_SEPARATOR, charsets[i]);
		loaded = g_file_get_contents (path, &content, &length, &err);
		g_free (path);
		
		if (!loaded) {
			for (j = 0; j < i; j++) {
				g_free (expected[j]->content);
				g_free (expected[j]);
			}
			
			g_free (expected);
			
			return FAILED ("Failed to load content for %s: %s", charsets[i], err->message);
		}
		
		expected[i] = g_malloc (sizeof (convert_result_t));
		expected[i]->content = content;
		expected[i]->length = length;
	}
	
	/* test conversion from every charset to every other charset */
	for (i = 0; i < G_N_ELEMENTS (charsets); i++) {
		for (j = 0; j < G_N_ELEMENTS (charsets); j++) {
			converted = g_convert (expected[i]->content, expected[i]->length, charsets[j],
					       charsets[i], NULL, &converted_length, NULL);
			
			if (converted == NULL) {
				for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
					g_free (expected[k]->content);
					g_free (expected[k]);
				}
				
				g_free (expected);
				
				return FAILED ("Failed to convert from %s to %s: NULL", charsets[i], charsets[j]);
			}
			
			if (converted_length != expected[j]->length) {
				length = expected[j]->length;
				
				for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
					g_free (expected[k]->content);
					g_free (expected[k]);
				}
				
				g_free (converted);
				g_free (expected);
				
				return FAILED ("Failed to convert from %s to %s: expected %u bytes, got %u",
					       charsets[i], charsets[j], length, converted_length);
			}
			
			for (n = 0; n < converted_length; n++) {
				if (converted[n] != expected[j]->content[n]) {
					c = expected[j]->content[n];
					
					for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
						g_free (expected[k]->content);
						g_free (expected[k]);
					}
					
					g_free (converted);
					g_free (expected);
					
					return FAILED ("Failed to convert from %s to %s: expected 0x%x at offset %u, got 0x%x",
						       charsets[i], charsets[j], c, n, converted[n]);
				}
			}
			
			g_free (converted);
		}
	}
	
	for (k = 0; k < G_N_ELEMENTS (charsets); k++) {
		g_free (expected[k]->content);
		g_free (expected[k]);
	}
	
	g_free (expected);
	
	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;
}

static RESULT
ucs4_to_utf16_check_result (const gunichar2 *result_str, const gunichar2 *expected_str,
			    glong result_items_read, glong expected_items_read,
			    glong result_items_written, glong expected_items_written,
			    GError* result_error, gboolean expect_error)
{
	glong i;
	if (result_items_read != expected_items_read)
		return FAILED("Incorrect number of items read; expected %d, got %d", expected_items_read, result_items_read);
	if (result_items_written != expected_items_written)
		return FAILED("Incorrect number of items written; expected %d, got %d", expected_items_written, result_items_written);
	if (result_error && !expect_error)
		return FAILED("There should not be an error code.");
	if (!result_error && expect_error)
		return FAILED("Unexpected error object.");
	if (expect_error && result_str)
		return FAILED("NULL should be returned when an error occurs.");
	if (!expect_error && !result_str)
		return FAILED("When no error occurs NULL should not be returned.");
	for (i=0; i<expected_items_written;i++) {
		if (result_str [i] != expected_str [i])
			return FAILED("Incorrect value %d at index %d", result_str [i], i);
	}
	if (result_str && result_str[expected_items_written] != '\0') 
		return FAILED("Null termination not found at the end of the string.");
	
	return OK;
}

RESULT
test_ucs4_to_utf16 ()
{
	static gunichar str1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
	static gunichar2 exp1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
	static gunichar str2[3] = {'h',0x80000000,'\0'};
	static gunichar2 exp2[2] = {'h','\0'};
	static gunichar str3[3] = {'h',0xDA00,'\0'};
	static gunichar str4[3] = {'h',0x10FFFF,'\0'};
	static gunichar2 exp4[4] = {'h',0xdbff,0xdfff,'\0'};
	static gunichar str5[7] = {0xD7FF,0xD800,0xDFFF,0xE000,0x110000,0x10FFFF,'\0'};
	static gunichar2 exp5[5] = {0xD7FF,0xE000,0xdbff,0xdfff,'\0'};
	static gunichar str6[2] = {0x10400, '\0'};
	static gunichar2 exp6[3] = {0xD801, 0xDC00, '\0'};
	static glong read_write[12] = {1,1,0,0,0,0,1,1,0,0,1,2};
	gunichar2* res;
	glong items_read, items_written, current_write_index;
	GError* err=0;
	RESULT check_result;
	glong i;
	
	res = g_ucs4_to_utf16 (str1, 12, &items_read, &items_written, &err);
	check_result = ucs4_to_utf16_check_result (res, exp1, items_read, 11, items_written, 11, err, FALSE);
	if (check_result) return check_result;
	g_free (res);

	items_read = items_written = 0;
	res = g_ucs4_to_utf16 (str2, 0, &items_read, &items_written, &err);
	check_result = ucs4_to_utf16_check_result (res, exp2, items_read, 0, items_written, 0, err, FALSE);
	if (check_result) return check_result;
	g_free (res);

	items_read = items_written = 0;
	res = g_ucs4_to_utf16 (str2, 1, &items_read, &items_written, &err);
	check_result = ucs4_to_utf16_check_result (res, exp2, items_read, 1, items_written, 1, err, FALSE);
	if (check_result) return check_result;
	g_free (res);

	items_read = items_written = 0;
	res = g_ucs4_to_utf16 (str2, 2, &items_read, &items_written, &err);
	check_result = ucs4_to_utf16_check_result (res, 0, items_read, 1, items_written, 0, err, TRUE);
	g_free (res);
	if (check_result) return check_result;

	items_read = items_written = 0;
	err = 0;
	res = g_ucs4_to_utf16 (str3, 2, &items_read, &items_written, &err);
	check_result = ucs4_to_utf16_check_result (res, 0, items_read, 1, items_written, 0, err, TRUE);
	if (check_result) return check_result;
	g_free (res);

	items_read = items_written = 0;
	err = 0;
	res = g_ucs4_to_utf16 (str4, 5, &items_read, &items_written, &err);
	check_result = ucs4_to_utf16_check_result (res, exp4, items_read, 2, items_written, 3, err, FALSE);
	if (check_result) return check_result;
	g_free (res);

	// This loop tests the bounds of the conversion algorithm
	current_write_index = 0;
	for (i=0;i<6;i++) {
		items_read = items_written = 0;
		err = 0;
		res = g_ucs4_to_utf16 (&str5[i], 1, &items_read, &items_written, &err);
		check_result = ucs4_to_utf16_check_result (res, &exp5[current_write_index], 
					items_read, read_write[i*2], items_written, read_write[(i*2)+1], err, !read_write[(i*2)+1]);
		if (check_result) return check_result;
		g_free (res);
		current_write_index += items_written;
	}

	items_read = items_written = 0;
	err = 0;
	res = g_ucs4_to_utf16 (str6, 1, &items_read, &items_written, &err);
	check_result = ucs4_to_utf16_check_result (res, exp6, items_read, 1, items_written, 2, err, FALSE);
	if (check_result) return check_result;
	g_free (res);

	return OK;
}

static RESULT
utf16_to_ucs4_check_result (const gunichar *result_str, const gunichar *expected_str,
			    glong result_items_read, glong expected_items_read,
			    glong result_items_written, glong expected_items_written,
			    GError* result_error, gboolean expect_error)
{
	glong i;
	if (result_items_read != expected_items_read)
		return FAILED("Incorrect number of items read; expected %d, got %d", expected_items_read, result_items_read);
	if (result_items_written != expected_items_written)
		return FAILED("Incorrect number of items written; expected %d, got %d", expected_items_written, result_items_written);
	if (result_error && !expect_error)
		return FAILED("There should not be an error code.");
	if (!result_error && expect_error)
		return FAILED("Unexpected error object.");
	if (expect_error && result_str)
		return FAILED("NULL should be returned when an error occurs.");
	if (!expect_error && !result_str)
		return FAILED("When no error occurs NULL should not be returned.");
	for (i=0; i<expected_items_written;i++) {
		if (result_str [i] != expected_str [i])
			return FAILED("Incorrect value %d at index %d", result_str [i], i);
	}
	if (result_str && result_str[expected_items_written] != '\0') 
		return FAILED("Null termination not found at the end of the string.");
	
	return OK;
}

RESULT
test_utf16_to_ucs4 ()
{
	static gunichar2 str1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
	static gunichar exp1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
	static gunichar2 str2[7] = {'H', 0xD800, 0xDC01,0xD800,0xDBFF,'l','\0'};
	static gunichar exp2[3] = {'H',0x00010001,'\0'};
	static gunichar2 str3[4] = {'H', 0xDC00 ,'l','\0'};
	static gunichar exp3[2] = {'H','\0'};
	static gunichar2 str4[20] = {0xDC00,0xDFFF,0xDFF,0xD800,0xDBFF,0xD800,0xDC00,0xD800,0xDFFF,
				     0xD800,0xE000,0xDBFF,0xDBFF,0xDBFF,0xDC00,0xDBFF,0xDFFF,0xDBFF,0xE000,'\0'};
	static gunichar exp4[6] = {0xDFF,0x10000,0x103ff,0x10fc00,0x10FFFF,'\0'};
	static gunichar2 str5[3] = {0xD801, 0xDC00, 0};
	static gunichar exp5[2] = {0x10400, 0};
	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};
	gunichar* res;
	glong items_read, items_written, current_read_index,current_write_index;
	GError* err=0;
	RESULT check_result;
	glong i;
	
	res = g_utf16_to_ucs4 (str1, 12, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp1, items_read, 11, items_written, 11, err, FALSE);
	if (check_result) return check_result;
	g_free (res);
	
	items_read = items_written = 0;
	res = g_utf16_to_ucs4 (str2, 0, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 0, items_written, 0, err, FALSE);
	if (check_result) return check_result;
	g_free (res);
	
	items_read = items_written = 0;
	res = g_utf16_to_ucs4 (str2, 1, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 1, items_written, 1, err, FALSE);
	if (check_result) return check_result;
	g_free (res);
	
	items_read = items_written = 0;
	res = g_utf16_to_ucs4 (str2, 2, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 1, items_written, 1, err, FALSE);
	if (check_result) return check_result;
	g_free (res);
	
	items_read = items_written = 0;
	res = g_utf16_to_ucs4 (str2, 3, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 3, items_written, 2, err, FALSE);
	if (check_result) return check_result;
	g_free (res);
	
	items_read = items_written = 0;
	res = g_utf16_to_ucs4 (str2, 4, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 3, items_written, 2, err, FALSE);
	if (check_result) return check_result;
	g_free (res);
	
	items_read = items_written = 0;
	res = g_utf16_to_ucs4 (str2, 5, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp2, items_read, 4, items_written, 0, err, TRUE);
	if (check_result) return check_result;
	g_free (res);
	
	items_read = items_written = 0;
	err = 0;
	res = g_utf16_to_ucs4 (str3, 5, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp3, items_read, 1, items_written, 0, err, TRUE);
	if (check_result) return check_result;
	g_free (res);
	
	// This loop tests the bounds of the conversion algorithm
	current_read_index = current_write_index = 0;
	for (i=0;i<11;i++) {
		items_read = items_written = 0;
		err = 0;
		res = g_utf16_to_ucs4 (&str4[current_read_index], read_write[i*3], &items_read, &items_written, &err);
		check_result = utf16_to_ucs4_check_result (res, &exp4[current_write_index], items_read, 
					     read_write[(i*3)+1], items_written, read_write[(i*3)+2], err, 
					     !read_write[(i*3)+2]);
		if (check_result) return check_result;
		g_free (res);
		current_read_index += read_write[i*3];
		current_write_index += items_written;
	}

	items_read = items_written = 0;
	err = 0;
	res = g_utf16_to_ucs4 (str5, 2, &items_read, &items_written, &err);
	check_result = utf16_to_ucs4_check_result (res, exp5, items_read, 2, items_written, 1, err, FALSE);
	if (check_result) return check_result;
	g_free (res);

	return OK;
}
RESULT
test_utf8_strlen ()
{
	gchar word1 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'};//Valid, len = 5
	gchar word2 [] = {0xF1, 0x82, 0x82, 0x82,0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,'\0'};//Valid, len = 5
	gchar word3 [] = {'h','e',0xC2, 0x82,0x45,'\0'};										//Valid, len = 4
	gchar word4 [] = {0x62,0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,'\0'}; 					//Valid, len = 5
	
	glong len = 0;
	
	//Test word1
	len = g_utf8_strlen (word1,-1);
	if (len != 5)
		return FAILED ("Word1 expected length of 5, but was %i", len);
	//Do tests with different values for max parameter.
	len = g_utf8_strlen (word1,1);
	if (len != 0)
		return FAILED ("Word1, max = 1, expected length of 0, but was %i", len);
	len = g_utf8_strlen (word1,2);
	if (len != 1)
		return FAILED ("Word1, max = 1, expected length of 1, but was %i", len);
	len = g_utf8_strlen (word1,3);
	if (len != 2)
		return FAILED ("Word1, max = 2, expected length of 2, but was %i", len);
		
	//Test word2
	len = g_utf8_strlen (word2,-1);
	if (len != 5)
		return FAILED ("Word2 expected length of 5, but was %i", len);
		
	//Test word3
	len = g_utf8_strlen (word3,-1);
	if (len != 4)
		return FAILED ("Word3 expected length of 4, but was %i", len);
		
	//Test word4
	len = g_utf8_strlen (word4,-1);
	if (len != 5)
		return FAILED ("Word4 expected length of 5, but was %i", len);
		
	//Test null case
	len = g_utf8_strlen(NULL,0);
	if (len != 0)
		return FAILED ("Expected passing null to result in a length of 0");
	return OK;
}

RESULT
test_utf8_get_char()
{
	gchar word1 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'}; //Valid, len = 5

	gunichar value = g_utf8_get_char (&word1 [0]);
	if (value != 0x82UL)
		return FAILED ("Expected value of 0x82, but was %x", value);
	value = g_utf8_get_char (&word1 [2]);
	if (value != 0x45UL)
		return FAILED ("Expected value of 0x45, but was %x", value);
	value = g_utf8_get_char (&word1 [3]);
	if (value != 0x1043UL)
		return FAILED ("Expected value of 0x1043, but was %x", value);
	value = g_utf8_get_char (&word1 [6]);
	if (value != 0x58UL)
		return FAILED ("Expected value of 0x58, but was %x", value);
	value = g_utf8_get_char (&word1 [7]);
	if (value != 0x42082UL)
		return FAILED ("Expected value of 0x42082, but was %x", value);

	return OK;
}

RESULT
test_utf8_next_char()
{
	gchar word1 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'}; //Valid, len = 5
	gchar word2 [] = {0xF1, 0x82, 0x82, 0x82,0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,'\0'}; //Valid, len = 5
	gchar word1ExpectedValues [] = {0xC2, 0x45,0xE1, 0x58, 0xF1};
	gchar word2ExpectedValues [] = {0xF1, 0xC2, 0x45, 0xE1, 0x58};
	
	gchar* ptr = word1;
	gint count = 0;
	//Test word1
	while (*ptr != 0) {
		if (count > 4)
			return FAILED ("Word1 has gone past its expected length");
		if (*ptr != word1ExpectedValues[count])
			return FAILED ("Word1 has an incorrect next_char at index %i", count);
		ptr = g_utf8_next_char (ptr);
		count++;
	}
	
	//Test word2
	count = 0;
	ptr = word2;
	while (*ptr != 0) {
		if (count > 4)
			return FAILED ("Word2 has gone past its expected length");
		if (*ptr != word2ExpectedValues[count])
			return FAILED ("Word2 has an incorrect next_char at index %i", count);
		ptr = g_utf8_next_char (ptr);
		count++;
	}
	
	return OK;
}

RESULT
test_utf8_validate()
{
	gchar invalidWord1 [] = {0xC3, 0x82, 0xC1,0x90,'\0'}; //Invalid, 1nd oct Can't be 0xC0 or 0xC1
	gchar invalidWord2 [] = {0xC1, 0x89, 0x60, '\0'}; //Invalid, 1st oct can not be 0xC1
	gchar invalidWord3 [] = {0xC2, 0x45,0xE1, 0x81, 0x83,0x58,'\0'}; //Invalid, oct after 0xC2 must be > 0x80

	gchar validWord1 [] = {0xC2, 0x82, 0xC3,0xA0,'\0'}; //Valid
	gchar validWord2 [] = {0xC2, 0x82,0x45,0xE1, 0x81, 0x83,0x58,0xF1, 0x82, 0x82, 0x82,'\0'}; //Valid
	
	const gchar* end;
	gboolean retVal = g_utf8_validate (invalidWord1, -1, &end);
	if (retVal != FALSE)
		return FAILED ("Expected invalidWord1 to be invalid");
	if (end != &invalidWord1 [2])
		return FAILED ("Expected end parameter to be pointing to invalidWord1[2]");

	end = NULL;
	retVal = g_utf8_validate (invalidWord2, -1, &end);
	if (retVal != FALSE)
		return FAILED ("Expected invalidWord2 to be invalid");
	if (end != &invalidWord2 [0])
		return FAILED ("Expected end parameter to be pointing to invalidWord2[0]");

	end = NULL;
	retVal = g_utf8_validate (invalidWord3, -1, &end);
	if (retVal != FALSE)
		return FAILED ("Expected invalidWord3 to be invalid");
	if (end != &invalidWord3 [0])
		return FAILED ("Expected end parameter to be pointing to invalidWord3[1]");

	end = NULL;
	retVal = g_utf8_validate (validWord1, -1, &end);
	if (retVal != TRUE)
		return FAILED ("Expected validWord1 to be valid");
	if (end != &validWord1 [4])
		return FAILED ("Expected end parameter to be pointing to validWord1[4]");

	end = NULL;
	retVal = g_utf8_validate (validWord2, -1, &end);
	if (retVal != TRUE)
		return FAILED ("Expected validWord2 to be valid");
	if (end != &validWord2 [11])
		return FAILED ("Expected end parameter to be pointing to validWord2[11]");
	return OK;
}

glong
utf8_byteslen (const gchar *src)
{
	int i = 0;
	do {
		if (src [i] == '\0')
			return i;
		i++;
	} while (TRUE);
}

RESULT
test_utf8_strcase_each (const gchar *src, const gchar *expected, gboolean strup)
{
	gchar *tmp;
	glong len, len2;
	RESULT r;

	len = utf8_byteslen (src);
	tmp = strup ? g_utf8_strup (src, len) : g_utf8_strdown (src, len);
	len2 = utf8_byteslen (tmp);
	r = compare_strings_utf8_RESULT (expected, tmp, len < len2 ? len2 : len);
	g_free (tmp);
	return r;
}

RESULT
test_utf8_strup_each (const gchar *src, const gchar *expected)
{
	return test_utf8_strcase_each (src, expected, TRUE);
}

RESULT
test_utf8_strdown_each (const gchar *src, const gchar *expected)
{
	return test_utf8_strcase_each (src, expected, FALSE);
}

/*
 * g_utf8_strup
 */
RESULT
test_utf8_strup ()
{
	RESULT r;

	if ((r = test_utf8_strup_each ("aBc", "ABC")) != OK)
		return r;
	if ((r = test_utf8_strup_each ("x86-64", "X86-64")) != OK)
		return r;
	// U+3B1 U+392 -> U+391 U+392
	if ((r = test_utf8_strup_each ("\xCE\xB1\xCE\x92", "\xCE\x91\xCE\x92")) != OK)
		return r;
	// U+FF21 -> U+FF21
	if ((r = test_utf8_strup_each ("\xEF\xBC\xA1", "\xEF\xBC\xA1")) != OK)
		return r;
	// U+FF41 -> U+FF21
	if ((r = test_utf8_strup_each ("\xEF\xBD\x81", "\xEF\xBC\xA1")) != OK)
		return r;
	// U+10428 -> U+10400
	if ((r = test_utf8_strup_each ("\xF0\x90\x90\xA8", "\xF0\x90\x90\x80")) != OK)
		return r;

	return OK;
}

/*
 * g_utf8_strdown
 */
RESULT
test_utf8_strdown ()
{
	RESULT r;

	if ((r = test_utf8_strdown_each ("aBc", "abc")) != OK)
		return r;
	if ((r = test_utf8_strdown_each ("X86-64", "x86-64")) != OK)
		return r;
	// U+391 U+3B2 -> U+3B1 U+3B2
	if ((r = test_utf8_strdown_each ("\xCE\x91\xCE\xB2", "\xCE\xB1\xCE\xB2")) != OK)
		return r;
/*
	// U+FF41 -> U+FF41
	if ((r = test_utf8_strdown_each ("\xEF\xBC\x81", "\xEF\xBC\x81")) != OK)
		return r;
	// U+FF21 -> U+FF41
	if ((r = test_utf8_strdown_each ("\xEF\xBC\xA1", "\xEF\xBD\x81")) != OK)
		return r;
	// U+10400 -> U+10428
	if ((r = test_utf8_strdown_each ("\xF0\x90\x90\x80", "\xF0\x90\x90\xA8")) != OK)
		return r;
*/
	return OK;
}

/*
 * test initialization
 */

static Test utf8_tests [] = {
	{"g_utf16_to_utf8", test_utf16_to_utf8},
	{"g_utf8_to_utf16", test_utf8_to_utf16},
	{"g_utf8_to_utf16_with_nuls", test_utf8_to_utf16_with_nuls},
	{"g_utf8_seq", test_utf8_seq},
	{"g_convert", test_convert },
	{"g_unichar_xdigit_value", test_xdigit },
	{"g_ucs4_to_utf16", test_ucs4_to_utf16 },
	{"g_utf16_to_ucs4", test_utf16_to_ucs4 },
	{"g_utf8_strlen", test_utf8_strlen },
	{"g_utf8_get_char", test_utf8_get_char },
	{"g_utf8_next_char", test_utf8_next_char },
	{"g_utf8_validate", test_utf8_validate },
	{"g_utf8_strup", test_utf8_strup},
	{"g_utf8_strdown", test_utf8_strdown},
	{NULL, NULL}
};

DEFINE_TEST_GROUP_INIT(utf8_tests_init, utf8_tests)