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 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. */
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
49 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
50 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
52 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
53 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
55 /* ------------------------- Test mem_cd_iconv() ------------------------- */
57 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
59 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
60 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
63 int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
66 ASSERT (length == strlen (expected));
67 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
71 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
73 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
74 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
77 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
80 ASSERT (length == strlen (expected));
81 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
85 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
87 static const char input[] = "\342\202\254"; /* EURO SIGN */
90 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
92 ASSERT (retval == -1 && errno == EILSEQ);
93 ASSERT (result == NULL);
96 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
98 static const char input[] = "\342";
101 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
103 ASSERT (retval == 0);
104 ASSERT (length == 0);
108 /* ------------------------- Test str_cd_iconv() ------------------------- */
110 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
112 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
113 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
114 char *result = str_cd_iconv (input, cd_88591_to_utf8);
115 ASSERT (result != NULL);
116 ASSERT (strcmp (result, expected) == 0);
120 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
122 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
123 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
124 char *result = str_cd_iconv (input, cd_utf8_to_88591);
125 ASSERT (result != NULL);
126 ASSERT (strcmp (result, expected) == 0);
130 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
132 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
133 char *result = str_cd_iconv (input, cd_utf8_to_88591);
134 ASSERT (result == NULL && errno == EILSEQ);
137 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
139 static const char input[] = "\342";
140 char *result = str_cd_iconv (input, cd_utf8_to_88591);
141 ASSERT (result != NULL);
142 ASSERT (strcmp (result, "") == 0);
146 iconv_close (cd_88591_to_utf8);
147 iconv_close (cd_utf8_to_88591);
149 /* -------------------------- Test str_iconv() -------------------------- */
151 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
153 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
154 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
155 char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
156 ASSERT (result != NULL);
157 ASSERT (strcmp (result, expected) == 0);
161 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
163 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
164 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
165 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
166 ASSERT (result != NULL);
167 ASSERT (strcmp (result, expected) == 0);
171 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
173 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
174 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
175 ASSERT (result == NULL && errno == EILSEQ);
178 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
180 static const char input[] = "\342";
181 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
182 ASSERT (result != NULL);
183 ASSERT (strcmp (result, "") == 0);