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. */
35 #define ASSERT(expr) \
40 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
50 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
52 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
53 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
55 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
56 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
58 /* ------------------------- Test mem_cd_iconv() ------------------------- */
60 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
62 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
63 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
66 int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
69 ASSERT (length == strlen (expected));
70 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
74 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
76 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
77 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
80 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
83 ASSERT (length == strlen (expected));
84 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
88 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
90 static const char input[] = "\342\202\254"; /* EURO SIGN */
93 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
95 ASSERT (retval == -1 && errno == EILSEQ);
96 ASSERT (result == NULL);
99 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
101 static const char input[] = "\342";
104 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
106 ASSERT (retval == 0);
107 ASSERT (length == 0);
112 /* ------------------------- Test str_cd_iconv() ------------------------- */
114 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
116 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
117 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
118 char *result = str_cd_iconv (input, cd_88591_to_utf8);
119 ASSERT (result != NULL);
120 ASSERT (strcmp (result, expected) == 0);
124 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
126 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
127 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
128 char *result = str_cd_iconv (input, cd_utf8_to_88591);
129 ASSERT (result != NULL);
130 ASSERT (strcmp (result, expected) == 0);
134 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
136 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
137 char *result = str_cd_iconv (input, cd_utf8_to_88591);
138 ASSERT (result == NULL && errno == EILSEQ);
141 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
143 static const char input[] = "\342";
144 char *result = str_cd_iconv (input, cd_utf8_to_88591);
145 ASSERT (result != NULL);
146 ASSERT (strcmp (result, "") == 0);
150 iconv_close (cd_88591_to_utf8);
151 iconv_close (cd_utf8_to_88591);
153 /* -------------------------- Test str_iconv() -------------------------- */
155 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
157 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
158 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
159 char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
160 ASSERT (result != NULL);
161 ASSERT (strcmp (result, expected) == 0);
165 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
167 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
168 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
169 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
170 ASSERT (result != NULL);
171 ASSERT (strcmp (result, expected) == 0);
175 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
177 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
178 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
179 ASSERT (result == NULL && errno == EILSEQ);
182 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
184 static const char input[] = "\342";
185 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
186 ASSERT (result != NULL);
187 ASSERT (strcmp (result, "") == 0);