1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include <uchar.h>
19
20 #include <gtest/gtest.h>
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <locale.h>
25 #include <stdint.h>
26
27 #include "utils.h"
28
29 // Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
30 // newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
31 // and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
32 // https://datatracker.ietf.org/doc/html/rfc2279.
33 //
34 // Bionic's unicode implementation was written after the high values were
35 // excluded, so it has never supported them. Other implementations (at least
36 // as of glibc 2.36), do support those sequences.
37 #if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
38 constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
39 #elif defined(__GLIBC__)
40 constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
41 #else
42 #error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
43 #endif
44
TEST(uchar,sizeof_uchar_t)45 TEST(uchar, sizeof_uchar_t) {
46 EXPECT_EQ(2U, sizeof(char16_t));
47 EXPECT_EQ(4U, sizeof(char32_t));
48 }
49
TEST(uchar,start_state)50 TEST(uchar, start_state) {
51 // C23 does not appear to specify the behavior of the conversion functions if
52 // a state is reused before the character is completed. In the wchar.h section
53 // (7.31.6.3) it says:
54 //
55 // If an mbstate_t object has been altered by any of the functions
56 // described in this subclause, and is then used with a different
57 // multibyte character sequence, or in the other conversion direction, or
58 // with a different LC_CTYPE category setting than on earlier function
59 // calls, the behavior is undefined.
60 //
61 // But "described in this subclause" refers to the wchar.h functions, not the
62 // uchar.h ones.
63 //
64 // Since C has no opinion, we need to make a choice. While no caller should
65 // ever do this (what does it mean to begin decoding a UTF-32 character while
66 // still in the middle of a UTF-8 sequence?), considering that a decoding
67 // error seems the least surprising. Bionic and glibc both have that behavior.
68 // musl ignores the state (it also doesn't make much sense to read the state
69 // when the entire conversion completes in a single call) and decodes the
70 // UTF-32 character.
71 #if !defined(ANDROID_HOST_MUSL)
72 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
73 uselocale(LC_GLOBAL_LOCALE);
74
75 char out[MB_LEN_MAX];
76 mbstate_t ps;
77
78 memset(&ps, 0, sizeof(ps));
79 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
80 errno = 0;
81 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
82 EXPECT_ERRNO(EILSEQ);
83
84 // Similarly (but not in compliance with the standard afaict), musl seems to
85 // ignore the state entirely for the UTF-32 functions rather than reset it.
86
87 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
88 // state should be reset.
89 memset(&ps, 0, sizeof(ps));
90 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
91 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
92 EXPECT_TRUE(mbsinit(&ps));
93
94 memset(&ps, 0, sizeof(ps));
95 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
96 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
97 EXPECT_TRUE(mbsinit(&ps));
98 #endif
99 }
100
TEST(uchar,c16rtomb_null_out)101 TEST(uchar, c16rtomb_null_out) {
102 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
103 uselocale(LC_GLOBAL_LOCALE);
104
105 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
106 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
107 }
108
TEST(uchar,c16rtomb_null_char)109 TEST(uchar, c16rtomb_null_char) {
110 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
111 uselocale(LC_GLOBAL_LOCALE);
112
113 char bytes[MB_LEN_MAX];
114 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
115 }
116
TEST(uchar,c16rtomb)117 TEST(uchar, c16rtomb) {
118 char bytes[MB_LEN_MAX];
119
120 memset(bytes, 0, sizeof(bytes));
121 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
122 EXPECT_EQ('h', bytes[0]);
123
124 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
125 uselocale(LC_GLOBAL_LOCALE);
126
127 // 1-byte UTF-8.
128 memset(bytes, 0, sizeof(bytes));
129 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
130 EXPECT_EQ('h', bytes[0]);
131 // 2-byte UTF-8.
132 memset(bytes, 0, sizeof(bytes));
133 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
134 EXPECT_EQ('\xc2', bytes[0]);
135 EXPECT_EQ('\xa2', bytes[1]);
136 // 3-byte UTF-8.
137 memset(bytes, 0, sizeof(bytes));
138 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
139 EXPECT_EQ('\xe2', bytes[0]);
140 EXPECT_EQ('\x82', bytes[1]);
141 EXPECT_EQ('\xac', bytes[2]);
142 // 4-byte UTF-8 from a surrogate pair...
143 memset(bytes, 0, sizeof(bytes));
144 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
145 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
146 EXPECT_EQ('\xf4', bytes[0]);
147 EXPECT_EQ('\x8a', bytes[1]);
148 EXPECT_EQ('\xaf', bytes[2]);
149 EXPECT_EQ('\x8d', bytes[3]);
150 }
151
TEST(uchar,c16rtomb_invalid)152 TEST(uchar, c16rtomb_invalid) {
153 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
154 uselocale(LC_GLOBAL_LOCALE);
155
156 char bytes[MB_LEN_MAX];
157
158 memset(bytes, 0, sizeof(bytes));
159 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
160
161 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
162 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
163 }
164
TEST(uchar,mbrtoc16_null)165 TEST(uchar, mbrtoc16_null) {
166 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
167 uselocale(LC_GLOBAL_LOCALE);
168
169 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
170 }
171
TEST(uchar,mbrtoc16_zero_len)172 TEST(uchar, mbrtoc16_zero_len) {
173 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
174 uselocale(LC_GLOBAL_LOCALE);
175
176 char16_t out;
177
178 out = L'x';
179 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "hello", 0, nullptr));
180 EXPECT_EQ(L'x', out);
181
182 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "hello", 0, nullptr));
183 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "", 0, nullptr));
184 EXPECT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
185 EXPECT_EQ(L'h', out);
186 }
187
TEST(uchar,mbrtoc16)188 TEST(uchar, mbrtoc16) {
189 char16_t out;
190
191 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
192 uselocale(LC_GLOBAL_LOCALE);
193
194 // 1-byte UTF-8.
195 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
196 ASSERT_EQ(L'a', out);
197 // 2-byte UTF-8.
198 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
199 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
200 // 3-byte UTF-8.
201 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
202 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
203 // 4-byte UTF-8 will be returned as a surrogate pair...
204 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
205 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
206 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
207 "\xf4\x8a\xaf\x8d"
208 "ef",
209 6, nullptr));
210 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
211 }
212
TEST(uchar,mbrtoc16_long_sequences)213 TEST(uchar, mbrtoc16_long_sequences) {
214 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
215 uselocale(LC_GLOBAL_LOCALE);
216
217 char16_t out = u'\0';
218 errno = 0;
219 auto result = mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr);
220 if (kLibcRejectsOverLongUtf8Sequences) {
221 EXPECT_EQ(static_cast<size_t>(-1), result);
222 EXPECT_ERRNO(EILSEQ);
223 EXPECT_EQ(u'\0', out);
224 } else {
225 EXPECT_EQ(5U, result);
226 EXPECT_ERRNO(0);
227 EXPECT_EQ(u'\uf94a', out);
228 }
229 }
230
TEST(uchar,mbrtoc16_reserved_range)231 TEST(uchar, mbrtoc16_reserved_range) {
232 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
233 uselocale(LC_GLOBAL_LOCALE);
234
235 errno = 0;
236 char16_t out = u'\0';
237 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
238 EXPECT_EQ(u'\0', out);
239 EXPECT_ERRNO(EILSEQ);
240 }
241
TEST(uchar,mbrtoc16_beyond_range)242 TEST(uchar, mbrtoc16_beyond_range) {
243 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
244 uselocale(LC_GLOBAL_LOCALE);
245
246 errno = 0;
247 char16_t out = u'\0';
248 auto result = mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr);
249 if (kLibcRejectsOverLongUtf8Sequences) {
250 EXPECT_EQ(static_cast<size_t>(-1), result);
251 EXPECT_EQ(u'\0', out);
252 EXPECT_ERRNO(EILSEQ);
253 } else {
254 EXPECT_EQ(4U, result);
255 EXPECT_EQ(u'\xdcc0', out);
256 EXPECT_ERRNO(0);
257 }
258 }
259
test_mbrtoc16_incomplete(mbstate_t * ps)260 void test_mbrtoc16_incomplete(mbstate_t* ps) {
261 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
262 uselocale(LC_GLOBAL_LOCALE);
263
264 char16_t out;
265 // 2-byte UTF-8.
266 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
267 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
268 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
269 ASSERT_TRUE(mbsinit(ps));
270 // 3-byte UTF-8.
271 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
272 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
273 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
274 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
275 ASSERT_TRUE(mbsinit(ps));
276 // 4-byte UTF-8.
277 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
278 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
279 ASSERT_EQ(1U, mbrtoc16(&out,
280 "\x8d"
281 "ef",
282 3, ps));
283 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
284 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
285 "\x80"
286 "ef",
287 3, ps));
288 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
289 ASSERT_TRUE(mbsinit(ps));
290
291 // Invalid 2-byte
292 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
293 errno = 0;
294 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
295 ASSERT_ERRNO(EILSEQ);
296 }
297
TEST(uchar,mbrtoc16_incomplete)298 TEST(uchar, mbrtoc16_incomplete) {
299 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
300 uselocale(LC_GLOBAL_LOCALE);
301
302 mbstate_t ps;
303 memset(&ps, 0, sizeof(ps));
304
305 test_mbrtoc16_incomplete(&ps);
306 test_mbrtoc16_incomplete(nullptr);
307 }
308
TEST(uchar,c32rtomb)309 TEST(uchar, c32rtomb) {
310 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
311 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
312
313 char bytes[MB_LEN_MAX];
314
315 memset(bytes, 1, sizeof(bytes));
316 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
317 EXPECT_EQ('\0', bytes[0]);
318 EXPECT_EQ('\x01', bytes[1]);
319
320 memset(bytes, 0, sizeof(bytes));
321 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
322 EXPECT_EQ('h', bytes[0]);
323
324 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
325 uselocale(LC_GLOBAL_LOCALE);
326
327 // 1-byte UTF-8.
328 memset(bytes, 0, sizeof(bytes));
329 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
330 EXPECT_EQ('h', bytes[0]);
331 // 2-byte UTF-8.
332 memset(bytes, 0, sizeof(bytes));
333 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
334 EXPECT_EQ('\xc2', bytes[0]);
335 EXPECT_EQ('\xa2', bytes[1]);
336 // 3-byte UTF-8.
337 memset(bytes, 0, sizeof(bytes));
338 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
339 EXPECT_EQ('\xe2', bytes[0]);
340 EXPECT_EQ('\x82', bytes[1]);
341 EXPECT_EQ('\xac', bytes[2]);
342 // 4-byte UTF-8.
343 memset(bytes, 0, sizeof(bytes));
344 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
345 EXPECT_EQ('\xf0', bytes[0]);
346 EXPECT_EQ('\xa4', bytes[1]);
347 EXPECT_EQ('\xad', bytes[2]);
348 EXPECT_EQ('\xa2', bytes[3]);
349 // Invalid code point.
350 errno = 0;
351 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
352 EXPECT_ERRNO(EILSEQ);
353 }
354
TEST(uchar,mbrtoc32_valid_non_characters)355 TEST(uchar, mbrtoc32_valid_non_characters) {
356 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
357 uselocale(LC_GLOBAL_LOCALE);
358
359 char32_t out[8] = {};
360 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
361 ASSERT_EQ(0xfffeU, out[0]);
362 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
363 ASSERT_EQ(0xffffU, out[0]);
364 }
365
TEST(uchar,mbrtoc32_out_of_range)366 TEST(uchar, mbrtoc32_out_of_range) {
367 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
368 uselocale(LC_GLOBAL_LOCALE);
369
370 char32_t out = U'\0';
371 errno = 0;
372 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
373 if (kLibcRejectsOverLongUtf8Sequences) {
374 EXPECT_EQ(static_cast<size_t>(-1), result);
375 EXPECT_ERRNO(EILSEQ);
376 EXPECT_EQ(U'\0', out);
377 } else {
378 EXPECT_EQ(4U, result);
379 EXPECT_ERRNO(0);
380 EXPECT_EQ(U'\x140000', out);
381 }
382 }
383
TEST(uchar,mbrtoc32)384 TEST(uchar, mbrtoc32) {
385 char32_t out[8];
386
387 out[0] = L'x';
388 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "hello", 0, nullptr));
389 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
390
391 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "hello", 0, nullptr));
392 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "", 0, nullptr));
393 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
394 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
395
396 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "hello", 0, nullptr));
397 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "", 0, nullptr));
398 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
399
400 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
401
402 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
403 uselocale(LC_GLOBAL_LOCALE);
404
405 // 1-byte UTF-8.
406 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
407 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
408 // 2-byte UTF-8.
409 EXPECT_EQ(2U, mbrtoc32(out,
410 "\xc2\xa2"
411 "cdef",
412 6, nullptr));
413 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
414 // 3-byte UTF-8.
415 EXPECT_EQ(3U, mbrtoc32(out,
416 "\xe2\x82\xac"
417 "def",
418 6, nullptr));
419 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
420 // 4-byte UTF-8.
421 EXPECT_EQ(4U, mbrtoc32(out,
422 "\xf0\xa4\xad\xa2"
423 "ef",
424 6, nullptr));
425 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
426 #if defined(__BIONIC__) // glibc allows this.
427 // Illegal 5-byte UTF-8.
428 errno = 0;
429 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
430 "\xf8\xa1\xa2\xa3\xa4"
431 "f",
432 6, nullptr));
433 EXPECT_ERRNO(EILSEQ);
434 #endif
435 // Illegal over-long sequence.
436 errno = 0;
437 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
438 "\xf0\x82\x82\xac"
439 "ef",
440 6, nullptr));
441 EXPECT_ERRNO(EILSEQ);
442 }
443
test_mbrtoc32_incomplete(mbstate_t * ps)444 void test_mbrtoc32_incomplete(mbstate_t* ps) {
445 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
446 uselocale(LC_GLOBAL_LOCALE);
447
448 char32_t out;
449 // 2-byte UTF-8.
450 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
451 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
452 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
453 ASSERT_TRUE(mbsinit(ps));
454 // 3-byte UTF-8.
455 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
456 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
457 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
458 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
459 ASSERT_TRUE(mbsinit(ps));
460 // 4-byte UTF-8.
461 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
462 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
463 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
464 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
465 ASSERT_TRUE(mbsinit(ps));
466
467 // Invalid 2-byte
468 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
469 errno = 0;
470 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
471 ASSERT_ERRNO(EILSEQ);
472 }
473
TEST(uchar,mbrtoc32_incomplete)474 TEST(uchar, mbrtoc32_incomplete) {
475 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
476 uselocale(LC_GLOBAL_LOCALE);
477
478 mbstate_t ps;
479 memset(&ps, 0, sizeof(ps));
480
481 test_mbrtoc32_incomplete(&ps);
482 test_mbrtoc32_incomplete(nullptr);
483 }
484