1 /* Test of character set conversion.
2 Copyright (C) 2007-2012 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 *));
45 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
47 iconv_t cd_88591_to_utf8
= iconv_open ("UTF-8", "ISO-8859-1");
48 iconv_t cd_utf8_to_88591
= iconv_open ("ISO-8859-1", "UTF-8");
50 ASSERT (cd_88591_to_utf8
!= (iconv_t
)(-1));
51 ASSERT (cd_utf8_to_88591
!= (iconv_t
)(-1));
53 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
55 static const char input
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
56 static const char expected
[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
58 const char *inptr
= input
;
59 size_t inbytesleft
= strlen (input
);
61 size_t outbytesleft
= sizeof (buf
);
62 size_t res
= iconv (cd_88591_to_utf8
,
63 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
64 &outptr
, &outbytesleft
);
65 ASSERT (res
== 0 && inbytesleft
== 0);
66 ASSERT (outptr
== buf
+ strlen (expected
));
67 ASSERT (memcmp (buf
, expected
, strlen (expected
)) == 0);
70 /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG. */
72 static const char input
[] = "\304";
73 static char buf
[2] = { (char)0xDE, (char)0xAD };
74 const char *inptr
= input
;
75 size_t inbytesleft
= 1;
77 size_t outbytesleft
= 1;
78 size_t res
= iconv (cd_88591_to_utf8
,
79 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
80 &outptr
, &outbytesleft
);
81 ASSERT (res
== (size_t)(-1) && errno
== E2BIG
);
82 ASSERT (inbytesleft
== 1);
83 ASSERT (outbytesleft
== 1);
84 ASSERT ((unsigned char) buf
[1] == 0xAD);
85 ASSERT ((unsigned char) buf
[0] == 0xDE);
88 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
90 static const char input
[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
91 static const char expected
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
93 const char *inptr
= input
;
94 size_t inbytesleft
= strlen (input
);
96 size_t outbytesleft
= sizeof (buf
);
97 size_t res
= iconv (cd_utf8_to_88591
,
98 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
99 &outptr
, &outbytesleft
);
100 ASSERT (res
== 0 && inbytesleft
== 0);
101 ASSERT (outptr
== buf
+ strlen (expected
));
102 ASSERT (memcmp (buf
, expected
, strlen (expected
)) == 0);
105 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
107 static const char input
[] = "\342\202\254"; /* EURO SIGN */
109 const char *inptr
= input
;
110 size_t inbytesleft
= strlen (input
);
112 size_t outbytesleft
= sizeof (buf
);
113 size_t res
= iconv (cd_utf8_to_88591
,
114 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
115 &outptr
, &outbytesleft
);
116 if (res
== (size_t)(-1))
118 ASSERT (errno
== EILSEQ
);
119 ASSERT (inbytesleft
== strlen (input
) && outptr
== buf
);
124 ASSERT (inbytesleft
== 0);
128 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
130 static const char input
[] = "\342";
132 const char *inptr
= input
;
133 size_t inbytesleft
= 1;
135 size_t outbytesleft
= sizeof (buf
);
136 size_t res
= iconv (cd_utf8_to_88591
,
137 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
138 &outptr
, &outbytesleft
);
139 ASSERT (res
== (size_t)(-1) && errno
== EINVAL
);
140 ASSERT (inbytesleft
== 1 && outptr
== buf
);
143 iconv_close (cd_88591_to_utf8
);
144 iconv_close (cd_utf8_to_88591
);