1 /* Test of character set conversion.
2 Copyright (C) 2007-2009 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. */
25 # define ICONV_CONST /* empty */
28 #include "signature.h"
29 SIGNATURE_CHECK (iconv, size_t, (iconv_t, ICONV_CONST char **, size_t *,
31 SIGNATURE_CHECK (iconv_close, int, (iconv_t x));
32 SIGNATURE_CHECK (iconv_open, iconv_t, (char const *, char const *));
41 #define ASSERT(expr) \
46 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
57 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
59 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
60 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
62 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
63 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
65 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
67 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
68 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
70 const char *inptr = input;
71 size_t inbytesleft = strlen (input);
73 size_t outbytesleft = sizeof (buf);
74 size_t res = iconv (cd_88591_to_utf8,
75 (ICONV_CONST char **) &inptr, &inbytesleft,
76 &outptr, &outbytesleft);
77 ASSERT (res == 0 && inbytesleft == 0);
78 ASSERT (outptr == buf + strlen (expected));
79 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
82 /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG. */
84 static const char input[] = "\304";
85 static char buf[2] = { (char)0xDE, (char)0xAD };
86 const char *inptr = input;
87 size_t inbytesleft = 1;
89 size_t outbytesleft = 1;
90 size_t res = iconv (cd_88591_to_utf8,
91 (ICONV_CONST char **) &inptr, &inbytesleft,
92 &outptr, &outbytesleft);
93 ASSERT (res == (size_t)(-1) && errno == E2BIG);
94 ASSERT (inbytesleft == 1);
95 ASSERT (outbytesleft == 1);
96 ASSERT ((unsigned char) buf[1] == 0xAD);
97 ASSERT ((unsigned char) buf[0] == 0xDE);
100 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
102 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
103 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
105 const char *inptr = input;
106 size_t inbytesleft = strlen (input);
108 size_t outbytesleft = sizeof (buf);
109 size_t res = iconv (cd_utf8_to_88591,
110 (ICONV_CONST char **) &inptr, &inbytesleft,
111 &outptr, &outbytesleft);
112 ASSERT (res == 0 && inbytesleft == 0);
113 ASSERT (outptr == buf + strlen (expected));
114 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
117 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
119 static const char input[] = "\342\202\254"; /* EURO SIGN */
121 const char *inptr = input;
122 size_t inbytesleft = strlen (input);
124 size_t outbytesleft = sizeof (buf);
125 size_t res = iconv (cd_utf8_to_88591,
126 (ICONV_CONST char **) &inptr, &inbytesleft,
127 &outptr, &outbytesleft);
128 if (res == (size_t)(-1))
130 ASSERT (errno == EILSEQ);
131 ASSERT (inbytesleft == strlen (input) && outptr == buf);
136 ASSERT (inbytesleft == 0);
140 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
142 static const char input[] = "\342";
144 const char *inptr = input;
145 size_t inbytesleft = 1;
147 size_t outbytesleft = sizeof (buf);
148 size_t res = iconv (cd_utf8_to_88591,
149 (ICONV_CONST char **) &inptr, &inbytesleft,
150 &outptr, &outbytesleft);
151 ASSERT (res == (size_t)(-1) && errno == EINVAL);
152 ASSERT (inbytesleft == 1 && outptr == buf);
155 iconv_close (cd_88591_to_utf8);
156 iconv_close (cd_utf8_to_88591);