blob: c9abeef1e79e778122205f21f343a5936768e9d0 [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 Hugheseb802902025-04-23 13:43:05 -070028#include <limits>
29
Elliott Hughes966d8a32017-08-29 11:29:28 -070030#include "utils.h"
Elliott Hughes7f0849f2016-08-26 16:17:17 -070031
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070032#define NUM_WCHARS(num_bytes) ((num_bytes)/sizeof(wchar_t))
Christopher Ferris5c7d9582014-11-13 15:48:39 -080033
Dan Albert686e67d2023-08-03 19:00:09 +000034#ifdef __GLIBC__
35// glibc immediately dereferences the locale passed to all wcsto*_l functions,
36// even if it won't be used, and even if it's LC_GLOBAL_LOCALE, which isn't a
37// pointer to valid memory.
38static locale_t SAFE_LC_GLOBAL_LOCALE = duplocale(LC_GLOBAL_LOCALE);
39#else
40static locale_t SAFE_LC_GLOBAL_LOCALE = LC_GLOBAL_LOCALE;
41#endif
42
Dan Albert53256532023-08-03 19:39:39 +000043// Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
44// newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
45// and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
46// https://datatracker.ietf.org/doc/html/rfc2279.
47//
48// Bionic's unicode implementation was written after the high values were
49// excluded, so it has never supported them. Other implementations (at least
50// as of glibc 2.36), do support those sequences.
51#if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
52constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
53#elif defined(__GLIBC__)
54constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
55#else
56#error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
57#endif
58
Dan Albert9f30c6b2023-08-03 19:50:28 +000059#if defined(__GLIBC__)
60constexpr bool kLibcSupportsParsingBinaryLiterals = __GLIBC_PREREQ(2, 38);
61#else
62constexpr bool kLibcSupportsParsingBinaryLiterals = true;
63#endif
64
Elliott Hughes77e944f2014-04-04 17:34:51 -070065TEST(wchar, sizeof_wchar_t) {
66 EXPECT_EQ(4U, sizeof(wchar_t));
Elliott Hugheseb802902025-04-23 13:43:05 -070067}
68
69TEST(wchar, sizeof_wint_t) {
Elliott Hughes77e944f2014-04-04 17:34:51 -070070 EXPECT_EQ(4U, sizeof(wint_t));
71}
72
Elliott Hugheseb802902025-04-23 13:43:05 -070073TEST(stdint, wchar_sign) {
74#if defined(__arm__) || defined(__aarch64__)
75 EXPECT_FALSE(std::numeric_limits<wchar_t>::is_signed);
76#else
77 EXPECT_TRUE(std::numeric_limits<wchar_t>::is_signed);
78#endif
79}
80
81#if !defined(__WINT_UNSIGNED__)
82#error wint_t is unsigned on Android
83#endif
84
85TEST(stdint, wint_sign) {
86 EXPECT_FALSE(std::numeric_limits<wint_t>::is_signed);
87}
88
Elliott Hughes77e944f2014-04-04 17:34:51 -070089TEST(wchar, mbrlen) {
90 char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
Dan Albert16007d52023-07-20 23:38:57 +000091 EXPECT_EQ(static_cast<size_t>(-2), mbrlen(&bytes[0], 0, nullptr));
Yi Kong32bc0fc2018-08-02 17:31:13 -070092 EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070093
Yi Kong32bc0fc2018-08-02 17:31:13 -070094 EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
95 EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070096}
97
98TEST(wchar, wctomb_wcrtomb) {
99 // wctomb and wcrtomb behave differently when s == NULL.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700100 EXPECT_EQ(0, wctomb(nullptr, L'h'));
101 EXPECT_EQ(0, wctomb(nullptr, L'\0'));
102 EXPECT_EQ(1U, wcrtomb(nullptr, L'\0', nullptr));
103 EXPECT_EQ(1U, wcrtomb(nullptr, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700104
105 char bytes[MB_LEN_MAX];
106
107 // wctomb and wcrtomb behave similarly for the null wide character.
108 EXPECT_EQ(1, wctomb(bytes, L'\0'));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700110
111 // ...and for regular characters.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700112 memset(bytes, 0, sizeof(bytes));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700113 EXPECT_EQ(1, wctomb(bytes, L'h'));
114 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700115 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700116 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -0700117 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700118
119 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
120 uselocale(LC_GLOBAL_LOCALE);
121
122 // 1-byte UTF-8.
123 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700124 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700125 EXPECT_EQ('h', bytes[0]);
126 // 2-byte UTF-8.
127 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700128 EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700129 EXPECT_EQ('\xc2', bytes[0]);
130 EXPECT_EQ('\xa2', bytes[1]);
131 // 3-byte UTF-8.
132 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700133 EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700134 EXPECT_EQ('\xe2', bytes[0]);
135 EXPECT_EQ('\x82', bytes[1]);
136 EXPECT_EQ('\xac', bytes[2]);
137 // 4-byte UTF-8.
138 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700139 EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700140 EXPECT_EQ('\xf0', bytes[0]);
141 EXPECT_EQ('\xa4', bytes[1]);
142 EXPECT_EQ('\xad', bytes[2]);
143 EXPECT_EQ('\xa2', bytes[3]);
144 // Invalid code point.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700145 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700146 EXPECT_ERRNO(EILSEQ);
Elliott Hughes77e944f2014-04-04 17:34:51 -0700147}
Elliott Hughes05493712014-04-17 17:30:03 -0700148
Calin Juravle15a63102014-05-08 14:38:35 +0100149TEST(wchar, wcrtomb_start_state) {
Dan Albert9f78c512023-08-03 19:49:09 +0000150 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
151 uselocale(LC_GLOBAL_LOCALE);
152
Calin Juravle15a63102014-05-08 14:38:35 +0100153 char out[MB_LEN_MAX];
Christopher Ferris2a391882024-12-19 13:44:35 -0800154 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100155
156 // Any non-initial state is invalid when calling wcrtomb.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700157 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100158 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700159 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100160
161 // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
162 // state should be reset.
Christopher Ferris2a391882024-12-19 13:44:35 -0800163 ps = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700164 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
165 EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100166 EXPECT_TRUE(mbsinit(&ps));
167
Christopher Ferris2a391882024-12-19 13:44:35 -0800168 ps = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700169 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100170 EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
171 EXPECT_TRUE(mbsinit(&ps));
172}
173
Elliott Hughes05493712014-04-17 17:30:03 -0700174TEST(wchar, wcstombs_wcrtombs) {
Dan Albert9f78c512023-08-03 19:49:09 +0000175 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
176 uselocale(LC_GLOBAL_LOCALE);
177
Elliott Hughes1b836ee2014-04-18 13:32:33 -0700178 const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
Elliott Hughesf83e6442014-05-01 17:14:59 -0700179 const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
Elliott Hughes05493712014-04-17 17:30:03 -0700180 const wchar_t* src;
181 char bytes[BUFSIZ];
182
183 // Given a NULL destination, these functions count valid characters.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700184 EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
185 EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
186 EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700187 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700188 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700189 EXPECT_EQ(&chars[0], src);
190 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700191 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700192 EXPECT_EQ(&chars[0], src);
193 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700195 EXPECT_EQ(&chars[0], src);
196
197 // An unrepresentable char just returns an error from wcstombs...
198 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700199 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700200 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700201 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700202 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
Elliott Hughes95646e62023-09-21 14:11:19 -0700203 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700204
205 // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
206 // to actually convert anything...
207 errno = 0;
208 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700209 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700210 EXPECT_EQ(&bad_chars[0], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700211 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700212 errno = 0;
213 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700214 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700215 EXPECT_EQ(&bad_chars[0], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700216 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700217
218 // Okay, now let's test actually converting something...
219 memset(bytes, 'x', sizeof(bytes));
220 EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
221 memset(bytes, 'x', sizeof(bytes));
222 EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
223 bytes[5] = 0;
224 EXPECT_STREQ("hellx", bytes);
225 memset(bytes, 'x', sizeof(bytes));
226 EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
227 EXPECT_STREQ("hello", bytes);
228 memset(bytes, 'x', sizeof(bytes));
229 EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
230 EXPECT_STREQ("hello", bytes);
231 errno = 0;
232 memset(bytes, 'x', sizeof(bytes));
233 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
Elliott Hughes95646e62023-09-21 14:11:19 -0700234 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700235 bytes[3] = 0;
236 EXPECT_STREQ("hix", bytes);
237
238 // wcsrtombs is a bit more informative...
239 memset(bytes, 'x', sizeof(bytes));
240 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700241 EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700242 EXPECT_EQ(&chars[0], src); // No input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700243 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700244
245 memset(bytes, 'x', sizeof(bytes));
246 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700247 EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700248 EXPECT_EQ(&chars[4], src); // Some input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700249 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700250 bytes[5] = 0;
251 EXPECT_STREQ("hellx", bytes);
252
253 memset(bytes, 'x', sizeof(bytes));
254 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700255 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
256 EXPECT_EQ(nullptr, src); // All input consumed!
Elliott Hughes95646e62023-09-21 14:11:19 -0700257 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700258 EXPECT_STREQ("hello", bytes);
259
260 memset(bytes, 'x', sizeof(bytes));
261 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700262 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
263 EXPECT_EQ(nullptr, src); // All input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700264 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700265 EXPECT_STREQ("hello", bytes);
266
267 memset(bytes, 'x', sizeof(bytes));
268 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700269 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700270 EXPECT_EQ(&bad_chars[2], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700271 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700272 bytes[3] = 0;
273 EXPECT_STREQ("hix", bytes);
Calin Juravle15a63102014-05-08 14:38:35 +0100274
275 // Any non-initial state is invalid when calling wcsrtombs.
Christopher Ferris2a391882024-12-19 13:44:35 -0800276 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100277 src = chars;
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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700280 EXPECT_ERRNO(EILSEQ);
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 Albert16007d52023-07-20 23:38:57 +0000314 // mbtowc and all the mbrto* APIs behave slightly differently when n is 0:
Dan Albert512469a2023-08-03 19:34:42 +0000315 //
Dan Albert16007d52023-07-20 23:38:57 +0000316 // mbrtowc returns 0 "if the next n or fewer bytes complete the multibyte
317 // character that corresponds to the null wide character"
Dan Albert512469a2023-08-03 19:34:42 +0000318 //
Dan Albertac243732023-10-25 20:31:48 +0000319 // mbrtoc (C23 7.24.7.2.4) says:
Dan Albert16007d52023-07-20 23:38:57 +0000320 //
Dan Albertac243732023-10-25 20:31:48 +0000321 // If s is not a null pointer, the mbtowc function either returns 0 (if s
322 // points to the null character), or returns the number of bytes that are
323 // contained in the converted multibyte character (if the next n or fewer
324 // bytes form a valid multibyte character), or returns -1 (if they do not
325 // form a valid multibyte character).
Dan Albert16007d52023-07-20 23:38:57 +0000326 //
Dan Albertac243732023-10-25 20:31:48 +0000327 // glibc's interpretation differs from all the BSDs (including macOS) and
328 // bionic (by way of openbsd). glibc returns 0 since s does point to the null
329 // character, whereas the BSDs return -1 because the next 0 bytes do not form
330 // a valid multibyte chatacter. glibc's interpretation is probably more
331 // correct from a strict interpretation of the spec, but considering the other
332 // APIs behave more like the BSD interpretation that may be a bug in the spec.
Dan Albert16007d52023-07-20 23:38:57 +0000333#ifdef __GLIBC__
334 int expected_result_for_zero_length_empty_string = 0;
Dan Albert512469a2023-08-03 19:34:42 +0000335#else
Dan Albert16007d52023-07-20 23:38:57 +0000336 int expected_result_for_zero_length_empty_string = -1;
Dan Albert512469a2023-08-03 19:34:42 +0000337#endif
338
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700339 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000340 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000341 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700342
Dan Albert16007d52023-07-20 23:38:57 +0000343 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
344 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(out, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000345 EXPECT_EQ(1, mbtowc(out, "hello", 1));
346 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700347
Dan Albert16007d52023-07-20 23:38:57 +0000348 EXPECT_EQ(-1, mbtowc(nullptr, "hello", 0));
349 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(nullptr, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000350 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700351
Dan Albert512469a2023-08-03 19:34:42 +0000352 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700353}
354
355TEST(wchar, mbrtowc) {
356 wchar_t out[8];
357
358 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000359 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000360 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700361
Dan Albert16007d52023-07-20 23:38:57 +0000362 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
363 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000364 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
365 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700366
Dan Albert16007d52023-07-20 23:38:57 +0000367 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "hello", 0, nullptr));
368 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000369 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700370
Dan Albert512469a2023-08-03 19:34:42 +0000371 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700372
Dan Albert512469a2023-08-03 19:34:42 +0000373 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700374 uselocale(LC_GLOBAL_LOCALE);
375
376 // 1-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000377 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
378 EXPECT_EQ(L'a', out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700379 // 2-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000380 EXPECT_EQ(2U, mbrtowc(out,
381 "\xc2\xa2"
382 "cdef",
383 6, nullptr));
384 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700385 // 3-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000386 EXPECT_EQ(3U, mbrtowc(out,
387 "\xe2\x82\xac"
388 "def",
389 6, nullptr));
390 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700391 // 4-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000392 EXPECT_EQ(4U, mbrtowc(out,
393 "\xf0\xa4\xad\xa2"
394 "ef",
395 6, nullptr));
396 EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
Elliott Hughes063525c2014-05-13 11:19:57 -0700397#if defined(__BIONIC__) // glibc allows this.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700398 // Illegal 5-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000399 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
400 "\xf8\xa1\xa2\xa3\xa4"
401 "f",
402 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700403 EXPECT_ERRNO(EILSEQ);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700404#endif
405 // Illegal over-long sequence.
Dan Albert512469a2023-08-03 19:34:42 +0000406 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
407 "\xf0\x82\x82\xac"
408 "ef",
409 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700410 EXPECT_ERRNO(EILSEQ);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700411}
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700412
Elliott Hughes402c7622018-07-06 17:18:05 -0700413TEST(wchar, mbrtowc_valid_non_characters) {
414 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
415 uselocale(LC_GLOBAL_LOCALE);
416
417 wchar_t out[8] = {};
418
419 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
420 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
421 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
422 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
423}
424
425TEST(wchar, mbrtowc_out_of_range) {
426 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
427 uselocale(LC_GLOBAL_LOCALE);
428
429 wchar_t out[8] = {};
430 errno = 0;
Dan Albert53256532023-08-03 19:39:39 +0000431 auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
432 if (kLibcRejectsOverLongUtf8Sequences) {
433 ASSERT_EQ(static_cast<size_t>(-1), result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700434 ASSERT_ERRNO(EILSEQ);
Dan Albert53256532023-08-03 19:39:39 +0000435 } else {
436 ASSERT_EQ(4U, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700437 ASSERT_ERRNO(0);
Dan Albert53256532023-08-03 19:39:39 +0000438 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700439}
440
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700441static void test_mbrtowc_incomplete(mbstate_t* ps) {
Calin Juravle15a63102014-05-08 14:38:35 +0100442 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
443 uselocale(LC_GLOBAL_LOCALE);
444
445 wchar_t out;
446 // 2-byte UTF-8.
447 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
448 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700449 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100450 ASSERT_TRUE(mbsinit(ps));
451 // 3-byte UTF-8.
452 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
453 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
454 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700455 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100456 ASSERT_TRUE(mbsinit(ps));
457 // 4-byte UTF-8.
458 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
459 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
460 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700461 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100462 ASSERT_TRUE(mbsinit(ps));
463
464 // Invalid 2-byte
465 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
466 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700467 ASSERT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100468}
469
470TEST(wchar, mbrtowc_incomplete) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800471 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100472
473 test_mbrtowc_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700474 test_mbrtowc_incomplete(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100475}
476
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700477static void test_mbsrtowcs(mbstate_t* ps) {
478 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
479 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
480 constexpr const char* INCOMPLETE = "A" "\xc2";
Calin Juravle15a63102014-05-08 14:38:35 +0100481 wchar_t out[4];
482
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700483 const char* valid = VALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100484 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
485 ASSERT_EQ(L'A', out[0]);
Ben Chengcaff5f22014-05-19 14:27:31 -0700486 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
487 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
488 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
Dan Albert6b55ba52014-07-20 11:51:26 -0700489 // Check that valid has advanced to the next unread character.
Calin Juravle15a63102014-05-08 14:38:35 +0100490 ASSERT_EQ('e', *valid);
491
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800492 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
Dan Albert6b55ba52014-07-20 11:51:26 -0700493 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
494 ASSERT_EQ(L'e', out[0]);
495 ASSERT_EQ(L'f', out[1]);
496 ASSERT_EQ(L'\0', out[2]);
497 // Check that we didn't clobber the rest of out.
498 ASSERT_EQ(L'x', out[3]);
499 // Check that valid has advanced to the end of the string.
Dan Albertb6cc8e02014-07-31 11:31:03 -0700500 ASSERT_EQ(nullptr, valid);
Dan Albert6b55ba52014-07-20 11:51:26 -0700501
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700502 const char* invalid = INVALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100503 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700504 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100505 ASSERT_EQ('\xc2', *invalid);
506
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700507 const char* incomplete = INCOMPLETE;
Calin Juravle15a63102014-05-08 14:38:35 +0100508 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700509 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100510 ASSERT_EQ('\xc2', *incomplete);
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700511
512 // If dst is null, *src shouldn't be updated.
513 // https://code.google.com/p/android/issues/detail?id=166381
514 const char* mbs = VALID;
515 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
516 EXPECT_EQ(VALID, mbs);
517 mbs = INVALID;
518 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
519 EXPECT_EQ(INVALID, mbs);
520 mbs = INCOMPLETE;
521 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
522 EXPECT_EQ(INCOMPLETE, mbs);
Calin Juravle15a63102014-05-08 14:38:35 +0100523}
524
525TEST(wchar, mbsrtowcs) {
526 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
527 uselocale(LC_GLOBAL_LOCALE);
528
Christopher Ferris2a391882024-12-19 13:44:35 -0800529 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100530 test_mbsrtowcs(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700531 test_mbsrtowcs(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100532
533 // Invalid multi byte continuation.
534 const char* invalid = "\x20";
535 wchar_t out;
536 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
537 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700538 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100539 ASSERT_EQ('\x20', *invalid);
540}
541
Dan Albert6805c2d2017-08-09 14:55:27 -0700542template <typename T>
543using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
544
545template <typename T>
546void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
547 T expected_value, ptrdiff_t expected_len) {
548 wchar_t* p;
Dan Alberta40159f2023-08-03 19:49:37 +0000549 EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
550 EXPECT_EQ(expected_len, p - str) << str << " " << base;
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700551}
552
Dan Albert6805c2d2017-08-09 14:55:27 -0700553template <typename T>
554void TestWcsToInt(WcsToIntFn<T> fn) {
555 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
556 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
557 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
558 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
559 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
560 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
561 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
562 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
Dan Albert9f30c6b2023-08-03 19:50:28 +0000563 if (kLibcSupportsParsingBinaryLiterals) {
564 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
565 }
Dan Albert6805c2d2017-08-09 14:55:27 -0700566}
567
568template <typename T>
569void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
570 const wchar_t* max_str) {
571 if (std::is_signed<T>::value) {
572 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
573 } else {
574 // If the subject sequence begins with a <hyphen-minus>, the value resulting
575 // from the conversion shall be negated.
Elliott Hughes5f3b8e02024-08-14 14:50:59 +0000576 // https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/strtoul.html
Dan Albert6805c2d2017-08-09 14:55:27 -0700577 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
578 }
579 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
580}
581
582TEST(wchar, wcstol) {
583 TestWcsToInt(wcstol);
584}
585
586TEST(wchar, wcstol_limits) {
587 if (sizeof(long) == 8) {
588 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
589 } else {
590 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
591 }
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700592}
593
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700594TEST(wchar, wcstoul) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700595 TestWcsToInt(wcstoul);
596}
597
598TEST(wchar, wcstoul_limits) {
599 if (sizeof(long) == 8) {
600 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
601 } else {
602 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
603 }
604}
605
606TEST(wchar, wcstoll) {
607 TestWcsToInt(wcstoll);
608}
609
610TEST(wchar, wcstoll_limits) {
611 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700612}
613
614TEST(wchar, wcstoull) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700615 TestWcsToInt(wcstoull);
616}
617
618TEST(wchar, wcstoull_limits) {
619 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
620}
621
622TEST(wchar, wcstoimax) {
623 TestWcsToInt(wcstoimax);
624}
625
626TEST(wchar, wcstoimax_limits) {
627 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
628 L"9223372036854775808");
629}
630
631TEST(wchar, wcstoumax) {
632 TestWcsToInt(wcstoumax);
633}
634
635TEST(wchar, wcstoumax_limits) {
636 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700637}
638
639TEST(wchar, mbsnrtowcs) {
640 wchar_t dst[128];
641 const char* s = "hello, world!";
642 const char* src;
643
644 memset(dst, 0, sizeof(dst));
645 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700646 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700647
648 memset(dst, 0, sizeof(dst));
649 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700650 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700651 ASSERT_EQ(L'h', dst[0]);
652 ASSERT_EQ(L'e', dst[1]);
653 ASSERT_EQ(&s[2], src);
654
655 memset(dst, 0, sizeof(dst));
656 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700657 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700658 ASSERT_EQ(L'h', dst[0]);
659 ASSERT_EQ(L'e', dst[1]);
660 ASSERT_EQ(L'l', dst[2]);
661 ASSERT_EQ(&s[3], src);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700662
663 memset(dst, 0, sizeof(dst));
664 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
665 src = incomplete;
666 errno = 0;
667 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700668 ASSERT_ERRNO(EILSEQ);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700669
670 src = incomplete;
671 errno = 0;
672 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700673 ASSERT_ERRNO(EILSEQ);
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700674}
Elliott Hughesefaa4612014-05-02 15:53:03 -0700675
Elliott Hughes3376c232018-02-13 23:14:12 -0800676TEST(wchar, wcsftime__wcsftime_l) {
Elliott Hughesefaa4612014-05-02 15:53:03 -0700677 setenv("TZ", "UTC", 1);
678
Christopher Ferris2a391882024-12-19 13:44:35 -0800679 struct tm t = {.tm_year = 200, .tm_mon = 2, .tm_mday = 10};
Elliott Hughesefaa4612014-05-02 15:53:03 -0700680 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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700819 ASSERT_ERRNO(EINVAL);
Elliott Hughes6b841db2014-08-20 16:10:49 -0700820
821 // Invalid size.
822 errno = 0;
823 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700824 ASSERT_ERRNO(EINVAL);
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 Hughes95646e62023-09-21 14:11:19 -0700834 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700835 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700836 wcstol(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700837 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700838 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700839 wcstol(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700840 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700841}
842
843TEST(wchar, wcstoll_EINVAL) {
844 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700845 wcstoll(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700846 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700847 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700848 wcstoll(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700849 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700850 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700851 wcstoll(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700852 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700853}
854
855TEST(wchar, wcstoul_EINVAL) {
856 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700857 wcstoul(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700858 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700859 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700860 wcstoul(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700861 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700862 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700863 wcstoul(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700864 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700865}
866
867TEST(wchar, wcstoull_EINVAL) {
868 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700869 wcstoull(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700870 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700871 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700872 wcstoull(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700873 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700874 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700875 wcstoull(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700876 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700877}
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 Hughes95646e62023-09-21 14:11:19 -0700882 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700883 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000884 wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700885 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700886 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000887 wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700888 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700889}
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 Hughes95646e62023-09-21 14:11:19 -0700894 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700895 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000896 wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700897 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700898 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000899 wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700900 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700901}
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) {
1086 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1087 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
Elliott Hughesc41b5602017-07-27 17:08:08 -07001088 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1089}
1090
Elliott Hughes20a9f992024-05-29 21:45:51 +00001091TEST(wchar, wcwidth_non_spacing_special_cases) {
Elliott Hughes20a9f992024-05-29 21:45:51 +00001092 // U+00AD is a soft hyphen, which normally shouldn't be rendered at all.
1093 // I think the assumption here is that you elide the soft hyphen character
1094 // completely in that case, and never call wcwidth() if you don't want to
1095 // render it as an actual hyphen. Whereas if you do want to render it,
1096 // you call wcwidth(), and 1 is the right answer. This is what Markus Kuhn's
1097 // original https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c did,
1098 // and glibc and iOS do the same.
1099 // See also: https://en.wikipedia.org/wiki/Soft_hyphen#Text_to_be_formatted_by_the_recipient
1100 EXPECT_EQ(1, wcwidth(0x00ad)); // Soft hyphen (SHY).
1101
1102 // U+115F is the Hangeul choseong filler (for a degenerate composed
1103 // character missing an initial consonant (as opposed to one with a
1104 // leading ieung). Since the code points for combining jungseong (medial
1105 // vowels) and jongseong (trailing consonants) have width 0, the choseong
1106 // (initial consonant) has width 2 to cover the entire syllable. So unless
1107 // U+115f has width 2, a degenerate composed "syllable" without an initial
1108 // consonant or ieung would have a total width of 0, which is silly.
1109 // The following sequence is effectively "약" without the leading ieung...
1110 EXPECT_EQ(2, wcwidth(0x115f)); // Hangeul choseong filler.
1111 EXPECT_EQ(0, wcwidth(0x1163)); // Hangeul jungseong "ya".
1112 EXPECT_EQ(0, wcwidth(0x11a8)); // Hangeul jongseong "kiyeok".
1113
1114 // U+1160, the jungseong filler, has width 0 because it must have been
1115 // preceded by either a choseong or choseong filler.
1116 EXPECT_EQ(0, wcwidth(0x1160));
1117}
1118
Elliott Hughesc41b5602017-07-27 17:08:08 -07001119TEST(wchar, wcwidth_cjk) {
1120 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1121 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1122 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1123 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1124 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1125 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1126}
1127
1128TEST(wchar, wcwidth_korean_combining_jamo) {
1129 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1130 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1131 EXPECT_EQ(0, wcwidth(0xd7cb));
1132}
1133
1134TEST(wchar, wcwidth_korean_jeongeul_syllables) {
1135 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
Elliott Hughes20a9f992024-05-29 21:45:51 +00001136 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points as of Unicode 15.
1137
1138 // Undefined characters at the end of the block currently have width 1,
1139 // but since they're undefined, we don't test that.
Elliott Hughesc41b5602017-07-27 17:08:08 -07001140}
1141
1142TEST(wchar, wcwidth_kana) {
1143 // Hiragana (most, not undefined).
1144 AssertWcwidthRange(0x3041, 0x3097, 2);
1145 // Katakana.
1146 AssertWcwidthRange(0x30a0, 0x3100, 2);
1147}
1148
1149TEST(wchar, wcwidth_circled_two_digit_cjk) {
1150 // Circled two-digit CJK "speed sign" numbers are wide,
1151 // though EastAsianWidth is ambiguous.
1152 AssertWcwidthRange(0x3248, 0x3250, 2);
1153}
1154
1155TEST(wchar, wcwidth_hexagrams) {
1156 // Hexagrams are wide, though EastAsianWidth is neutral.
1157 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1158}
1159
1160TEST(wchar, wcwidth_default_ignorables) {
1161 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1162 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1163}
1164
Elliott Hughes20a9f992024-05-29 21:45:51 +00001165TEST(wchar, wcwidth_hangeul_compatibility_jamo) {
Elliott Hughes20a9f992024-05-29 21:45:51 +00001166 // These are actually the *compatibility* jamo code points, *not* the regular
1167 // jamo code points (U+1100-U+11FF) using a jungseong filler. If you use the
1168 // Android IME to type any of these, you get these code points.
1169
1170 // (Half of) the Korean "crying" emoticon "ㅠㅠ".
1171 // Actually U+3160 "Hangeul Letter Yu" from Hangeul Compatibility Jamo.
1172 EXPECT_EQ(2, wcwidth(L'ㅠ'));
1173 // The two halves of the Korean internet shorthand "ㄱㅅ" (short for 감사).
1174 // Actually U+3131 "Hangeul Letter Kiyeok" and U+3145 "Hangeul Letter Sios"
1175 // from Hangeul Compatibility Jamo.
1176 EXPECT_EQ(2, wcwidth(L'ㄱ'));
1177 EXPECT_EQ(2, wcwidth(L'ㅅ'));
Elliott Hughesc41b5602017-07-27 17:08:08 -07001178}
Elliott Hughesfe50a0c2021-04-09 08:51:09 -07001179
1180TEST(wchar, wcswidth) {
1181 EXPECT_EQ(2, wcswidth(L"abc", 2));
1182 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1183 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1184}
1185
1186TEST(wchar, wcslcpy) {
1187#if defined(__BIONIC__)
1188 wchar_t dst[32];
1189 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1190 ASSERT_STREQ(L"he", dst);
1191 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1192 ASSERT_STREQ(L"hello world", dst);
1193#else
1194 GTEST_SKIP() << "no wcslcpy in glibc";
1195#endif
1196}
1197
1198TEST(wchar, wcscat) {
1199 wchar_t dst[32];
1200 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1201 ASSERT_STREQ(dst, L"hello");
1202 ASSERT_EQ(dst, wcscat(dst, L" world"));
1203 ASSERT_STREQ(dst, L"hello world");
1204}
1205
1206TEST(wchar, wcscpy) {
1207 wchar_t dst[32];
1208 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1209 ASSERT_STREQ(dst, L"hello");
1210 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1211 ASSERT_STREQ(dst, L"world");
1212}
1213
1214TEST(wchar, wcscasecmp) {
1215 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1216 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1217 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1218 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1219 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1220}
1221
1222TEST(wchar, wcscspn) {
1223 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1224 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1225 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1226}
1227
1228TEST(wchar, wcsspn) {
1229 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1230 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1231 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1232}
1233
1234TEST(wchar, wcsdup) {
1235 wchar_t* s = wcsdup(L"hello");
1236 ASSERT_STREQ(s, L"hello");
1237 free(s);
1238}
1239
1240TEST(wchar, wcslcat) {
1241#if defined(__BIONIC__)
1242 wchar_t dst[4] = {};
1243 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1244 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1245 ASSERT_STREQ(dst, L"abc");
1246#else
1247 GTEST_SKIP() << "no wcslcpy in glibc";
1248#endif
1249}
1250
1251TEST(wchar, wcsncasecmp) {
1252 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1253
1254 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1255 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1256 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1257 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1258 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1259}
1260
1261TEST(wchar, wcsncat) {
1262 wchar_t dst[32];
1263 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1264 ASSERT_STREQ(dst, L"hello");
1265 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1266 ASSERT_STREQ(dst, L"hello");
1267 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1268 ASSERT_STREQ(dst, L"hello, world!");
1269}
1270
1271TEST(wchar, wcsncmp) {
1272 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1273 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1274 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1275 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1276}
1277
1278TEST(wchar, wcsnlen) {
1279 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1280 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1281 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1282}
1283
1284TEST(wchar, wcspbrk) {
1285 const wchar_t* s = L"hello, world!";
1286 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1287 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1288 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1289 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1290}
1291
1292TEST(wchar, wcstok) {
1293 wchar_t s[] = L"this is\ta\nstring";
1294 wchar_t* p;
1295 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1296 ASSERT_STREQ(s, L"this");
1297 ASSERT_STREQ(p, L"is\ta\nstring");
1298 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1299 ASSERT_STREQ(s + 5, L"is");
1300 ASSERT_STREQ(p, L"a\nstring");
1301 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1302 ASSERT_STREQ(s + 8, L"a");
1303 ASSERT_STREQ(p, L"string");
1304 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1305 ASSERT_STREQ(s + 10, L"string");
1306 ASSERT_EQ(nullptr, p);
1307}
1308
1309TEST(wchar, wmemchr) {
1310 const wchar_t* s = L"hello, world!";
1311 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1312 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1313 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1314 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1315}
1316
1317TEST(wchar, wmemcmp) {
1318 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1319 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1320 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1321}
1322
1323TEST(wchar, wmemcpy) {
1324 wchar_t dst[32] = {};
1325 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1326 ASSERT_STREQ(dst, L"hello");
1327}
1328
1329TEST(wchar, wmemmove) {
1330 wchar_t dst[32] = {};
1331 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1332 ASSERT_STREQ(dst, L"hello");
1333}
1334
1335TEST(wchar, wmemset) {
1336 wchar_t dst[4] = {};
1337 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1338 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1339 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1340 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1341 ASSERT_EQ(dst[3], wchar_t(0));
1342 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1343 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1344}