1 /* Test of character set conversion.
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. */
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
50 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
51 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
53 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
54 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
56 /* ------------------------- Test mem_cd_iconv() ------------------------- */
58 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
60 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
61 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
64 int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
67 ASSERT (length == strlen (expected));
68 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
72 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
74 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
75 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
78 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
81 ASSERT (length == strlen (expected));
82 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
86 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
88 static const char input[] = "\342\202\254"; /* EURO SIGN */
91 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
93 ASSERT (retval == -1 && errno == EILSEQ);
94 ASSERT (result == NULL);
97 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
99 static const char input[] = "\342";
102 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
104 ASSERT (retval == 0);
105 ASSERT (length == 0);
110 /* ------------------------- Test str_cd_iconv() ------------------------- */
112 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
114 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
115 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
116 char *result = str_cd_iconv (input, cd_88591_to_utf8);
117 ASSERT (result != NULL);
118 ASSERT (strcmp (result, expected) == 0);
122 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
124 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
125 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
126 char *result = str_cd_iconv (input, cd_utf8_to_88591);
127 ASSERT (result != NULL);
128 ASSERT (strcmp (result, expected) == 0);
132 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
134 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
135 char *result = str_cd_iconv (input, cd_utf8_to_88591);
136 ASSERT (result == NULL && errno == EILSEQ);
139 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
141 static const char input[] = "\342";
142 char *result = str_cd_iconv (input, cd_utf8_to_88591);
143 ASSERT (result != NULL);
144 ASSERT (strcmp (result, "") == 0);
148 iconv_close (cd_88591_to_utf8);
149 iconv_close (cd_utf8_to_88591);
151 /* -------------------------- Test str_iconv() -------------------------- */
153 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
155 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
156 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
157 char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
158 ASSERT (result != NULL);
159 ASSERT (strcmp (result, expected) == 0);
163 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
165 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
166 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
167 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
168 ASSERT (result != NULL);
169 ASSERT (strcmp (result, expected) == 0);
173 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
175 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
176 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
177 ASSERT (result == NULL && errno == EILSEQ);
180 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
182 static const char input[] = "\342";
183 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
184 ASSERT (result != NULL);
185 ASSERT (strcmp (result, "") == 0);