1 /* Test of character set conversion with error handling and autodetection.
2 Copyright (C) 2007 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
24 #include "striconveha.h"
34 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
35 #define ASSERT(expr) if (!(expr)) abort ();
37 /* Magic number for detecting bounds violations. */
38 #define MAGIC 0x1983EFF1
41 new_offsets (size_t n)
43 size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
51 static enum iconv_ilseq_handler handlers[] =
52 { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
58 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
59 ISO-8859-2, and UTF-8. */
61 /* ------------------------- Test mem_iconveha() ------------------------- */
63 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
64 for (h = 0; h < SIZEOF (handlers); h++)
66 enum iconv_ilseq_handler handler = handlers[h];
67 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
68 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
69 for (o = 0; o < 2; o++)
71 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
74 int retval = mem_iconveha (input, strlen (input),
75 "ISO-8859-2", "ISO-8859-1",
80 ASSERT (length == strlen (expected));
81 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
84 for (i = 0; i < 37; i++)
85 ASSERT (offsets[i] == i);
86 ASSERT (offsets[37] == MAGIC);
93 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
94 for (h = 0; h < SIZEOF (handlers); h++)
96 enum iconv_ilseq_handler handler = handlers[h];
97 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
98 for (o = 0; o < 2; o++)
100 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
103 int retval = mem_iconveha (input, strlen (input),
104 "ISO-8859-2", "ISO-8859-1",
111 ASSERT (retval == -1 && errno == EILSEQ);
112 ASSERT (result == NULL);
116 case iconveh_question_mark:
118 static const char expected[] = "Rafa? Maszkowski";
119 ASSERT (retval == 0);
120 ASSERT (length == strlen (expected));
121 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
124 for (i = 0; i < 16; i++)
125 ASSERT (offsets[i] == i);
126 ASSERT (offsets[16] == MAGIC);
132 case iconveh_escape_sequence:
134 static const char expected[] = "Rafa\\u0142 Maszkowski";
135 ASSERT (retval == 0);
136 ASSERT (length == strlen (expected));
137 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
140 for (i = 0; i < 16; i++)
141 ASSERT (offsets[i] == (i < 5 ? i :
143 ASSERT (offsets[16] == MAGIC);
153 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
154 for (h = 0; h < SIZEOF (handlers); h++)
156 enum iconv_ilseq_handler handler = handlers[h];
157 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
158 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
159 for (o = 0; o < 2; o++)
161 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
164 int retval = mem_iconveha (input, strlen (input),
165 "ISO-8859-1", "UTF-8",
169 ASSERT (retval == 0);
170 ASSERT (length == strlen (expected));
171 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
174 for (i = 0; i < 37; i++)
175 ASSERT (offsets[i] == (i < 1 ? i :
179 ASSERT (offsets[37] == MAGIC);
186 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
187 for (h = 0; h < SIZEOF (handlers); h++)
189 enum iconv_ilseq_handler handler = handlers[h];
190 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
191 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
192 for (o = 0; o < 2; o++)
194 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
197 int retval = mem_iconveha (input, strlen (input),
198 "UTF-8", "ISO-8859-1",
202 ASSERT (retval == 0);
203 ASSERT (length == strlen (expected));
204 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
207 for (i = 0; i < 41; i++)
208 ASSERT (offsets[i] == (i < 1 ? i :
209 i == 1 ? (size_t)(-1) :
211 i == 13 ? (size_t)(-1) :
213 i == 20 ? (size_t)(-1) :
216 ASSERT (offsets[41] == MAGIC);
223 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
224 for (h = 0; h < SIZEOF (handlers); h++)
226 enum iconv_ilseq_handler handler = handlers[h];
227 static const char input[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
228 for (o = 0; o < 2; o++)
230 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
233 int retval = mem_iconveha (input, strlen (input),
234 "UTF-8", "ISO-8859-1",
241 ASSERT (retval == -1 && errno == EILSEQ);
242 ASSERT (result == NULL);
246 case iconveh_question_mark:
248 static const char expected[] = "Rafa? Maszkowski";
249 ASSERT (retval == 0);
250 ASSERT (length == strlen (expected));
251 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
254 for (i = 0; i < 17; i++)
255 ASSERT (offsets[i] == (i < 5 ? i :
256 i == 5 ? (size_t)(-1) :
258 ASSERT (offsets[17] == MAGIC);
264 case iconveh_escape_sequence:
266 static const char expected[] = "Rafa\\u0142 Maszkowski";
267 ASSERT (retval == 0);
268 ASSERT (length == strlen (expected));
269 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
272 for (i = 0; i < 17; i++)
273 ASSERT (offsets[i] == (i < 5 ? i :
274 i == 5 ? (size_t)(-1) :
276 ASSERT (offsets[17] == MAGIC);
286 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
287 for (h = 0; h < SIZEOF (handlers); h++)
289 enum iconv_ilseq_handler handler = handlers[h];
290 static const char input[] = "\342";
291 for (o = 0; o < 2; o++)
293 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
296 int retval = mem_iconveha (input, strlen (input),
297 "UTF-8", "ISO-8859-1",
301 ASSERT (retval == 0);
302 ASSERT (length == 0);
305 ASSERT (offsets[0] == 0);
306 ASSERT (offsets[1] == MAGIC);
314 /* Test conversions from autodetect_jp to UTF-8. */
315 for (h = 0; h < SIZEOF (handlers); h++)
317 enum iconv_ilseq_handler handler = handlers[h];
318 static const char input[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
319 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
320 for (o = 0; o < 2; o++)
322 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
325 int retval = mem_iconveha (input, strlen (input),
326 "autodetect_jp", "UTF-8",
330 ASSERT (retval == 0);
331 ASSERT (length == strlen (expected));
332 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
335 for (i = 0; i < 10; i++)
336 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
337 ASSERT (offsets[10] == MAGIC);
343 for (h = 0; h < SIZEOF (handlers); h++)
345 enum iconv_ilseq_handler handler = handlers[h];
346 static const char input[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
347 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
348 for (o = 0; o < 2; o++)
350 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
353 int retval = mem_iconveha (input, strlen (input),
354 "autodetect_jp", "UTF-8",
358 ASSERT (retval == 0);
359 ASSERT (length == strlen (expected));
360 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
363 for (i = 0; i < 10; i++)
364 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
365 ASSERT (offsets[10] == MAGIC);
371 for (h = 0; h < SIZEOF (handlers); h++)
373 enum iconv_ilseq_handler handler = handlers[h];
374 static const char input[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
375 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
376 for (o = 0; o < 2; o++)
378 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
381 int retval = mem_iconveha (input, strlen (input),
382 "autodetect_jp", "UTF-8",
386 ASSERT (retval == 0);
387 ASSERT (length == strlen (expected));
388 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
391 for (i = 0; i < 16; i++)
392 ASSERT (offsets[i] == (i == 0 ? 0 :
399 ASSERT (offsets[16] == MAGIC);
406 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 || _LIBICONV_VERSION >= 0x0105
407 /* Test conversion from UTF-8 to ISO-8859-1 with transliteration. */
408 for (h = 0; h < SIZEOF (handlers); h++)
410 enum iconv_ilseq_handler handler = handlers[h];
411 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
412 static const char expected[] = "Costs: 27 EUR";
413 for (o = 0; o < 2; o++)
415 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
418 int retval = mem_iconveha (input, strlen (input),
419 "UTF-8", "ISO-8859-1",
423 ASSERT (retval == 0);
424 ASSERT (length == strlen (expected));
425 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
428 for (i = 0; i < 13; i++)
429 ASSERT (offsets[i] == (i < 11 ? i : (size_t)(-1)));
430 ASSERT (offsets[13] == MAGIC);
438 /* ------------------------- Test str_iconveha() ------------------------- */
440 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
441 for (h = 0; h < SIZEOF (handlers); h++)
443 enum iconv_ilseq_handler handler = handlers[h];
444 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
445 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
446 char *result = str_iconveha (input, "ISO-8859-2", "ISO-8859-1", false, handler);
447 ASSERT (result != NULL);
448 ASSERT (strcmp (result, expected) == 0);
452 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
453 for (h = 0; h < SIZEOF (handlers); h++)
455 enum iconv_ilseq_handler handler = handlers[h];
456 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
457 char *result = str_iconveha (input, "ISO-8859-2", "ISO-8859-1", false, handler);
461 ASSERT (result == NULL && errno == EILSEQ);
463 case iconveh_question_mark:
465 static const char expected[] = "Rafa? Maszkowski";
466 ASSERT (result != NULL);
467 ASSERT (strcmp (result, expected) == 0);
471 case iconveh_escape_sequence:
473 static const char expected[] = "Rafa\\u0142 Maszkowski";
474 ASSERT (result != NULL);
475 ASSERT (strcmp (result, expected) == 0);
482 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
483 for (h = 0; h < SIZEOF (handlers); h++)
485 enum iconv_ilseq_handler handler = handlers[h];
486 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
487 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
488 char *result = str_iconveha (input, "ISO-8859-1", "UTF-8", false, handler);
489 ASSERT (result != NULL);
490 ASSERT (strcmp (result, expected) == 0);
494 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
495 for (h = 0; h < SIZEOF (handlers); h++)
497 enum iconv_ilseq_handler handler = handlers[h];
498 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
499 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
500 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", false, handler);
501 ASSERT (result != NULL);
502 ASSERT (strcmp (result, expected) == 0);
506 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
507 for (h = 0; h < SIZEOF (handlers); h++)
509 enum iconv_ilseq_handler handler = handlers[h];
510 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
511 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", false, handler);
515 ASSERT (result == NULL && errno == EILSEQ);
517 case iconveh_question_mark:
519 static const char expected[] = "Costs: 27 ?";
520 ASSERT (result != NULL);
521 ASSERT (strcmp (result, expected) == 0);
525 case iconveh_escape_sequence:
527 static const char expected[] = "Costs: 27 \\u20AC";
528 ASSERT (result != NULL);
529 ASSERT (strcmp (result, expected) == 0);
536 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
537 for (h = 0; h < SIZEOF (handlers); h++)
539 enum iconv_ilseq_handler handler = handlers[h];
540 static const char input[] = "\342";
541 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", false, handler);
542 ASSERT (result != NULL);
543 ASSERT (strcmp (result, "") == 0);
547 /* Test conversions from autodetect_jp to UTF-8. */
548 for (h = 0; h < SIZEOF (handlers); h++)
550 enum iconv_ilseq_handler handler = handlers[h];
551 static const char input[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
552 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
553 char *result = str_iconveha (input, "autodetect_jp", "UTF-8", false, handler);
554 ASSERT (result != NULL);
555 ASSERT (strcmp (result, expected) == 0);
558 for (h = 0; h < SIZEOF (handlers); h++)
560 enum iconv_ilseq_handler handler = handlers[h];
561 static const char input[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
562 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
563 char *result = str_iconveha (input, "autodetect_jp", "UTF-8", false, handler);
564 ASSERT (result != NULL);
565 ASSERT (strcmp (result, expected) == 0);
568 for (h = 0; h < SIZEOF (handlers); h++)
570 enum iconv_ilseq_handler handler = handlers[h];
571 static const char input[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
572 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
573 char *result = str_iconveha (input, "autodetect_jp", "UTF-8", false, handler);
574 ASSERT (result != NULL);
575 ASSERT (strcmp (result, expected) == 0);
579 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 || _LIBICONV_VERSION >= 0x0105
580 /* Test conversion from UTF-8 to ISO-8859-1 with transliteration. */
581 for (h = 0; h < SIZEOF (handlers); h++)
583 enum iconv_ilseq_handler handler = handlers[h];
584 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
585 static const char expected[] = "Costs: 27 EUR";
586 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", true, handler);
587 ASSERT (result != NULL);
588 ASSERT (strcmp (result, expected) == 0);