blob: 522d5ac3c32f0f7bb0b8487d8f5eb61cc169dc8a [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
18#include <sys/cdefs.h>
19#if defined(__BIONIC__)
20#define HAVE_UCHAR 1
21#elif defined(__GLIBC__)
Dan Albert7a7f9952014-06-02 11:33:04 -070022#define HAVE_UCHAR __GLIBC_PREREQ(2, 16)
23#endif
24
25#include <gtest/gtest.h>
26
27#include <errno.h>
28#include <limits.h>
29#include <locale.h>
30#include <stdint.h>
31
32#if HAVE_UCHAR
33#include <uchar.h>
34#endif
35
36TEST(uchar, sizeof_uchar_t) {
37#if HAVE_UCHAR
38 EXPECT_EQ(2U, sizeof(char16_t));
39 EXPECT_EQ(4U, sizeof(char32_t));
40#else
41 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
42#endif
43}
44
45TEST(uchar, start_state) {
46#if HAVE_UCHAR
47 char out[MB_LEN_MAX];
48 mbstate_t ps;
49
50 // Any non-initial state is invalid when calling c32rtomb.
51 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070052 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070053 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
54 EXPECT_EQ(EILSEQ, errno);
55
Yi Kong32bc0fc2018-08-02 17:31:13 -070056 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
Dan Albert7a7f9952014-06-02 11:33:04 -070057 // state should be reset.
58 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070059 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
60 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070061 EXPECT_TRUE(mbsinit(&ps));
62
63 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070064 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070065 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
66 EXPECT_TRUE(mbsinit(&ps));
67#else
68 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
69#endif
70}
71
72TEST(uchar, c16rtomb_null_out) {
73#if HAVE_UCHAR
Yi Kong32bc0fc2018-08-02 17:31:13 -070074 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
75 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070076#else
77 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
78#endif
79}
80
81TEST(uchar, c16rtomb_null_char) {
82#if HAVE_UCHAR
83 char bytes[MB_LEN_MAX];
Yi Kong32bc0fc2018-08-02 17:31:13 -070084 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070085#else
86 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
87#endif
88}
89
90TEST(uchar, c16rtomb) {
91#if HAVE_UCHAR
92 char bytes[MB_LEN_MAX];
93
94 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -070096 EXPECT_EQ('h', bytes[0]);
97
98 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
99 uselocale(LC_GLOBAL_LOCALE);
100
101 // 1-byte UTF-8.
102 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700103 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700104 EXPECT_EQ('h', bytes[0]);
105 // 2-byte UTF-8.
106 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700107 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700108 EXPECT_EQ('\xc2', bytes[0]);
109 EXPECT_EQ('\xa2', bytes[1]);
110 // 3-byte UTF-8.
111 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700112 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700113 EXPECT_EQ('\xe2', bytes[0]);
114 EXPECT_EQ('\x82', bytes[1]);
115 EXPECT_EQ('\xac', bytes[2]);
116#else
117 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
118#endif
119}
120
121TEST(uchar, c16rtomb_surrogate) {
122#if HAVE_UCHAR
123 char bytes[MB_LEN_MAX];
124
125 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700126 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
127 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700128 EXPECT_EQ('\xf4', bytes[0]);
129 EXPECT_EQ('\x8a', bytes[1]);
130 EXPECT_EQ('\xaf', bytes[2]);
131 EXPECT_EQ('\x8d', bytes[3]);
132#else
133 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
134#endif
135}
136
137TEST(uchar, c16rtomb_invalid) {
138#if HAVE_UCHAR
139 char bytes[MB_LEN_MAX];
140
141 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700142 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700143
Yi Kong32bc0fc2018-08-02 17:31:13 -0700144 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
145 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700146#else
147 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
148#endif
149}
150
151TEST(uchar, mbrtoc16_null) {
152#if HAVE_UCHAR
Yi Kong32bc0fc2018-08-02 17:31:13 -0700153 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700154#else
155 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
156#endif
157}
158
159TEST(uchar, mbrtoc16_zero_len) {
160#if HAVE_UCHAR
161 char16_t out;
162
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400163 out = L'x';
Yi Kong32bc0fc2018-08-02 17:31:13 -0700164 ASSERT_EQ(0U, mbrtoc16(&out, "hello", 0, nullptr));
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400165 ASSERT_EQ(L'x', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700166
Yi Kong32bc0fc2018-08-02 17:31:13 -0700167 ASSERT_EQ(0U, mbrtoc16(&out, "hello", 0, nullptr));
168 ASSERT_EQ(0U, mbrtoc16(&out, "", 0, nullptr));
169 ASSERT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700170 ASSERT_EQ(L'h', out);
171#else
172 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
173#endif
174}
175
176TEST(uchar, mbrtoc16) {
177#if HAVE_UCHAR
178 char16_t out;
179
180 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
181 uselocale(LC_GLOBAL_LOCALE);
182
183 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700184 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700185 ASSERT_EQ(L'a', out);
186 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700187 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700188 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
189 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700190 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700191 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
192#else
193 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
194#endif
195}
196
197TEST(uchar, mbrtoc16_surrogate) {
198#if HAVE_UCHAR
199 char16_t out;
200
201 ASSERT_EQ(static_cast<size_t>(-3),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700202 mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700203 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700204 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d" "ef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700205 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
206#else
207 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
208#endif
209}
210
211TEST(uchar, mbrtoc16_reserved_range) {
212#if HAVE_UCHAR
213 char16_t out;
214 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700215 mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700216#else
217 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
218#endif
219}
220
221TEST(uchar, mbrtoc16_beyond_range) {
222#if HAVE_UCHAR
223 char16_t out;
224 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700225 mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700226#else
227 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
228#endif
229}
230
231#if HAVE_UCHAR
232void test_mbrtoc16_incomplete(mbstate_t* ps) {
233 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
234 uselocale(LC_GLOBAL_LOCALE);
235
236 char16_t out;
237 // 2-byte UTF-8.
238 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
239 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
240 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
241 ASSERT_TRUE(mbsinit(ps));
242 // 3-byte UTF-8.
243 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
244 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
245 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
246 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
247 ASSERT_TRUE(mbsinit(ps));
248 // 4-byte UTF-8.
249 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
250 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
251 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
252 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
253 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
254 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
255 ASSERT_TRUE(mbsinit(ps));
256
257 // Invalid 2-byte
258 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
259 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
260 ASSERT_EQ(EILSEQ, errno);
261}
262#endif
263
264TEST(uchar, mbrtoc16_incomplete) {
265#if HAVE_UCHAR
266 mbstate_t ps;
267 memset(&ps, 0, sizeof(ps));
268
269 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700270 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700271#else
272 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
273#endif
274}
275
276TEST(uchar, c32rtomb) {
277#if HAVE_UCHAR
Yi Kong32bc0fc2018-08-02 17:31:13 -0700278 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
279 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700280
281 char bytes[MB_LEN_MAX];
282
Elliott Hughes697f42a2017-07-14 17:00:05 -0700283 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700285 EXPECT_EQ('\0', bytes[0]);
286 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700287
288 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700289 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700290 EXPECT_EQ('h', bytes[0]);
291
292 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
293 uselocale(LC_GLOBAL_LOCALE);
294
295 // 1-byte UTF-8.
296 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700297 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700298 EXPECT_EQ('h', bytes[0]);
299 // 2-byte UTF-8.
300 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700301 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700302 EXPECT_EQ('\xc2', bytes[0]);
303 EXPECT_EQ('\xa2', bytes[1]);
304 // 3-byte UTF-8.
305 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700306 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700307 EXPECT_EQ('\xe2', bytes[0]);
308 EXPECT_EQ('\x82', bytes[1]);
309 EXPECT_EQ('\xac', bytes[2]);
310 // 4-byte UTF-8.
311 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700312 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700313 EXPECT_EQ('\xf0', bytes[0]);
314 EXPECT_EQ('\xa4', bytes[1]);
315 EXPECT_EQ('\xad', bytes[2]);
316 EXPECT_EQ('\xa2', bytes[3]);
317 // Invalid code point.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700318 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700319 EXPECT_EQ(EILSEQ, errno);
320#else
321 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
322#endif
323}
324
Elliott Hughes402c7622018-07-06 17:18:05 -0700325TEST(uchar, mbrtoc32_valid_non_characters) {
326#if HAVE_UCHAR
327 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
328 uselocale(LC_GLOBAL_LOCALE);
329
330 char32_t out[8] = {};
331 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
332 ASSERT_EQ(0xfffeU, out[0]);
333 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
334 ASSERT_EQ(0xffffU, out[0]);
335#else
336 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
337#endif
338}
339
340TEST(uchar, mbrtoc32_out_of_range) {
341#if HAVE_UCHAR
342 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
343 uselocale(LC_GLOBAL_LOCALE);
344
345 char32_t out[8] = {};
346 errno = 0;
347 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(out, "\xf5\x80\x80\x80", 4, nullptr));
348 ASSERT_EQ(EILSEQ, errno);
349#else
350 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
351#endif
352}
353
Dan Albert7a7f9952014-06-02 11:33:04 -0700354TEST(uchar, mbrtoc32) {
355#if HAVE_UCHAR
356 char32_t out[8];
357
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400358 out[0] = L'x';
Yi Kong32bc0fc2018-08-02 17:31:13 -0700359 ASSERT_EQ(0U, mbrtoc32(out, "hello", 0, nullptr));
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400360 ASSERT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700361
Yi Kong32bc0fc2018-08-02 17:31:13 -0700362 ASSERT_EQ(0U, mbrtoc32(out, "hello", 0, nullptr));
363 ASSERT_EQ(0U, mbrtoc32(out, "", 0, nullptr));
364 ASSERT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400365 ASSERT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700366
Yi Kong32bc0fc2018-08-02 17:31:13 -0700367 ASSERT_EQ(0U, mbrtoc32(nullptr, "hello", 0, nullptr));
368 ASSERT_EQ(0U, mbrtoc32(nullptr, "", 0, nullptr));
369 ASSERT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700370
Yi Kong32bc0fc2018-08-02 17:31:13 -0700371 ASSERT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700372
373 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
374 uselocale(LC_GLOBAL_LOCALE);
375
376 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700377 ASSERT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400378 ASSERT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700379 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700380 ASSERT_EQ(2U, mbrtoc32(out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700381 ASSERT_EQ(static_cast<char32_t>(0x00a2), out[0]);
382 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700383 ASSERT_EQ(3U, mbrtoc32(out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700384 ASSERT_EQ(static_cast<char32_t>(0x20ac), out[0]);
385 // 4-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700386 ASSERT_EQ(4U, mbrtoc32(out, "\xf0\xa4\xad\xa2" "ef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700387 ASSERT_EQ(static_cast<char32_t>(0x24b62), out[0]);
388#if defined(__BIONIC__) // glibc allows this.
389 // Illegal 5-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700390 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(out, "\xf8\xa1\xa2\xa3\xa4" "f", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700391 ASSERT_EQ(EILSEQ, errno);
392#endif
393 // Illegal over-long sequence.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700394 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(out, "\xf0\x82\x82\xac" "ef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700395 ASSERT_EQ(EILSEQ, errno);
396#else
397 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
398#endif
399}
400
401#if HAVE_UCHAR
402void test_mbrtoc32_incomplete(mbstate_t* ps) {
403 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
404 uselocale(LC_GLOBAL_LOCALE);
405
406 char32_t out;
407 // 2-byte UTF-8.
408 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
409 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
410 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
411 ASSERT_TRUE(mbsinit(ps));
412 // 3-byte UTF-8.
413 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
414 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
415 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
416 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
417 ASSERT_TRUE(mbsinit(ps));
418 // 4-byte UTF-8.
419 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
420 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
421 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
422 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
423 ASSERT_TRUE(mbsinit(ps));
424
425 // Invalid 2-byte
426 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
427 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
428 ASSERT_EQ(EILSEQ, errno);
429}
430#endif
431
432TEST(uchar, mbrtoc32_incomplete) {
433#if HAVE_UCHAR
434 mbstate_t ps;
435 memset(&ps, 0, sizeof(ps));
436
437 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700438 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700439#else
440 GTEST_LOG_(INFO) << "uchar.h is unavailable.\n";
441#endif
442}