1 /* Test of character set conversion with error handling.
2 Copyright (C) 2007-2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
21 #include "striconveh.h"
32 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 /* Magic number for detecting bounds violations. */
46 #define MAGIC 0x1983EFF1
49 new_offsets (size_t n)
51 size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
59 static enum iconv_ilseq_handler handlers[] =
60 { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
67 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
68 ISO-8859-2, and UTF-8. */
69 iconv_t cd_88591_to_88592 = iconv_open ("ISO-8859-2", "ISO-8859-1");
70 iconv_t cd_88592_to_88591 = iconv_open ("ISO-8859-1", "ISO-8859-2");
71 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
72 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
73 iconv_t cd_88592_to_utf8 = iconv_open ("UTF-8", "ISO-8859-2");
74 iconv_t cd_utf8_to_88592 = iconv_open ("ISO-8859-2", "UTF-8");
75 iconv_t cd_utf7_to_utf8 = iconv_open ("UTF-8", "UTF-7");
76 iconveh_t cdeh_88592_to_88591;
77 iconveh_t cdeh_88592_to_88591_indirectly;
78 iconveh_t cdeh_88591_to_utf8;
79 iconveh_t cdeh_utf8_to_88591;
80 iconveh_t cdeh_utf7_to_utf8;
82 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
83 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
84 ASSERT (cd_88592_to_utf8 != (iconv_t)(-1));
85 ASSERT (cd_utf8_to_88592 != (iconv_t)(-1));
87 cdeh_88592_to_88591.cd = cd_88592_to_88591;
88 cdeh_88592_to_88591.cd1 = cd_88592_to_utf8;
89 cdeh_88592_to_88591.cd2 = cd_utf8_to_88591;
91 cdeh_88592_to_88591_indirectly.cd = (iconv_t)(-1);
92 cdeh_88592_to_88591_indirectly.cd1 = cd_88592_to_utf8;
93 cdeh_88592_to_88591_indirectly.cd2 = cd_utf8_to_88591;
95 cdeh_88591_to_utf8.cd = cd_88591_to_utf8;
96 cdeh_88591_to_utf8.cd1 = cd_88591_to_utf8;
97 cdeh_88591_to_utf8.cd2 = (iconv_t)(-1);
99 cdeh_utf8_to_88591.cd = cd_utf8_to_88591;
100 cdeh_utf8_to_88591.cd1 = (iconv_t)(-1);
101 cdeh_utf8_to_88591.cd2 = cd_utf8_to_88591;
103 cdeh_utf7_to_utf8.cd = cd_utf7_to_utf8;
104 cdeh_utf7_to_utf8.cd1 = cd_utf7_to_utf8;
105 cdeh_utf7_to_utf8.cd2 = (iconv_t)(-1);
107 /* ------------------------ Test mem_cd_iconveh() ------------------------ */
109 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
110 for (indirect = 0; indirect <= 1; indirect++)
112 for (h = 0; h < SIZEOF (handlers); h++)
114 enum iconv_ilseq_handler handler = handlers[h];
115 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
116 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
117 for (o = 0; o < 2; o++)
119 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
122 int retval = mem_cd_iconveh (input, strlen (input),
124 ? &cdeh_88592_to_88591_indirectly
125 : &cdeh_88592_to_88591),
129 ASSERT (retval == 0);
130 ASSERT (length == strlen (expected));
131 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
134 for (i = 0; i < 37; i++)
135 ASSERT (offsets[i] == i);
136 ASSERT (offsets[37] == MAGIC);
144 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
145 for (indirect = 0; indirect <= 1; indirect++)
147 for (h = 0; h < SIZEOF (handlers); h++)
149 enum iconv_ilseq_handler handler = handlers[h];
150 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
151 for (o = 0; o < 2; o++)
153 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
156 int retval = mem_cd_iconveh (input, strlen (input),
158 ? &cdeh_88592_to_88591_indirectly
159 : &cdeh_88592_to_88591),
166 ASSERT (retval == -1 && errno == EILSEQ);
167 ASSERT (result == NULL);
171 case iconveh_question_mark:
173 static const char expected[] = "Rafa? Maszkowski";
174 ASSERT (retval == 0);
175 ASSERT (length == strlen (expected));
176 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
179 for (i = 0; i < 16; i++)
180 ASSERT (offsets[i] == i);
181 ASSERT (offsets[16] == MAGIC);
187 case iconveh_escape_sequence:
189 static const char expected[] = "Rafa\\u0142 Maszkowski";
190 ASSERT (retval == 0);
191 ASSERT (length == strlen (expected));
192 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
195 for (i = 0; i < 16; i++)
196 ASSERT (offsets[i] == (i < 5 ? i :
198 ASSERT (offsets[16] == MAGIC);
209 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
210 for (h = 0; h < SIZEOF (handlers); h++)
212 enum iconv_ilseq_handler handler = handlers[h];
213 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
214 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
215 for (o = 0; o < 2; o++)
217 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
220 int retval = mem_cd_iconveh (input, strlen (input),
225 ASSERT (retval == 0);
226 ASSERT (length == strlen (expected));
227 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
230 for (i = 0; i < 37; i++)
231 ASSERT (offsets[i] == (i < 1 ? i :
235 ASSERT (offsets[37] == MAGIC);
242 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
243 for (h = 0; h < SIZEOF (handlers); h++)
245 enum iconv_ilseq_handler handler = handlers[h];
246 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
247 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
248 for (o = 0; o < 2; o++)
250 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
253 int retval = mem_cd_iconveh (input, strlen (input),
258 ASSERT (retval == 0);
259 ASSERT (length == strlen (expected));
260 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
263 for (i = 0; i < 41; i++)
264 ASSERT (offsets[i] == (i < 1 ? i :
265 i == 1 ? (size_t)(-1) :
267 i == 13 ? (size_t)(-1) :
269 i == 20 ? (size_t)(-1) :
272 ASSERT (offsets[41] == MAGIC);
279 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
280 for (h = 0; h < SIZEOF (handlers); h++)
282 enum iconv_ilseq_handler handler = handlers[h];
283 static const char input[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
284 for (o = 0; o < 2; o++)
286 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
289 int retval = mem_cd_iconveh (input, strlen (input),
297 ASSERT (retval == -1 && errno == EILSEQ);
298 ASSERT (result == NULL);
302 case iconveh_question_mark:
304 static const char expected[] = "Rafa? Maszkowski";
305 ASSERT (retval == 0);
306 ASSERT (length == strlen (expected));
307 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
310 for (i = 0; i < 17; i++)
311 ASSERT (offsets[i] == (i < 5 ? i :
312 i == 5 ? (size_t)(-1) :
314 ASSERT (offsets[17] == MAGIC);
320 case iconveh_escape_sequence:
322 static const char expected[] = "Rafa\\u0142 Maszkowski";
323 ASSERT (retval == 0);
324 ASSERT (length == strlen (expected));
325 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
328 for (i = 0; i < 17; i++)
329 ASSERT (offsets[i] == (i < 5 ? i :
330 i == 5 ? (size_t)(-1) :
332 ASSERT (offsets[17] == MAGIC);
342 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
343 for (h = 0; h < SIZEOF (handlers); h++)
345 enum iconv_ilseq_handler handler = handlers[h];
346 static const char input[] = "\342";
347 for (o = 0; o < 2; o++)
349 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
352 int retval = mem_cd_iconveh (input, strlen (input),
357 ASSERT (retval == 0);
358 ASSERT (length == 0);
361 ASSERT (offsets[0] == 0);
362 ASSERT (offsets[1] == MAGIC);
369 if (cd_utf7_to_utf8 != (iconv_t)(-1))
371 /* Disabled on Solaris, because Solaris 9 iconv() is buggy: it returns
372 -1 / EILSEQ when converting the 7th byte of the input "+VDLYP9hA". */
373 # if !(defined __sun && !defined _LIBICONV_VERSION)
374 /* Test conversion from UTF-7 to UTF-8 with EINVAL. */
375 for (h = 0; h < SIZEOF (handlers); h++)
377 enum iconv_ilseq_handler handler = handlers[h];
378 /* This is base64 encoded 0x54 0x32 0xD8 0x3F 0xD8 0x40. It would
379 convert to U+5432 U+D83F U+D840 but these are Unicode surrogates. */
380 static const char input[] = "+VDLYP9hA";
381 static const char expected1[] = "\345\220\262"; /* 吲 glibc */
382 static const char expected2[] = ""; /* libiconv */
385 int retval = mem_cd_iconveh (input, 7,
390 ASSERT (retval == 0);
391 ASSERT (length == strlen (expected1) || length == strlen (expected2));
392 ASSERT (result != NULL);
393 if (length == strlen (expected1))
394 ASSERT (memcmp (result, expected1, strlen (expected1)) == 0);
396 ASSERT (memcmp (result, expected2, strlen (expected2)) == 0);
400 /* Test conversion from UTF-7 to UTF-8 with EILSEQ. */
401 for (h = 0; h < SIZEOF (handlers); h++)
403 enum iconv_ilseq_handler handler = handlers[h];
404 /* This is base64 encoded 0xD8 0x3F 0xD8 0x40 0xD8 0x41. It would
405 convert to U+D83F U+D840 U+D841 but these are Unicode surrogates. */
406 static const char input[] = "+2D/YQNhB";
409 int retval = mem_cd_iconveh (input, strlen (input),
417 ASSERT (retval == -1 && errno == EILSEQ);
418 ASSERT (result == NULL);
420 case iconveh_question_mark:
421 case iconveh_escape_sequence:
424 static const char expected1[] = "?????";
425 /* libiconv <= 1.12 result */
426 static const char expected2[] = "?2D/YQNhB";
427 /* libiconv behaviour changed in version 1.13: the result is
428 '?' U+0FF6 U+1036; this is U+D83F U+D840 U+D841 shifted left
430 static const char expected3[] = "?\340\277\266\341\200\266";
431 ASSERT (retval == 0);
432 ASSERT (length == strlen (expected1)
433 || length == strlen (expected2)
434 || length == strlen (expected3));
435 ASSERT (result != NULL);
436 if (length == strlen (expected1))
437 ASSERT (memcmp (result, expected1, strlen (expected1)) == 0);
438 else if (length == strlen (expected2))
439 ASSERT (memcmp (result, expected2, strlen (expected2)) == 0);
441 ASSERT (memcmp (result, expected3, strlen (expected3)) == 0);
450 /* ------------------------ Test str_cd_iconveh() ------------------------ */
452 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
453 for (indirect = 0; indirect <= 1; indirect++)
455 for (h = 0; h < SIZEOF (handlers); h++)
457 enum iconv_ilseq_handler handler = handlers[h];
458 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
459 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
460 char *result = str_cd_iconveh (input,
462 ? &cdeh_88592_to_88591_indirectly
463 : &cdeh_88592_to_88591),
465 ASSERT (result != NULL);
466 ASSERT (strcmp (result, expected) == 0);
471 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
472 for (indirect = 0; indirect <= 1; indirect++)
474 for (h = 0; h < SIZEOF (handlers); h++)
476 enum iconv_ilseq_handler handler = handlers[h];
477 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
478 char *result = str_cd_iconveh (input,
480 ? &cdeh_88592_to_88591_indirectly
481 : &cdeh_88592_to_88591),
486 ASSERT (result == NULL && errno == EILSEQ);
488 case iconveh_question_mark:
490 static const char expected[] = "Rafa? Maszkowski";
491 ASSERT (result != NULL);
492 ASSERT (strcmp (result, expected) == 0);
496 case iconveh_escape_sequence:
498 static const char expected[] = "Rafa\\u0142 Maszkowski";
499 ASSERT (result != NULL);
500 ASSERT (strcmp (result, expected) == 0);
508 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
509 for (h = 0; h < SIZEOF (handlers); h++)
511 enum iconv_ilseq_handler handler = handlers[h];
512 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
513 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
514 char *result = str_cd_iconveh (input,
517 ASSERT (result != NULL);
518 ASSERT (strcmp (result, expected) == 0);
522 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
523 for (h = 0; h < SIZEOF (handlers); h++)
525 enum iconv_ilseq_handler handler = handlers[h];
526 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
527 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
528 char *result = str_cd_iconveh (input,
531 ASSERT (result != NULL);
532 ASSERT (strcmp (result, expected) == 0);
536 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
537 for (h = 0; h < SIZEOF (handlers); h++)
539 enum iconv_ilseq_handler handler = handlers[h];
540 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
541 char *result = str_cd_iconveh (input,
547 ASSERT (result == NULL && errno == EILSEQ);
549 case iconveh_question_mark:
551 static const char expected[] = "Costs: 27 ?";
552 ASSERT (result != NULL);
553 ASSERT (strcmp (result, expected) == 0);
557 case iconveh_escape_sequence:
559 static const char expected[] = "Costs: 27 \\u20AC";
560 ASSERT (result != NULL);
561 ASSERT (strcmp (result, expected) == 0);
568 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
569 for (h = 0; h < SIZEOF (handlers); h++)
571 enum iconv_ilseq_handler handler = handlers[h];
572 static const char input[] = "\342";
573 char *result = str_cd_iconveh (input,
576 ASSERT (result != NULL);
577 ASSERT (strcmp (result, "") == 0);
581 if (cd_88591_to_88592 != (iconv_t)(-1))
582 iconv_close (cd_88591_to_88592);
583 if (cd_88592_to_88591 != (iconv_t)(-1))
584 iconv_close (cd_88592_to_88591);
585 iconv_close (cd_88591_to_utf8);
586 iconv_close (cd_utf8_to_88591);
587 iconv_close (cd_88592_to_utf8);
588 iconv_close (cd_utf8_to_88592);
590 /* ------------------------- Test mem_iconveh() ------------------------- */
592 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
593 for (h = 0; h < SIZEOF (handlers); h++)
595 enum iconv_ilseq_handler handler = handlers[h];
596 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
597 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
598 for (o = 0; o < 2; o++)
600 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
603 int retval = mem_iconveh (input, strlen (input),
604 "ISO-8859-2", "ISO-8859-1",
608 ASSERT (retval == 0);
609 ASSERT (length == strlen (expected));
610 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
613 for (i = 0; i < 37; i++)
614 ASSERT (offsets[i] == i);
615 ASSERT (offsets[37] == MAGIC);
622 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
623 for (h = 0; h < SIZEOF (handlers); h++)
625 enum iconv_ilseq_handler handler = handlers[h];
626 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
627 for (o = 0; o < 2; o++)
629 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
632 int retval = mem_iconveh (input, strlen (input),
633 "ISO-8859-2", "ISO-8859-1",
640 ASSERT (retval == -1 && errno == EILSEQ);
641 ASSERT (result == NULL);
645 case iconveh_question_mark:
647 static const char expected[] = "Rafa? Maszkowski";
648 ASSERT (retval == 0);
649 ASSERT (length == strlen (expected));
650 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
653 for (i = 0; i < 16; i++)
654 ASSERT (offsets[i] == i);
655 ASSERT (offsets[16] == MAGIC);
661 case iconveh_escape_sequence:
663 static const char expected[] = "Rafa\\u0142 Maszkowski";
664 ASSERT (retval == 0);
665 ASSERT (length == strlen (expected));
666 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
669 for (i = 0; i < 16; i++)
670 ASSERT (offsets[i] == (i < 5 ? i :
672 ASSERT (offsets[16] == MAGIC);
682 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
683 for (h = 0; h < SIZEOF (handlers); h++)
685 enum iconv_ilseq_handler handler = handlers[h];
686 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
687 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
688 for (o = 0; o < 2; o++)
690 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
693 int retval = mem_iconveh (input, strlen (input),
694 "ISO-8859-1", "UTF-8",
698 ASSERT (retval == 0);
699 ASSERT (length == strlen (expected));
700 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
703 for (i = 0; i < 37; i++)
704 ASSERT (offsets[i] == (i < 1 ? i :
708 ASSERT (offsets[37] == MAGIC);
715 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
716 for (h = 0; h < SIZEOF (handlers); h++)
718 enum iconv_ilseq_handler handler = handlers[h];
719 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
720 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
721 for (o = 0; o < 2; o++)
723 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
726 int retval = mem_iconveh (input, strlen (input),
727 "UTF-8", "ISO-8859-1",
731 ASSERT (retval == 0);
732 ASSERT (length == strlen (expected));
733 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
736 for (i = 0; i < 41; i++)
737 ASSERT (offsets[i] == (i < 1 ? i :
738 i == 1 ? (size_t)(-1) :
740 i == 13 ? (size_t)(-1) :
742 i == 20 ? (size_t)(-1) :
745 ASSERT (offsets[41] == MAGIC);
752 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
753 for (h = 0; h < SIZEOF (handlers); h++)
755 enum iconv_ilseq_handler handler = handlers[h];
756 static const char input[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
757 for (o = 0; o < 2; o++)
759 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
762 int retval = mem_iconveh (input, strlen (input),
763 "UTF-8", "ISO-8859-1",
770 ASSERT (retval == -1 && errno == EILSEQ);
771 ASSERT (result == NULL);
775 case iconveh_question_mark:
777 static const char expected[] = "Rafa? Maszkowski";
778 ASSERT (retval == 0);
779 ASSERT (length == strlen (expected));
780 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
783 for (i = 0; i < 17; i++)
784 ASSERT (offsets[i] == (i < 5 ? i :
785 i == 5 ? (size_t)(-1) :
787 ASSERT (offsets[17] == MAGIC);
793 case iconveh_escape_sequence:
795 static const char expected[] = "Rafa\\u0142 Maszkowski";
796 ASSERT (retval == 0);
797 ASSERT (length == strlen (expected));
798 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
801 for (i = 0; i < 17; i++)
802 ASSERT (offsets[i] == (i < 5 ? i :
803 i == 5 ? (size_t)(-1) :
805 ASSERT (offsets[17] == MAGIC);
815 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
816 for (h = 0; h < SIZEOF (handlers); h++)
818 enum iconv_ilseq_handler handler = handlers[h];
819 static const char input[] = "\342";
820 for (o = 0; o < 2; o++)
822 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
825 int retval = mem_iconveh (input, strlen (input),
826 "UTF-8", "ISO-8859-1",
830 ASSERT (retval == 0);
831 ASSERT (length == 0);
834 ASSERT (offsets[0] == 0);
835 ASSERT (offsets[1] == MAGIC);
842 /* ------------------------- Test str_iconveh() ------------------------- */
844 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
845 for (h = 0; h < SIZEOF (handlers); h++)
847 enum iconv_ilseq_handler handler = handlers[h];
848 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
849 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
850 char *result = str_iconveh (input, "ISO-8859-2", "ISO-8859-1", handler);
851 ASSERT (result != NULL);
852 ASSERT (strcmp (result, expected) == 0);
856 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
857 for (h = 0; h < SIZEOF (handlers); h++)
859 enum iconv_ilseq_handler handler = handlers[h];
860 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
861 char *result = str_iconveh (input, "ISO-8859-2", "ISO-8859-1", handler);
865 ASSERT (result == NULL && errno == EILSEQ);
867 case iconveh_question_mark:
869 static const char expected[] = "Rafa? Maszkowski";
870 ASSERT (result != NULL);
871 ASSERT (strcmp (result, expected) == 0);
875 case iconveh_escape_sequence:
877 static const char expected[] = "Rafa\\u0142 Maszkowski";
878 ASSERT (result != NULL);
879 ASSERT (strcmp (result, expected) == 0);
886 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
887 for (h = 0; h < SIZEOF (handlers); h++)
889 enum iconv_ilseq_handler handler = handlers[h];
890 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
891 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
892 char *result = str_iconveh (input, "ISO-8859-1", "UTF-8", handler);
893 ASSERT (result != NULL);
894 ASSERT (strcmp (result, expected) == 0);
898 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
899 for (h = 0; h < SIZEOF (handlers); h++)
901 enum iconv_ilseq_handler handler = handlers[h];
902 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
903 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
904 char *result = str_iconveh (input, "UTF-8", "ISO-8859-1", handler);
905 ASSERT (result != NULL);
906 ASSERT (strcmp (result, expected) == 0);
910 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
911 for (h = 0; h < SIZEOF (handlers); h++)
913 enum iconv_ilseq_handler handler = handlers[h];
914 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
915 char *result = str_iconveh (input, "UTF-8", "ISO-8859-1", handler);
919 ASSERT (result == NULL && errno == EILSEQ);
921 case iconveh_question_mark:
923 static const char expected[] = "Costs: 27 ?";
924 ASSERT (result != NULL);
925 ASSERT (strcmp (result, expected) == 0);
929 case iconveh_escape_sequence:
931 static const char expected[] = "Costs: 27 \\u20AC";
932 ASSERT (result != NULL);
933 ASSERT (strcmp (result, expected) == 0);
940 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
941 for (h = 0; h < SIZEOF (handlers); h++)
943 enum iconv_ilseq_handler handler = handlers[h];
944 static const char input[] = "\342";
945 char *result = str_iconveh (input, "UTF-8", "ISO-8859-1", handler);
946 ASSERT (result != NULL);
947 ASSERT (strcmp (result, "") == 0);