blob: 793a5b3a0d30e7e2dc780b8120d0b44208e5622d [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 Albert9a9bbe52023-07-18 23:12:47 +000043// C23 7.30.1 (for each `mbrtoc*` function) says:
44//
45// Returns:
46//
47// 0 if the next n or fewer bytes complete the multibyte character that
48// corresponds to the null wide character (which is the value stored).
49//
50// (size_t)(-2) if the next n bytes contribute to an incomplete (but
51// potentially valid) multibyte character, and all n bytes have been
52// processed (no value is stored).
53//
54// Bionic historically interpreted the behavior when n is 0 to be the next 0
55// bytes decoding to the null. That's a pretty bad interpretation, and both
56// glibc and musl return -2 for that case.
57//
58// The tests currently checks the incorrect behavior for bionic because gtest
59// doesn't support explicit xfail annotations. The behavior difference here
60// should be fixed, but danalbert wants to add more tests before tackling the
61// bugs.
62#ifdef __ANDROID__
63constexpr size_t kExpectedResultForZeroLength = 0U;
64#else
65constexpr size_t kExpectedResultForZeroLength = static_cast<size_t>(-2);
66#endif
67
Dan Albert7a7f9952014-06-02 11:33:04 -070068TEST(uchar, sizeof_uchar_t) {
Dan Albert7a7f9952014-06-02 11:33:04 -070069 EXPECT_EQ(2U, sizeof(char16_t));
70 EXPECT_EQ(4U, sizeof(char32_t));
Dan Albert7a7f9952014-06-02 11:33:04 -070071}
72
73TEST(uchar, start_state) {
Dan Albert7a7f9952014-06-02 11:33:04 -070074 char out[MB_LEN_MAX];
75 mbstate_t ps;
76
77 // Any non-initial state is invalid when calling c32rtomb.
78 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070079 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -070080 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -070081 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
82 EXPECT_EQ(EILSEQ, errno);
83
Yi Kong32bc0fc2018-08-02 17:31:13 -070084 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
Dan Albert7a7f9952014-06-02 11:33:04 -070085 // state should be reset.
86 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070087 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
88 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070089 EXPECT_TRUE(mbsinit(&ps));
90
91 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070092 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070093 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
94 EXPECT_TRUE(mbsinit(&ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070095}
96
97TEST(uchar, c16rtomb_null_out) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
99 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700100}
101
102TEST(uchar, c16rtomb_null_char) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700103 char bytes[MB_LEN_MAX];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700104 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700105}
106
107TEST(uchar, c16rtomb) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700108 char bytes[MB_LEN_MAX];
109
110 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700111 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700112 EXPECT_EQ('h', bytes[0]);
113
114 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
115 uselocale(LC_GLOBAL_LOCALE);
116
117 // 1-byte UTF-8.
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 // 2-byte UTF-8.
122 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700123 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700124 EXPECT_EQ('\xc2', bytes[0]);
125 EXPECT_EQ('\xa2', bytes[1]);
126 // 3-byte UTF-8.
127 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700128 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700129 EXPECT_EQ('\xe2', bytes[0]);
130 EXPECT_EQ('\x82', bytes[1]);
131 EXPECT_EQ('\xac', bytes[2]);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700132 // 4-byte UTF-8 from a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700133 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700134 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
135 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700136 EXPECT_EQ('\xf4', bytes[0]);
137 EXPECT_EQ('\x8a', bytes[1]);
138 EXPECT_EQ('\xaf', bytes[2]);
139 EXPECT_EQ('\x8d', bytes[3]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700140}
141
142TEST(uchar, c16rtomb_invalid) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700143 char bytes[MB_LEN_MAX];
144
145 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700146 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700147
Yi Kong32bc0fc2018-08-02 17:31:13 -0700148 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
149 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700150}
151
152TEST(uchar, mbrtoc16_null) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700153 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700154}
155
156TEST(uchar, mbrtoc16_zero_len) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700157 char16_t out;
158
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400159 out = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000160 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "hello", 0, nullptr));
161 EXPECT_EQ(L'x', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700162
Dan Albert9a9bbe52023-07-18 23:12:47 +0000163 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "hello", 0, nullptr));
164 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "", 0, nullptr));
165 EXPECT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
166 EXPECT_EQ(L'h', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700167}
168
169TEST(uchar, mbrtoc16) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700170 char16_t out;
171
172 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
173 uselocale(LC_GLOBAL_LOCALE);
174
175 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700176 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700177 ASSERT_EQ(L'a', out);
178 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700179 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700180 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
181 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700182 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700183 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700184 // 4-byte UTF-8 will be returned as a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700185 ASSERT_EQ(static_cast<size_t>(-3),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700186 mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700187 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700188 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d" "ef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700189 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1252ab02023-07-14 21:23:36 +0000190}
191
192TEST(uchar, mbrtoc16_long_sequences) {
193 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
194 uselocale(LC_GLOBAL_LOCALE);
195
196 char16_t out = u'\0';
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700197 errno = 0;
Dan Albert1252ab02023-07-14 21:23:36 +0000198 auto result = mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr);
199 if (kLibcSupportsLongUtf8Sequences) {
200 EXPECT_EQ(5U, result);
201 EXPECT_EQ(0, errno);
202 EXPECT_EQ(u'\uf94a', out);
203 } else {
204 EXPECT_EQ(static_cast<size_t>(-1), result);
205 EXPECT_EQ(EILSEQ, errno);
206 EXPECT_EQ(u'\0', out);
207 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700208}
209
210TEST(uchar, mbrtoc16_reserved_range) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700211 char16_t out;
212 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700213 mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700214}
215
216TEST(uchar, mbrtoc16_beyond_range) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700217 char16_t out;
218 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700219 mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700220}
221
Dan Albert7a7f9952014-06-02 11:33:04 -0700222void test_mbrtoc16_incomplete(mbstate_t* ps) {
223 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
224 uselocale(LC_GLOBAL_LOCALE);
225
226 char16_t out;
227 // 2-byte UTF-8.
228 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
229 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
230 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
231 ASSERT_TRUE(mbsinit(ps));
232 // 3-byte UTF-8.
233 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
234 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
235 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
236 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
237 ASSERT_TRUE(mbsinit(ps));
238 // 4-byte UTF-8.
239 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
240 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
241 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
242 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
243 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
244 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
245 ASSERT_TRUE(mbsinit(ps));
246
247 // Invalid 2-byte
248 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700249 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700250 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
251 ASSERT_EQ(EILSEQ, errno);
252}
Dan Albert7a7f9952014-06-02 11:33:04 -0700253
254TEST(uchar, mbrtoc16_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700255 mbstate_t ps;
256 memset(&ps, 0, sizeof(ps));
257
258 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700259 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700260}
261
262TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700263 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
264 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700265
266 char bytes[MB_LEN_MAX];
267
Elliott Hughes697f42a2017-07-14 17:00:05 -0700268 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700269 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700270 EXPECT_EQ('\0', bytes[0]);
271 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700272
273 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700274 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700275 EXPECT_EQ('h', bytes[0]);
276
277 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
278 uselocale(LC_GLOBAL_LOCALE);
279
280 // 1-byte UTF-8.
281 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700282 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700283 EXPECT_EQ('h', bytes[0]);
284 // 2-byte UTF-8.
285 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700286 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700287 EXPECT_EQ('\xc2', bytes[0]);
288 EXPECT_EQ('\xa2', bytes[1]);
289 // 3-byte UTF-8.
290 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700291 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700292 EXPECT_EQ('\xe2', bytes[0]);
293 EXPECT_EQ('\x82', bytes[1]);
294 EXPECT_EQ('\xac', bytes[2]);
295 // 4-byte UTF-8.
296 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700297 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700298 EXPECT_EQ('\xf0', bytes[0]);
299 EXPECT_EQ('\xa4', bytes[1]);
300 EXPECT_EQ('\xad', bytes[2]);
301 EXPECT_EQ('\xa2', bytes[3]);
302 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700303 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700304 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700305 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700306}
307
Elliott Hughes402c7622018-07-06 17:18:05 -0700308TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700309 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
310 uselocale(LC_GLOBAL_LOCALE);
311
312 char32_t out[8] = {};
313 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
314 ASSERT_EQ(0xfffeU, out[0]);
315 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
316 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700317}
318
319TEST(uchar, mbrtoc32_out_of_range) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700320 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
321 uselocale(LC_GLOBAL_LOCALE);
322
Dan Albert9fa76f12023-07-18 22:58:53 +0000323 char32_t out = U'\0';
Elliott Hughes402c7622018-07-06 17:18:05 -0700324 errno = 0;
Dan Albert9fa76f12023-07-18 22:58:53 +0000325 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
326 if (kLibcSupportsLongUtf8Sequences) {
327 EXPECT_EQ(4U, result);
328 EXPECT_EQ(0, errno);
329 EXPECT_EQ(U'\x140000', out);
330 } else {
331 EXPECT_EQ(static_cast<size_t>(-1), result);
332 EXPECT_EQ(EILSEQ, errno);
333 EXPECT_EQ(U'\0', out);
334 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700335}
336
Dan Albert7a7f9952014-06-02 11:33:04 -0700337TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700338 char32_t out[8];
339
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400340 out[0] = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000341 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000342 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700343
Dan Albert9a9bbe52023-07-18 23:12:47 +0000344 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
345 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000346 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
347 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700348
Dan Albert9a9bbe52023-07-18 23:12:47 +0000349 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "hello", 0, nullptr));
350 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000351 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700352
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000353 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700354
355 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
356 uselocale(LC_GLOBAL_LOCALE);
357
358 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000359 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
360 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700361 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000362 EXPECT_EQ(2U, mbrtoc32(out,
363 "\xc2\xa2"
364 "cdef",
365 6, nullptr));
366 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700367 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000368 EXPECT_EQ(3U, mbrtoc32(out,
369 "\xe2\x82\xac"
370 "def",
371 6, nullptr));
372 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700373 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000374 EXPECT_EQ(4U, mbrtoc32(out,
375 "\xf0\xa4\xad\xa2"
376 "ef",
377 6, nullptr));
378 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700379#if defined(__BIONIC__) // glibc allows this.
380 // Illegal 5-byte UTF-8.
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 "\xf8\xa1\xa2\xa3\xa4"
384 "f",
385 6, nullptr));
386 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700387#endif
388 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700389 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000390 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
391 "\xf0\x82\x82\xac"
392 "ef",
393 6, nullptr));
394 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700395}
396
Dan Albert7a7f9952014-06-02 11:33:04 -0700397void test_mbrtoc32_incomplete(mbstate_t* ps) {
398 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
399 uselocale(LC_GLOBAL_LOCALE);
400
401 char32_t out;
402 // 2-byte UTF-8.
403 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
404 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
405 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
406 ASSERT_TRUE(mbsinit(ps));
407 // 3-byte UTF-8.
408 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
409 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
410 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
411 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
412 ASSERT_TRUE(mbsinit(ps));
413 // 4-byte UTF-8.
414 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
415 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
416 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
417 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
418 ASSERT_TRUE(mbsinit(ps));
419
420 // Invalid 2-byte
421 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700422 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700423 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
424 ASSERT_EQ(EILSEQ, errno);
425}
Dan Albert7a7f9952014-06-02 11:33:04 -0700426
427TEST(uchar, mbrtoc32_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700428 mbstate_t ps;
429 memset(&ps, 0, sizeof(ps));
430
431 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700432 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700433}