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 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. */
31 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 /* Magic number for detecting bounds violations. */
44 #define MAGIC 0x1983EFF1
47 new_offsets (size_t n)
49 size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
57 static enum iconv_ilseq_handler handlers[] =
58 { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
64 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
65 ISO-8859-2, and UTF-8. */
67 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
68 for (h = 0; h < SIZEOF (handlers); h++)
70 enum iconv_ilseq_handler handler = handlers[h];
71 static const uint8_t input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
72 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
73 for (o = 0; o < 2; o++)
75 size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
78 int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
79 input, u8_strlen (input),
83 ASSERT (length == strlen (expected));
84 ASSERT (result != NULL);
85 ASSERT (memcmp (result, expected, length) == 0);
88 for (i = 0; i < 41; i++)
89 ASSERT (offsets[i] == (i < 1 ? i :
90 i == 1 ? (size_t)(-1) :
92 i == 13 ? (size_t)(-1) :
94 i == 20 ? (size_t)(-1) :
96 i == 40 ? (size_t)(-1) :
98 ASSERT (offsets[41] == MAGIC);
105 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
106 for (h = 0; h < SIZEOF (handlers); h++)
108 enum iconv_ilseq_handler handler = handlers[h];
109 static const uint8_t input[] = "Rafa\305\202 Maszkowski"; /* RafaĆ Maszkowski */
110 for (o = 0; o < 2; o++)
112 size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
115 int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
116 input, u8_strlen (input),
122 ASSERT (retval == -1 && errno == EILSEQ);
123 ASSERT (result == NULL);
124 ASSERT (length == 0);
126 case iconveh_question_mark:
128 static const char expected[] = "Rafa? Maszkowski";
129 static const char expected_translit[] = "Rafal Maszkowski";
130 ASSERT (retval == 0);
131 ASSERT (length == strlen (expected));
132 ASSERT (result != NULL);
133 ASSERT (memcmp (result, expected, length) == 0
134 || memcmp (result, expected_translit, length) == 0);
137 for (i = 0; i < 17; i++)
138 ASSERT (offsets[i] == (i < 5 ? i :
139 i == 5 ? (size_t)(-1) :
141 ASSERT (offsets[17] == MAGIC);
147 case iconveh_escape_sequence:
149 static const char expected[] = "Rafa\\u0142 Maszkowski";
150 ASSERT (retval == 0);
151 ASSERT (length == strlen (expected));
152 ASSERT (result != NULL);
153 ASSERT (memcmp (result, expected, length) == 0);
156 for (i = 0; i < 17; i++)
157 ASSERT (offsets[i] == (i < 5 ? i :
158 i == 5 ? (size_t)(-1) :
160 ASSERT (offsets[17] == MAGIC);
170 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
171 for (h = 0; h < SIZEOF (handlers); h++)
173 enum iconv_ilseq_handler handler = handlers[h];
174 static const uint8_t input[] = "\342";
175 for (o = 0; o < 2; o++)
177 size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
180 int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
181 input, u8_strlen (input),
184 ASSERT (retval == 0);
185 ASSERT (length == strlen (""));
186 ASSERT (result != NULL);
189 ASSERT (offsets[0] == 0);
190 ASSERT (offsets[1] == MAGIC);