blob: 512f098ebeda4f41952b2a03f993c88a45a284e9 [file] [log] [blame]
Dan Albert7a7f9952014-06-02 11:33:04 -07001/*
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
Elliott Hughes18181e62019-01-30 13:52:36 -080018#include <uchar.h>
Dan Albert7a7f9952014-06-02 11:33:04 -070019
20#include <gtest/gtest.h>
21
22#include <errno.h>
23#include <limits.h>
24#include <locale.h>
25#include <stdint.h>
26
Dan Albert1252ab02023-07-14 21:23:36 +000027// Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
28// newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
29// and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
30// https://datatracker.ietf.org/doc/html/rfc2279.
31//
32// Bionic's unicode implementation was written after the high values were
33// excluded, so it has never supported them. Other implementations (at least
34// as of glibc 2.36), do support those sequences.
35#if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
Dan Albert78da2922023-07-19 18:23:46 +000036constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
Dan Albert1252ab02023-07-14 21:23:36 +000037#elif defined(__GLIBC__)
Dan Albert78da2922023-07-19 18:23:46 +000038constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
Dan Albert1252ab02023-07-14 21:23:36 +000039#else
Dan Albert78da2922023-07-19 18:23:46 +000040#error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
Dan Albert1252ab02023-07-14 21:23:36 +000041#endif
42
Dan Albert7a7f9952014-06-02 11:33:04 -070043TEST(uchar, sizeof_uchar_t) {
Dan Albert7a7f9952014-06-02 11:33:04 -070044 EXPECT_EQ(2U, sizeof(char16_t));
45 EXPECT_EQ(4U, sizeof(char32_t));
Dan Albert7a7f9952014-06-02 11:33:04 -070046}
47
48TEST(uchar, start_state) {
Dan Albert09d3b502023-07-20 22:09:30 +000049 // C23 does not appear to specify the behavior of the conversion functions if
50 // a state is reused before the character is completed. In the wchar.h section
51 // (7.31.6.3) it says:
52 //
53 // If an mbstate_t object has been altered by any of the functions
54 // described in this subclause, and is then used with a different
55 // multibyte character sequence, or in the other conversion direction, or
56 // with a different LC_CTYPE category setting than on earlier function
57 // calls, the behavior is undefined.
58 //
59 // But "described in this subclause" refers to the wchar.h functions, not the
60 // uchar.h ones.
61 //
62 // Since C has no opinion, we need to make a choice. While no caller should
63 // ever do this (what does it mean to begin decoding a UTF-32 character while
64 // still in the middle of a UTF-8 sequence?), considering that a decoding
65 // error seems the least surprising. Bionic and glibc both have that behavior.
66 // musl ignores the state (it also doesn't make much sense to read the state
67 // when the entire conversion completes in a single call) and decodes the
68 // UTF-32 character.
69#if !defined(ANDROID_HOST_MUSL)
Dan Alberta0d0e352023-07-19 22:12:59 +000070 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
71 uselocale(LC_GLOBAL_LOCALE);
72
Dan Albert7a7f9952014-06-02 11:33:04 -070073 char out[MB_LEN_MAX];
74 mbstate_t ps;
75
Dan Albert7a7f9952014-06-02 11:33:04 -070076 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070077 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -070078 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -070079 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
80 EXPECT_EQ(EILSEQ, errno);
81
Dan Albert09d3b502023-07-20 22:09:30 +000082 // Similarly (but not in compliance with the standard afaict), musl seems to
83 // ignore the state entirely for the UTF-32 functions rather than reset it.
84
Yi Kong32bc0fc2018-08-02 17:31:13 -070085 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
Dan Albert7a7f9952014-06-02 11:33:04 -070086 // state should be reset.
87 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070088 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
89 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070090 EXPECT_TRUE(mbsinit(&ps));
91
92 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070093 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070094 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
95 EXPECT_TRUE(mbsinit(&ps));
Dan Albert09d3b502023-07-20 22:09:30 +000096#endif
Dan Albert7a7f9952014-06-02 11:33:04 -070097}
98
99TEST(uchar, c16rtomb_null_out) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000100 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
101 uselocale(LC_GLOBAL_LOCALE);
102
Yi Kong32bc0fc2018-08-02 17:31:13 -0700103 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
104 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700105}
106
107TEST(uchar, c16rtomb_null_char) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000108 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
109 uselocale(LC_GLOBAL_LOCALE);
110
Dan Albert7a7f9952014-06-02 11:33:04 -0700111 char bytes[MB_LEN_MAX];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700112 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700113}
114
115TEST(uchar, c16rtomb) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700116 char bytes[MB_LEN_MAX];
117
118 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700119 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700120 EXPECT_EQ('h', bytes[0]);
121
122 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
123 uselocale(LC_GLOBAL_LOCALE);
124
125 // 1-byte UTF-8.
126 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700127 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700128 EXPECT_EQ('h', bytes[0]);
129 // 2-byte UTF-8.
130 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700131 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700132 EXPECT_EQ('\xc2', bytes[0]);
133 EXPECT_EQ('\xa2', bytes[1]);
134 // 3-byte UTF-8.
135 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700136 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700137 EXPECT_EQ('\xe2', bytes[0]);
138 EXPECT_EQ('\x82', bytes[1]);
139 EXPECT_EQ('\xac', bytes[2]);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700140 // 4-byte UTF-8 from a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700141 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700142 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
143 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700144 EXPECT_EQ('\xf4', bytes[0]);
145 EXPECT_EQ('\x8a', bytes[1]);
146 EXPECT_EQ('\xaf', bytes[2]);
147 EXPECT_EQ('\x8d', bytes[3]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700148}
149
150TEST(uchar, c16rtomb_invalid) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000151 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
152 uselocale(LC_GLOBAL_LOCALE);
153
Dan Albert7a7f9952014-06-02 11:33:04 -0700154 char bytes[MB_LEN_MAX];
155
156 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700157 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700158
Yi Kong32bc0fc2018-08-02 17:31:13 -0700159 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
160 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700161}
162
163TEST(uchar, mbrtoc16_null) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000164 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
165 uselocale(LC_GLOBAL_LOCALE);
166
Yi Kong32bc0fc2018-08-02 17:31:13 -0700167 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700168}
169
170TEST(uchar, mbrtoc16_zero_len) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000171 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
172 uselocale(LC_GLOBAL_LOCALE);
173
Dan Albert7a7f9952014-06-02 11:33:04 -0700174 char16_t out;
175
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400176 out = L'x';
Dan Albert16007d52023-07-20 23:38:57 +0000177 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "hello", 0, nullptr));
Dan Albert9a9bbe52023-07-18 23:12:47 +0000178 EXPECT_EQ(L'x', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700179
Dan Albert16007d52023-07-20 23:38:57 +0000180 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "hello", 0, nullptr));
181 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "", 0, nullptr));
Dan Albert9a9bbe52023-07-18 23:12:47 +0000182 EXPECT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
183 EXPECT_EQ(L'h', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700184}
185
186TEST(uchar, mbrtoc16) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700187 char16_t out;
188
189 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
190 uselocale(LC_GLOBAL_LOCALE);
191
192 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700193 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700194 ASSERT_EQ(L'a', out);
195 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700196 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700197 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
198 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700199 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700200 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700201 // 4-byte UTF-8 will be returned as a surrogate pair...
Dan Albert1e8e0c12023-07-19 19:14:45 +0000202 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
203 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
204 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
205 "\xf4\x8a\xaf\x8d"
206 "ef",
207 6, nullptr));
208 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1252ab02023-07-14 21:23:36 +0000209}
210
211TEST(uchar, mbrtoc16_long_sequences) {
212 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
213 uselocale(LC_GLOBAL_LOCALE);
214
215 char16_t out = u'\0';
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700216 errno = 0;
Dan Albert1252ab02023-07-14 21:23:36 +0000217 auto result = mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000218 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert1252ab02023-07-14 21:23:36 +0000219 EXPECT_EQ(static_cast<size_t>(-1), result);
220 EXPECT_EQ(EILSEQ, errno);
221 EXPECT_EQ(u'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000222 } else {
223 EXPECT_EQ(5U, result);
224 EXPECT_EQ(0, errno);
225 EXPECT_EQ(u'\uf94a', out);
Dan Albert1252ab02023-07-14 21:23:36 +0000226 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700227}
228
229TEST(uchar, mbrtoc16_reserved_range) {
Dan Albertef8e1582023-07-19 19:34:12 +0000230 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Dan Alberta0d0e352023-07-19 22:12:59 +0000231 uselocale(LC_GLOBAL_LOCALE);
Dan Albertef8e1582023-07-19 19:34:12 +0000232
233 errno = 0;
234 char16_t out = u'\0';
235 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
236 EXPECT_EQ(u'\0', out);
237 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700238}
239
240TEST(uchar, mbrtoc16_beyond_range) {
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000241 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Dan Alberta0d0e352023-07-19 22:12:59 +0000242 uselocale(LC_GLOBAL_LOCALE);
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000243
244 errno = 0;
245 char16_t out = u'\0';
246 auto result = mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr);
247 if (kLibcRejectsOverLongUtf8Sequences) {
248 EXPECT_EQ(static_cast<size_t>(-1), result);
249 EXPECT_EQ(u'\0', out);
250 EXPECT_EQ(EILSEQ, errno);
251 } else {
252 EXPECT_EQ(4U, result);
253 EXPECT_EQ(u'\xdcc0', out);
254 EXPECT_EQ(0, errno);
255 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700256}
257
Dan Albert7a7f9952014-06-02 11:33:04 -0700258void test_mbrtoc16_incomplete(mbstate_t* ps) {
259 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
260 uselocale(LC_GLOBAL_LOCALE);
261
262 char16_t out;
263 // 2-byte UTF-8.
264 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
265 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
266 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
267 ASSERT_TRUE(mbsinit(ps));
268 // 3-byte UTF-8.
269 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
270 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
271 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
272 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
273 ASSERT_TRUE(mbsinit(ps));
274 // 4-byte UTF-8.
275 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
276 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
Dan Albert1e8e0c12023-07-19 19:14:45 +0000277 ASSERT_EQ(1U, mbrtoc16(&out,
278 "\x8d"
279 "ef",
280 3, ps));
281 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
282 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
283 "\x80"
284 "ef",
285 3, ps));
286 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700287 ASSERT_TRUE(mbsinit(ps));
288
289 // Invalid 2-byte
290 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700291 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700292 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
293 ASSERT_EQ(EILSEQ, errno);
294}
Dan Albert7a7f9952014-06-02 11:33:04 -0700295
296TEST(uchar, mbrtoc16_incomplete) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000297 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
298 uselocale(LC_GLOBAL_LOCALE);
299
Dan Albert7a7f9952014-06-02 11:33:04 -0700300 mbstate_t ps;
301 memset(&ps, 0, sizeof(ps));
302
303 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700304 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700305}
306
307TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700308 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
309 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700310
311 char bytes[MB_LEN_MAX];
312
Elliott Hughes697f42a2017-07-14 17:00:05 -0700313 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700314 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700315 EXPECT_EQ('\0', bytes[0]);
316 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700317
318 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700319 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700320 EXPECT_EQ('h', bytes[0]);
321
322 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
323 uselocale(LC_GLOBAL_LOCALE);
324
325 // 1-byte UTF-8.
326 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700327 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700328 EXPECT_EQ('h', bytes[0]);
329 // 2-byte UTF-8.
330 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700331 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700332 EXPECT_EQ('\xc2', bytes[0]);
333 EXPECT_EQ('\xa2', bytes[1]);
334 // 3-byte UTF-8.
335 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700336 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700337 EXPECT_EQ('\xe2', bytes[0]);
338 EXPECT_EQ('\x82', bytes[1]);
339 EXPECT_EQ('\xac', bytes[2]);
340 // 4-byte UTF-8.
341 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700342 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700343 EXPECT_EQ('\xf0', bytes[0]);
344 EXPECT_EQ('\xa4', bytes[1]);
345 EXPECT_EQ('\xad', bytes[2]);
346 EXPECT_EQ('\xa2', bytes[3]);
347 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700348 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700349 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700350 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700351}
352
Elliott Hughes402c7622018-07-06 17:18:05 -0700353TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700354 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
355 uselocale(LC_GLOBAL_LOCALE);
356
357 char32_t out[8] = {};
358 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
359 ASSERT_EQ(0xfffeU, out[0]);
360 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
361 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700362}
363
364TEST(uchar, mbrtoc32_out_of_range) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700365 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
366 uselocale(LC_GLOBAL_LOCALE);
367
Dan Albert9fa76f12023-07-18 22:58:53 +0000368 char32_t out = U'\0';
Elliott Hughes402c7622018-07-06 17:18:05 -0700369 errno = 0;
Dan Albert9fa76f12023-07-18 22:58:53 +0000370 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000371 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert9fa76f12023-07-18 22:58:53 +0000372 EXPECT_EQ(static_cast<size_t>(-1), result);
373 EXPECT_EQ(EILSEQ, errno);
374 EXPECT_EQ(U'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000375 } else {
376 EXPECT_EQ(4U, result);
377 EXPECT_EQ(0, errno);
378 EXPECT_EQ(U'\x140000', out);
Dan Albert9fa76f12023-07-18 22:58:53 +0000379 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700380}
381
Dan Albert7a7f9952014-06-02 11:33:04 -0700382TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700383 char32_t out[8];
384
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400385 out[0] = L'x';
Dan Albert16007d52023-07-20 23:38:57 +0000386 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "hello", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000387 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700388
Dan Albert16007d52023-07-20 23:38:57 +0000389 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "hello", 0, nullptr));
390 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000391 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
392 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700393
Dan Albert16007d52023-07-20 23:38:57 +0000394 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "hello", 0, nullptr));
395 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000396 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700397
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000398 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700399
400 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
401 uselocale(LC_GLOBAL_LOCALE);
402
403 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000404 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
405 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700406 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000407 EXPECT_EQ(2U, mbrtoc32(out,
408 "\xc2\xa2"
409 "cdef",
410 6, nullptr));
411 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700412 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000413 EXPECT_EQ(3U, mbrtoc32(out,
414 "\xe2\x82\xac"
415 "def",
416 6, nullptr));
417 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700418 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000419 EXPECT_EQ(4U, mbrtoc32(out,
420 "\xf0\xa4\xad\xa2"
421 "ef",
422 6, nullptr));
423 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700424#if defined(__BIONIC__) // glibc allows this.
425 // Illegal 5-byte UTF-8.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700426 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000427 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
428 "\xf8\xa1\xa2\xa3\xa4"
429 "f",
430 6, nullptr));
431 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700432#endif
433 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700434 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000435 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
436 "\xf0\x82\x82\xac"
437 "ef",
438 6, nullptr));
439 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700440}
441
Dan Albert7a7f9952014-06-02 11:33:04 -0700442void test_mbrtoc32_incomplete(mbstate_t* ps) {
443 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
444 uselocale(LC_GLOBAL_LOCALE);
445
446 char32_t out;
447 // 2-byte UTF-8.
448 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
449 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
450 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
451 ASSERT_TRUE(mbsinit(ps));
452 // 3-byte UTF-8.
453 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
454 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
455 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
456 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
457 ASSERT_TRUE(mbsinit(ps));
458 // 4-byte UTF-8.
459 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
460 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
461 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
462 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
463 ASSERT_TRUE(mbsinit(ps));
464
465 // Invalid 2-byte
466 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700467 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700468 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
469 ASSERT_EQ(EILSEQ, errno);
470}
Dan Albert7a7f9952014-06-02 11:33:04 -0700471
472TEST(uchar, mbrtoc32_incomplete) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000473 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
474 uselocale(LC_GLOBAL_LOCALE);
475
Dan Albert7a7f9952014-06-02 11:33:04 -0700476 mbstate_t ps;
477 memset(&ps, 0, sizeof(ps));
478
479 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700480 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700481}