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. */
34 #define ASSERT(expr) if (!(expr)) abort ();
40 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
42 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
43 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
45 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
46 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
48 /* ------------------------- Test mem_cd_iconv() ------------------------- */
50 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
52 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
53 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
56 int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
59 ASSERT (length == strlen (expected));
60 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
64 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
66 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
67 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
70 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
73 ASSERT (length == strlen (expected));
74 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
78 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
80 static const char input[] = "\342\202\254"; /* EURO SIGN */
83 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
85 ASSERT (retval == -1 && errno == EILSEQ);
86 ASSERT (result == NULL);
89 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
91 static const char input[] = "\342";
94 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
102 /* ------------------------- Test str_cd_iconv() ------------------------- */
104 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
106 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
107 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
108 char *result = str_cd_iconv (input, cd_88591_to_utf8);
109 ASSERT (result != NULL);
110 ASSERT (strcmp (result, expected) == 0);
114 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
116 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
117 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
118 char *result = str_cd_iconv (input, cd_utf8_to_88591);
119 ASSERT (result != NULL);
120 ASSERT (strcmp (result, expected) == 0);
124 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
126 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
127 char *result = str_cd_iconv (input, cd_utf8_to_88591);
128 ASSERT (result == NULL && errno == EILSEQ);
131 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
133 static const char input[] = "\342";
134 char *result = str_cd_iconv (input, cd_utf8_to_88591);
135 ASSERT (result != NULL);
136 ASSERT (strcmp (result, "") == 0);
140 iconv_close (cd_88591_to_utf8);
141 iconv_close (cd_utf8_to_88591);
143 /* -------------------------- Test str_iconv() -------------------------- */
145 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
147 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
148 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
149 char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
150 ASSERT (result != NULL);
151 ASSERT (strcmp (result, expected) == 0);
155 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
157 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
158 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
159 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
160 ASSERT (result != NULL);
161 ASSERT (strcmp (result, expected) == 0);
165 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
167 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
168 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
169 ASSERT (result == NULL && errno == EILSEQ);
172 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
174 static const char input[] = "\342";
175 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
176 ASSERT (result != NULL);
177 ASSERT (strcmp (result, "") == 0);