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. */
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
46 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
48 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
49 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
51 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
52 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
54 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
56 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
57 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
59 const char *inptr = input;
60 size_t inbytesleft = strlen (input);
62 size_t outbytesleft = sizeof (buf);
63 size_t res = iconv (cd_88591_to_utf8,
64 (ICONV_CONST char **) &inptr, &inbytesleft,
65 &outptr, &outbytesleft);
66 ASSERT (res == 0 && inbytesleft == 0);
67 ASSERT (outptr == buf + strlen (expected));
68 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
71 /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG. */
73 static const char input[] = "\304";
74 static char buf[2] = { (char)0xDE, (char)0xAD };
75 const char *inptr = input;
76 size_t inbytesleft = 1;
78 size_t outbytesleft = 1;
79 size_t res = iconv (cd_88591_to_utf8,
80 (ICONV_CONST char **) &inptr, &inbytesleft,
81 &outptr, &outbytesleft);
82 ASSERT (res == (size_t)(-1) && errno == E2BIG);
83 ASSERT (inbytesleft == 1);
84 ASSERT (outbytesleft == 1);
85 ASSERT ((unsigned char) buf[1] == 0xAD);
86 ASSERT ((unsigned char) buf[0] == 0xDE);
89 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
91 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
92 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
94 const char *inptr = input;
95 size_t inbytesleft = strlen (input);
97 size_t outbytesleft = sizeof (buf);
98 size_t res = iconv (cd_utf8_to_88591,
99 (ICONV_CONST char **) &inptr, &inbytesleft,
100 &outptr, &outbytesleft);
101 ASSERT (res == 0 && inbytesleft == 0);
102 ASSERT (outptr == buf + strlen (expected));
103 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
106 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
108 static const char input[] = "\342\202\254"; /* EURO SIGN */
110 const char *inptr = input;
111 size_t inbytesleft = strlen (input);
113 size_t outbytesleft = sizeof (buf);
114 size_t res = iconv (cd_utf8_to_88591,
115 (ICONV_CONST char **) &inptr, &inbytesleft,
116 &outptr, &outbytesleft);
117 if (res == (size_t)(-1))
119 ASSERT (errno == EILSEQ);
120 ASSERT (inbytesleft == strlen (input) && outptr == buf);
125 ASSERT (inbytesleft == 0);
129 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
131 static const char input[] = "\342";
133 const char *inptr = input;
134 size_t inbytesleft = 1;
136 size_t outbytesleft = sizeof (buf);
137 size_t res = iconv (cd_utf8_to_88591,
138 (ICONV_CONST char **) &inptr, &inbytesleft,
139 &outptr, &outbytesleft);
140 ASSERT (res == (size_t)(-1) && errno == EINVAL);
141 ASSERT (inbytesleft == 1 && outptr == buf);
144 iconv_close (cd_88591_to_utf8);
145 iconv_close (cd_utf8_to_88591);