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. */
32 #define ASSERT(expr) if (!(expr)) abort ();
38 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
40 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
41 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
43 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
44 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
46 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
48 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
49 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
51 const char *inptr = input;
52 size_t inbytesleft = strlen (input);
54 size_t outbytesleft = sizeof (buf);
55 size_t res = iconv (cd_88591_to_utf8,
56 (ICONV_CONST char **) &inptr, &inbytesleft,
57 &outptr, &outbytesleft);
58 ASSERT (res == 0 && inbytesleft == 0);
59 ASSERT (outptr == buf + strlen (expected));
60 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
63 /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG. */
65 static const char input[] = "\304";
66 static char buf[2] = { (char)0xDE, (char)0xAD };
67 const char *inptr = input;
68 size_t inbytesleft = 1;
70 size_t outbytesleft = 1;
71 size_t res = iconv (cd_88591_to_utf8,
72 (ICONV_CONST char **) &inptr, &inbytesleft,
73 &outptr, &outbytesleft);
74 ASSERT (res == (size_t)(-1) && errno == E2BIG);
75 ASSERT (inbytesleft == 1);
76 ASSERT (outbytesleft == 1);
77 ASSERT ((unsigned char) buf[1] == 0xAD);
78 ASSERT ((unsigned char) buf[0] == 0xDE);
81 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
83 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
84 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
86 const char *inptr = input;
87 size_t inbytesleft = strlen (input);
89 size_t outbytesleft = sizeof (buf);
90 size_t res = iconv (cd_utf8_to_88591,
91 (ICONV_CONST char **) &inptr, &inbytesleft,
92 &outptr, &outbytesleft);
93 ASSERT (res == 0 && inbytesleft == 0);
94 ASSERT (outptr == buf + strlen (expected));
95 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
98 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
100 static const char input[] = "\342\202\254"; /* EURO SIGN */
102 const char *inptr = input;
103 size_t inbytesleft = strlen (input);
105 size_t outbytesleft = sizeof (buf);
106 size_t res = iconv (cd_utf8_to_88591,
107 (ICONV_CONST char **) &inptr, &inbytesleft,
108 &outptr, &outbytesleft);
109 if (res == (size_t)(-1))
111 ASSERT (errno == EILSEQ);
112 ASSERT (inbytesleft == strlen (input) && outptr == buf);
117 ASSERT (inbytesleft == 0);
121 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
123 static const char input[] = "\342";
125 const char *inptr = input;
126 size_t inbytesleft = 1;
128 size_t outbytesleft = sizeof (buf);
129 size_t res = iconv (cd_utf8_to_88591,
130 (ICONV_CONST char **) &inptr, &inbytesleft,
131 &outptr, &outbytesleft);
132 ASSERT (res == (size_t)(-1) && errno == EINVAL);
133 ASSERT (inbytesleft == 1 && outptr == buf);
136 iconv_close (cd_88591_to_utf8);
137 iconv_close (cd_utf8_to_88591);