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 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 "striconveha.h"
32 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44 /* Magic number for detecting bounds violations. */
45 #define MAGIC 0x1983EFF1
48 new_offsets (size_t n)
50 size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
58 static enum iconv_ilseq_handler handlers[] =
59 { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
65 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
66 ISO-8859-2, and UTF-8. */
68 /* ------------------------- Test mem_iconveha() ------------------------- */
70 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
71 for (h = 0; h < SIZEOF (handlers); h++)
73 enum iconv_ilseq_handler handler = handlers[h];
74 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
75 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
76 for (o = 0; o < 2; o++)
78 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
81 int retval = mem_iconveha (input, strlen (input),
82 "ISO-8859-2", "ISO-8859-1",
87 ASSERT (length == strlen (expected));
88 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
91 for (i = 0; i < 37; i++)
92 ASSERT (offsets[i] == i);
93 ASSERT (offsets[37] == MAGIC);
100 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
101 for (h = 0; h < SIZEOF (handlers); h++)
103 enum iconv_ilseq_handler handler = handlers[h];
104 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
105 for (o = 0; o < 2; o++)
107 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
110 int retval = mem_iconveha (input, strlen (input),
111 "ISO-8859-2", "ISO-8859-1",
118 ASSERT (retval == -1 && errno == EILSEQ);
119 ASSERT (result == NULL);
123 case iconveh_question_mark:
125 static const char expected[] = "Rafa? Maszkowski";
126 ASSERT (retval == 0);
127 ASSERT (length == strlen (expected));
128 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
131 for (i = 0; i < 16; i++)
132 ASSERT (offsets[i] == i);
133 ASSERT (offsets[16] == MAGIC);
139 case iconveh_escape_sequence:
141 static const char expected[] = "Rafa\\u0142 Maszkowski";
142 ASSERT (retval == 0);
143 ASSERT (length == strlen (expected));
144 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
147 for (i = 0; i < 16; i++)
148 ASSERT (offsets[i] == (i < 5 ? i :
150 ASSERT (offsets[16] == MAGIC);
160 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
161 for (h = 0; h < SIZEOF (handlers); h++)
163 enum iconv_ilseq_handler handler = handlers[h];
164 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
165 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
166 for (o = 0; o < 2; o++)
168 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
171 int retval = mem_iconveha (input, strlen (input),
172 "ISO-8859-1", "UTF-8",
176 ASSERT (retval == 0);
177 ASSERT (length == strlen (expected));
178 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
181 for (i = 0; i < 37; i++)
182 ASSERT (offsets[i] == (i < 1 ? i :
186 ASSERT (offsets[37] == MAGIC);
193 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
194 for (h = 0; h < SIZEOF (handlers); h++)
196 enum iconv_ilseq_handler handler = handlers[h];
197 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
198 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
199 for (o = 0; o < 2; o++)
201 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
204 int retval = mem_iconveha (input, strlen (input),
205 "UTF-8", "ISO-8859-1",
209 ASSERT (retval == 0);
210 ASSERT (length == strlen (expected));
211 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
214 for (i = 0; i < 41; i++)
215 ASSERT (offsets[i] == (i < 1 ? i :
216 i == 1 ? (size_t)(-1) :
218 i == 13 ? (size_t)(-1) :
220 i == 20 ? (size_t)(-1) :
223 ASSERT (offsets[41] == MAGIC);
230 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
231 for (h = 0; h < SIZEOF (handlers); h++)
233 enum iconv_ilseq_handler handler = handlers[h];
234 static const char input[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
235 for (o = 0; o < 2; o++)
237 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
240 int retval = mem_iconveha (input, strlen (input),
241 "UTF-8", "ISO-8859-1",
248 ASSERT (retval == -1 && errno == EILSEQ);
249 ASSERT (result == NULL);
253 case iconveh_question_mark:
255 static const char expected[] = "Rafa? Maszkowski";
256 ASSERT (retval == 0);
257 ASSERT (length == strlen (expected));
258 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
261 for (i = 0; i < 17; i++)
262 ASSERT (offsets[i] == (i < 5 ? i :
263 i == 5 ? (size_t)(-1) :
265 ASSERT (offsets[17] == MAGIC);
271 case iconveh_escape_sequence:
273 static const char expected[] = "Rafa\\u0142 Maszkowski";
274 ASSERT (retval == 0);
275 ASSERT (length == strlen (expected));
276 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
279 for (i = 0; i < 17; i++)
280 ASSERT (offsets[i] == (i < 5 ? i :
281 i == 5 ? (size_t)(-1) :
283 ASSERT (offsets[17] == MAGIC);
293 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
294 for (h = 0; h < SIZEOF (handlers); h++)
296 enum iconv_ilseq_handler handler = handlers[h];
297 static const char input[] = "\342";
298 for (o = 0; o < 2; o++)
300 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
303 int retval = mem_iconveha (input, strlen (input),
304 "UTF-8", "ISO-8859-1",
308 ASSERT (retval == 0);
309 ASSERT (length == 0);
312 ASSERT (offsets[0] == 0);
313 ASSERT (offsets[1] == MAGIC);
320 /* autodetect_jp is only supported when iconv() support ISO-2022-JP-2. */
321 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__ || defined __sun)
322 /* Test conversions from autodetect_jp to UTF-8. */
323 for (h = 0; h < SIZEOF (handlers); h++)
325 enum iconv_ilseq_handler handler = handlers[h];
326 static const char input[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
327 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
328 for (o = 0; o < 2; o++)
330 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
333 int retval = mem_iconveha (input, strlen (input),
334 "autodetect_jp", "UTF-8",
338 ASSERT (retval == 0);
339 ASSERT (length == strlen (expected));
340 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
343 for (i = 0; i < 10; i++)
344 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
345 ASSERT (offsets[10] == MAGIC);
351 for (h = 0; h < SIZEOF (handlers); h++)
353 enum iconv_ilseq_handler handler = handlers[h];
354 static const char input[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
355 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
356 for (o = 0; o < 2; o++)
358 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
361 int retval = mem_iconveha (input, strlen (input),
362 "autodetect_jp", "UTF-8",
366 ASSERT (retval == 0);
367 ASSERT (length == strlen (expected));
368 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
371 for (i = 0; i < 10; i++)
372 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
373 ASSERT (offsets[10] == MAGIC);
379 for (h = 0; h < SIZEOF (handlers); h++)
381 enum iconv_ilseq_handler handler = handlers[h];
382 static const char input[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
383 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
384 for (o = 0; o < 2; o++)
386 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
389 int retval = mem_iconveha (input, strlen (input),
390 "autodetect_jp", "UTF-8",
394 ASSERT (retval == 0);
395 ASSERT (length == strlen (expected));
396 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
399 for (i = 0; i < 16; i++)
400 ASSERT (offsets[i] == (i == 0 ? 0 :
407 ASSERT (offsets[16] == MAGIC);
415 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 || _LIBICONV_VERSION >= 0x0105
416 /* Test conversion from UTF-8 to ISO-8859-1 with transliteration. */
417 for (h = 0; h < SIZEOF (handlers); h++)
419 enum iconv_ilseq_handler handler = handlers[h];
420 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
421 static const char expected[] = "Costs: 27 EUR";
422 for (o = 0; o < 2; o++)
424 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
427 int retval = mem_iconveha (input, strlen (input),
428 "UTF-8", "ISO-8859-1",
432 ASSERT (retval == 0);
433 ASSERT (length == strlen (expected));
434 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
437 for (i = 0; i < 13; i++)
438 ASSERT (offsets[i] == (i < 11 ? i : (size_t)(-1)));
439 ASSERT (offsets[13] == MAGIC);
447 /* ------------------------- Test str_iconveha() ------------------------- */
449 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
450 for (h = 0; h < SIZEOF (handlers); h++)
452 enum iconv_ilseq_handler handler = handlers[h];
453 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
454 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
455 char *result = str_iconveha (input, "ISO-8859-2", "ISO-8859-1", false, handler);
456 ASSERT (result != NULL);
457 ASSERT (strcmp (result, expected) == 0);
461 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
462 for (h = 0; h < SIZEOF (handlers); h++)
464 enum iconv_ilseq_handler handler = handlers[h];
465 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
466 char *result = str_iconveha (input, "ISO-8859-2", "ISO-8859-1", false, handler);
470 ASSERT (result == NULL && errno == EILSEQ);
472 case iconveh_question_mark:
474 static const char expected[] = "Rafa? Maszkowski";
475 ASSERT (result != NULL);
476 ASSERT (strcmp (result, expected) == 0);
480 case iconveh_escape_sequence:
482 static const char expected[] = "Rafa\\u0142 Maszkowski";
483 ASSERT (result != NULL);
484 ASSERT (strcmp (result, expected) == 0);
491 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
492 for (h = 0; h < SIZEOF (handlers); h++)
494 enum iconv_ilseq_handler handler = handlers[h];
495 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
496 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
497 char *result = str_iconveha (input, "ISO-8859-1", "UTF-8", false, handler);
498 ASSERT (result != NULL);
499 ASSERT (strcmp (result, expected) == 0);
503 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
504 for (h = 0; h < SIZEOF (handlers); h++)
506 enum iconv_ilseq_handler handler = handlers[h];
507 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
508 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
509 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", false, handler);
510 ASSERT (result != NULL);
511 ASSERT (strcmp (result, expected) == 0);
515 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
516 for (h = 0; h < SIZEOF (handlers); h++)
518 enum iconv_ilseq_handler handler = handlers[h];
519 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
520 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", false, handler);
524 ASSERT (result == NULL && errno == EILSEQ);
526 case iconveh_question_mark:
528 static const char expected[] = "Costs: 27 ?";
529 ASSERT (result != NULL);
530 ASSERT (strcmp (result, expected) == 0);
534 case iconveh_escape_sequence:
536 static const char expected[] = "Costs: 27 \\u20AC";
537 ASSERT (result != NULL);
538 ASSERT (strcmp (result, expected) == 0);
545 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
546 for (h = 0; h < SIZEOF (handlers); h++)
548 enum iconv_ilseq_handler handler = handlers[h];
549 static const char input[] = "\342";
550 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", false, handler);
551 ASSERT (result != NULL);
552 ASSERT (strcmp (result, "") == 0);
556 /* autodetect_jp is only supported when iconv() support ISO-2022-JP-2. */
557 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__ || defined __sun)
558 /* Test conversions from autodetect_jp to UTF-8. */
559 for (h = 0; h < SIZEOF (handlers); h++)
561 enum iconv_ilseq_handler handler = handlers[h];
562 static const char input[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
563 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
564 char *result = str_iconveha (input, "autodetect_jp", "UTF-8", false, handler);
565 ASSERT (result != NULL);
566 ASSERT (strcmp (result, expected) == 0);
569 for (h = 0; h < SIZEOF (handlers); h++)
571 enum iconv_ilseq_handler handler = handlers[h];
572 static const char input[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
573 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
574 char *result = str_iconveha (input, "autodetect_jp", "UTF-8", false, handler);
575 ASSERT (result != NULL);
576 ASSERT (strcmp (result, expected) == 0);
579 for (h = 0; h < SIZEOF (handlers); h++)
581 enum iconv_ilseq_handler handler = handlers[h];
582 static const char input[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
583 static const char expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
584 char *result = str_iconveha (input, "autodetect_jp", "UTF-8", false, handler);
585 ASSERT (result != NULL);
586 ASSERT (strcmp (result, expected) == 0);
591 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 || _LIBICONV_VERSION >= 0x0105
592 /* Test conversion from UTF-8 to ISO-8859-1 with transliteration. */
593 for (h = 0; h < SIZEOF (handlers); h++)
595 enum iconv_ilseq_handler handler = handlers[h];
596 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
597 static const char expected[] = "Costs: 27 EUR";
598 char *result = str_iconveha (input, "UTF-8", "ISO-8859-1", true, handler);
599 ASSERT (result != NULL);
600 ASSERT (strcmp (result, expected) == 0);