1 /* Test of conversion of string to 32-bit wide string.
2 Copyright (C) 2008-2020 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
23 #include "signature.h"
24 SIGNATURE_CHECK (mbsnrtoc32s
, size_t,
25 (char32_t
*, const char **, size_t, size_t, mbstate_t *));
34 main (int argc
, char *argv
[])
40 /* configure should already have checked that the locale is supported. */
41 if (setlocale (LC_ALL
, "") == NULL
)
44 /* Test NUL byte input. */
48 memset (&state
, '\0', sizeof (mbstate_t));
51 ret
= mbsnrtoc32s (NULL
, &src
, 1, 0, &state
);
53 ASSERT (mbsinit (&state
));
56 ret
= mbsnrtoc32s (NULL
, &src
, 1, 1, &state
);
58 ASSERT (mbsinit (&state
));
60 wc
= (char32_t
) 0xBADFACE;
62 ret
= mbsnrtoc32s (&wc
, &src
, 1, 0, &state
);
64 ASSERT (wc
== (char32_t
) 0xBADFACE);
65 ASSERT (mbsinit (&state
));
67 wc
= (char32_t
) 0xBADFACE;
69 ret
= mbsnrtoc32s (&wc
, &src
, 1, 1, &state
);
72 ASSERT (mbsinit (&state
));
79 for (unlimited
= 0; unlimited
< 2; unlimited
++)
82 char32_t buf
[BUFSIZE
];
88 for (i
= 0; i
< BUFSIZE
; i
++)
89 buf
[i
] = (char32_t
) 0xBADFACE;
95 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
97 char input
[] = "B\374\337er"; /* "Büßer" */
98 memset (&state
, '\0', sizeof (mbstate_t));
100 wc
= (char32_t
) 0xBADFACE;
101 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
104 ASSERT (mbsinit (&state
));
107 wc
= (char32_t
) 0xBADFACE;
108 ret
= mbrtoc32 (&wc
, input
+ 1, 1, &state
);
110 ASSERT (c32tob (wc
) == (unsigned char) '\374');
111 ASSERT (mbsinit (&state
));
116 ret
= mbsnrtoc32s (NULL
, &src
, 4, unlimited
? BUFSIZE
: 1, &temp_state
);
118 ASSERT (src
== input
+ 2);
119 ASSERT (mbsinit (&state
));
122 ret
= mbsnrtoc32s (buf
, &src
, 4, unlimited
? BUFSIZE
: 1, &state
);
123 ASSERT (ret
== (unlimited
? 3 : 1));
124 ASSERT (src
== (unlimited
? NULL
: input
+ 3));
125 ASSERT (c32tob (buf
[0]) == (unsigned char) '\337');
128 ASSERT (buf
[1] == 'e');
129 ASSERT (buf
[2] == 'r');
130 ASSERT (buf
[3] == 0);
131 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
134 ASSERT (buf
[1] == (char32_t
) 0xBADFACE);
135 ASSERT (mbsinit (&state
));
140 /* Locale encoding is UTF-8. */
142 char input
[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
143 memset (&state
, '\0', sizeof (mbstate_t));
145 wc
= (char32_t
) 0xBADFACE;
146 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
149 ASSERT (mbsinit (&state
));
152 wc
= (char32_t
) 0xBADFACE;
153 ret
= mbrtoc32 (&wc
, input
+ 1, 1, &state
);
154 ASSERT (ret
== (size_t)(-2));
155 ASSERT (wc
== (char32_t
) 0xBADFACE);
156 ASSERT (!mbsinit (&state
));
161 ret
= mbsnrtoc32s (NULL
, &src
, 9, unlimited
? BUFSIZE
: 2, &temp_state
);
163 ASSERT (src
== input
+ 2);
164 ASSERT (!mbsinit (&state
));
167 ret
= mbsnrtoc32s (buf
, &src
, 9, unlimited
? BUFSIZE
: 2, &state
);
168 ASSERT (ret
== (unlimited
? 4 : 2));
169 ASSERT (src
== (unlimited
? NULL
: input
+ 5));
170 ASSERT (c32tob (buf
[0]) == EOF
);
171 ASSERT (c32tob (buf
[1]) == EOF
);
174 ASSERT (buf
[2] == 0x1F60B); /* expect Unicode encoding */
175 ASSERT (buf
[3] == '!');
176 ASSERT (buf
[4] == 0);
177 ASSERT (buf
[5] == (char32_t
) 0xBADFACE);
180 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
181 ASSERT (mbsinit (&state
));
186 /* Locale encoding is EUC-JP. */
188 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
189 memset (&state
, '\0', sizeof (mbstate_t));
191 wc
= (char32_t
) 0xBADFACE;
192 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
195 ASSERT (mbsinit (&state
));
198 wc
= (char32_t
) 0xBADFACE;
199 ret
= mbrtoc32 (&wc
, input
+ 1, 2, &state
);
201 ASSERT (c32tob (wc
) == EOF
);
202 ASSERT (mbsinit (&state
));
206 wc
= (char32_t
) 0xBADFACE;
207 ret
= mbrtoc32 (&wc
, input
+ 3, 1, &state
);
208 ASSERT (ret
== (size_t)(-2));
209 ASSERT (wc
== (char32_t
) 0xBADFACE);
210 ASSERT (!mbsinit (&state
));
215 ret
= mbsnrtoc32s (NULL
, &src
, 5, unlimited
? BUFSIZE
: 2, &temp_state
);
217 ASSERT (src
== input
+ 4);
218 ASSERT (!mbsinit (&state
));
221 ret
= mbsnrtoc32s (buf
, &src
, 5, unlimited
? BUFSIZE
: 2, &state
);
222 ASSERT (ret
== (unlimited
? 3 : 2));
223 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
224 ASSERT (c32tob (buf
[0]) == EOF
);
225 ASSERT (c32tob (buf
[1]) == EOF
);
228 ASSERT (buf
[2] == '>');
229 ASSERT (buf
[3] == 0);
230 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
233 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
234 ASSERT (mbsinit (&state
));
239 /* Locale encoding is GB18030. */
241 char input
[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
242 memset (&state
, '\0', sizeof (mbstate_t));
244 wc
= (char32_t
) 0xBADFACE;
245 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
248 ASSERT (mbsinit (&state
));
251 wc
= (char32_t
) 0xBADFACE;
252 ret
= mbrtoc32 (&wc
, input
+ 1, 1, &state
);
253 ASSERT (ret
== (size_t)(-2));
254 ASSERT (wc
== (char32_t
) 0xBADFACE);
255 ASSERT (!mbsinit (&state
));
260 ret
= mbsnrtoc32s (NULL
, &src
, 11, unlimited
? BUFSIZE
: 2, &temp_state
);
262 ASSERT (src
== input
+ 2);
263 ASSERT (!mbsinit (&state
));
266 ret
= mbsnrtoc32s (buf
, &src
, 11, unlimited
? BUFSIZE
: 2, &state
);
267 ASSERT (ret
== (unlimited
? 4 : 2));
268 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
269 ASSERT (c32tob (buf
[0]) == EOF
);
270 ASSERT (c32tob (buf
[1]) == EOF
);
273 ASSERT (c32tob (buf
[2]) == EOF
);
274 ASSERT (buf
[3] == '!');
275 ASSERT (buf
[4] == 0);
276 ASSERT (buf
[5] == (char32_t
) 0xBADFACE);
279 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
280 ASSERT (mbsinit (&state
));