blob: e321d83bee97f68330f5965696ecdf63a0a351c3 [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)
36constexpr bool kLibcSupportsLongUtf8Sequences = 0;
37#elif defined(__GLIBC__)
38constexpr bool kLibcSupportsLongUtf8Sequences = 1;
39#else
40#error kLibcSupportsLongUtf8Sequences must be configured for this platform
41#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 Albert7a7f9952014-06-02 11:33:04 -070049 char out[MB_LEN_MAX];
50 mbstate_t ps;
51
52 // Any non-initial state is invalid when calling c32rtomb.
53 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070054 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -070055 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -070056 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
57 EXPECT_EQ(EILSEQ, errno);
58
Yi Kong32bc0fc2018-08-02 17:31:13 -070059 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
Dan Albert7a7f9952014-06-02 11:33:04 -070060 // state should be reset.
61 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070062 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
63 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070064 EXPECT_TRUE(mbsinit(&ps));
65
66 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070067 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070068 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
69 EXPECT_TRUE(mbsinit(&ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070070}
71
72TEST(uchar, c16rtomb_null_out) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070073 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
74 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070075}
76
77TEST(uchar, c16rtomb_null_char) {
Dan Albert7a7f9952014-06-02 11:33:04 -070078 char bytes[MB_LEN_MAX];
Yi Kong32bc0fc2018-08-02 17:31:13 -070079 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070080}
81
82TEST(uchar, c16rtomb) {
Dan Albert7a7f9952014-06-02 11:33:04 -070083 char bytes[MB_LEN_MAX];
84
85 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -070086 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070087 EXPECT_EQ('h', bytes[0]);
88
89 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
90 uselocale(LC_GLOBAL_LOCALE);
91
92 // 1-byte UTF-8.
93 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -070094 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070095 EXPECT_EQ('h', bytes[0]);
96 // 2-byte UTF-8.
97 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070099 EXPECT_EQ('\xc2', bytes[0]);
100 EXPECT_EQ('\xa2', bytes[1]);
101 // 3-byte UTF-8.
102 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700103 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700104 EXPECT_EQ('\xe2', bytes[0]);
105 EXPECT_EQ('\x82', bytes[1]);
106 EXPECT_EQ('\xac', bytes[2]);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700107 // 4-byte UTF-8 from a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700108 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
110 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700111 EXPECT_EQ('\xf4', bytes[0]);
112 EXPECT_EQ('\x8a', bytes[1]);
113 EXPECT_EQ('\xaf', bytes[2]);
114 EXPECT_EQ('\x8d', bytes[3]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700115}
116
117TEST(uchar, c16rtomb_invalid) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700118 char bytes[MB_LEN_MAX];
119
120 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700122
Yi Kong32bc0fc2018-08-02 17:31:13 -0700123 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
124 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700125}
126
127TEST(uchar, mbrtoc16_null) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700128 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700129}
130
131TEST(uchar, mbrtoc16_zero_len) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700132 char16_t out;
133
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400134 out = L'x';
Yi Kong32bc0fc2018-08-02 17:31:13 -0700135 ASSERT_EQ(0U, mbrtoc16(&out, "hello", 0, nullptr));
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400136 ASSERT_EQ(L'x', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700137
Yi Kong32bc0fc2018-08-02 17:31:13 -0700138 ASSERT_EQ(0U, mbrtoc16(&out, "hello", 0, nullptr));
139 ASSERT_EQ(0U, mbrtoc16(&out, "", 0, nullptr));
140 ASSERT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700141 ASSERT_EQ(L'h', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700142}
143
144TEST(uchar, mbrtoc16) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700145 char16_t out;
146
147 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
148 uselocale(LC_GLOBAL_LOCALE);
149
150 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700151 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700152 ASSERT_EQ(L'a', out);
153 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700154 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700155 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
156 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700157 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700158 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700159 // 4-byte UTF-8 will be returned as a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700160 ASSERT_EQ(static_cast<size_t>(-3),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700161 mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700162 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700163 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d" "ef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700164 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1252ab02023-07-14 21:23:36 +0000165}
166
167TEST(uchar, mbrtoc16_long_sequences) {
168 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
169 uselocale(LC_GLOBAL_LOCALE);
170
171 char16_t out = u'\0';
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700172 errno = 0;
Dan Albert1252ab02023-07-14 21:23:36 +0000173 auto result = mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr);
174 if (kLibcSupportsLongUtf8Sequences) {
175 EXPECT_EQ(5U, result);
176 EXPECT_EQ(0, errno);
177 EXPECT_EQ(u'\uf94a', out);
178 } else {
179 EXPECT_EQ(static_cast<size_t>(-1), result);
180 EXPECT_EQ(EILSEQ, errno);
181 EXPECT_EQ(u'\0', out);
182 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700183}
184
185TEST(uchar, mbrtoc16_reserved_range) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700186 char16_t out;
187 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700188 mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700189}
190
191TEST(uchar, mbrtoc16_beyond_range) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700192 char16_t out;
193 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700195}
196
Dan Albert7a7f9952014-06-02 11:33:04 -0700197void test_mbrtoc16_incomplete(mbstate_t* ps) {
198 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
199 uselocale(LC_GLOBAL_LOCALE);
200
201 char16_t out;
202 // 2-byte UTF-8.
203 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
204 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
205 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
206 ASSERT_TRUE(mbsinit(ps));
207 // 3-byte UTF-8.
208 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
209 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
210 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
211 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
212 ASSERT_TRUE(mbsinit(ps));
213 // 4-byte UTF-8.
214 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
215 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
216 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
217 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
218 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
219 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
220 ASSERT_TRUE(mbsinit(ps));
221
222 // Invalid 2-byte
223 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700224 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700225 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
226 ASSERT_EQ(EILSEQ, errno);
227}
Dan Albert7a7f9952014-06-02 11:33:04 -0700228
229TEST(uchar, mbrtoc16_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700230 mbstate_t ps;
231 memset(&ps, 0, sizeof(ps));
232
233 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700234 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700235}
236
237TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700238 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
239 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700240
241 char bytes[MB_LEN_MAX];
242
Elliott Hughes697f42a2017-07-14 17:00:05 -0700243 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700244 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700245 EXPECT_EQ('\0', bytes[0]);
246 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700247
248 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700249 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700250 EXPECT_EQ('h', bytes[0]);
251
252 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
253 uselocale(LC_GLOBAL_LOCALE);
254
255 // 1-byte UTF-8.
256 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700257 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700258 EXPECT_EQ('h', bytes[0]);
259 // 2-byte UTF-8.
260 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700261 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700262 EXPECT_EQ('\xc2', bytes[0]);
263 EXPECT_EQ('\xa2', bytes[1]);
264 // 3-byte UTF-8.
265 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700266 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700267 EXPECT_EQ('\xe2', bytes[0]);
268 EXPECT_EQ('\x82', bytes[1]);
269 EXPECT_EQ('\xac', bytes[2]);
270 // 4-byte UTF-8.
271 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700272 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700273 EXPECT_EQ('\xf0', bytes[0]);
274 EXPECT_EQ('\xa4', bytes[1]);
275 EXPECT_EQ('\xad', bytes[2]);
276 EXPECT_EQ('\xa2', bytes[3]);
277 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700278 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700279 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700280 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700281}
282
Elliott Hughes402c7622018-07-06 17:18:05 -0700283TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700284 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
285 uselocale(LC_GLOBAL_LOCALE);
286
287 char32_t out[8] = {};
288 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
289 ASSERT_EQ(0xfffeU, out[0]);
290 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
291 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700292}
293
294TEST(uchar, mbrtoc32_out_of_range) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700295 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
296 uselocale(LC_GLOBAL_LOCALE);
297
298 char32_t out[8] = {};
299 errno = 0;
300 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(out, "\xf5\x80\x80\x80", 4, nullptr));
301 ASSERT_EQ(EILSEQ, errno);
Elliott Hughes402c7622018-07-06 17:18:05 -0700302}
303
Dan Albert7a7f9952014-06-02 11:33:04 -0700304TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700305 char32_t out[8];
306
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000307 // C23 says:
308 //
309 // Returns:
310 //
311 // 0 if the next n or fewer bytes complete the multibyte character that
312 // corresponds to the null wide character (which is the value stored).
313 //
314 // (size_t)(-2) if the next n bytes contribute to an incomplete (but
315 // potentially valid) multibyte character, and all n bytes have been
316 // processed (no value is stored).
317 //
318 // Bionic historically interpreted the behavior when n is 0 to be the next 0
319 // bytes decoding to the null. That's a pretty bad interpretation, and both
320 // glibc and musl return -2 for that case.
321 //
322 // The test currently checks the incorrect behavior for bionic because gtest
323 // doesn't support explicit xfail annotations. The behavior difference here
324 // should be fixed, but danalbert wants to add more tests before tackling the
325 // bugs.
326#ifdef __ANDROID__
327 const size_t expected_result_for_zero_length = 0U;
328#else
329 const size_t expected_result_for_zero_length = static_cast<size_t>(-2);
330#endif
331
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400332 out[0] = L'x';
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000333 EXPECT_EQ(expected_result_for_zero_length, mbrtoc32(out, "hello", 0, nullptr));
334 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700335
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000336 EXPECT_EQ(expected_result_for_zero_length, mbrtoc32(out, "hello", 0, nullptr));
337 EXPECT_EQ(expected_result_for_zero_length, mbrtoc32(out, "", 0, nullptr));
338 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
339 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700340
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000341 EXPECT_EQ(expected_result_for_zero_length, mbrtoc32(nullptr, "hello", 0, nullptr));
342 EXPECT_EQ(expected_result_for_zero_length, mbrtoc32(nullptr, "", 0, nullptr));
343 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700344
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000345 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700346
347 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
348 uselocale(LC_GLOBAL_LOCALE);
349
350 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000351 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
352 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700353 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000354 EXPECT_EQ(2U, mbrtoc32(out,
355 "\xc2\xa2"
356 "cdef",
357 6, nullptr));
358 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700359 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000360 EXPECT_EQ(3U, mbrtoc32(out,
361 "\xe2\x82\xac"
362 "def",
363 6, nullptr));
364 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700365 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000366 EXPECT_EQ(4U, mbrtoc32(out,
367 "\xf0\xa4\xad\xa2"
368 "ef",
369 6, nullptr));
370 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700371#if defined(__BIONIC__) // glibc allows this.
372 // Illegal 5-byte UTF-8.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700373 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000374 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
375 "\xf8\xa1\xa2\xa3\xa4"
376 "f",
377 6, nullptr));
378 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700379#endif
380 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700381 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000382 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
383 "\xf0\x82\x82\xac"
384 "ef",
385 6, nullptr));
386 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700387}
388
Dan Albert7a7f9952014-06-02 11:33:04 -0700389void test_mbrtoc32_incomplete(mbstate_t* ps) {
390 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
391 uselocale(LC_GLOBAL_LOCALE);
392
393 char32_t out;
394 // 2-byte UTF-8.
395 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
396 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
397 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
398 ASSERT_TRUE(mbsinit(ps));
399 // 3-byte UTF-8.
400 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
401 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
402 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
403 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
404 ASSERT_TRUE(mbsinit(ps));
405 // 4-byte UTF-8.
406 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
407 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
408 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
409 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
410 ASSERT_TRUE(mbsinit(ps));
411
412 // Invalid 2-byte
413 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700414 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700415 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
416 ASSERT_EQ(EILSEQ, errno);
417}
Dan Albert7a7f9952014-06-02 11:33:04 -0700418
419TEST(uchar, mbrtoc32_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700420 mbstate_t ps;
421 memset(&ps, 0, sizeof(ps));
422
423 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700424 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700425}