First public contribution.
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #undef G_DISABLE_ASSERT
35 #include "mrt2_glib2_test.h"
40 test_conversion (void)
42 gchar *in = "\xa5\xa6\xa7\xa8";
43 gchar *expected = "\xc2\xa5\xc2\xa6\xc2\xa7\xc2\xa8";
46 gsize bytes_written = 0;
49 out = g_convert (in, -1, "UTF-8", "ISO-8859-1",
50 &bytes_read, &bytes_written, &error);
52 g_assert (error == NULL);
53 g_assert (bytes_read == 4);
54 g_assert (bytes_written == 8);
55 g_assert (strcmp (out, expected) == 0);
65 test_iconv_state (void)
67 gchar *in = "\xf4\xe5\xf8\xe5\xed";
68 gchar *expected = "\xd7\xa4\xd7\x95\xd7\xa8\xd7\x95\xd7\x9d";
71 gsize bytes_written = 0;
74 out = g_convert (in, -1, "UTF-8", "windows-1255",
75 &bytes_read, &bytes_written, &error);
77 g_assert (error == NULL);
78 g_assert (bytes_read == 5);
79 g_assert (bytes_written == 10);
80 g_assert (strcmp (out, expected) == 0);
84 /* some tests involving "vulgar fraction one half" */
88 gchar *in = "\xc2\xbd";
91 gsize bytes_written = 0;
94 out = g_convert (in, -1,
95 "ISO-8859-1", "UTF-8",
96 &bytes_read, &bytes_written,
99 g_assert (error == NULL);
100 g_assert (bytes_read == 2);
101 g_assert (bytes_written == 1);
102 g_assert (strcmp (out, "\xbd") == 0);
105 out = g_convert (in, -1,
106 "ISO-8859-15", "UTF-8",
107 &bytes_read, &bytes_written,
110 g_assert (error && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE);
111 g_assert (bytes_read == 0);
112 g_assert (bytes_written == 0);
113 g_assert (out == NULL);
114 g_clear_error (&error);
117 out = g_convert_with_fallback (in, -1,
118 "ISO-8859-15", "UTF-8",
120 &bytes_read, &bytes_written,
123 g_assert (error == NULL);
124 g_assert (bytes_read == 2);
125 g_assert (bytes_written == 1);
126 g_assert (strcmp (out, "a") == 0);
131 test_byte_order (void)
133 gchar in_be[2] = { 0x03, 0x93}; /* capital gamma */
134 gchar in_le[2] = { 0x93, 0x03};
135 gchar *expected = "\xce\x93";
137 gsize bytes_read = 0;
138 gsize bytes_written = 0;
139 GError *error = NULL;
141 out = g_convert (in_be, sizeof (in_be),
143 &bytes_read, &bytes_written,
146 g_assert (error == NULL);
147 g_assert (bytes_read == 2);
148 g_assert (bytes_written == 2);
149 g_assert (strcmp (out, expected) == 0);
152 out = g_convert (in_le, sizeof (in_le),
154 &bytes_read, &bytes_written,
157 g_assert (error == NULL);
158 g_assert (bytes_read == 2);
159 g_assert (bytes_written == 2);
160 g_assert (strcmp (out, expected) == 0);
165 check_utf8_to_ucs4 (const char *utf8,
167 const gunichar *ucs4,
171 gunichar *result, *result2, *result3;
172 glong items_read, items_read2;
173 glong items_written, items_written2;
174 GError *error, *error2, *error3;
179 /* check the fast conversion */
180 result = g_utf8_to_ucs4_fast (utf8, utf8_len, &items_written);
182 g_assert (items_written == ucs4_len);
184 for (i = 0; i <= items_written; i++)
185 g_assert (result[i] == ucs4[i]);
191 result = g_utf8_to_ucs4 (utf8, utf8_len, &items_read, &items_written, &error);
193 if (utf8_len == strlen (utf8))
195 /* check that len == -1 yields identical results */
197 result2 = g_utf8_to_ucs4 (utf8, -1, &items_read2, &items_written2, &error2);
198 g_assert (error || items_read2 == items_read);
199 g_assert (error || items_written2 == items_written2);
200 g_assert (!!result == !!result2);
201 g_assert (!!error == !!error2);
203 for (i = 0; i <= items_written; i++)
204 g_assert (result[i] == result2[i]);
208 g_error_free (error2);
212 result3 = g_utf8_to_ucs4 (utf8, utf8_len, NULL, NULL, &error3);
214 if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
216 g_assert (error == NULL);
217 g_assert (items_read == error_pos);
218 g_assert (items_written == ucs4_len);
220 for (i = 0; i <= items_written; i++)
221 g_assert (result[i] == ucs4[i]);
225 g_assert (error != NULL);
226 g_assert (result == NULL);
227 g_assert (items_read == error_pos);
228 g_error_free (error);
230 g_assert (error3 != NULL);
231 g_assert (result3 == NULL);
232 g_error_free (error3);
236 g_assert (error == NULL);
237 g_assert (items_read == utf8_len);
238 g_assert (items_written == ucs4_len);
240 for (i = 0; i <= items_written; i++)
241 g_assert (result[i] == ucs4[i]);
243 g_assert (error3 == NULL);
245 for (i = 0; i <= ucs4_len; i++)
246 g_assert (result3[i] == ucs4[i]);
254 check_ucs4_to_utf8 (const gunichar *ucs4,
260 gchar *result, *result2, *result3;
261 glong items_read, items_read2;
262 glong items_written, items_written2;
263 GError *error, *error2, *error3;
266 result = g_ucs4_to_utf8 (ucs4, ucs4_len, &items_read, &items_written, &error);
268 if (ucs4[ucs4_len] == 0)
270 /* check that len == -1 yields identical results */
272 result2 = g_ucs4_to_utf8 (ucs4, -1, &items_read2, &items_written2, &error2);
274 g_assert (error || items_read2 == items_read);
275 g_assert (error || items_written2 == items_written);
276 g_assert (!!result == !!result2);
277 g_assert (!!error == !!error2);
279 g_assert (strcmp (result, result2) == 0);
283 g_error_free (error2);
287 result3 = g_ucs4_to_utf8 (ucs4, ucs4_len, NULL, NULL, &error3);
291 g_assert (error != NULL);
292 g_assert (result == NULL);
293 g_assert (items_read == error_pos);
294 g_error_free (error);
296 g_assert (error3 != NULL);
297 g_assert (result3 == NULL);
298 g_error_free (error3);
302 g_assert (error == NULL);
303 g_assert (items_read == ucs4_len);
304 g_assert (items_written == utf8_len);
306 g_assert (strcmp (result, utf8) == 0);
308 g_assert (error3 == NULL);
310 g_assert (strcmp (result3, utf8) == 0);
318 check_utf8_to_utf16 (const char *utf8,
320 const gunichar2 *utf16,
324 gunichar2 *result, *result2, *result3;
325 glong items_read, items_read2;
326 glong items_written, items_written2;
327 GError *error, *error2, *error3;
331 result = g_utf8_to_utf16 (utf8, utf8_len, &items_read, &items_written, &error);
333 if (utf8_len == strlen (utf8))
335 /* check that len == -1 yields identical results */
337 result2 = g_utf8_to_utf16 (utf8, -1, &items_read2, &items_written2, &error2);
338 g_assert (error || items_read2 == items_read);
339 g_assert (error || items_written2 == items_written2);
340 g_assert (!!result == !!result2);
341 g_assert (!!error == !!error2);
343 for (i = 0; i <= items_written; i++)
344 g_assert (result[i] == result2[i]);
348 g_error_free (error2);
352 result3 = g_utf8_to_utf16 (utf8, utf8_len, NULL, NULL, &error3);
354 if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
356 g_assert (error == NULL);
357 g_assert (items_read == error_pos);
358 g_assert (items_written == utf16_len);
360 for (i = 0; i <= items_written; i++)
361 g_assert (result[i] == utf16[i]);
365 g_assert (error != NULL);
366 g_assert (result == NULL);
367 g_assert (items_read == error_pos);
368 g_error_free (error);
370 g_assert (error3 != NULL);
371 g_assert (result3 == NULL);
372 g_error_free (error3);
376 g_assert (error == NULL);
377 g_assert (items_read == utf8_len);
378 g_assert (items_written == utf16_len);
380 for (i = 0; i <= items_written; i++)
381 g_assert (result[i] == utf16[i]);
383 g_assert (error3 == NULL);
385 for (i = 0; i <= utf16_len; i++)
386 g_assert (result3[i] == utf16[i]);
394 check_utf16_to_utf8 (const gunichar2 *utf16,
400 gchar *result, *result2, *result3;
401 glong items_read, items_read2;
402 glong items_written, items_written2;
403 GError *error, *error2, *error3;
406 result = g_utf16_to_utf8 (utf16, utf16_len, &items_read, &items_written, &error);
407 if (utf16[utf16_len] == 0)
409 /* check that len == -1 yields identical results */
411 result2 = g_utf16_to_utf8 (utf16, -1, &items_read2, &items_written2, &error2);
413 g_assert (error || items_read2 == items_read);
414 g_assert (error || items_written2 == items_written);
415 g_assert (!!result == !!result2);
416 g_assert (!!error == !!error2);
418 g_assert (strcmp (result, result2) == 0);
422 g_error_free (error2);
426 result3 = g_utf16_to_utf8 (utf16, utf16_len, NULL, NULL, &error3);
428 if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
430 g_assert (error == NULL);
431 g_assert (items_read == error_pos);
432 g_assert (items_read + 1 == utf16_len);
433 g_assert (items_written == utf8_len);
435 g_assert (strcmp (result, utf8) == 0);
439 g_assert (error != NULL);
440 g_assert (result == NULL);
441 g_assert (items_read == error_pos);
442 g_error_free (error);
444 g_assert (error3 != NULL);
445 g_assert (result3 == NULL);
446 g_error_free (error3);
450 g_assert (error == NULL);
451 g_assert (items_read == utf16_len);
452 g_assert (items_written == utf8_len);
454 g_assert (strcmp (result, utf8) == 0);
456 g_assert (error3 == NULL);
458 g_assert (strcmp (result3, utf8) == 0);
466 check_ucs4_to_utf16 (const gunichar *ucs4,
468 const gunichar2 *utf16,
472 gunichar2 *result, *result2, *result3;
473 glong items_read, items_read2;
474 glong items_written, items_written2;
475 GError *error, *error2, *error3;
479 result = g_ucs4_to_utf16 (ucs4, ucs4_len, &items_read, &items_written, &error);
481 if (ucs4[ucs4_len] == 0)
483 /* check that len == -1 yields identical results */
485 result2 = g_ucs4_to_utf16 (ucs4, -1, &items_read2, &items_written2, &error2);
487 g_assert (error || items_read2 == items_read);
488 g_assert (error || items_written2 == items_written);
489 g_assert (!!result == !!result2);
490 g_assert (!!error == !!error2);
492 for (i = 0; i <= utf16_len; i++)
493 g_assert (result[i] == result2[i]);
497 g_error_free (error2);
501 result3 = g_ucs4_to_utf16 (ucs4, -1, NULL, NULL, &error3);
505 g_assert (error != NULL);
506 g_assert (result == NULL);
507 g_assert (items_read == error_pos);
508 g_error_free (error);
510 g_assert (error3 != NULL);
511 g_assert (result3 == NULL);
512 g_error_free (error3);
516 g_assert (error == NULL);
517 g_assert (items_read == ucs4_len);
518 g_assert (items_written == utf16_len);
520 for (i = 0; i <= utf16_len; i++)
521 g_assert (result[i] == utf16[i]);
523 g_assert (error3 == NULL);
525 for (i = 0; i <= utf16_len; i++)
526 g_assert (result3[i] == utf16[i]);
534 check_utf16_to_ucs4 (const gunichar2 *utf16,
536 const gunichar *ucs4,
540 gunichar *result, *result2, *result3;
541 glong items_read, items_read2;
542 glong items_written, items_written2;
543 GError *error, *error2, *error3;
547 result = g_utf16_to_ucs4 (utf16, utf16_len, &items_read, &items_written, &error);
548 if (utf16[utf16_len] == 0)
550 /* check that len == -1 yields identical results */
552 result2 = g_utf16_to_ucs4 (utf16, -1, &items_read2, &items_written2, &error2);
553 g_assert (error || items_read2 == items_read);
554 g_assert (error || items_written2 == items_written2);
555 g_assert (!!result == !!result2);
556 g_assert (!!error == !!error2);
558 for (i = 0; i <= items_written; i++)
559 g_assert (result[i] == result2[i]);
563 g_error_free (error2);
567 result3 = g_utf16_to_ucs4 (utf16, utf16_len, NULL, NULL, &error3);
569 if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
571 g_assert (error == NULL);
572 g_assert (items_read == error_pos);
573 g_assert (items_read + 1 == utf16_len);
574 g_assert (items_written == ucs4_len);
576 for (i = 0; i <= items_written; i++)
577 g_assert (result[i] == ucs4[i]);
581 g_assert (error != NULL);
582 g_assert (result == NULL);
583 g_assert (items_read == error_pos);
584 g_error_free (error);
586 g_assert (error3 != NULL);
587 g_assert (result3 == NULL);
588 g_error_free (error3);
592 g_assert (error == NULL);
593 g_assert (items_read == utf16_len);
594 g_assert (items_written == ucs4_len);
596 for (i = 0; i <= ucs4_len; i++)
597 g_assert (result[i] == ucs4[i]);
599 g_assert (error3 == NULL);
601 for (i = 0; i <= ucs4_len; i++)
602 g_assert (result3[i] == ucs4[i]);
610 test_unicode_conversions (void)
614 gunichar2 utf16[100];
617 ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0x63; ucs4[3] = 0;
618 utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0x63; utf16[3] = 0;
620 check_utf8_to_ucs4 (utf8, 3, ucs4, 3, 0);
621 check_ucs4_to_utf8 (ucs4, 3, utf8, 3, 0);
622 check_utf8_to_utf16 (utf8, 3, utf16, 3, 0);
623 check_utf16_to_utf8 (utf16, 3, utf8, 3, 0);
624 check_ucs4_to_utf16 (ucs4, 3, utf16, 3, 0);
625 check_utf16_to_ucs4 (utf16, 3, ucs4, 3, 0);
627 utf8 = "\316\261\316\262\316\263";
628 ucs4[0] = 0x03b1; ucs4[1] = 0x03b2; ucs4[2] = 0x03b3; ucs4[3] = 0;
629 utf16[0] = 0x03b1; utf16[1] = 0x03b2; utf16[2] = 0x03b3; utf16[3] = 0;
631 check_utf8_to_ucs4 (utf8, 6, ucs4, 3, 0);
632 check_ucs4_to_utf8 (ucs4, 3, utf8, 6, 0);
633 check_utf8_to_utf16 (utf8, 6, utf16, 3, 0);
634 check_utf16_to_utf8 (utf16, 3, utf8, 6, 0);
635 check_ucs4_to_utf16 (ucs4, 3, utf16, 3, 0);
636 check_utf16_to_ucs4 (utf16, 3, ucs4, 3, 0);
638 /* partial utf8 character */
640 ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0x63; ucs4[3] = 0;
641 utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0x63; utf16[3] = 0;
643 check_utf8_to_ucs4 (utf8, 4, ucs4, 3, 3);
644 check_utf8_to_utf16 (utf8, 4, utf16, 3, 3);
647 utf8 = "abc\316\316";
651 Some of the test cases are not executed below by putting them under SYMBIAN
652 flag. The reason is that the input is invalid therefore, the test cases are
653 supposed to fail. Eventhough there is nothing wrong with the library code the
654 test case will fail as it is test whether failure takes place or not.
655 Hence there is no point executing them as they will uncessary
656 reflect bad on the pass rate.
660 check_utf8_to_ucs4 (utf8, 5, ucs4, 0, 3);
661 check_utf8_to_utf16 (utf8, 5, utf16, 0, 3);
664 /* partial utf16 character */
666 ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0;
667 utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0xd801; utf16[3] = 0;
669 check_utf16_to_utf8 (utf16, 3, utf8, 2, 2);
670 check_utf16_to_ucs4 (utf16, 3, ucs4, 2, 2);
675 utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0xdc01; utf16[3] = 0;
678 check_utf16_to_utf8 (utf16, 3, utf8, 0, 2);
679 check_utf16_to_ucs4 (utf16, 3, ucs4, 0, 2);
684 ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0x80000000; ucs4[3] = 0;
688 check_ucs4_to_utf8 (ucs4, 3, utf8, 0, 2);
689 check_ucs4_to_utf16 (ucs4, 3, utf16, 0, 2);
694 main (int argc, char *argv[])
697 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
698 g_set_print_handler(mrtPrintHandler);
705 test_unicode_conversions ();
708 testResultXml("convert-test");
709 #endif /* EMULATOR */