blob: f895b16854b73dc499d89a7404f83f258ec836f6 [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 Albert512469a2023-08-03 19:34:42 +000041// C23 7.31.6.3.2 (mbrtowc) says:
42//
43// Returns:
44//
45// 0 if the next n or fewer bytes complete the multibyte character that
46// corresponds to the null wide character (which is the value stored).
47//
48// (size_t)(-2) if the next n bytes contribute to an incomplete (but
49// potentially valid) multibyte character, and all n bytes have been
50// processed (no value is stored).
51//
52// Bionic historically interpreted the behavior when n is 0 to be the next 0
53// bytes decoding to the null. That's a pretty bad interpretation, and both
54// glibc and musl return -2 for that case.
55//
56// The tests currently checks the incorrect behavior for bionic because gtest
57// doesn't support explicit xfail annotations. The behavior difference here
58// should be fixed, but danalbert wants to add more tests before tackling the
59// bugs.
60#ifdef __ANDROID__
61constexpr size_t kExpectedResultForZeroLength = 0U;
62#else
63constexpr size_t kExpectedResultForZeroLength = static_cast<size_t>(-2);
64#endif
65
Elliott Hughes77e944f2014-04-04 17:34:51 -070066TEST(wchar, sizeof_wchar_t) {
67 EXPECT_EQ(4U, sizeof(wchar_t));
68 EXPECT_EQ(4U, sizeof(wint_t));
69}
70
71TEST(wchar, mbrlen) {
72 char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
Dan Albert512469a2023-08-03 19:34:42 +000073 EXPECT_EQ(kExpectedResultForZeroLength, mbrlen(&bytes[0], 0, nullptr));
Yi Kong32bc0fc2018-08-02 17:31:13 -070074 EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070075
Yi Kong32bc0fc2018-08-02 17:31:13 -070076 EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
77 EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070078}
79
80TEST(wchar, wctomb_wcrtomb) {
81 // wctomb and wcrtomb behave differently when s == NULL.
Yi Kong32bc0fc2018-08-02 17:31:13 -070082 EXPECT_EQ(0, wctomb(nullptr, L'h'));
83 EXPECT_EQ(0, wctomb(nullptr, L'\0'));
84 EXPECT_EQ(1U, wcrtomb(nullptr, L'\0', nullptr));
85 EXPECT_EQ(1U, wcrtomb(nullptr, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070086
87 char bytes[MB_LEN_MAX];
88
89 // wctomb and wcrtomb behave similarly for the null wide character.
90 EXPECT_EQ(1, wctomb(bytes, L'\0'));
Yi Kong32bc0fc2018-08-02 17:31:13 -070091 EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070092
93 // ...and for regular characters.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070094 memset(bytes, 0, sizeof(bytes));
Elliott Hughes77e944f2014-04-04 17:34:51 -070095 EXPECT_EQ(1, wctomb(bytes, L'h'));
96 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070097 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070099 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700100
101 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
102 uselocale(LC_GLOBAL_LOCALE);
103
104 // 1-byte UTF-8.
105 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700106 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700107 EXPECT_EQ('h', bytes[0]);
108 // 2-byte UTF-8.
109 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700110 EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700111 EXPECT_EQ('\xc2', bytes[0]);
112 EXPECT_EQ('\xa2', bytes[1]);
113 // 3-byte UTF-8.
114 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700115 EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700116 EXPECT_EQ('\xe2', bytes[0]);
117 EXPECT_EQ('\x82', bytes[1]);
118 EXPECT_EQ('\xac', bytes[2]);
119 // 4-byte UTF-8.
120 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700122 EXPECT_EQ('\xf0', bytes[0]);
123 EXPECT_EQ('\xa4', bytes[1]);
124 EXPECT_EQ('\xad', bytes[2]);
125 EXPECT_EQ('\xa2', bytes[3]);
126 // Invalid code point.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700127 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700128 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes77e944f2014-04-04 17:34:51 -0700129}
Elliott Hughes05493712014-04-17 17:30:03 -0700130
Calin Juravle15a63102014-05-08 14:38:35 +0100131TEST(wchar, wcrtomb_start_state) {
132 char out[MB_LEN_MAX];
133 mbstate_t ps;
134
135 // Any non-initial state is invalid when calling wcrtomb.
136 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700137 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100138 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
139 EXPECT_EQ(EILSEQ, errno);
140
141 // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
142 // state should be reset.
143 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700144 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
145 EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100146 EXPECT_TRUE(mbsinit(&ps));
147
148 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700149 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100150 EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
151 EXPECT_TRUE(mbsinit(&ps));
152}
153
Elliott Hughes05493712014-04-17 17:30:03 -0700154TEST(wchar, wcstombs_wcrtombs) {
Elliott Hughes1b836ee2014-04-18 13:32:33 -0700155 const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
Elliott Hughesf83e6442014-05-01 17:14:59 -0700156 const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
Elliott Hughes05493712014-04-17 17:30:03 -0700157 const wchar_t* src;
158 char bytes[BUFSIZ];
159
160 // Given a NULL destination, these functions count valid characters.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700161 EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
162 EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
163 EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700164 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700165 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700166 EXPECT_EQ(&chars[0], src);
167 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700168 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700169 EXPECT_EQ(&chars[0], src);
170 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700171 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700172 EXPECT_EQ(&chars[0], src);
173
174 // An unrepresentable char just returns an error from wcstombs...
175 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700176 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
Elliott Hughes05493712014-04-17 17:30:03 -0700177 EXPECT_EQ(EILSEQ, errno);
178 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700179 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700180 EXPECT_EQ(EILSEQ, errno);
181
182 // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
183 // to actually convert anything...
184 errno = 0;
185 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700186 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700187 EXPECT_EQ(&bad_chars[0], src);
188 EXPECT_EQ(EILSEQ, errno);
189 errno = 0;
190 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700191 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700192 EXPECT_EQ(&bad_chars[0], src);
193 EXPECT_EQ(EILSEQ, errno);
194
195 // Okay, now let's test actually converting something...
196 memset(bytes, 'x', sizeof(bytes));
197 EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
198 memset(bytes, 'x', sizeof(bytes));
199 EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
200 bytes[5] = 0;
201 EXPECT_STREQ("hellx", bytes);
202 memset(bytes, 'x', sizeof(bytes));
203 EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
204 EXPECT_STREQ("hello", bytes);
205 memset(bytes, 'x', sizeof(bytes));
206 EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
207 EXPECT_STREQ("hello", bytes);
208 errno = 0;
209 memset(bytes, 'x', sizeof(bytes));
210 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
211 EXPECT_EQ(EILSEQ, errno);
212 bytes[3] = 0;
213 EXPECT_STREQ("hix", bytes);
214
215 // wcsrtombs is a bit more informative...
216 memset(bytes, 'x', sizeof(bytes));
217 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700218 EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700219 EXPECT_EQ(&chars[0], src); // No input consumed.
220 EXPECT_EQ(EILSEQ, errno);
221
222 memset(bytes, 'x', sizeof(bytes));
223 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700224 EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700225 EXPECT_EQ(&chars[4], src); // Some input consumed.
226 EXPECT_EQ(EILSEQ, errno);
227 bytes[5] = 0;
228 EXPECT_STREQ("hellx", bytes);
229
230 memset(bytes, 'x', sizeof(bytes));
231 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700232 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
233 EXPECT_EQ(nullptr, src); // All input consumed!
Elliott Hughes05493712014-04-17 17:30:03 -0700234 EXPECT_EQ(EILSEQ, errno);
235 EXPECT_STREQ("hello", bytes);
236
237 memset(bytes, 'x', sizeof(bytes));
238 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700239 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
240 EXPECT_EQ(nullptr, src); // All input consumed.
Elliott Hughes05493712014-04-17 17:30:03 -0700241 EXPECT_EQ(EILSEQ, errno);
242 EXPECT_STREQ("hello", bytes);
243
244 memset(bytes, 'x', sizeof(bytes));
245 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700246 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700247 EXPECT_EQ(&bad_chars[2], src);
248 EXPECT_EQ(EILSEQ, errno);
249 bytes[3] = 0;
250 EXPECT_STREQ("hix", bytes);
Calin Juravle15a63102014-05-08 14:38:35 +0100251
252 // Any non-initial state is invalid when calling wcsrtombs.
253 mbstate_t ps;
254 src = chars;
255 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700256 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
257 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100258 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes05493712014-04-17 17:30:03 -0700259}
Elliott Hughes83c07b52014-04-21 18:09:46 -0700260
261TEST(wchar, limits) {
262 ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
263}
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700264
Dan Albert001f8f02014-06-04 09:53:06 -0700265TEST(wchar, wcsstr) {
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700266 const wchar_t* haystack = L"big daddy/giant haystacks!";
267 const wchar_t* empty_haystack = L"";
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700268
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700269 // The empty needle is a special case.
270 ASSERT_EQ(haystack, wcsstr(haystack, L""));
271 ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
272
273 ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
274 ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
275 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
276 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
277 ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
278 ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
279
280 ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
281 ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700282}
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700283
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800284TEST(wchar, wcsstr_80199) {
285 // https://code.google.com/p/android/issues/detail?id=80199
Yi Kong32bc0fc2018-08-02 17:31:13 -0700286 ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800287}
288
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700289TEST(wchar, mbtowc) {
290 wchar_t out[8];
291
Dan Albert512469a2023-08-03 19:34:42 +0000292 // bionic has the same misunderstanding of the result for a zero-length
293 // conversion for mbtowc as it does for all the other multibyte conversion
294 // functions but mbtowc returns different values than all the others:
295 //
296 // C23 7.24.7.2.4:
297 //
298 // If s is a null pointer, the mbtowc function returns a nonzero or zero
299 // value, if multibyte character encodings, respectively, do or do not have
300 // state-dependent encodings. If s is not a null pointer, the mbtowc function
301 // either returns 0 (if s points to the null character), or returns the number
302 // of bytes that are contained in the converted multibyte character (if the
303 // next n or fewer bytes form a valid multibyte character), or returns -1 (if
304 // they do not form a valid multibyte character).
305
306#ifdef __BIONIC__
307 int expected_result_for_zero_length = 0;
308#else
309 int expected_result_for_zero_length = -1;
310#endif
311
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700312 out[0] = 'x';
Dan Albert512469a2023-08-03 19:34:42 +0000313 EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
314 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700315
Dan Albert512469a2023-08-03 19:34:42 +0000316 EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
317 EXPECT_EQ(0, mbtowc(out, "", 0));
318 EXPECT_EQ(1, mbtowc(out, "hello", 1));
319 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700320
Dan Albert512469a2023-08-03 19:34:42 +0000321 EXPECT_EQ(expected_result_for_zero_length, mbtowc(nullptr, "hello", 0));
322 EXPECT_EQ(0, mbtowc(nullptr, "", 0));
323 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700324
Dan Albert512469a2023-08-03 19:34:42 +0000325 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700326}
327
328TEST(wchar, mbrtowc) {
329 wchar_t out[8];
330
331 out[0] = 'x';
Dan Albert512469a2023-08-03 19:34:42 +0000332 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
333 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700334
Dan Albert512469a2023-08-03 19:34:42 +0000335 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
336 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "", 0, nullptr));
337 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
338 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700339
Dan Albert512469a2023-08-03 19:34:42 +0000340 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "hello", 0, nullptr));
341 EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "", 0, nullptr));
342 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700343
Dan Albert512469a2023-08-03 19:34:42 +0000344 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700345
Dan Albert512469a2023-08-03 19:34:42 +0000346 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700347 uselocale(LC_GLOBAL_LOCALE);
348
349 // 1-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000350 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
351 EXPECT_EQ(L'a', out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700352 // 2-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000353 EXPECT_EQ(2U, mbrtowc(out,
354 "\xc2\xa2"
355 "cdef",
356 6, nullptr));
357 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700358 // 3-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000359 EXPECT_EQ(3U, mbrtowc(out,
360 "\xe2\x82\xac"
361 "def",
362 6, nullptr));
363 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700364 // 4-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000365 EXPECT_EQ(4U, mbrtowc(out,
366 "\xf0\xa4\xad\xa2"
367 "ef",
368 6, nullptr));
369 EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
Elliott Hughes063525c2014-05-13 11:19:57 -0700370#if defined(__BIONIC__) // glibc allows this.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700371 // Illegal 5-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000372 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
373 "\xf8\xa1\xa2\xa3\xa4"
374 "f",
375 6, nullptr));
376 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700377#endif
378 // Illegal over-long sequence.
Dan Albert512469a2023-08-03 19:34:42 +0000379 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
380 "\xf0\x82\x82\xac"
381 "ef",
382 6, nullptr));
383 EXPECT_EQ(EILSEQ, errno);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700384}
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700385
Elliott Hughes402c7622018-07-06 17:18:05 -0700386TEST(wchar, mbrtowc_valid_non_characters) {
387 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
388 uselocale(LC_GLOBAL_LOCALE);
389
390 wchar_t out[8] = {};
391
392 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
393 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
394 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
395 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
396}
397
398TEST(wchar, mbrtowc_out_of_range) {
399 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
400 uselocale(LC_GLOBAL_LOCALE);
401
402 wchar_t out[8] = {};
403 errno = 0;
404 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr));
405 ASSERT_EQ(EILSEQ, errno);
406}
407
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700408static void test_mbrtowc_incomplete(mbstate_t* ps) {
Calin Juravle15a63102014-05-08 14:38:35 +0100409 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
410 uselocale(LC_GLOBAL_LOCALE);
411
412 wchar_t out;
413 // 2-byte UTF-8.
414 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
415 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700416 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100417 ASSERT_TRUE(mbsinit(ps));
418 // 3-byte UTF-8.
419 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
420 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
421 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700422 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100423 ASSERT_TRUE(mbsinit(ps));
424 // 4-byte UTF-8.
425 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
426 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
427 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700428 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100429 ASSERT_TRUE(mbsinit(ps));
430
431 // Invalid 2-byte
432 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
433 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
434 ASSERT_EQ(EILSEQ, errno);
435}
436
437TEST(wchar, mbrtowc_incomplete) {
438 mbstate_t ps;
439 memset(&ps, 0, sizeof(ps));
440
441 test_mbrtowc_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700442 test_mbrtowc_incomplete(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100443}
444
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700445static void test_mbsrtowcs(mbstate_t* ps) {
446 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
447 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
448 constexpr const char* INCOMPLETE = "A" "\xc2";
Calin Juravle15a63102014-05-08 14:38:35 +0100449 wchar_t out[4];
450
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700451 const char* valid = VALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100452 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
453 ASSERT_EQ(L'A', out[0]);
Ben Chengcaff5f22014-05-19 14:27:31 -0700454 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
455 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
456 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
Dan Albert6b55ba52014-07-20 11:51:26 -0700457 // Check that valid has advanced to the next unread character.
Calin Juravle15a63102014-05-08 14:38:35 +0100458 ASSERT_EQ('e', *valid);
459
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800460 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
Dan Albert6b55ba52014-07-20 11:51:26 -0700461 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
462 ASSERT_EQ(L'e', out[0]);
463 ASSERT_EQ(L'f', out[1]);
464 ASSERT_EQ(L'\0', out[2]);
465 // Check that we didn't clobber the rest of out.
466 ASSERT_EQ(L'x', out[3]);
467 // Check that valid has advanced to the end of the string.
Dan Albertb6cc8e02014-07-31 11:31:03 -0700468 ASSERT_EQ(nullptr, valid);
Dan Albert6b55ba52014-07-20 11:51:26 -0700469
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700470 const char* invalid = INVALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100471 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
472 EXPECT_EQ(EILSEQ, errno);
473 ASSERT_EQ('\xc2', *invalid);
474
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700475 const char* incomplete = INCOMPLETE;
Calin Juravle15a63102014-05-08 14:38:35 +0100476 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
477 EXPECT_EQ(EILSEQ, errno);
478 ASSERT_EQ('\xc2', *incomplete);
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700479
480 // If dst is null, *src shouldn't be updated.
481 // https://code.google.com/p/android/issues/detail?id=166381
482 const char* mbs = VALID;
483 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
484 EXPECT_EQ(VALID, mbs);
485 mbs = INVALID;
486 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
487 EXPECT_EQ(INVALID, mbs);
488 mbs = INCOMPLETE;
489 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
490 EXPECT_EQ(INCOMPLETE, mbs);
Calin Juravle15a63102014-05-08 14:38:35 +0100491}
492
493TEST(wchar, mbsrtowcs) {
494 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
495 uselocale(LC_GLOBAL_LOCALE);
496
497 mbstate_t ps;
498 memset(&ps, 0, sizeof(ps));
499 test_mbsrtowcs(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700500 test_mbsrtowcs(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100501
502 // Invalid multi byte continuation.
503 const char* invalid = "\x20";
504 wchar_t out;
505 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
506 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
507 EXPECT_EQ(EILSEQ, errno);
508 ASSERT_EQ('\x20', *invalid);
509}
510
Dan Albert6805c2d2017-08-09 14:55:27 -0700511template <typename T>
512using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
513
514template <typename T>
515void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
516 T expected_value, ptrdiff_t expected_len) {
517 wchar_t* p;
518 ASSERT_EQ(expected_value, fn(str, &p, base));
519 ASSERT_EQ(expected_len, p - str) << str;
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700520}
521
Dan Albert6805c2d2017-08-09 14:55:27 -0700522template <typename T>
523void TestWcsToInt(WcsToIntFn<T> fn) {
524 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
525 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
526 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
527 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
528 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
529 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
530 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
531 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
Elliott Hughes1f462de2022-08-05 22:51:05 +0000532 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
Dan Albert6805c2d2017-08-09 14:55:27 -0700533}
534
535template <typename T>
536void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
537 const wchar_t* max_str) {
538 if (std::is_signed<T>::value) {
539 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
540 } else {
541 // If the subject sequence begins with a <hyphen-minus>, the value resulting
542 // from the conversion shall be negated.
543 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
544 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
545 }
546 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
547}
548
549TEST(wchar, wcstol) {
550 TestWcsToInt(wcstol);
551}
552
553TEST(wchar, wcstol_limits) {
554 if (sizeof(long) == 8) {
555 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
556 } else {
557 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
558 }
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700559}
560
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700561TEST(wchar, wcstoul) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700562 TestWcsToInt(wcstoul);
563}
564
565TEST(wchar, wcstoul_limits) {
566 if (sizeof(long) == 8) {
567 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
568 } else {
569 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
570 }
571}
572
573TEST(wchar, wcstoll) {
574 TestWcsToInt(wcstoll);
575}
576
577TEST(wchar, wcstoll_limits) {
578 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700579}
580
581TEST(wchar, wcstoull) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700582 TestWcsToInt(wcstoull);
583}
584
585TEST(wchar, wcstoull_limits) {
586 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
587}
588
589TEST(wchar, wcstoimax) {
590 TestWcsToInt(wcstoimax);
591}
592
593TEST(wchar, wcstoimax_limits) {
594 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
595 L"9223372036854775808");
596}
597
598TEST(wchar, wcstoumax) {
599 TestWcsToInt(wcstoumax);
600}
601
602TEST(wchar, wcstoumax_limits) {
603 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700604}
605
606TEST(wchar, mbsnrtowcs) {
607 wchar_t dst[128];
608 const char* s = "hello, world!";
609 const char* src;
610
611 memset(dst, 0, sizeof(dst));
612 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700613 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700614
615 memset(dst, 0, sizeof(dst));
616 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700617 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700618 ASSERT_EQ(L'h', dst[0]);
619 ASSERT_EQ(L'e', dst[1]);
620 ASSERT_EQ(&s[2], src);
621
622 memset(dst, 0, sizeof(dst));
623 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700624 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700625 ASSERT_EQ(L'h', dst[0]);
626 ASSERT_EQ(L'e', dst[1]);
627 ASSERT_EQ(L'l', dst[2]);
628 ASSERT_EQ(&s[3], src);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700629
630 memset(dst, 0, sizeof(dst));
631 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
632 src = incomplete;
633 errno = 0;
634 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
635 ASSERT_EQ(EILSEQ, errno);
636
637 src = incomplete;
638 errno = 0;
639 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
640 ASSERT_EQ(EILSEQ, errno);
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700641}
Elliott Hughesefaa4612014-05-02 15:53:03 -0700642
Elliott Hughes3376c232018-02-13 23:14:12 -0800643TEST(wchar, wcsftime__wcsftime_l) {
Elliott Hughesefaa4612014-05-02 15:53:03 -0700644 setenv("TZ", "UTC", 1);
645
646 struct tm t;
647 memset(&t, 0, sizeof(tm));
648 t.tm_year = 200;
649 t.tm_mon = 2;
650 t.tm_mday = 10;
651
652 wchar_t buf[64];
653
654 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
655 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Dan Albert686e67d2023-08-03 19:00:09 +0000656 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -0800657 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Elliott Hughesefaa4612014-05-02 15:53:03 -0700658}
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200659
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800660TEST(wchar, wmemmove_smoke) {
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200661 const wchar_t const_wstr[] = L"This is a test of something or other.....";
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800662 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200663
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800664 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200665 EXPECT_STREQ(const_wstr, wstr);
666
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800667 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
Christopher Ferris8bf50d52014-05-29 15:42:42 -0700668 EXPECT_STREQ(L"This This is a test of something or other", wstr);
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200669}
Elliott Hughes69f05d22014-06-05 20:10:09 -0700670
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800671TEST(wchar, wmemcpy_smoke) {
672 const wchar_t src[] = L"Source string";
673 wchar_t dst[NUM_WCHARS(sizeof(src))];
674
675 EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
676 EXPECT_STREQ(dst, src);
677}
678
679TEST(wchar, wcpcpy_smoke) {
680 const wchar_t src[] = L"Source string";
681 wchar_t dst[NUM_WCHARS(sizeof(src))];
682
683 EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
684 EXPECT_STREQ(dst, src);
685}
686
687TEST(wchar, wcpncpy_smoke) {
688 const wchar_t src[] = L"Source string";
689 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
690
691 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
692 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
693 EXPECT_STREQ(dst, src);
694
695 EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
696 dst[6] = L'\0';
697 EXPECT_STREQ(dst, L"Source");
698
699 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
700 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
701 EXPECT_STREQ(dst, src);
702 EXPECT_EQ(dst[src_len], L'\0');
703 EXPECT_EQ(dst[src_len+1], L'\0');
704 EXPECT_EQ(dst[src_len+2], L'\0');
705 EXPECT_EQ(dst[src_len+3], L'\0');
706 EXPECT_EQ(dst[src_len+4], L'x');
707}
708
709TEST(wchar, wcscpy_smoke) {
710 const wchar_t src[] = L"Source string";
711 wchar_t dst[NUM_WCHARS(sizeof(src))];
712
713 EXPECT_EQ(dst, wcscpy(dst, src));
714 EXPECT_STREQ(src, dst);
715}
716
717TEST(wchar, wcsncpy_smoke) {
718 const wchar_t src[] = L"Source string";
719 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
720
721 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
722 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
723 EXPECT_STREQ(dst, src);
724
725 EXPECT_EQ(dst, wcsncpy(dst, src, 6));
726 dst[6] = L'\0';
727 EXPECT_STREQ(dst, L"Source");
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700728 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
729 EXPECT_STREQ(dst, L"Source");
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800730
731 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
732 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
733 EXPECT_STREQ(dst, src);
734 EXPECT_EQ(dst[src_len], L'\0');
735 EXPECT_EQ(dst[src_len+1], L'\0');
736 EXPECT_EQ(dst[src_len+2], L'\0');
737 EXPECT_EQ(dst[src_len+3], L'\0');
738 EXPECT_EQ(dst[src_len+4], L'x');
739}
740
Elliott Hughes69f05d22014-06-05 20:10:09 -0700741TEST(wchar, mbrtowc_15439554) {
742 // http://b/15439554
743 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
744 uselocale(LC_GLOBAL_LOCALE);
745
746 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
747 ASSERT_GE(MB_CUR_MAX, 4U);
748
749 wchar_t wc;
750 size_t n;
751
752 // 1-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700753 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700754 EXPECT_EQ(1U, n);
755 EXPECT_EQ(L'x', wc);
756 // 2-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700757 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700758 EXPECT_EQ(2U, n);
759 EXPECT_EQ(L'¢', wc);
760 // 3-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700761 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700762 EXPECT_EQ(3U, n);
763 EXPECT_EQ(L'€', wc);
764 // 4-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700765 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700766 EXPECT_EQ(4U, n);
767 EXPECT_EQ(L'𤭢', wc);
768}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700769
770TEST(wchar, open_wmemstream) {
771 wchar_t* p = nullptr;
772 size_t size = 0;
773 FILE* fp = open_wmemstream(&p, &size);
774 ASSERT_NE(EOF, fputws(L"hello, world!", fp));
775 fclose(fp);
776
777 ASSERT_STREQ(L"hello, world!", p);
778 ASSERT_EQ(wcslen(L"hello, world!"), size);
779 free(p);
780}
781
782TEST(stdio, open_wmemstream_EINVAL) {
783#if defined(__BIONIC__)
784 wchar_t* p;
785 size_t size;
zijunzhao7ce2f952023-04-03 23:13:57 +0000786#pragma clang diagnostic push
787#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes6b841db2014-08-20 16:10:49 -0700788 // Invalid buffer.
789 errno = 0;
790 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
791 ASSERT_EQ(EINVAL, errno);
792
793 // Invalid size.
794 errno = 0;
795 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
796 ASSERT_EQ(EINVAL, errno);
zijunzhao7ce2f952023-04-03 23:13:57 +0000797#pragma clang diagnostic pop
Elliott Hughes6b841db2014-08-20 16:10:49 -0700798#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800799 GTEST_SKIP() << "This test is bionic-specific";
Elliott Hughes6b841db2014-08-20 16:10:49 -0700800#endif
801}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700802
803TEST(wchar, wcstol_EINVAL) {
804 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700805 wcstol(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700806 ASSERT_EQ(EINVAL, errno);
807 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700808 wcstol(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700809 ASSERT_EQ(EINVAL, errno);
810 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700811 wcstol(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700812 ASSERT_EQ(EINVAL, errno);
813}
814
815TEST(wchar, wcstoll_EINVAL) {
816 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700817 wcstoll(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700818 ASSERT_EQ(EINVAL, errno);
819 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700820 wcstoll(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700821 ASSERT_EQ(EINVAL, errno);
822 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700823 wcstoll(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700824 ASSERT_EQ(EINVAL, errno);
825}
826
827TEST(wchar, wcstoul_EINVAL) {
828 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700829 wcstoul(L"123", nullptr, -1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700830 ASSERT_EQ(EINVAL, errno);
831 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700832 wcstoul(L"123", nullptr, 1);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700833 ASSERT_EQ(EINVAL, errno);
834 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700835 wcstoul(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700836 ASSERT_EQ(EINVAL, errno);
837}
838
839TEST(wchar, wcstoull_EINVAL) {
840 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700841 wcstoull(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 wcstoull(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 wcstoull(L"123", nullptr, 37);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700848 ASSERT_EQ(EINVAL, errno);
849}
850
851TEST(wchar, wcstoll_l_EINVAL) {
852 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000853 wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700854 ASSERT_EQ(EINVAL, errno);
855 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000856 wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700857 ASSERT_EQ(EINVAL, errno);
858 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000859 wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700860 ASSERT_EQ(EINVAL, errno);
861}
862
863TEST(wchar, wcstoull_l_EINVAL) {
864 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000865 wcstoull_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700866 ASSERT_EQ(EINVAL, errno);
867 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000868 wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700869 ASSERT_EQ(EINVAL, errno);
870 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000871 wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700872 ASSERT_EQ(EINVAL, errno);
873}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800874
875TEST(wchar, wmempcpy) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700876#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800877 wchar_t dst[6];
878 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
Colin Cross7da20342021-07-28 11:18:11 -0700879#else
880 GTEST_SKIP() << "musl doesn't have wmempcpy";
881#endif
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800882}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700883
884template <typename T>
Dan Albert6805c2d2017-08-09 14:55:27 -0700885using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700886
Dan Albert6805c2d2017-08-09 14:55:27 -0700887template <typename T>
888void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
889 T expected_value, ptrdiff_t expected_len) {
Dan Albertf6346552016-12-02 12:02:03 -0800890 wchar_t* p;
Dan Albert6805c2d2017-08-09 14:55:27 -0700891 ASSERT_EQ(expected_value, fn(str, &p));
892 ASSERT_EQ(expected_len, p - str);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700893}
894
Dan Albert6805c2d2017-08-09 14:55:27 -0700895template <typename T>
896void TestWcsToFloat(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800897 TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
898 TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
899 TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
900 TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
901 TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
902 TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
Dan Albert6805c2d2017-08-09 14:55:27 -0700903}
904
905template <typename T>
906void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800907 TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
908 TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
909 TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
910 TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
Dan Albert6805c2d2017-08-09 14:55:27 -0700911}
912
913template <typename T>
914void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
915 ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
916 ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
917 ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
918
919 ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
920 ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
921 ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
922
923 wchar_t* p;
924 ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
925 ASSERT_STREQ(L"ny", p);
926 ASSERT_TRUE(isnan(fn(L"nanny", &p)));
927 ASSERT_STREQ(L"ny", p);
928 ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
929 ASSERT_STREQ(L"ny", p);
930
931 ASSERT_EQ(0, fn(L"muppet", &p));
932 ASSERT_STREQ(L"muppet", p);
933 ASSERT_EQ(0, fn(L" muppet", &p));
934 ASSERT_STREQ(L" muppet", p);
935
936 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
937 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
938 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
939
940 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
941 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
942 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
943
944 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
945 ASSERT_STREQ(L"initude", p);
946 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
947 ASSERT_STREQ(L"initude", p);
948 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
949 ASSERT_STREQ(L"initude", p);
950
951 // Check case-insensitivity.
952 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
953 ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700954}
955
956TEST(wchar, wcstof) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700957 TestWcsToFloat(wcstof);
958}
959
960TEST(wchar, wcstof_hex_floats) {
961 TestWcsToFloatHexFloats(wcstof);
962}
963
964TEST(wchar, wcstof_hex_inf_nan) {
965 TestWcsToFloatInfNan(wcstof);
966}
967
968TEST(wchar, wcstod) {
969 TestWcsToFloat(wcstod);
970}
971
972TEST(wchar, wcstod_hex_floats) {
973 TestWcsToFloatHexFloats(wcstod);
974}
975
976TEST(wchar, wcstod_hex_inf_nan) {
977 TestWcsToFloatInfNan(wcstod);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700978}
979
980TEST(wchar, wcstold) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700981 TestWcsToFloat(wcstold);
982}
983
984TEST(wchar, wcstold_hex_floats) {
985 TestWcsToFloatHexFloats(wcstold);
986}
987
988TEST(wchar, wcstold_hex_inf_nan) {
989 TestWcsToFloatInfNan(wcstold);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700990}
Elliott Hughesc41b5602017-07-27 17:08:08 -0700991
Elliott Hughes3376c232018-02-13 23:14:12 -0800992TEST(wchar, wcstod_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700993#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +0000994 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -0700995#else
996 GTEST_SKIP() << "musl doesn't have wcstod_l";
997#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800998}
999
1000TEST(wchar, wcstof_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001001#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001002 EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001003#else
1004 GTEST_SKIP() << "musl doesn't have wcstof_l";
1005#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001006}
1007
1008TEST(wchar, wcstol_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001009#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001010 EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001011#else
1012 GTEST_SKIP() << "musl doesn't have wcstol_l";
1013#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001014}
1015
1016TEST(wchar, wcstold_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001017 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001018}
1019
1020TEST(wchar, wcstoll_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001021 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001022}
1023
1024TEST(wchar, wcstoul_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001025#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001026 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001027#else
1028 GTEST_SKIP() << "musl doesn't have wcstoul_l";
1029#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001030}
1031
1032TEST(wchar, wcstoull_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001033 EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001034}
1035
Elliott Hughesc41b5602017-07-27 17:08:08 -07001036static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
1037 for (wchar_t i = begin; i < end; ++i) {
1038 EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
1039 }
1040}
1041
1042TEST(wchar, wcwidth_NUL) {
1043 // NUL is defined to return 0 rather than -1, despite being a C0 control.
1044 EXPECT_EQ(0, wcwidth(0));
1045}
1046
1047TEST(wchar, wcwidth_ascii) {
1048 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1049}
1050
1051TEST(wchar, wcwidth_controls) {
1052 AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
1053 EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
1054 AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
1055}
1056
1057TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001058 if (!have_dl()) return;
1059
Elliott Hughesc41b5602017-07-27 17:08:08 -07001060 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1061 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
1062 EXPECT_EQ(0, wcwidth(0x00ad)); // Soft hyphen (SHY).
1063 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1064}
1065
1066TEST(wchar, wcwidth_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001067 if (!have_dl()) return;
1068
Elliott Hughesc41b5602017-07-27 17:08:08 -07001069 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1070 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1071 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1072 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1073 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1074 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1075}
1076
1077TEST(wchar, wcwidth_korean_combining_jamo) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001078 if (!have_dl()) return;
1079
Elliott Hughesc41b5602017-07-27 17:08:08 -07001080 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1081 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1082 EXPECT_EQ(0, wcwidth(0xd7cb));
1083}
1084
1085TEST(wchar, wcwidth_korean_jeongeul_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001086 if (!have_dl()) return;
1087
Elliott Hughesc41b5602017-07-27 17:08:08 -07001088 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
1089 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points in Unicode 7.
1090 // Undefined characters at the end of the block have width 1.
1091}
1092
1093TEST(wchar, wcwidth_kana) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001094 if (!have_dl()) return;
1095
Elliott Hughesc41b5602017-07-27 17:08:08 -07001096 // Hiragana (most, not undefined).
1097 AssertWcwidthRange(0x3041, 0x3097, 2);
1098 // Katakana.
1099 AssertWcwidthRange(0x30a0, 0x3100, 2);
1100}
1101
1102TEST(wchar, wcwidth_circled_two_digit_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001103 if (!have_dl()) return;
1104
Elliott Hughesc41b5602017-07-27 17:08:08 -07001105 // Circled two-digit CJK "speed sign" numbers are wide,
1106 // though EastAsianWidth is ambiguous.
1107 AssertWcwidthRange(0x3248, 0x3250, 2);
1108}
1109
1110TEST(wchar, wcwidth_hexagrams) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001111 if (!have_dl()) return;
1112
Elliott Hughesc41b5602017-07-27 17:08:08 -07001113 // Hexagrams are wide, though EastAsianWidth is neutral.
1114 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1115}
1116
1117TEST(wchar, wcwidth_default_ignorables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001118 if (!have_dl()) return;
1119
Elliott Hughesc41b5602017-07-27 17:08:08 -07001120 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1121 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1122}
1123
1124TEST(wchar, wcwidth_korean_common_non_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001125 if (!have_dl()) return;
1126
Elliott Hughesc41b5602017-07-27 17:08:08 -07001127 EXPECT_EQ(2, wcwidth(L'ㅜ')); // Korean "crying" emoticon.
1128 EXPECT_EQ(2, wcwidth(L'ã…‹')); // Korean "laughing" emoticon.
1129}
Elliott Hughesfe50a0c2021-04-09 08:51:09 -07001130
1131TEST(wchar, wcswidth) {
1132 EXPECT_EQ(2, wcswidth(L"abc", 2));
1133 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1134 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1135}
1136
1137TEST(wchar, wcslcpy) {
1138#if defined(__BIONIC__)
1139 wchar_t dst[32];
1140 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1141 ASSERT_STREQ(L"he", dst);
1142 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1143 ASSERT_STREQ(L"hello world", dst);
1144#else
1145 GTEST_SKIP() << "no wcslcpy in glibc";
1146#endif
1147}
1148
1149TEST(wchar, wcscat) {
1150 wchar_t dst[32];
1151 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1152 ASSERT_STREQ(dst, L"hello");
1153 ASSERT_EQ(dst, wcscat(dst, L" world"));
1154 ASSERT_STREQ(dst, L"hello world");
1155}
1156
1157TEST(wchar, wcscpy) {
1158 wchar_t dst[32];
1159 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1160 ASSERT_STREQ(dst, L"hello");
1161 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1162 ASSERT_STREQ(dst, L"world");
1163}
1164
1165TEST(wchar, wcscasecmp) {
1166 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1167 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1168 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1169 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1170 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1171}
1172
1173TEST(wchar, wcscspn) {
1174 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1175 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1176 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1177}
1178
1179TEST(wchar, wcsspn) {
1180 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1181 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1182 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1183}
1184
1185TEST(wchar, wcsdup) {
1186 wchar_t* s = wcsdup(L"hello");
1187 ASSERT_STREQ(s, L"hello");
1188 free(s);
1189}
1190
1191TEST(wchar, wcslcat) {
1192#if defined(__BIONIC__)
1193 wchar_t dst[4] = {};
1194 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1195 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1196 ASSERT_STREQ(dst, L"abc");
1197#else
1198 GTEST_SKIP() << "no wcslcpy in glibc";
1199#endif
1200}
1201
1202TEST(wchar, wcsncasecmp) {
1203 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1204
1205 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1206 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1207 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1208 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1209 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1210}
1211
1212TEST(wchar, wcsncat) {
1213 wchar_t dst[32];
1214 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1215 ASSERT_STREQ(dst, L"hello");
1216 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1217 ASSERT_STREQ(dst, L"hello");
1218 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1219 ASSERT_STREQ(dst, L"hello, world!");
1220}
1221
1222TEST(wchar, wcsncmp) {
1223 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1224 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1225 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1226 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1227}
1228
1229TEST(wchar, wcsnlen) {
1230 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1231 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1232 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1233}
1234
1235TEST(wchar, wcspbrk) {
1236 const wchar_t* s = L"hello, world!";
1237 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1238 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1239 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1240 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1241}
1242
1243TEST(wchar, wcstok) {
1244 wchar_t s[] = L"this is\ta\nstring";
1245 wchar_t* p;
1246 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1247 ASSERT_STREQ(s, L"this");
1248 ASSERT_STREQ(p, L"is\ta\nstring");
1249 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1250 ASSERT_STREQ(s + 5, L"is");
1251 ASSERT_STREQ(p, L"a\nstring");
1252 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1253 ASSERT_STREQ(s + 8, L"a");
1254 ASSERT_STREQ(p, L"string");
1255 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1256 ASSERT_STREQ(s + 10, L"string");
1257 ASSERT_EQ(nullptr, p);
1258}
1259
1260TEST(wchar, wmemchr) {
1261 const wchar_t* s = L"hello, world!";
1262 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1263 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1264 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1265 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1266}
1267
1268TEST(wchar, wmemcmp) {
1269 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1270 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1271 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1272}
1273
1274TEST(wchar, wmemcpy) {
1275 wchar_t dst[32] = {};
1276 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1277 ASSERT_STREQ(dst, L"hello");
1278}
1279
1280TEST(wchar, wmemmove) {
1281 wchar_t dst[32] = {};
1282 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1283 ASSERT_STREQ(dst, L"hello");
1284}
1285
1286TEST(wchar, wmemset) {
1287 wchar_t dst[4] = {};
1288 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1289 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1290 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1291 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1292 ASSERT_EQ(dst[3], wchar_t(0));
1293 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1294 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1295}