1 /* Test of conversion to UTF-8 from legacy encodings.
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. */
30 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 /* Magic number for detecting bounds violations. */
43 #define MAGIC 0x1983EFF1
46 new_offsets (size_t n)
48 size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
56 static enum iconv_ilseq_handler handlers[] =
57 { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
63 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
64 ISO-8859-2, and UTF-8. */
66 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
67 for (h = 0; h < SIZEOF (handlers); h++)
69 enum iconv_ilseq_handler handler = handlers[h];
70 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
71 static const uint8_t expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
72 for (o = 0; o < 2; o++)
74 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
75 uint8_t *result = NULL;
77 int retval = u8_conv_from_encoding ("ISO-8859-1", handler,
78 input, strlen (input),
82 ASSERT (length == u8_strlen (expected));
83 ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
86 for (i = 0; i < 37; i++)
87 ASSERT (offsets[i] == (i < 1 ? i :
91 ASSERT (offsets[37] == MAGIC);
98 /* Test conversion from ISO-8859-2 to UTF-8 with no errors. */
99 for (h = 0; h < SIZEOF (handlers); h++)
101 enum iconv_ilseq_handler handler = handlers[h];
102 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
103 static const uint8_t expected[] = "Rafa\305\202 Maszkowski";
104 for (o = 0; o < 2; o++)
106 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
107 uint8_t *result = NULL;
109 int retval = u8_conv_from_encoding ("ISO-8859-2", handler,
110 input, strlen (input),
113 ASSERT (retval == 0);
114 ASSERT (length == u8_strlen (expected));
115 ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
118 for (i = 0; i < 16; i++)
119 ASSERT (offsets[i] == (i < 5 ? i :
121 ASSERT (offsets[16] == MAGIC);
128 /* autodetect_jp is only supported when iconv() support ISO-2022-JP-2. */
129 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__)
130 /* Test conversions from autodetect_jp to UTF-8. */
131 for (h = 0; h < SIZEOF (handlers); h++)
133 enum iconv_ilseq_handler handler = handlers[h];
134 static const char input[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
135 static const uint8_t expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
136 for (o = 0; o < 2; o++)
138 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
139 uint8_t *result = NULL;
141 int retval = u8_conv_from_encoding ("autodetect_jp", handler,
142 input, strlen (input),
145 ASSERT (retval == 0);
146 ASSERT (length == u8_strlen (expected));
147 ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
150 for (i = 0; i < 10; i++)
151 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
152 ASSERT (offsets[10] == MAGIC);
158 for (h = 0; h < SIZEOF (handlers); h++)
160 enum iconv_ilseq_handler handler = handlers[h];
161 static const char input[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
162 static const uint8_t expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
163 for (o = 0; o < 2; o++)
165 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
166 uint8_t *result = NULL;
168 int retval = u8_conv_from_encoding ("autodetect_jp", handler,
169 input, strlen (input),
172 ASSERT (retval == 0);
173 ASSERT (length == u8_strlen (expected));
174 ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
177 for (i = 0; i < 10; i++)
178 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
179 ASSERT (offsets[10] == MAGIC);
185 for (h = 0; h < SIZEOF (handlers); h++)
187 enum iconv_ilseq_handler handler = handlers[h];
188 static const char input[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
189 static const uint8_t expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
190 for (o = 0; o < 2; o++)
192 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
193 uint8_t *result = NULL;
195 int retval = u8_conv_from_encoding ("autodetect_jp", handler,
196 input, strlen (input),
199 ASSERT (retval == 0);
200 ASSERT (length == u8_strlen (expected));
201 ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
204 for (i = 0; i < 16; i++)
205 ASSERT (offsets[i] == (i == 0 ? 0 :
212 ASSERT (offsets[16] == MAGIC);