8 compare_strings_utf8_pos (const gchar *expected, const gchar *actual, glong size)
11 for (i = 0; i < size; i++)
12 if (expected [i] != actual [i])
18 compare_strings_utf8_RESULT (const gchar *expected, const gchar *actual, glong size)
22 ret = compare_strings_utf8_pos (expected, actual, size);
25 return FAILED ("Incorrect output: expected '%s' but was '%s', differ at %d\n", expected, actual, ret);
29 gchar_to_gunichar2 (gunichar2 ret[], const gchar *src)
33 for (i = 0; src [i]; i++)
39 compare_utf16_to_utf8_explicit (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out, glong size_spec)
44 glong in_read, out_read;
49 ret = g_utf16_to_utf8 (utf16, size_spec, &in_read, &out_read, &error);
51 result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
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);
62 result = compare_strings_utf8_RESULT (expected, ret, len_out);
72 compare_utf16_to_utf8 (const gchar *expected, const gunichar2 *utf16, glong len_in, glong len_out)
76 result = compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, -1);
79 return compare_utf16_to_utf8_explicit (expected, utf16, len_in, len_out, len_in);
85 const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27";
86 gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0};
89 gchar_to_gunichar2 (str1, src1);
92 result = compare_utf16_to_utf8 (src0, str0, 0, 0);
96 result = compare_utf16_to_utf8 (src1, str1, 5, 5);
99 result = compare_utf16_to_utf8 (src2, str2, 2, 4);
111 compare_strings_utf16_pos (const gunichar2 *expected, const gunichar2 *actual, glong size)
114 for (i = 0; i < size; i++)
115 if (expected [i] != actual [i])
121 compare_strings_utf16_RESULT (const gunichar2 *expected, const gunichar2 *actual, glong size)
125 ret = compare_strings_utf16_pos (expected, actual, size);
128 return FAILED ("Incorrect output: expected '%s' but was '%s'\n", expected, actual);
132 compare_utf8_to_utf16_explicit (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out, glong size_spec)
137 glong in_read, out_read;
142 ret = g_utf8_to_utf16 (utf8, size_spec, &in_read, &out_read, &error);
144 result = FAILED ("The error is %d %s\n", (error)->code, (error)->message);
145 g_error_free (error);
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);
155 result = compare_strings_utf16_RESULT (expected, ret, len_out);
166 compare_utf8_to_utf16 (const gunichar2 *expected, const gchar *utf8, glong len_in, glong len_out)
170 result = compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, -1);
173 return compare_utf8_to_utf16_explicit (expected, utf8, len_in, len_out, len_in);
179 const gchar *src = "\xE5\xB9\xB4\x27";
180 glong in_read, out_read;
181 //gunichar2 expected [6];
182 GError *error = NULL;
185 printf ("got: %s\n", src);
186 dst = g_utf8_to_utf16 (src, (glong)strlen (src), &in_read, &out_read, &error);
188 return error->message;
192 return FAILED ("in_read is expected to be 4 but was %d\n", in_read);
195 return FAILED ("out_read is expected to be 2 but was %d\n", out_read);
202 test_utf8_to_utf16 ()
204 const gchar *src0 = "", *src1 = "ABCDE", *src2 = "\xE5\xB9\xB4\x27";
205 gunichar2 str0 [] = {0}, str1 [6], str2 [] = {0x5E74, 39, 0};
208 gchar_to_gunichar2 (str1, src1);
211 result = compare_utf8_to_utf16 (str0, src0, 0, 0);
215 result = compare_utf8_to_utf16 (str1, src1, 5, 5);
218 result = compare_utf8_to_utf16 (str2, src2, 4, 2);
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;
233 return FAILED ("Expected 24 bytes, got: NULL");
235 if (strlen (s) != 24)
236 return FAILED ("Expected 24 bytes, got: %d", strlen (s));
238 if (u [1] != 162 || u [2] != 194 ||
239 u [3] != 161 || u [4] != 194 ||
240 u [5] != 163 || u [6] != 194)
241 return FAILED ("Incorrect conversion");
252 static char test_chars[] = {
253 '0', '1', '2', '3', '4',
254 '5', '6', '7', '8', '9',
255 'a', 'b', 'c', 'd', 'e', 'f', 'g',
256 'A', 'B', 'C', 'D', 'E', 'F', 'G'};
257 static gint32 test_values[] = {
260 10, 11, 12, 13, 14, 15, -1,
261 10, 11, 12, 13, 14, 15, -1};
265 for (i = 0; i < sizeof(test_chars); i++)
266 if (g_unichar_xdigit_value ((gunichar)test_chars[i]) != test_values[i])
267 return FAILED("Incorrect value %d at index %d", test_values[i], i);
273 * test initialization
276 static Test utf8_tests [] = {
277 {"g_utf16_to_utf8", test_utf16_to_utf8},
278 {"g_utf8_to_utf16", test_utf8_to_utf16},
279 {"g_utf8_seq", test_utf8_seq},
280 {"g_convert", test_convert },
281 {"g_unichar_xdigit_value", test_xdigit },
285 DEFINE_TEST_GROUP_INIT(utf8_tests_init, utf8_tests)