1 /* Test of conversion of multibyte character to wide character.
2 Copyright (C) 2008-2024 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>, 2023. */
23 #include "signature.h"
24 SIGNATURE_CHECK (mbrlen
, size_t, (char const *, size_t, mbstate_t *));
34 main (int argc
, char *argv
[])
39 /* configure should already have checked that the locale is supported. */
40 if (setlocale (LC_ALL
, "") == NULL
)
43 /* Test zero-length input. */
45 memset (&state
, '\0', sizeof (mbstate_t));
46 ret
= mbrlen ("x", 0, &state
);
47 ASSERT (ret
== (size_t)(-2));
48 ASSERT (mbsinit (&state
));
51 /* Test NUL byte input. */
53 memset (&state
, '\0', sizeof (mbstate_t));
54 ret
= mbrlen ("", 1, &state
);
56 ASSERT (mbsinit (&state
));
59 /* Test single-byte input. */
64 memset (&state
, '\0', sizeof (mbstate_t));
65 for (c
= 0; c
< 0x100; c
++)
68 case '\t': case '\v': case '\f':
69 case ' ': case '!': case '"': case '#': case '%':
70 case '&': case '\'': case '(': case ')': case '*':
71 case '+': case ',': case '-': case '.': case '/':
72 case '0': case '1': case '2': case '3': case '4':
73 case '5': case '6': case '7': case '8': case '9':
74 case ':': case ';': case '<': case '=': case '>':
76 case 'A': case 'B': case 'C': case 'D': case 'E':
77 case 'F': case 'G': case 'H': case 'I': case 'J':
78 case 'K': case 'L': case 'M': case 'N': case 'O':
79 case 'P': case 'Q': case 'R': case 'S': case 'T':
80 case 'U': case 'V': case 'W': case 'X': case 'Y':
82 case '[': case '\\': case ']': case '^': case '_':
83 case 'a': case 'b': case 'c': case 'd': case 'e':
84 case 'f': case 'g': case 'h': case 'i': case 'j':
85 case 'k': case 'l': case 'm': case 'n': case 'o':
86 case 'p': case 'q': case 'r': case 's': case 't':
87 case 'u': case 'v': case 'w': case 'x': case 'y':
88 case 'z': case '{': case '|': case '}': case '~':
89 /* c is in the ISO C "basic character set". */
91 /* c is an ASCII character. */
94 ret
= mbrlen (buf
, 1, &state
);
96 ASSERT (mbsinit (&state
));
104 /* Test special calling convention, passing a NULL pointer. */
106 memset (&state
, '\0', sizeof (mbstate_t));
107 ret
= mbrlen (NULL
, 5, &state
);
109 ASSERT (mbsinit (&state
));
113 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
114 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
115 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
116 that is, effectively the "C.UTF-8" locale. */
117 if (argc
> 1 && strcmp (argv
[1], "1") == 0 && MB_CUR_MAX
> 1)
125 /* C or POSIX locale. */
130 memset (&state
, '\0', sizeof (mbstate_t));
131 for (c
= 0; c
< 0x100; c
++)
134 /* We are testing all nonnull bytes. */
137 ret
= mbrlen (buf
, 1, &state
);
138 /* POSIX:2018 says: "In the POSIX locale an [EILSEQ] error
139 cannot occur since all byte values are valid characters." */
141 ASSERT (mbsinit (&state
));
144 return test_exit_status
;
147 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
149 char input
[] = "B\374\337er"; /* "Büßer" */
150 memset (&state
, '\0', sizeof (mbstate_t));
152 ret
= mbrlen (input
, 1, &state
);
154 ASSERT (mbsinit (&state
));
157 ret
= mbrlen (input
+ 1, 1, &state
);
159 ASSERT (mbsinit (&state
));
162 ret
= mbrlen (input
+ 2, 3, &state
);
164 ASSERT (mbsinit (&state
));
167 ret
= mbrlen (input
+ 3, 2, &state
);
169 ASSERT (mbsinit (&state
));
172 ret
= mbrlen (input
+ 4, 1, &state
);
174 ASSERT (mbsinit (&state
));
176 return test_exit_status
;
179 /* Locale encoding is UTF-8. */
181 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
182 memset (&state
, '\0', sizeof (mbstate_t));
184 ret
= mbrlen (input
, 1, &state
);
186 ASSERT (mbsinit (&state
));
189 ret
= mbrlen (input
+ 1, 1, &state
);
190 ASSERT (ret
== (size_t)(-2));
191 ASSERT (!mbsinit (&state
));
194 ret
= mbrlen (input
+ 2, 5, &state
);
196 ASSERT (mbsinit (&state
));
199 ret
= mbrlen (input
+ 3, 4, &state
);
201 ASSERT (mbsinit (&state
));
205 ret
= mbrlen (input
+ 5, 2, &state
);
207 ASSERT (mbsinit (&state
));
210 ret
= mbrlen (input
+ 6, 1, &state
);
212 ASSERT (mbsinit (&state
));
214 return test_exit_status
;
217 /* Locale encoding is EUC-JP. */
219 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
220 memset (&state
, '\0', sizeof (mbstate_t));
222 ret
= mbrlen (input
, 1, &state
);
224 ASSERT (mbsinit (&state
));
227 ret
= mbrlen (input
+ 1, 2, &state
);
229 ASSERT (mbsinit (&state
));
233 ret
= mbrlen (input
+ 3, 1, &state
);
234 ASSERT (ret
== (size_t)(-2));
235 ASSERT (!mbsinit (&state
));
238 ret
= mbrlen (input
+ 4, 4, &state
);
240 ASSERT (mbsinit (&state
));
243 ret
= mbrlen (input
+ 5, 3, &state
);
245 ASSERT (mbsinit (&state
));
249 ret
= mbrlen (input
+ 7, 1, &state
);
251 ASSERT (mbsinit (&state
));
253 return test_exit_status
;
256 /* Locale encoding is GB18030. */
258 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
259 memset (&state
, '\0', sizeof (mbstate_t));
261 ret
= mbrlen (input
, 1, &state
);
263 ASSERT (mbsinit (&state
));
266 ret
= mbrlen (input
+ 1, 1, &state
);
267 ASSERT (ret
== (size_t)(-2));
268 ASSERT (!mbsinit (&state
));
271 ret
= mbrlen (input
+ 2, 7, &state
);
273 ASSERT (mbsinit (&state
));
276 ret
= mbrlen (input
+ 3, 6, &state
);
278 ASSERT (mbsinit (&state
));
284 ret
= mbrlen (input
+ 7, 2, &state
);
286 ASSERT (mbsinit (&state
));
289 ret
= mbrlen (input
+ 8, 1, &state
);
291 ASSERT (mbsinit (&state
));
293 return test_exit_status
;