blob: 04932ba2112f2a0134bc33527ee2c2a69450f5d3 [file] [log] [blame]
Elliott Hughes77e944f2014-04-04 17:34:51 -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#include <gtest/gtest.h>
18
Elliott Hughes05493712014-04-17 17:30:03 -070019#include <errno.h>
Dan Albert6805c2d2017-08-09 14:55:27 -070020#include <inttypes.h>
Elliott Hughes77e944f2014-04-04 17:34:51 -070021#include <limits.h>
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070022#include <locale.h>
Elliott Hughes966d8a32017-08-29 11:29:28 -070023#include <math.h>
Elliott Hughes3d7a0d92014-04-29 14:46:56 -070024#include <stdint.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070025#include <sys/cdefs.h>
Elliott Hughes77e944f2014-04-04 17:34:51 -070026#include <wchar.h>
27
Elliott Hughes966d8a32017-08-29 11:29:28 -070028#include "utils.h"
Elliott Hughes7f0849f2016-08-26 16:17:17 -070029
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070030#define NUM_WCHARS(num_bytes) ((num_bytes)/sizeof(wchar_t))
Christopher Ferris5c7d9582014-11-13 15:48:39 -080031
Dan Albert686e67d2023-08-03 19:00:09 +000032#ifdef __GLIBC__
33// glibc immediately dereferences the locale passed to all wcsto*_l functions,
34// even if it won't be used, and even if it's LC_GLOBAL_LOCALE, which isn't a
35// pointer to valid memory.
36static locale_t SAFE_LC_GLOBAL_LOCALE = duplocale(LC_GLOBAL_LOCALE);
37#else
38static locale_t SAFE_LC_GLOBAL_LOCALE = LC_GLOBAL_LOCALE;
39#endif
40
Dan Albert53256532023-08-03 19:39:39 +000041// Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
42// newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
43// and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
44// https://datatracker.ietf.org/doc/html/rfc2279.
45//
46// Bionic's unicode implementation was written after the high values were
47// excluded, so it has never supported them. Other implementations (at least
48// as of glibc 2.36), do support those sequences.
49#if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
50constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
51#elif defined(__GLIBC__)
52constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
53#else
54#error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
55#endif
56
Dan Albert512469a2023-08-03 19:34:42 +000057// C23 7.31.6.3.2 (mbrtowc) says:
58//
59// Returns:
60//
61// 0 if the next n or fewer bytes complete the multibyte character that
62// corresponds to the null wide character (which is the value stored).
63//
64// (size_t)(-2) if the next n bytes contribute to an incomplete (but
65// potentially valid) multibyte character, and all n bytes have been
66// processed (no value is stored).
67//
68// Bionic historically interpreted the behavior when n is 0 to be the next 0
69// bytes decoding to the null. That's a pretty bad interpretation, and both
70// glibc and musl return -2 for that case.
71//
72// The tests currently checks the incorrect behavior for bionic because gtest
73// doesn't support explicit xfail annotations. The behavior difference here
74// should be fixed, but danalbert wants to add more tests before tackling the
75// bugs.
76#ifdef __ANDROID__
77constexpr size_t kExpectedResultForZeroLength = 0U;
78#else
79constexpr size_t kExpectedResultForZeroLength = static_cast<size_t>(-2);
80#endif
81
Dan Albert9f30c6b2023-08-03 19:50:28 +000082#if defined(__GLIBC__)
83constexpr bool kLibcSupportsParsingBinaryLiterals = __GLIBC_PREREQ(2, 38);
84#else
85constexpr bool kLibcSupportsParsingBinaryLiterals = true;
86#endif
87
Elliott Hughes77e944f2014-04-04 17:34:51 -070088TEST(wchar, sizeof_wchar_t) {
89 EXPECT_EQ(4U, sizeof(wchar_t));
90 EXPECT_EQ(4U, sizeof(wint_t));
91}
92
93TEST(wchar, mbrlen) {
94 char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
Dan Albert512469a2023-08-03 19:34:42 +000095 EXPECT_EQ(kExpectedResultForZeroLength, mbrlen(&bytes[0], 0, nullptr));
Yi Kong32bc0fc2018-08-02 17:31:13 -070096 EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070097
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
99 EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700100}
101
102TEST(wchar, wctomb_wcrtomb) {
103 // wctomb and wcrtomb behave differently when s == NULL.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700104 EXPECT_EQ(0, wctomb(nullptr, L'h'));
105 EXPECT_EQ(0, wctomb(nullptr, L'\0'));
106 EXPECT_EQ(1U, wcrtomb(nullptr, L'\0', nullptr));
107 EXPECT_EQ(1U, wcrtomb(nullptr, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700108
109 char bytes[MB_LEN_MAX];
110
111 // wctomb and wcrtomb behave similarly for the null wide character.
112 EXPECT_EQ(1, wctomb(bytes, L'\0'));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700113 EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700114
115 // ...and for regular characters.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700116 memset(bytes, 0, sizeof(bytes));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700117 EXPECT_EQ(1, wctomb(bytes, L'h'));
118 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700119 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700120 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700121 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700122
123 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
124 uselocale(LC_GLOBAL_LOCALE);
125
126 // 1-byte UTF-8.
127 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700128 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700129 EXPECT_EQ('h', bytes[0]);
130 // 2-byte UTF-8.
131 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700132 EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700133 EXPECT_EQ('\xc2', bytes[0]);
134 EXPECT_EQ('\xa2', bytes[1]);
135 // 3-byte UTF-8.
136 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700137 EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700138 EXPECT_EQ('\xe2', bytes[0]);
139 EXPECT_EQ('\x82', bytes[1]);
140 EXPECT_EQ('\xac', bytes[2]);
141 // 4-byte UTF-8.
142 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700143 EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700144 EXPECT_EQ('\xf0', bytes[0]);
145 EXPECT_EQ('\xa4', bytes[1]);
146 EXPECT_EQ('\xad', bytes[2]);
147 EXPECT_EQ('\xa2', bytes[3]);
148 // Invalid code point.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700149 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700150 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes77e944f2014-04-04 17:34:51 -0700151}
Elliott Hughes05493712014-04-17 17:30:03 -0700152
Calin Juravle15a63102014-05-08 14:38:35 +0100153TEST(wchar, wcrtomb_start_state) {
Dan Albert9f78c512023-08-03 19:49:09 +0000154 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
155 uselocale(LC_GLOBAL_LOCALE);
156
Calin Juravle15a63102014-05-08 14:38:35 +0100157 char out[MB_LEN_MAX];
158 mbstate_t ps;
159
160 // Any non-initial state is invalid when calling wcrtomb.
161 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700162 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100163 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
164 EXPECT_EQ(EILSEQ, errno);
165
166 // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
167 // state should be reset.
168 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700169 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
170 EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100171 EXPECT_TRUE(mbsinit(&ps));
172
173 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700174 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100175 EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
176 EXPECT_TRUE(mbsinit(&ps));
177}
178
Elliott Hughes05493712014-04-17 17:30:03 -0700179TEST(wchar, wcstombs_wcrtombs) {
Dan Albert9f78c512023-08-03 19:49:09 +0000180 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
181 uselocale(LC_GLOBAL_LOCALE);
182
Elliott Hughes1b836ee2014-04-18 13:32:33 -0700183 const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
Elliott Hughesf83e6442014-05-01 17:14:59 -0700184 const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
Elliott Hughes05493712014-04-17 17:30:03 -0700185 const wchar_t* src;
186 char bytes[BUFSIZ];
187
188 // Given a NULL destination, these functions count valid characters.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700189 EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
190 EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
191 EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700192 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700193 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700194 EXPECT_EQ(&chars[0], src);
195 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700196 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700197 EXPECT_EQ(&chars[0], src);
198 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700199 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700200 EXPECT_EQ(&chars[0], src);
201
202 // An unrepresentable char just returns an error from wcstombs...
203 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700204 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
Elliott Hughes05493712014-04-17 17:30:03 -0700205 EXPECT_EQ(EILSEQ, errno);
206 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700207 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700208 EXPECT_EQ(EILSEQ, errno);
209
210 // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
211 // to actually convert anything...
212 errno = 0;
213 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700214 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700215 EXPECT_EQ(&bad_chars[0], src);
216 EXPECT_EQ(EILSEQ, errno);
217 errno = 0;
218 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700219 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700220 EXPECT_EQ(&bad_chars[0], src);
221 EXPECT_EQ(EILSEQ, errno);
222
223 // Okay, now let's test actually converting something...
224 memset(bytes, 'x', sizeof(bytes));
225 EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
226 memset(bytes, 'x', sizeof(bytes));
227 EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
228 bytes[5] = 0;
229 EXPECT_STREQ("hellx", bytes);
230 memset(bytes, 'x', sizeof(bytes));
231 EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
232 EXPECT_STREQ("hello", bytes);
233 memset(bytes, 'x', sizeof(bytes));
234 EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
235 EXPECT_STREQ("hello", bytes);
236 errno = 0;
237 memset(bytes, 'x', sizeof(bytes));
238 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
239 EXPECT_EQ(EILSEQ, errno);
240 bytes[3] = 0;
241 EXPECT_STREQ("hix", bytes);
242
243 // wcsrtombs is a bit more informative...
244 memset(bytes, 'x', sizeof(bytes));
245 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700246 EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700247 EXPECT_EQ(&chars[0], src); // No input consumed.
248 EXPECT_EQ(EILSEQ, errno);
249
250 memset(bytes, 'x', sizeof(bytes));
251 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700252 EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700253 EXPECT_EQ(&chars[4], src); // Some input consumed.
254 EXPECT_EQ(EILSEQ, errno);
255 bytes[5] = 0;
256 EXPECT_STREQ("hellx", bytes);
257
258 memset(bytes, 'x', sizeof(bytes));
259 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700260 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
261 EXPECT_EQ(nullptr, src); // All input consumed!
Elliott Hughes05493712014-04-17 17:30:03 -0700262 EXPECT_EQ(EILSEQ, errno);
263 EXPECT_STREQ("hello", bytes);
264
265 memset(bytes, 'x', sizeof(bytes));
266 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700267 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
268 EXPECT_EQ(nullptr, src); // All input consumed.
Elliott Hughes05493712014-04-17 17:30:03 -0700269 EXPECT_EQ(EILSEQ, errno);
270 EXPECT_STREQ("hello", bytes);
271
272 memset(bytes, 'x', sizeof(bytes));
273 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700274 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700275 EXPECT_EQ(&bad_chars[2], src);
276 EXPECT_EQ(EILSEQ, errno);
277 bytes[3] = 0;
278 EXPECT_STREQ("hix", bytes);
Calin Juravle15a63102014-05-08 14:38:35 +0100279
280 // Any non-initial state is invalid when calling wcsrtombs.
281 mbstate_t ps;
282 src = chars;
283 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
285 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100286 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes05493712014-04-17 17:30:03 -0700287}
Elliott Hughes83c07b52014-04-21 18:09:46 -0700288
289TEST(wchar, limits) {
290 ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
291}
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700292
Dan Albert001f8f02014-06-04 09:53:06 -0700293TEST(wchar, wcsstr) {
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700294 const wchar_t* haystack = L"big daddy/giant haystacks!";
295 const wchar_t* empty_haystack = L"";
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700296
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700297 // The empty needle is a special case.
298 ASSERT_EQ(haystack, wcsstr(haystack, L""));
299 ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
300
301 ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
302 ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
303 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
304 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
305 ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
306 ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
307
308 ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
309 ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700310}
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700311
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800312TEST(wchar, wcsstr_80199) {
313 // https://code.google.com/p/android/issues/detail?id=80199
Yi Kong32bc0fc2018-08-02 17:31:13 -0700314 ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800315}
316
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700317TEST(wchar, mbtowc) {
318 wchar_t out[8];
319
Dan Albert512469a2023-08-03 19:34:42 +0000320 // bionic has the same misunderstanding of the result for a zero-length
321 // conversion for mbtowc as it does for all the other multibyte conversion
322 // functions but mbtowc returns different values than all the others:
323 //
324 // C23 7.24.7.2.4:
325 //
326 // If s is a null pointer, the mbtowc function returns a nonzero or zero
327 // value, if multibyte character encodings, respectively, do or do not have
328 // state-dependent encodings. If s is not a null pointer, the mbtowc function
329 // either returns 0 (if s points to the null character), or returns the number
330 // of bytes that are contained in the converted multibyte character (if the
331 // next n or fewer bytes form a valid multibyte character), or returns -1 (if
332 // they do not form a valid multibyte character).
333
334#ifdef __BIONIC__
335 int expected_result_for_zero_length = 0;
336#else
337 int expected_result_for_zero_length = -1;
338#endif
339
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700340 out[0] = 'x';
Dan Albert512469a2023-08-03 19:34:42 +0000341 EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
342 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700343
Dan Albert512469a2023-08-03 19:34:42 +0000344 EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
345 EXPECT_EQ(0, mbtowc(out, "", 0));
346 EXPECT_EQ(1, mbtowc(out, "hello", 1));
347 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700348
Dan Albert512469a2023-08-03 19:34:42 +0000349 EXPECT_EQ(expected_result_for_zero_length, mbtowc(nullptr, "hello", 0));
350 EXPECT_EQ(0, mbtowc(nullptr, "", 0));
351 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700352
Dan Albert512469a2023-08-03 19:34:42 +0000353 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700354}
355
356TEST(wchar, mbrtowc) {
357 wchar_t out[8];
358
359 out[0] = 'x';
Dan Albert512469a2023-08-03 19:34:42 +0000360 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
361 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700362
Dan Albert512469a2023-08-03 19:34:42 +0000363 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
364 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "", 0, nullptr));
365 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
366 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700367
Dan Albert512469a2023-08-03 19:34:42 +0000368 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "hello", 0, nullptr));
369 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "", 0, nullptr));
370 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700371
Dan Albert512469a2023-08-03 19:34:42 +0000372 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700373
Dan Albert512469a2023-08-03 19:34:42 +0000374 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700375 uselocale(LC_GLOBAL_LOCALE);
376
377 // 1-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000378 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
379 EXPECT_EQ(L'a', out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700380 // 2-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000381 EXPECT_EQ(2U, mbrtowc(out,
382 "\xc2\xa2"
383 "cdef",
384 6, nullptr));
385 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700386 // 3-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000387 EXPECT_EQ(3U, mbrtowc(out,
388 "\xe2\x82\xac"
389 "def",
390 6, nullptr));
391 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700392 // 4-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000393 EXPECT_EQ(4U, mbrtowc(out,
394 "\xf0\xa4\xad\xa2"
395 "ef",
396 6, nullptr));
397 EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
Elliott Hughes063525c2014-05-13 11:19:57 -0700398#if defined(__BIONIC__) // glibc allows this.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700399 // Illegal 5-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000400 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
401 "\xf8\xa1\xa2\xa3\xa4"
402 "f",
403 6, nullptr));
404 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700405#endif
406 // Illegal over-long sequence.
Dan Albert512469a2023-08-03 19:34:42 +0000407 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
408 "\xf0\x82\x82\xac"
409 "ef",
410 6, nullptr));
411 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700412}
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700413
Elliott Hughes402c7622018-07-06 17:18:05 -0700414TEST(wchar, mbrtowc_valid_non_characters) {
415 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
416 uselocale(LC_GLOBAL_LOCALE);
417
418 wchar_t out[8] = {};
419
420 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
421 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
422 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
423 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
424}
425
426TEST(wchar, mbrtowc_out_of_range) {
427 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
428 uselocale(LC_GLOBAL_LOCALE);
429
430 wchar_t out[8] = {};
431 errno = 0;
Dan Albert53256532023-08-03 19:39:39 +0000432 auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
433 if (kLibcRejectsOverLongUtf8Sequences) {
434 ASSERT_EQ(static_cast<size_t>(-1), result);
435 ASSERT_EQ(EILSEQ, errno);
436 } else {
437 ASSERT_EQ(4U, result);
438 ASSERT_EQ(0, errno);
439 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700440}
441
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700442static void test_mbrtowc_incomplete(mbstate_t* ps) {
Calin Juravle15a63102014-05-08 14:38:35 +0100443 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
444 uselocale(LC_GLOBAL_LOCALE);
445
446 wchar_t out;
447 // 2-byte UTF-8.
448 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
449 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700450 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100451 ASSERT_TRUE(mbsinit(ps));
452 // 3-byte UTF-8.
453 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
454 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
455 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700456 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100457 ASSERT_TRUE(mbsinit(ps));
458 // 4-byte UTF-8.
459 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
460 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
461 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700462 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100463 ASSERT_TRUE(mbsinit(ps));
464
465 // Invalid 2-byte
466 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
467 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
468 ASSERT_EQ(EILSEQ, errno);
469}
470
471TEST(wchar, mbrtowc_incomplete) {
472 mbstate_t ps;
473 memset(&ps, 0, sizeof(ps));
474
475 test_mbrtowc_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700476 test_mbrtowc_incomplete(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100477}
478
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700479static void test_mbsrtowcs(mbstate_t* ps) {
480 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
481 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
482 constexpr const char* INCOMPLETE = "A" "\xc2";
Calin Juravle15a63102014-05-08 14:38:35 +0100483 wchar_t out[4];
484
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700485 const char* valid = VALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100486 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
487 ASSERT_EQ(L'A', out[0]);
Ben Chengcaff5f22014-05-19 14:27:31 -0700488 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
489 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
490 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
Dan Albert6b55ba52014-07-20 11:51:26 -0700491 // Check that valid has advanced to the next unread character.
Calin Juravle15a63102014-05-08 14:38:35 +0100492 ASSERT_EQ('e', *valid);
493
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800494 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
Dan Albert6b55ba52014-07-20 11:51:26 -0700495 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
496 ASSERT_EQ(L'e', out[0]);
497 ASSERT_EQ(L'f', out[1]);
498 ASSERT_EQ(L'\0', out[2]);
499 // Check that we didn't clobber the rest of out.
500 ASSERT_EQ(L'x', out[3]);
501 // Check that valid has advanced to the end of the string.
Dan Albertb6cc8e02014-07-31 11:31:03 -0700502 ASSERT_EQ(nullptr, valid);
Dan Albert6b55ba52014-07-20 11:51:26 -0700503
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700504 const char* invalid = INVALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100505 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
506 EXPECT_EQ(EILSEQ, errno);
507 ASSERT_EQ('\xc2', *invalid);
508
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700509 const char* incomplete = INCOMPLETE;
Calin Juravle15a63102014-05-08 14:38:35 +0100510 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
511 EXPECT_EQ(EILSEQ, errno);
512 ASSERT_EQ('\xc2', *incomplete);
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700513
514 // If dst is null, *src shouldn't be updated.
515 // https://code.google.com/p/android/issues/detail?id=166381
516 const char* mbs = VALID;
517 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
518 EXPECT_EQ(VALID, mbs);
519 mbs = INVALID;
520 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
521 EXPECT_EQ(INVALID, mbs);
522 mbs = INCOMPLETE;
523 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
524 EXPECT_EQ(INCOMPLETE, mbs);
Calin Juravle15a63102014-05-08 14:38:35 +0100525}
526
527TEST(wchar, mbsrtowcs) {
528 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
529 uselocale(LC_GLOBAL_LOCALE);
530
531 mbstate_t ps;
532 memset(&ps, 0, sizeof(ps));
533 test_mbsrtowcs(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700534 test_mbsrtowcs(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100535
536 // Invalid multi byte continuation.
537 const char* invalid = "\x20";
538 wchar_t out;
539 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
540 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
541 EXPECT_EQ(EILSEQ, errno);
542 ASSERT_EQ('\x20', *invalid);
543}
544
Dan Albert6805c2d2017-08-09 14:55:27 -0700545template <typename T>
546using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
547
548template <typename T>
549void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
550 T expected_value, ptrdiff_t expected_len) {
551 wchar_t* p;
Dan Alberta40159f2023-08-03 19:49:37 +0000552 EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
553 EXPECT_EQ(expected_len, p - str) << str << " " << base;
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700554}
555
Dan Albert6805c2d2017-08-09 14:55:27 -0700556template <typename T>
557void TestWcsToInt(WcsToIntFn<T> fn) {
558 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
559 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
560 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
561 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
562 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
563 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
564 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
565 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
Dan Albert9f30c6b2023-08-03 19:50:28 +0000566 if (kLibcSupportsParsingBinaryLiterals) {
567 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
568 }
Dan Albert6805c2d2017-08-09 14:55:27 -0700569}
570
571template <typename T>
572void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
573 const wchar_t* max_str) {
574 if (std::is_signed<T>::value) {
575 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
576 } else {
577 // If the subject sequence begins with a <hyphen-minus>, the value resulting
578 // from the conversion shall be negated.
579 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
580 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
581 }
582 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
583}
584
585TEST(wchar, wcstol) {
586 TestWcsToInt(wcstol);
587}
588
589TEST(wchar, wcstol_limits) {
590 if (sizeof(long) == 8) {
591 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
592 } else {
593 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
594 }
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700595}
596
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700597TEST(wchar, wcstoul) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700598 TestWcsToInt(wcstoul);
599}
600
601TEST(wchar, wcstoul_limits) {
602 if (sizeof(long) == 8) {
603 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
604 } else {
605 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
606 }
607}
608
609TEST(wchar, wcstoll) {
610 TestWcsToInt(wcstoll);
611}
612
613TEST(wchar, wcstoll_limits) {
614 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700615}
616
617TEST(wchar, wcstoull) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700618 TestWcsToInt(wcstoull);
619}
620
621TEST(wchar, wcstoull_limits) {
622 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
623}
624
625TEST(wchar, wcstoimax) {
626 TestWcsToInt(wcstoimax);
627}
628
629TEST(wchar, wcstoimax_limits) {
630 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
631 L"9223372036854775808");
632}
633
634TEST(wchar, wcstoumax) {
635 TestWcsToInt(wcstoumax);
636}
637
638TEST(wchar, wcstoumax_limits) {
639 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700640}
641
642TEST(wchar, mbsnrtowcs) {
643 wchar_t dst[128];
644 const char* s = "hello, world!";
645 const char* src;
646
647 memset(dst, 0, sizeof(dst));
648 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700649 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700650
651 memset(dst, 0, sizeof(dst));
652 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700653 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700654 ASSERT_EQ(L'h', dst[0]);
655 ASSERT_EQ(L'e', dst[1]);
656 ASSERT_EQ(&s[2], src);
657
658 memset(dst, 0, sizeof(dst));
659 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700660 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700661 ASSERT_EQ(L'h', dst[0]);
662 ASSERT_EQ(L'e', dst[1]);
663 ASSERT_EQ(L'l', dst[2]);
664 ASSERT_EQ(&s[3], src);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700665
666 memset(dst, 0, sizeof(dst));
667 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
668 src = incomplete;
669 errno = 0;
670 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
671 ASSERT_EQ(EILSEQ, errno);
672
673 src = incomplete;
674 errno = 0;
675 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
676 ASSERT_EQ(EILSEQ, errno);
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700677}
Elliott Hughesefaa4612014-05-02 15:53:03 -0700678
Elliott Hughes3376c232018-02-13 23:14:12 -0800679TEST(wchar, wcsftime__wcsftime_l) {
Elliott Hughesefaa4612014-05-02 15:53:03 -0700680 setenv("TZ", "UTC", 1);
681
682 struct tm t;
683 memset(&t, 0, sizeof(tm));
684 t.tm_year = 200;
685 t.tm_mon = 2;
686 t.tm_mday = 10;
687
688 wchar_t buf[64];
689
690 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
691 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Dan Albert686e67d2023-08-03 19:00:09 +0000692 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -0800693 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Elliott Hughesefaa4612014-05-02 15:53:03 -0700694}
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200695
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800696TEST(wchar, wmemmove_smoke) {
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200697 const wchar_t const_wstr[] = L"This is a test of something or other.....";
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800698 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200699
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800700 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200701 EXPECT_STREQ(const_wstr, wstr);
702
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800703 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
Christopher Ferris8bf50d52014-05-29 15:42:42 -0700704 EXPECT_STREQ(L"This This is a test of something or other", wstr);
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200705}
Elliott Hughes69f05d22014-06-05 20:10:09 -0700706
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800707TEST(wchar, wmemcpy_smoke) {
708 const wchar_t src[] = L"Source string";
709 wchar_t dst[NUM_WCHARS(sizeof(src))];
710
711 EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
712 EXPECT_STREQ(dst, src);
713}
714
715TEST(wchar, wcpcpy_smoke) {
716 const wchar_t src[] = L"Source string";
717 wchar_t dst[NUM_WCHARS(sizeof(src))];
718
719 EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
720 EXPECT_STREQ(dst, src);
721}
722
723TEST(wchar, wcpncpy_smoke) {
724 const wchar_t src[] = L"Source string";
725 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
726
727 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
728 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
729 EXPECT_STREQ(dst, src);
730
731 EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
732 dst[6] = L'\0';
733 EXPECT_STREQ(dst, L"Source");
734
735 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
736 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
737 EXPECT_STREQ(dst, src);
738 EXPECT_EQ(dst[src_len], L'\0');
739 EXPECT_EQ(dst[src_len+1], L'\0');
740 EXPECT_EQ(dst[src_len+2], L'\0');
741 EXPECT_EQ(dst[src_len+3], L'\0');
742 EXPECT_EQ(dst[src_len+4], L'x');
743}
744
745TEST(wchar, wcscpy_smoke) {
746 const wchar_t src[] = L"Source string";
747 wchar_t dst[NUM_WCHARS(sizeof(src))];
748
749 EXPECT_EQ(dst, wcscpy(dst, src));
750 EXPECT_STREQ(src, dst);
751}
752
753TEST(wchar, wcsncpy_smoke) {
754 const wchar_t src[] = L"Source string";
755 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
756
757 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
758 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
759 EXPECT_STREQ(dst, src);
760
761 EXPECT_EQ(dst, wcsncpy(dst, src, 6));
762 dst[6] = L'\0';
763 EXPECT_STREQ(dst, L"Source");
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700764 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
765 EXPECT_STREQ(dst, L"Source");
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800766
767 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
768 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
769 EXPECT_STREQ(dst, src);
770 EXPECT_EQ(dst[src_len], L'\0');
771 EXPECT_EQ(dst[src_len+1], L'\0');
772 EXPECT_EQ(dst[src_len+2], L'\0');
773 EXPECT_EQ(dst[src_len+3], L'\0');
774 EXPECT_EQ(dst[src_len+4], L'x');
775}
776
Elliott Hughes69f05d22014-06-05 20:10:09 -0700777TEST(wchar, mbrtowc_15439554) {
778 // http://b/15439554
779 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
780 uselocale(LC_GLOBAL_LOCALE);
781
782 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
783 ASSERT_GE(MB_CUR_MAX, 4U);
784
785 wchar_t wc;
786 size_t n;
787
788 // 1-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700789 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700790 EXPECT_EQ(1U, n);
791 EXPECT_EQ(L'x', wc);
792 // 2-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700793 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700794 EXPECT_EQ(2U, n);
795 EXPECT_EQ(L'¢', wc);
796 // 3-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700797 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700798 EXPECT_EQ(3U, n);
799 EXPECT_EQ(L'€', wc);
800 // 4-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700801 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700802 EXPECT_EQ(4U, n);
803 EXPECT_EQ(L'𤭢', wc);
804}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700805
806TEST(wchar, open_wmemstream) {
807 wchar_t* p = nullptr;
808 size_t size = 0;
809 FILE* fp = open_wmemstream(&p, &size);
810 ASSERT_NE(EOF, fputws(L"hello, world!", fp));
811 fclose(fp);
812
813 ASSERT_STREQ(L"hello, world!", p);
814 ASSERT_EQ(wcslen(L"hello, world!"), size);
815 free(p);
816}
817
818TEST(stdio, open_wmemstream_EINVAL) {
819#if defined(__BIONIC__)
820 wchar_t* p;
821 size_t size;
zijunzhao7ce2f952023-04-03 23:13:57 +0000822#pragma clang diagnostic push
823#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes6b841db2014-08-20 16:10:49 -0700824 // Invalid buffer.
825 errno = 0;
826 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
827 ASSERT_EQ(EINVAL, errno);
828
829 // Invalid size.
830 errno = 0;
831 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
832 ASSERT_EQ(EINVAL, errno);
zijunzhao7ce2f952023-04-03 23:13:57 +0000833#pragma clang diagnostic pop
Elliott Hughes6b841db2014-08-20 16:10:49 -0700834#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800835 GTEST_SKIP() << "This test is bionic-specific";
Elliott Hughes6b841db2014-08-20 16:10:49 -0700836#endif
837}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700838
839TEST(wchar, wcstol_EINVAL) {
840 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700841 wcstol(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700842 ASSERT_EQ(EINVAL, errno);
843 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700844 wcstol(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700845 ASSERT_EQ(EINVAL, errno);
846 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700847 wcstol(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700848 ASSERT_EQ(EINVAL, errno);
849}
850
851TEST(wchar, wcstoll_EINVAL) {
852 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700853 wcstoll(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700854 ASSERT_EQ(EINVAL, errno);
855 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700856 wcstoll(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700857 ASSERT_EQ(EINVAL, errno);
858 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700859 wcstoll(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700860 ASSERT_EQ(EINVAL, errno);
861}
862
863TEST(wchar, wcstoul_EINVAL) {
864 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700865 wcstoul(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700866 ASSERT_EQ(EINVAL, errno);
867 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700868 wcstoul(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700869 ASSERT_EQ(EINVAL, errno);
870 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700871 wcstoul(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700872 ASSERT_EQ(EINVAL, errno);
873}
874
875TEST(wchar, wcstoull_EINVAL) {
876 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700877 wcstoull(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700878 ASSERT_EQ(EINVAL, errno);
879 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700880 wcstoull(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700881 ASSERT_EQ(EINVAL, errno);
882 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700883 wcstoull(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700884 ASSERT_EQ(EINVAL, errno);
885}
886
887TEST(wchar, wcstoll_l_EINVAL) {
888 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000889 wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700890 ASSERT_EQ(EINVAL, errno);
891 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000892 wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700893 ASSERT_EQ(EINVAL, errno);
894 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000895 wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700896 ASSERT_EQ(EINVAL, errno);
897}
898
899TEST(wchar, wcstoull_l_EINVAL) {
900 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000901 wcstoull_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700902 ASSERT_EQ(EINVAL, errno);
903 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000904 wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700905 ASSERT_EQ(EINVAL, errno);
906 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000907 wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700908 ASSERT_EQ(EINVAL, errno);
909}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800910
911TEST(wchar, wmempcpy) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700912#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800913 wchar_t dst[6];
914 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
Colin Cross7da20342021-07-28 11:18:11 -0700915#else
916 GTEST_SKIP() << "musl doesn't have wmempcpy";
917#endif
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800918}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700919
920template <typename T>
Dan Albert6805c2d2017-08-09 14:55:27 -0700921using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700922
Dan Albert6805c2d2017-08-09 14:55:27 -0700923template <typename T>
924void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
925 T expected_value, ptrdiff_t expected_len) {
Dan Albertf6346552016-12-02 12:02:03 -0800926 wchar_t* p;
Dan Albert6805c2d2017-08-09 14:55:27 -0700927 ASSERT_EQ(expected_value, fn(str, &p));
928 ASSERT_EQ(expected_len, p - str);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700929}
930
Dan Albert6805c2d2017-08-09 14:55:27 -0700931template <typename T>
932void TestWcsToFloat(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800933 TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
934 TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
935 TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
936 TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
937 TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
938 TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
Dan Albert6805c2d2017-08-09 14:55:27 -0700939}
940
941template <typename T>
942void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800943 TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
944 TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
945 TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
946 TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
Dan Albert6805c2d2017-08-09 14:55:27 -0700947}
948
949template <typename T>
950void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
951 ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
952 ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
953 ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
954
955 ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
956 ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
957 ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
958
959 wchar_t* p;
960 ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
961 ASSERT_STREQ(L"ny", p);
962 ASSERT_TRUE(isnan(fn(L"nanny", &p)));
963 ASSERT_STREQ(L"ny", p);
964 ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
965 ASSERT_STREQ(L"ny", p);
966
967 ASSERT_EQ(0, fn(L"muppet", &p));
968 ASSERT_STREQ(L"muppet", p);
969 ASSERT_EQ(0, fn(L" muppet", &p));
970 ASSERT_STREQ(L" muppet", p);
971
972 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
973 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
974 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
975
976 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
977 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
978 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
979
980 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
981 ASSERT_STREQ(L"initude", p);
982 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
983 ASSERT_STREQ(L"initude", p);
984 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
985 ASSERT_STREQ(L"initude", p);
986
987 // Check case-insensitivity.
988 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
989 ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700990}
991
992TEST(wchar, wcstof) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700993 TestWcsToFloat(wcstof);
994}
995
996TEST(wchar, wcstof_hex_floats) {
997 TestWcsToFloatHexFloats(wcstof);
998}
999
1000TEST(wchar, wcstof_hex_inf_nan) {
1001 TestWcsToFloatInfNan(wcstof);
1002}
1003
1004TEST(wchar, wcstod) {
1005 TestWcsToFloat(wcstod);
1006}
1007
1008TEST(wchar, wcstod_hex_floats) {
1009 TestWcsToFloatHexFloats(wcstod);
1010}
1011
1012TEST(wchar, wcstod_hex_inf_nan) {
1013 TestWcsToFloatInfNan(wcstod);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001014}
1015
1016TEST(wchar, wcstold) {
Dan Albert6805c2d2017-08-09 14:55:27 -07001017 TestWcsToFloat(wcstold);
1018}
1019
1020TEST(wchar, wcstold_hex_floats) {
1021 TestWcsToFloatHexFloats(wcstold);
1022}
1023
1024TEST(wchar, wcstold_hex_inf_nan) {
1025 TestWcsToFloatInfNan(wcstold);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001026}
Elliott Hughesc41b5602017-07-27 17:08:08 -07001027
Elliott Hughes3376c232018-02-13 23:14:12 -08001028TEST(wchar, wcstod_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001029#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001030 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001031#else
1032 GTEST_SKIP() << "musl doesn't have wcstod_l";
1033#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001034}
1035
1036TEST(wchar, wcstof_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001037#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001038 EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001039#else
1040 GTEST_SKIP() << "musl doesn't have wcstof_l";
1041#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001042}
1043
1044TEST(wchar, wcstol_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001045#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001046 EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001047#else
1048 GTEST_SKIP() << "musl doesn't have wcstol_l";
1049#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001050}
1051
1052TEST(wchar, wcstold_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001053 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001054}
1055
1056TEST(wchar, wcstoll_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001057 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001058}
1059
1060TEST(wchar, wcstoul_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001061#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001062 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001063#else
1064 GTEST_SKIP() << "musl doesn't have wcstoul_l";
1065#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001066}
1067
1068TEST(wchar, wcstoull_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001069 EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001070}
1071
Elliott Hughesc41b5602017-07-27 17:08:08 -07001072static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
1073 for (wchar_t i = begin; i < end; ++i) {
1074 EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
1075 }
1076}
1077
1078TEST(wchar, wcwidth_NUL) {
1079 // NUL is defined to return 0 rather than -1, despite being a C0 control.
1080 EXPECT_EQ(0, wcwidth(0));
1081}
1082
1083TEST(wchar, wcwidth_ascii) {
1084 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1085}
1086
1087TEST(wchar, wcwidth_controls) {
1088 AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
1089 EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
1090 AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
1091}
1092
1093TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001094 if (!have_dl()) return;
1095
Elliott Hughesc41b5602017-07-27 17:08:08 -07001096 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1097 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
1098 EXPECT_EQ(0, wcwidth(0x00ad)); // Soft hyphen (SHY).
1099 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1100}
1101
1102TEST(wchar, wcwidth_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001103 if (!have_dl()) return;
1104
Elliott Hughesc41b5602017-07-27 17:08:08 -07001105 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1106 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1107 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1108 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1109 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1110 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1111}
1112
1113TEST(wchar, wcwidth_korean_combining_jamo) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001114 if (!have_dl()) return;
1115
Elliott Hughesc41b5602017-07-27 17:08:08 -07001116 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1117 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1118 EXPECT_EQ(0, wcwidth(0xd7cb));
1119}
1120
1121TEST(wchar, wcwidth_korean_jeongeul_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001122 if (!have_dl()) return;
1123
Elliott Hughesc41b5602017-07-27 17:08:08 -07001124 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
1125 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points in Unicode 7.
1126 // Undefined characters at the end of the block have width 1.
1127}
1128
1129TEST(wchar, wcwidth_kana) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001130 if (!have_dl()) return;
1131
Elliott Hughesc41b5602017-07-27 17:08:08 -07001132 // Hiragana (most, not undefined).
1133 AssertWcwidthRange(0x3041, 0x3097, 2);
1134 // Katakana.
1135 AssertWcwidthRange(0x30a0, 0x3100, 2);
1136}
1137
1138TEST(wchar, wcwidth_circled_two_digit_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001139 if (!have_dl()) return;
1140
Elliott Hughesc41b5602017-07-27 17:08:08 -07001141 // Circled two-digit CJK "speed sign" numbers are wide,
1142 // though EastAsianWidth is ambiguous.
1143 AssertWcwidthRange(0x3248, 0x3250, 2);
1144}
1145
1146TEST(wchar, wcwidth_hexagrams) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001147 if (!have_dl()) return;
1148
Elliott Hughesc41b5602017-07-27 17:08:08 -07001149 // Hexagrams are wide, though EastAsianWidth is neutral.
1150 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1151}
1152
1153TEST(wchar, wcwidth_default_ignorables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001154 if (!have_dl()) return;
1155
Elliott Hughesc41b5602017-07-27 17:08:08 -07001156 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1157 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1158}
1159
1160TEST(wchar, wcwidth_korean_common_non_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001161 if (!have_dl()) return;
1162
Elliott Hughesc41b5602017-07-27 17:08:08 -07001163 EXPECT_EQ(2, wcwidth(L'ㅜ')); // Korean "crying" emoticon.
1164 EXPECT_EQ(2, wcwidth(L'ã…‹')); // Korean "laughing" emoticon.
1165}
Elliott Hughesfe50a0c2021-04-09 08:51:09 -07001166
1167TEST(wchar, wcswidth) {
1168 EXPECT_EQ(2, wcswidth(L"abc", 2));
1169 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1170 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1171}
1172
1173TEST(wchar, wcslcpy) {
1174#if defined(__BIONIC__)
1175 wchar_t dst[32];
1176 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1177 ASSERT_STREQ(L"he", dst);
1178 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1179 ASSERT_STREQ(L"hello world", dst);
1180#else
1181 GTEST_SKIP() << "no wcslcpy in glibc";
1182#endif
1183}
1184
1185TEST(wchar, wcscat) {
1186 wchar_t dst[32];
1187 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1188 ASSERT_STREQ(dst, L"hello");
1189 ASSERT_EQ(dst, wcscat(dst, L" world"));
1190 ASSERT_STREQ(dst, L"hello world");
1191}
1192
1193TEST(wchar, wcscpy) {
1194 wchar_t dst[32];
1195 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1196 ASSERT_STREQ(dst, L"hello");
1197 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1198 ASSERT_STREQ(dst, L"world");
1199}
1200
1201TEST(wchar, wcscasecmp) {
1202 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1203 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1204 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1205 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1206 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1207}
1208
1209TEST(wchar, wcscspn) {
1210 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1211 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1212 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1213}
1214
1215TEST(wchar, wcsspn) {
1216 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1217 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1218 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1219}
1220
1221TEST(wchar, wcsdup) {
1222 wchar_t* s = wcsdup(L"hello");
1223 ASSERT_STREQ(s, L"hello");
1224 free(s);
1225}
1226
1227TEST(wchar, wcslcat) {
1228#if defined(__BIONIC__)
1229 wchar_t dst[4] = {};
1230 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1231 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1232 ASSERT_STREQ(dst, L"abc");
1233#else
1234 GTEST_SKIP() << "no wcslcpy in glibc";
1235#endif
1236}
1237
1238TEST(wchar, wcsncasecmp) {
1239 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1240
1241 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1242 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1243 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1244 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1245 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1246}
1247
1248TEST(wchar, wcsncat) {
1249 wchar_t dst[32];
1250 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1251 ASSERT_STREQ(dst, L"hello");
1252 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1253 ASSERT_STREQ(dst, L"hello");
1254 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1255 ASSERT_STREQ(dst, L"hello, world!");
1256}
1257
1258TEST(wchar, wcsncmp) {
1259 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1260 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1261 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1262 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1263}
1264
1265TEST(wchar, wcsnlen) {
1266 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1267 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1268 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1269}
1270
1271TEST(wchar, wcspbrk) {
1272 const wchar_t* s = L"hello, world!";
1273 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1274 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1275 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1276 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1277}
1278
1279TEST(wchar, wcstok) {
1280 wchar_t s[] = L"this is\ta\nstring";
1281 wchar_t* p;
1282 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1283 ASSERT_STREQ(s, L"this");
1284 ASSERT_STREQ(p, L"is\ta\nstring");
1285 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1286 ASSERT_STREQ(s + 5, L"is");
1287 ASSERT_STREQ(p, L"a\nstring");
1288 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1289 ASSERT_STREQ(s + 8, L"a");
1290 ASSERT_STREQ(p, L"string");
1291 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1292 ASSERT_STREQ(s + 10, L"string");
1293 ASSERT_EQ(nullptr, p);
1294}
1295
1296TEST(wchar, wmemchr) {
1297 const wchar_t* s = L"hello, world!";
1298 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1299 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1300 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1301 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1302}
1303
1304TEST(wchar, wmemcmp) {
1305 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1306 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1307 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1308}
1309
1310TEST(wchar, wmemcpy) {
1311 wchar_t dst[32] = {};
1312 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1313 ASSERT_STREQ(dst, L"hello");
1314}
1315
1316TEST(wchar, wmemmove) {
1317 wchar_t dst[32] = {};
1318 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1319 ASSERT_STREQ(dst, L"hello");
1320}
1321
1322TEST(wchar, wmemset) {
1323 wchar_t dst[4] = {};
1324 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1325 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1326 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1327 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1328 ASSERT_EQ(dst[3], wchar_t(0));
1329 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1330 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1331}