1 /* Test of conversion from UTF-8 to 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 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. */
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 UTF-8 to ISO-8859-1 with no errors. */
67 for (h = 0; h < SIZEOF (handlers); h++)
69 enum iconv_ilseq_handler handler = handlers[h];
70 static const uint8_t input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
71 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
72 for (o = 0; o < 2; o++)
74 size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
77 int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
78 input, u8_strlen (input),
82 ASSERT (length == strlen (expected));
83 ASSERT (result != NULL);
84 ASSERT (memcmp (result, expected, length) == 0);
87 for (i = 0; i < 41; i++)
88 ASSERT (offsets[i] == (i < 1 ? i :
89 i == 1 ? (size_t)(-1) :
91 i == 13 ? (size_t)(-1) :
93 i == 20 ? (size_t)(-1) :
95 i == 40 ? (size_t)(-1) :
97 ASSERT (offsets[41] == MAGIC);
104 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
105 for (h = 0; h < SIZEOF (handlers); h++)
107 enum iconv_ilseq_handler handler = handlers[h];
108 static const uint8_t input[] = "Rafa\305\202 Maszkowski"; /* RafaĆ Maszkowski */
109 for (o = 0; o < 2; o++)
111 size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
114 int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
115 input, u8_strlen (input),
121 ASSERT (retval == -1 && errno == EILSEQ);
122 ASSERT (result == NULL);
123 ASSERT (length == 0);
125 case iconveh_question_mark:
127 static const char expected[] = "Rafa? Maszkowski";
128 static const char expected_translit[] = "Rafal Maszkowski";
129 ASSERT (retval == 0);
130 ASSERT (length == strlen (expected));
131 ASSERT (result != NULL);
132 ASSERT (memcmp (result, expected, length) == 0
133 || memcmp (result, expected_translit, length) == 0);
136 for (i = 0; i < 17; i++)
137 ASSERT (offsets[i] == (i < 5 ? i :
138 i == 5 ? (size_t)(-1) :
140 ASSERT (offsets[17] == MAGIC);
146 case iconveh_escape_sequence:
148 static const char expected[] = "Rafa\\u0142 Maszkowski";
149 ASSERT (retval == 0);
150 ASSERT (length == strlen (expected));
151 ASSERT (result != NULL);
152 ASSERT (memcmp (result, expected, length) == 0);
155 for (i = 0; i < 17; i++)
156 ASSERT (offsets[i] == (i < 5 ? i :
157 i == 5 ? (size_t)(-1) :
159 ASSERT (offsets[17] == MAGIC);
169 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
170 for (h = 0; h < SIZEOF (handlers); h++)
172 enum iconv_ilseq_handler handler = handlers[h];
173 static const uint8_t input[] = "\342";
174 for (o = 0; o < 2; o++)
176 size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
179 int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
180 input, u8_strlen (input),
183 ASSERT (retval == 0);
184 ASSERT (length == strlen (""));
185 ASSERT (result != NULL);
188 ASSERT (offsets[0] == 0);
189 ASSERT (offsets[1] == MAGIC);