blob: 9cef52c5e889ede35df7baaddb3d9acd2bf10d13 [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
Elliott Hughes77e944f2014-04-04 17:34:51 -070082TEST(wchar, sizeof_wchar_t) {
83 EXPECT_EQ(4U, sizeof(wchar_t));
84 EXPECT_EQ(4U, sizeof(wint_t));
85}
86
87TEST(wchar, mbrlen) {
88 char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
Dan Albert512469a2023-08-03 19:34:42 +000089 EXPECT_EQ(kExpectedResultForZeroLength, mbrlen(&bytes[0], 0, nullptr));
Yi Kong32bc0fc2018-08-02 17:31:13 -070090 EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070091
Yi Kong32bc0fc2018-08-02 17:31:13 -070092 EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
93 EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070094}
95
96TEST(wchar, wctomb_wcrtomb) {
97 // wctomb and wcrtomb behave differently when s == NULL.
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 EXPECT_EQ(0, wctomb(nullptr, L'h'));
99 EXPECT_EQ(0, wctomb(nullptr, L'\0'));
100 EXPECT_EQ(1U, wcrtomb(nullptr, L'\0', nullptr));
101 EXPECT_EQ(1U, wcrtomb(nullptr, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700102
103 char bytes[MB_LEN_MAX];
104
105 // wctomb and wcrtomb behave similarly for the null wide character.
106 EXPECT_EQ(1, wctomb(bytes, L'\0'));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700107 EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700108
109 // ...and for regular characters.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700110 memset(bytes, 0, sizeof(bytes));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700111 EXPECT_EQ(1, wctomb(bytes, L'h'));
112 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700113 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700114 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700115 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700116
117 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
118 uselocale(LC_GLOBAL_LOCALE);
119
120 // 1-byte UTF-8.
121 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700122 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700123 EXPECT_EQ('h', bytes[0]);
124 // 2-byte UTF-8.
125 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700126 EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700127 EXPECT_EQ('\xc2', bytes[0]);
128 EXPECT_EQ('\xa2', bytes[1]);
129 // 3-byte UTF-8.
130 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700131 EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700132 EXPECT_EQ('\xe2', bytes[0]);
133 EXPECT_EQ('\x82', bytes[1]);
134 EXPECT_EQ('\xac', bytes[2]);
135 // 4-byte UTF-8.
136 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700137 EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700138 EXPECT_EQ('\xf0', bytes[0]);
139 EXPECT_EQ('\xa4', bytes[1]);
140 EXPECT_EQ('\xad', bytes[2]);
141 EXPECT_EQ('\xa2', bytes[3]);
142 // Invalid code point.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700143 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700144 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes77e944f2014-04-04 17:34:51 -0700145}
Elliott Hughes05493712014-04-17 17:30:03 -0700146
Calin Juravle15a63102014-05-08 14:38:35 +0100147TEST(wchar, wcrtomb_start_state) {
Dan Albert9f78c512023-08-03 19:49:09 +0000148 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
149 uselocale(LC_GLOBAL_LOCALE);
150
Calin Juravle15a63102014-05-08 14:38:35 +0100151 char out[MB_LEN_MAX];
152 mbstate_t ps;
153
154 // Any non-initial state is invalid when calling wcrtomb.
155 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700156 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100157 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
158 EXPECT_EQ(EILSEQ, errno);
159
160 // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
161 // state should be reset.
162 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700163 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
164 EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100165 EXPECT_TRUE(mbsinit(&ps));
166
167 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700168 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100169 EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
170 EXPECT_TRUE(mbsinit(&ps));
171}
172
Elliott Hughes05493712014-04-17 17:30:03 -0700173TEST(wchar, wcstombs_wcrtombs) {
Dan Albert9f78c512023-08-03 19:49:09 +0000174 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
175 uselocale(LC_GLOBAL_LOCALE);
176
Elliott Hughes1b836ee2014-04-18 13:32:33 -0700177 const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
Elliott Hughesf83e6442014-05-01 17:14:59 -0700178 const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
Elliott Hughes05493712014-04-17 17:30:03 -0700179 const wchar_t* src;
180 char bytes[BUFSIZ];
181
182 // Given a NULL destination, these functions count valid characters.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700183 EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
184 EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
185 EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700186 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700187 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700188 EXPECT_EQ(&chars[0], src);
189 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700190 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700191 EXPECT_EQ(&chars[0], src);
192 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700193 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700194 EXPECT_EQ(&chars[0], src);
195
196 // An unrepresentable char just returns an error from wcstombs...
197 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700198 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
Elliott Hughes05493712014-04-17 17:30:03 -0700199 EXPECT_EQ(EILSEQ, errno);
200 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700201 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700202 EXPECT_EQ(EILSEQ, errno);
203
204 // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
205 // to actually convert anything...
206 errno = 0;
207 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700208 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700209 EXPECT_EQ(&bad_chars[0], src);
210 EXPECT_EQ(EILSEQ, errno);
211 errno = 0;
212 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700213 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700214 EXPECT_EQ(&bad_chars[0], src);
215 EXPECT_EQ(EILSEQ, errno);
216
217 // Okay, now let's test actually converting something...
218 memset(bytes, 'x', sizeof(bytes));
219 EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
220 memset(bytes, 'x', sizeof(bytes));
221 EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
222 bytes[5] = 0;
223 EXPECT_STREQ("hellx", bytes);
224 memset(bytes, 'x', sizeof(bytes));
225 EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
226 EXPECT_STREQ("hello", bytes);
227 memset(bytes, 'x', sizeof(bytes));
228 EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
229 EXPECT_STREQ("hello", bytes);
230 errno = 0;
231 memset(bytes, 'x', sizeof(bytes));
232 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
233 EXPECT_EQ(EILSEQ, errno);
234 bytes[3] = 0;
235 EXPECT_STREQ("hix", bytes);
236
237 // wcsrtombs is a bit more informative...
238 memset(bytes, 'x', sizeof(bytes));
239 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700240 EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700241 EXPECT_EQ(&chars[0], src); // No input consumed.
242 EXPECT_EQ(EILSEQ, errno);
243
244 memset(bytes, 'x', sizeof(bytes));
245 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700246 EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700247 EXPECT_EQ(&chars[4], src); // Some input consumed.
248 EXPECT_EQ(EILSEQ, errno);
249 bytes[5] = 0;
250 EXPECT_STREQ("hellx", bytes);
251
252 memset(bytes, 'x', sizeof(bytes));
253 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700254 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
255 EXPECT_EQ(nullptr, src); // All input consumed!
Elliott Hughes05493712014-04-17 17:30:03 -0700256 EXPECT_EQ(EILSEQ, errno);
257 EXPECT_STREQ("hello", bytes);
258
259 memset(bytes, 'x', sizeof(bytes));
260 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700261 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
262 EXPECT_EQ(nullptr, src); // All input consumed.
Elliott Hughes05493712014-04-17 17:30:03 -0700263 EXPECT_EQ(EILSEQ, errno);
264 EXPECT_STREQ("hello", bytes);
265
266 memset(bytes, 'x', sizeof(bytes));
267 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700268 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700269 EXPECT_EQ(&bad_chars[2], src);
270 EXPECT_EQ(EILSEQ, errno);
271 bytes[3] = 0;
272 EXPECT_STREQ("hix", bytes);
Calin Juravle15a63102014-05-08 14:38:35 +0100273
274 // Any non-initial state is invalid when calling wcsrtombs.
275 mbstate_t ps;
276 src = chars;
277 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700278 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
279 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100280 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes05493712014-04-17 17:30:03 -0700281}
Elliott Hughes83c07b52014-04-21 18:09:46 -0700282
283TEST(wchar, limits) {
284 ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
285}
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700286
Dan Albert001f8f02014-06-04 09:53:06 -0700287TEST(wchar, wcsstr) {
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700288 const wchar_t* haystack = L"big daddy/giant haystacks!";
289 const wchar_t* empty_haystack = L"";
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700290
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700291 // The empty needle is a special case.
292 ASSERT_EQ(haystack, wcsstr(haystack, L""));
293 ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
294
295 ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
296 ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
297 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
298 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
299 ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
300 ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
301
302 ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
303 ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700304}
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700305
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800306TEST(wchar, wcsstr_80199) {
307 // https://code.google.com/p/android/issues/detail?id=80199
Yi Kong32bc0fc2018-08-02 17:31:13 -0700308 ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800309}
310
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700311TEST(wchar, mbtowc) {
312 wchar_t out[8];
313
Dan Albert512469a2023-08-03 19:34:42 +0000314 // bionic has the same misunderstanding of the result for a zero-length
315 // conversion for mbtowc as it does for all the other multibyte conversion
316 // functions but mbtowc returns different values than all the others:
317 //
318 // C23 7.24.7.2.4:
319 //
320 // If s is a null pointer, the mbtowc function returns a nonzero or zero
321 // value, if multibyte character encodings, respectively, do or do not have
322 // state-dependent encodings. If s is not a null pointer, the mbtowc function
323 // either returns 0 (if s points to the null character), or returns the number
324 // of bytes that are contained in the converted multibyte character (if the
325 // next n or fewer bytes form a valid multibyte character), or returns -1 (if
326 // they do not form a valid multibyte character).
327
328#ifdef __BIONIC__
329 int expected_result_for_zero_length = 0;
330#else
331 int expected_result_for_zero_length = -1;
332#endif
333
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700334 out[0] = 'x';
Dan Albert512469a2023-08-03 19:34:42 +0000335 EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
336 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700337
Dan Albert512469a2023-08-03 19:34:42 +0000338 EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
339 EXPECT_EQ(0, mbtowc(out, "", 0));
340 EXPECT_EQ(1, mbtowc(out, "hello", 1));
341 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700342
Dan Albert512469a2023-08-03 19:34:42 +0000343 EXPECT_EQ(expected_result_for_zero_length, mbtowc(nullptr, "hello", 0));
344 EXPECT_EQ(0, mbtowc(nullptr, "", 0));
345 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700346
Dan Albert512469a2023-08-03 19:34:42 +0000347 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700348}
349
350TEST(wchar, mbrtowc) {
351 wchar_t out[8];
352
353 out[0] = 'x';
Dan Albert512469a2023-08-03 19:34:42 +0000354 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
355 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700356
Dan Albert512469a2023-08-03 19:34:42 +0000357 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
358 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "", 0, nullptr));
359 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
360 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700361
Dan Albert512469a2023-08-03 19:34:42 +0000362 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "hello", 0, nullptr));
363 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "", 0, nullptr));
364 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700365
Dan Albert512469a2023-08-03 19:34:42 +0000366 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700367
Dan Albert512469a2023-08-03 19:34:42 +0000368 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700369 uselocale(LC_GLOBAL_LOCALE);
370
371 // 1-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000372 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
373 EXPECT_EQ(L'a', out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700374 // 2-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000375 EXPECT_EQ(2U, mbrtowc(out,
376 "\xc2\xa2"
377 "cdef",
378 6, nullptr));
379 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700380 // 3-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000381 EXPECT_EQ(3U, mbrtowc(out,
382 "\xe2\x82\xac"
383 "def",
384 6, nullptr));
385 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700386 // 4-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000387 EXPECT_EQ(4U, mbrtowc(out,
388 "\xf0\xa4\xad\xa2"
389 "ef",
390 6, nullptr));
391 EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
Elliott Hughes063525c2014-05-13 11:19:57 -0700392#if defined(__BIONIC__) // glibc allows this.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700393 // Illegal 5-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000394 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
395 "\xf8\xa1\xa2\xa3\xa4"
396 "f",
397 6, nullptr));
398 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700399#endif
400 // Illegal over-long sequence.
Dan Albert512469a2023-08-03 19:34:42 +0000401 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
402 "\xf0\x82\x82\xac"
403 "ef",
404 6, nullptr));
405 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700406}
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700407
Elliott Hughes402c7622018-07-06 17:18:05 -0700408TEST(wchar, mbrtowc_valid_non_characters) {
409 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
410 uselocale(LC_GLOBAL_LOCALE);
411
412 wchar_t out[8] = {};
413
414 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
415 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
416 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
417 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
418}
419
420TEST(wchar, mbrtowc_out_of_range) {
421 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
422 uselocale(LC_GLOBAL_LOCALE);
423
424 wchar_t out[8] = {};
425 errno = 0;
Dan Albert53256532023-08-03 19:39:39 +0000426 auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
427 if (kLibcRejectsOverLongUtf8Sequences) {
428 ASSERT_EQ(static_cast<size_t>(-1), result);
429 ASSERT_EQ(EILSEQ, errno);
430 } else {
431 ASSERT_EQ(4U, result);
432 ASSERT_EQ(0, errno);
433 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700434}
435
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700436static void test_mbrtowc_incomplete(mbstate_t* ps) {
Calin Juravle15a63102014-05-08 14:38:35 +0100437 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
438 uselocale(LC_GLOBAL_LOCALE);
439
440 wchar_t out;
441 // 2-byte UTF-8.
442 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
443 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700444 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100445 ASSERT_TRUE(mbsinit(ps));
446 // 3-byte UTF-8.
447 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
448 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
449 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700450 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100451 ASSERT_TRUE(mbsinit(ps));
452 // 4-byte UTF-8.
453 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
454 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
455 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700456 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100457 ASSERT_TRUE(mbsinit(ps));
458
459 // Invalid 2-byte
460 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
461 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
462 ASSERT_EQ(EILSEQ, errno);
463}
464
465TEST(wchar, mbrtowc_incomplete) {
466 mbstate_t ps;
467 memset(&ps, 0, sizeof(ps));
468
469 test_mbrtowc_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700470 test_mbrtowc_incomplete(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100471}
472
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700473static void test_mbsrtowcs(mbstate_t* ps) {
474 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
475 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
476 constexpr const char* INCOMPLETE = "A" "\xc2";
Calin Juravle15a63102014-05-08 14:38:35 +0100477 wchar_t out[4];
478
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700479 const char* valid = VALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100480 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
481 ASSERT_EQ(L'A', out[0]);
Ben Chengcaff5f22014-05-19 14:27:31 -0700482 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
483 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
484 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
Dan Albert6b55ba52014-07-20 11:51:26 -0700485 // Check that valid has advanced to the next unread character.
Calin Juravle15a63102014-05-08 14:38:35 +0100486 ASSERT_EQ('e', *valid);
487
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800488 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
Dan Albert6b55ba52014-07-20 11:51:26 -0700489 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
490 ASSERT_EQ(L'e', out[0]);
491 ASSERT_EQ(L'f', out[1]);
492 ASSERT_EQ(L'\0', out[2]);
493 // Check that we didn't clobber the rest of out.
494 ASSERT_EQ(L'x', out[3]);
495 // Check that valid has advanced to the end of the string.
Dan Albertb6cc8e02014-07-31 11:31:03 -0700496 ASSERT_EQ(nullptr, valid);
Dan Albert6b55ba52014-07-20 11:51:26 -0700497
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700498 const char* invalid = INVALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100499 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
500 EXPECT_EQ(EILSEQ, errno);
501 ASSERT_EQ('\xc2', *invalid);
502
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700503 const char* incomplete = INCOMPLETE;
Calin Juravle15a63102014-05-08 14:38:35 +0100504 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
505 EXPECT_EQ(EILSEQ, errno);
506 ASSERT_EQ('\xc2', *incomplete);
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700507
508 // If dst is null, *src shouldn't be updated.
509 // https://code.google.com/p/android/issues/detail?id=166381
510 const char* mbs = VALID;
511 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
512 EXPECT_EQ(VALID, mbs);
513 mbs = INVALID;
514 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
515 EXPECT_EQ(INVALID, mbs);
516 mbs = INCOMPLETE;
517 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
518 EXPECT_EQ(INCOMPLETE, mbs);
Calin Juravle15a63102014-05-08 14:38:35 +0100519}
520
521TEST(wchar, mbsrtowcs) {
522 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
523 uselocale(LC_GLOBAL_LOCALE);
524
525 mbstate_t ps;
526 memset(&ps, 0, sizeof(ps));
527 test_mbsrtowcs(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700528 test_mbsrtowcs(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100529
530 // Invalid multi byte continuation.
531 const char* invalid = "\x20";
532 wchar_t out;
533 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
534 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
535 EXPECT_EQ(EILSEQ, errno);
536 ASSERT_EQ('\x20', *invalid);
537}
538
Dan Albert6805c2d2017-08-09 14:55:27 -0700539template <typename T>
540using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
541
542template <typename T>
543void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
544 T expected_value, ptrdiff_t expected_len) {
545 wchar_t* p;
546 ASSERT_EQ(expected_value, fn(str, &p, base));
547 ASSERT_EQ(expected_len, p - str) << str;
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700548}
549
Dan Albert6805c2d2017-08-09 14:55:27 -0700550template <typename T>
551void TestWcsToInt(WcsToIntFn<T> fn) {
552 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
553 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
554 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
555 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
556 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
557 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
558 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
559 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
Elliott Hughes1f462de2022-08-05 22:51:05 +0000560 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
Dan Albert6805c2d2017-08-09 14:55:27 -0700561}
562
563template <typename T>
564void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
565 const wchar_t* max_str) {
566 if (std::is_signed<T>::value) {
567 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
568 } else {
569 // If the subject sequence begins with a <hyphen-minus>, the value resulting
570 // from the conversion shall be negated.
571 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
572 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
573 }
574 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
575}
576
577TEST(wchar, wcstol) {
578 TestWcsToInt(wcstol);
579}
580
581TEST(wchar, wcstol_limits) {
582 if (sizeof(long) == 8) {
583 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
584 } else {
585 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
586 }
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700587}
588
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700589TEST(wchar, wcstoul) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700590 TestWcsToInt(wcstoul);
591}
592
593TEST(wchar, wcstoul_limits) {
594 if (sizeof(long) == 8) {
595 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
596 } else {
597 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
598 }
599}
600
601TEST(wchar, wcstoll) {
602 TestWcsToInt(wcstoll);
603}
604
605TEST(wchar, wcstoll_limits) {
606 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700607}
608
609TEST(wchar, wcstoull) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700610 TestWcsToInt(wcstoull);
611}
612
613TEST(wchar, wcstoull_limits) {
614 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
615}
616
617TEST(wchar, wcstoimax) {
618 TestWcsToInt(wcstoimax);
619}
620
621TEST(wchar, wcstoimax_limits) {
622 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
623 L"9223372036854775808");
624}
625
626TEST(wchar, wcstoumax) {
627 TestWcsToInt(wcstoumax);
628}
629
630TEST(wchar, wcstoumax_limits) {
631 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700632}
633
634TEST(wchar, mbsnrtowcs) {
635 wchar_t dst[128];
636 const char* s = "hello, world!";
637 const char* src;
638
639 memset(dst, 0, sizeof(dst));
640 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700641 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700642
643 memset(dst, 0, sizeof(dst));
644 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700645 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700646 ASSERT_EQ(L'h', dst[0]);
647 ASSERT_EQ(L'e', dst[1]);
648 ASSERT_EQ(&s[2], src);
649
650 memset(dst, 0, sizeof(dst));
651 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700652 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700653 ASSERT_EQ(L'h', dst[0]);
654 ASSERT_EQ(L'e', dst[1]);
655 ASSERT_EQ(L'l', dst[2]);
656 ASSERT_EQ(&s[3], src);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700657
658 memset(dst, 0, sizeof(dst));
659 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
660 src = incomplete;
661 errno = 0;
662 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
663 ASSERT_EQ(EILSEQ, errno);
664
665 src = incomplete;
666 errno = 0;
667 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
668 ASSERT_EQ(EILSEQ, errno);
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700669}
Elliott Hughesefaa4612014-05-02 15:53:03 -0700670
Elliott Hughes3376c232018-02-13 23:14:12 -0800671TEST(wchar, wcsftime__wcsftime_l) {
Elliott Hughesefaa4612014-05-02 15:53:03 -0700672 setenv("TZ", "UTC", 1);
673
674 struct tm t;
675 memset(&t, 0, sizeof(tm));
676 t.tm_year = 200;
677 t.tm_mon = 2;
678 t.tm_mday = 10;
679
680 wchar_t buf[64];
681
682 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
683 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Dan Albert686e67d2023-08-03 19:00:09 +0000684 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -0800685 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Elliott Hughesefaa4612014-05-02 15:53:03 -0700686}
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200687
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800688TEST(wchar, wmemmove_smoke) {
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200689 const wchar_t const_wstr[] = L"This is a test of something or other.....";
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800690 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200691
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800692 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200693 EXPECT_STREQ(const_wstr, wstr);
694
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800695 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
Christopher Ferris8bf50d52014-05-29 15:42:42 -0700696 EXPECT_STREQ(L"This This is a test of something or other", wstr);
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200697}
Elliott Hughes69f05d22014-06-05 20:10:09 -0700698
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800699TEST(wchar, wmemcpy_smoke) {
700 const wchar_t src[] = L"Source string";
701 wchar_t dst[NUM_WCHARS(sizeof(src))];
702
703 EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
704 EXPECT_STREQ(dst, src);
705}
706
707TEST(wchar, wcpcpy_smoke) {
708 const wchar_t src[] = L"Source string";
709 wchar_t dst[NUM_WCHARS(sizeof(src))];
710
711 EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
712 EXPECT_STREQ(dst, src);
713}
714
715TEST(wchar, wcpncpy_smoke) {
716 const wchar_t src[] = L"Source string";
717 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
718
719 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
720 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
721 EXPECT_STREQ(dst, src);
722
723 EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
724 dst[6] = L'\0';
725 EXPECT_STREQ(dst, L"Source");
726
727 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
728 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
729 EXPECT_STREQ(dst, src);
730 EXPECT_EQ(dst[src_len], L'\0');
731 EXPECT_EQ(dst[src_len+1], L'\0');
732 EXPECT_EQ(dst[src_len+2], L'\0');
733 EXPECT_EQ(dst[src_len+3], L'\0');
734 EXPECT_EQ(dst[src_len+4], L'x');
735}
736
737TEST(wchar, wcscpy_smoke) {
738 const wchar_t src[] = L"Source string";
739 wchar_t dst[NUM_WCHARS(sizeof(src))];
740
741 EXPECT_EQ(dst, wcscpy(dst, src));
742 EXPECT_STREQ(src, dst);
743}
744
745TEST(wchar, wcsncpy_smoke) {
746 const wchar_t src[] = L"Source string";
747 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
748
749 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
750 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
751 EXPECT_STREQ(dst, src);
752
753 EXPECT_EQ(dst, wcsncpy(dst, src, 6));
754 dst[6] = L'\0';
755 EXPECT_STREQ(dst, L"Source");
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700756 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
757 EXPECT_STREQ(dst, L"Source");
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800758
759 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
760 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
761 EXPECT_STREQ(dst, src);
762 EXPECT_EQ(dst[src_len], L'\0');
763 EXPECT_EQ(dst[src_len+1], L'\0');
764 EXPECT_EQ(dst[src_len+2], L'\0');
765 EXPECT_EQ(dst[src_len+3], L'\0');
766 EXPECT_EQ(dst[src_len+4], L'x');
767}
768
Elliott Hughes69f05d22014-06-05 20:10:09 -0700769TEST(wchar, mbrtowc_15439554) {
770 // http://b/15439554
771 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
772 uselocale(LC_GLOBAL_LOCALE);
773
774 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
775 ASSERT_GE(MB_CUR_MAX, 4U);
776
777 wchar_t wc;
778 size_t n;
779
780 // 1-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700781 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700782 EXPECT_EQ(1U, n);
783 EXPECT_EQ(L'x', wc);
784 // 2-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700785 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700786 EXPECT_EQ(2U, n);
787 EXPECT_EQ(L'¢', wc);
788 // 3-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700789 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700790 EXPECT_EQ(3U, n);
791 EXPECT_EQ(L'€', wc);
792 // 4-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700793 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700794 EXPECT_EQ(4U, n);
795 EXPECT_EQ(L'𤭢', wc);
796}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700797
798TEST(wchar, open_wmemstream) {
799 wchar_t* p = nullptr;
800 size_t size = 0;
801 FILE* fp = open_wmemstream(&p, &size);
802 ASSERT_NE(EOF, fputws(L"hello, world!", fp));
803 fclose(fp);
804
805 ASSERT_STREQ(L"hello, world!", p);
806 ASSERT_EQ(wcslen(L"hello, world!"), size);
807 free(p);
808}
809
810TEST(stdio, open_wmemstream_EINVAL) {
811#if defined(__BIONIC__)
812 wchar_t* p;
813 size_t size;
zijunzhao7ce2f952023-04-03 23:13:57 +0000814#pragma clang diagnostic push
815#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes6b841db2014-08-20 16:10:49 -0700816 // Invalid buffer.
817 errno = 0;
818 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
819 ASSERT_EQ(EINVAL, errno);
820
821 // Invalid size.
822 errno = 0;
823 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
824 ASSERT_EQ(EINVAL, errno);
zijunzhao7ce2f952023-04-03 23:13:57 +0000825#pragma clang diagnostic pop
Elliott Hughes6b841db2014-08-20 16:10:49 -0700826#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800827 GTEST_SKIP() << "This test is bionic-specific";
Elliott Hughes6b841db2014-08-20 16:10:49 -0700828#endif
829}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700830
831TEST(wchar, wcstol_EINVAL) {
832 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700833 wcstol(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700834 ASSERT_EQ(EINVAL, errno);
835 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700836 wcstol(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700837 ASSERT_EQ(EINVAL, errno);
838 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700839 wcstol(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700840 ASSERT_EQ(EINVAL, errno);
841}
842
843TEST(wchar, wcstoll_EINVAL) {
844 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700845 wcstoll(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700846 ASSERT_EQ(EINVAL, errno);
847 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700848 wcstoll(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700849 ASSERT_EQ(EINVAL, errno);
850 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700851 wcstoll(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700852 ASSERT_EQ(EINVAL, errno);
853}
854
855TEST(wchar, wcstoul_EINVAL) {
856 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700857 wcstoul(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700858 ASSERT_EQ(EINVAL, errno);
859 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700860 wcstoul(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700861 ASSERT_EQ(EINVAL, errno);
862 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700863 wcstoul(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700864 ASSERT_EQ(EINVAL, errno);
865}
866
867TEST(wchar, wcstoull_EINVAL) {
868 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700869 wcstoull(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700870 ASSERT_EQ(EINVAL, errno);
871 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700872 wcstoull(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700873 ASSERT_EQ(EINVAL, errno);
874 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700875 wcstoull(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700876 ASSERT_EQ(EINVAL, errno);
877}
878
879TEST(wchar, wcstoll_l_EINVAL) {
880 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000881 wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700882 ASSERT_EQ(EINVAL, errno);
883 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000884 wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700885 ASSERT_EQ(EINVAL, errno);
886 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000887 wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700888 ASSERT_EQ(EINVAL, errno);
889}
890
891TEST(wchar, wcstoull_l_EINVAL) {
892 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000893 wcstoull_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700894 ASSERT_EQ(EINVAL, errno);
895 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000896 wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700897 ASSERT_EQ(EINVAL, errno);
898 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000899 wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700900 ASSERT_EQ(EINVAL, errno);
901}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800902
903TEST(wchar, wmempcpy) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700904#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800905 wchar_t dst[6];
906 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
Colin Cross7da20342021-07-28 11:18:11 -0700907#else
908 GTEST_SKIP() << "musl doesn't have wmempcpy";
909#endif
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800910}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700911
912template <typename T>
Dan Albert6805c2d2017-08-09 14:55:27 -0700913using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700914
Dan Albert6805c2d2017-08-09 14:55:27 -0700915template <typename T>
916void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
917 T expected_value, ptrdiff_t expected_len) {
Dan Albertf6346552016-12-02 12:02:03 -0800918 wchar_t* p;
Dan Albert6805c2d2017-08-09 14:55:27 -0700919 ASSERT_EQ(expected_value, fn(str, &p));
920 ASSERT_EQ(expected_len, p - str);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700921}
922
Dan Albert6805c2d2017-08-09 14:55:27 -0700923template <typename T>
924void TestWcsToFloat(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800925 TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
926 TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
927 TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
928 TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
929 TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
930 TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
Dan Albert6805c2d2017-08-09 14:55:27 -0700931}
932
933template <typename T>
934void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800935 TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
936 TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
937 TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
938 TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
Dan Albert6805c2d2017-08-09 14:55:27 -0700939}
940
941template <typename T>
942void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
943 ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
944 ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
945 ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
946
947 ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
948 ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
949 ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
950
951 wchar_t* p;
952 ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
953 ASSERT_STREQ(L"ny", p);
954 ASSERT_TRUE(isnan(fn(L"nanny", &p)));
955 ASSERT_STREQ(L"ny", p);
956 ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
957 ASSERT_STREQ(L"ny", p);
958
959 ASSERT_EQ(0, fn(L"muppet", &p));
960 ASSERT_STREQ(L"muppet", p);
961 ASSERT_EQ(0, fn(L" muppet", &p));
962 ASSERT_STREQ(L" muppet", p);
963
964 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
965 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
966 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
967
968 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
969 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
970 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
971
972 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
973 ASSERT_STREQ(L"initude", p);
974 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
975 ASSERT_STREQ(L"initude", p);
976 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
977 ASSERT_STREQ(L"initude", p);
978
979 // Check case-insensitivity.
980 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
981 ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700982}
983
984TEST(wchar, wcstof) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700985 TestWcsToFloat(wcstof);
986}
987
988TEST(wchar, wcstof_hex_floats) {
989 TestWcsToFloatHexFloats(wcstof);
990}
991
992TEST(wchar, wcstof_hex_inf_nan) {
993 TestWcsToFloatInfNan(wcstof);
994}
995
996TEST(wchar, wcstod) {
997 TestWcsToFloat(wcstod);
998}
999
1000TEST(wchar, wcstod_hex_floats) {
1001 TestWcsToFloatHexFloats(wcstod);
1002}
1003
1004TEST(wchar, wcstod_hex_inf_nan) {
1005 TestWcsToFloatInfNan(wcstod);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001006}
1007
1008TEST(wchar, wcstold) {
Dan Albert6805c2d2017-08-09 14:55:27 -07001009 TestWcsToFloat(wcstold);
1010}
1011
1012TEST(wchar, wcstold_hex_floats) {
1013 TestWcsToFloatHexFloats(wcstold);
1014}
1015
1016TEST(wchar, wcstold_hex_inf_nan) {
1017 TestWcsToFloatInfNan(wcstold);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001018}
Elliott Hughesc41b5602017-07-27 17:08:08 -07001019
Elliott Hughes3376c232018-02-13 23:14:12 -08001020TEST(wchar, wcstod_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001021#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001022 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001023#else
1024 GTEST_SKIP() << "musl doesn't have wcstod_l";
1025#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001026}
1027
1028TEST(wchar, wcstof_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.23f, wcstof_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 wcstof_l";
1033#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001034}
1035
1036TEST(wchar, wcstol_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001037#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001038 EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001039#else
1040 GTEST_SKIP() << "musl doesn't have wcstol_l";
1041#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001042}
1043
1044TEST(wchar, wcstold_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001045 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001046}
1047
1048TEST(wchar, wcstoll_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001049 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001050}
1051
1052TEST(wchar, wcstoul_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001053#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001054 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001055#else
1056 GTEST_SKIP() << "musl doesn't have wcstoul_l";
1057#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001058}
1059
1060TEST(wchar, wcstoull_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001061 EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001062}
1063
Elliott Hughesc41b5602017-07-27 17:08:08 -07001064static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
1065 for (wchar_t i = begin; i < end; ++i) {
1066 EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
1067 }
1068}
1069
1070TEST(wchar, wcwidth_NUL) {
1071 // NUL is defined to return 0 rather than -1, despite being a C0 control.
1072 EXPECT_EQ(0, wcwidth(0));
1073}
1074
1075TEST(wchar, wcwidth_ascii) {
1076 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1077}
1078
1079TEST(wchar, wcwidth_controls) {
1080 AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
1081 EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
1082 AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
1083}
1084
1085TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001086 if (!have_dl()) return;
1087
Elliott Hughesc41b5602017-07-27 17:08:08 -07001088 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1089 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
1090 EXPECT_EQ(0, wcwidth(0x00ad)); // Soft hyphen (SHY).
1091 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1092}
1093
1094TEST(wchar, wcwidth_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001095 if (!have_dl()) return;
1096
Elliott Hughesc41b5602017-07-27 17:08:08 -07001097 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1098 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1099 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1100 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1101 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1102 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1103}
1104
1105TEST(wchar, wcwidth_korean_combining_jamo) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001106 if (!have_dl()) return;
1107
Elliott Hughesc41b5602017-07-27 17:08:08 -07001108 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1109 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1110 EXPECT_EQ(0, wcwidth(0xd7cb));
1111}
1112
1113TEST(wchar, wcwidth_korean_jeongeul_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001114 if (!have_dl()) return;
1115
Elliott Hughesc41b5602017-07-27 17:08:08 -07001116 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
1117 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points in Unicode 7.
1118 // Undefined characters at the end of the block have width 1.
1119}
1120
1121TEST(wchar, wcwidth_kana) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001122 if (!have_dl()) return;
1123
Elliott Hughesc41b5602017-07-27 17:08:08 -07001124 // Hiragana (most, not undefined).
1125 AssertWcwidthRange(0x3041, 0x3097, 2);
1126 // Katakana.
1127 AssertWcwidthRange(0x30a0, 0x3100, 2);
1128}
1129
1130TEST(wchar, wcwidth_circled_two_digit_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001131 if (!have_dl()) return;
1132
Elliott Hughesc41b5602017-07-27 17:08:08 -07001133 // Circled two-digit CJK "speed sign" numbers are wide,
1134 // though EastAsianWidth is ambiguous.
1135 AssertWcwidthRange(0x3248, 0x3250, 2);
1136}
1137
1138TEST(wchar, wcwidth_hexagrams) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001139 if (!have_dl()) return;
1140
Elliott Hughesc41b5602017-07-27 17:08:08 -07001141 // Hexagrams are wide, though EastAsianWidth is neutral.
1142 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1143}
1144
1145TEST(wchar, wcwidth_default_ignorables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001146 if (!have_dl()) return;
1147
Elliott Hughesc41b5602017-07-27 17:08:08 -07001148 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1149 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1150}
1151
1152TEST(wchar, wcwidth_korean_common_non_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001153 if (!have_dl()) return;
1154
Elliott Hughesc41b5602017-07-27 17:08:08 -07001155 EXPECT_EQ(2, wcwidth(L'ㅜ')); // Korean "crying" emoticon.
1156 EXPECT_EQ(2, wcwidth(L'ã…‹')); // Korean "laughing" emoticon.
1157}
Elliott Hughesfe50a0c2021-04-09 08:51:09 -07001158
1159TEST(wchar, wcswidth) {
1160 EXPECT_EQ(2, wcswidth(L"abc", 2));
1161 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1162 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1163}
1164
1165TEST(wchar, wcslcpy) {
1166#if defined(__BIONIC__)
1167 wchar_t dst[32];
1168 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1169 ASSERT_STREQ(L"he", dst);
1170 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1171 ASSERT_STREQ(L"hello world", dst);
1172#else
1173 GTEST_SKIP() << "no wcslcpy in glibc";
1174#endif
1175}
1176
1177TEST(wchar, wcscat) {
1178 wchar_t dst[32];
1179 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1180 ASSERT_STREQ(dst, L"hello");
1181 ASSERT_EQ(dst, wcscat(dst, L" world"));
1182 ASSERT_STREQ(dst, L"hello world");
1183}
1184
1185TEST(wchar, wcscpy) {
1186 wchar_t dst[32];
1187 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1188 ASSERT_STREQ(dst, L"hello");
1189 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1190 ASSERT_STREQ(dst, L"world");
1191}
1192
1193TEST(wchar, wcscasecmp) {
1194 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1195 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1196 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1197 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1198 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1199}
1200
1201TEST(wchar, wcscspn) {
1202 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1203 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1204 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1205}
1206
1207TEST(wchar, wcsspn) {
1208 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1209 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1210 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1211}
1212
1213TEST(wchar, wcsdup) {
1214 wchar_t* s = wcsdup(L"hello");
1215 ASSERT_STREQ(s, L"hello");
1216 free(s);
1217}
1218
1219TEST(wchar, wcslcat) {
1220#if defined(__BIONIC__)
1221 wchar_t dst[4] = {};
1222 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1223 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1224 ASSERT_STREQ(dst, L"abc");
1225#else
1226 GTEST_SKIP() << "no wcslcpy in glibc";
1227#endif
1228}
1229
1230TEST(wchar, wcsncasecmp) {
1231 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1232
1233 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1234 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1235 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1236 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1237 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1238}
1239
1240TEST(wchar, wcsncat) {
1241 wchar_t dst[32];
1242 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1243 ASSERT_STREQ(dst, L"hello");
1244 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1245 ASSERT_STREQ(dst, L"hello");
1246 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1247 ASSERT_STREQ(dst, L"hello, world!");
1248}
1249
1250TEST(wchar, wcsncmp) {
1251 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1252 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1253 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1254 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1255}
1256
1257TEST(wchar, wcsnlen) {
1258 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1259 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1260 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1261}
1262
1263TEST(wchar, wcspbrk) {
1264 const wchar_t* s = L"hello, world!";
1265 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1266 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1267 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1268 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1269}
1270
1271TEST(wchar, wcstok) {
1272 wchar_t s[] = L"this is\ta\nstring";
1273 wchar_t* p;
1274 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1275 ASSERT_STREQ(s, L"this");
1276 ASSERT_STREQ(p, L"is\ta\nstring");
1277 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1278 ASSERT_STREQ(s + 5, L"is");
1279 ASSERT_STREQ(p, L"a\nstring");
1280 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1281 ASSERT_STREQ(s + 8, L"a");
1282 ASSERT_STREQ(p, L"string");
1283 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1284 ASSERT_STREQ(s + 10, L"string");
1285 ASSERT_EQ(nullptr, p);
1286}
1287
1288TEST(wchar, wmemchr) {
1289 const wchar_t* s = L"hello, world!";
1290 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1291 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1292 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1293 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1294}
1295
1296TEST(wchar, wmemcmp) {
1297 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1298 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1299 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1300}
1301
1302TEST(wchar, wmemcpy) {
1303 wchar_t dst[32] = {};
1304 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1305 ASSERT_STREQ(dst, L"hello");
1306}
1307
1308TEST(wchar, wmemmove) {
1309 wchar_t dst[32] = {};
1310 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1311 ASSERT_STREQ(dst, L"hello");
1312}
1313
1314TEST(wchar, wmemset) {
1315 wchar_t dst[4] = {};
1316 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1317 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1318 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1319 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1320 ASSERT_EQ(dst[3], wchar_t(0));
1321 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1322 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1323}