blob: a811fd84d45659d916323420812d80492d389c7a [file] [log] [blame]
Elliott Hughes77e944f2014-04-04 17:34:51 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
Elliott Hughes05493712014-04-17 17:30:03 -070019#include <errno.h>
Dan Albert6805c2d2017-08-09 14:55:27 -070020#include <inttypes.h>
Elliott Hughes77e944f2014-04-04 17:34:51 -070021#include <limits.h>
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070022#include <locale.h>
Elliott Hughes966d8a32017-08-29 11:29:28 -070023#include <math.h>
Elliott Hughes3d7a0d92014-04-29 14:46:56 -070024#include <stdint.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070025#include <sys/cdefs.h>
Elliott Hughes77e944f2014-04-04 17:34:51 -070026#include <wchar.h>
27
Elliott Hughes966d8a32017-08-29 11:29:28 -070028#include "utils.h"
Elliott Hughes7f0849f2016-08-26 16:17:17 -070029
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070030#define NUM_WCHARS(num_bytes) ((num_bytes)/sizeof(wchar_t))
Christopher Ferris5c7d9582014-11-13 15:48:39 -080031
Dan Albert686e67d2023-08-03 19:00:09 +000032#ifdef __GLIBC__
33// glibc immediately dereferences the locale passed to all wcsto*_l functions,
34// even if it won't be used, and even if it's LC_GLOBAL_LOCALE, which isn't a
35// pointer to valid memory.
36static locale_t SAFE_LC_GLOBAL_LOCALE = duplocale(LC_GLOBAL_LOCALE);
37#else
38static locale_t SAFE_LC_GLOBAL_LOCALE = LC_GLOBAL_LOCALE;
39#endif
40
Dan Albert53256532023-08-03 19:39:39 +000041// Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
42// newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
43// and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
44// https://datatracker.ietf.org/doc/html/rfc2279.
45//
46// Bionic's unicode implementation was written after the high values were
47// excluded, so it has never supported them. Other implementations (at least
48// as of glibc 2.36), do support those sequences.
49#if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
50constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
51#elif defined(__GLIBC__)
52constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
53#else
54#error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
55#endif
56
Dan Albert9f30c6b2023-08-03 19:50:28 +000057#if defined(__GLIBC__)
58constexpr bool kLibcSupportsParsingBinaryLiterals = __GLIBC_PREREQ(2, 38);
59#else
60constexpr bool kLibcSupportsParsingBinaryLiterals = true;
61#endif
62
Elliott Hughes77e944f2014-04-04 17:34:51 -070063TEST(wchar, sizeof_wchar_t) {
64 EXPECT_EQ(4U, sizeof(wchar_t));
65 EXPECT_EQ(4U, sizeof(wint_t));
66}
67
68TEST(wchar, mbrlen) {
69 char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
Dan Albert16007d52023-07-20 23:38:57 +000070 EXPECT_EQ(static_cast<size_t>(-2), mbrlen(&bytes[0], 0, nullptr));
Yi Kong32bc0fc2018-08-02 17:31:13 -070071 EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070072
Yi Kong32bc0fc2018-08-02 17:31:13 -070073 EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
74 EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070075}
76
77TEST(wchar, wctomb_wcrtomb) {
78 // wctomb and wcrtomb behave differently when s == NULL.
Yi Kong32bc0fc2018-08-02 17:31:13 -070079 EXPECT_EQ(0, wctomb(nullptr, L'h'));
80 EXPECT_EQ(0, wctomb(nullptr, L'\0'));
81 EXPECT_EQ(1U, wcrtomb(nullptr, L'\0', nullptr));
82 EXPECT_EQ(1U, wcrtomb(nullptr, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070083
84 char bytes[MB_LEN_MAX];
85
86 // wctomb and wcrtomb behave similarly for the null wide character.
87 EXPECT_EQ(1, wctomb(bytes, L'\0'));
Yi Kong32bc0fc2018-08-02 17:31:13 -070088 EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070089
90 // ...and for regular characters.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070091 memset(bytes, 0, sizeof(bytes));
Elliott Hughes77e944f2014-04-04 17:34:51 -070092 EXPECT_EQ(1, wctomb(bytes, L'h'));
93 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070094 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes77e944f2014-04-04 17:34:51 -070096 EXPECT_EQ('h', bytes[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070097
98 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
99 uselocale(LC_GLOBAL_LOCALE);
100
101 // 1-byte UTF-8.
102 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700103 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700104 EXPECT_EQ('h', bytes[0]);
105 // 2-byte UTF-8.
106 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700107 EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700108 EXPECT_EQ('\xc2', bytes[0]);
109 EXPECT_EQ('\xa2', bytes[1]);
110 // 3-byte UTF-8.
111 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700112 EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700113 EXPECT_EQ('\xe2', bytes[0]);
114 EXPECT_EQ('\x82', bytes[1]);
115 EXPECT_EQ('\xac', bytes[2]);
116 // 4-byte UTF-8.
117 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700118 EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700119 EXPECT_EQ('\xf0', bytes[0]);
120 EXPECT_EQ('\xa4', bytes[1]);
121 EXPECT_EQ('\xad', bytes[2]);
122 EXPECT_EQ('\xa2', bytes[3]);
123 // Invalid code point.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700124 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700125 EXPECT_ERRNO(EILSEQ);
Elliott Hughes77e944f2014-04-04 17:34:51 -0700126}
Elliott Hughes05493712014-04-17 17:30:03 -0700127
Calin Juravle15a63102014-05-08 14:38:35 +0100128TEST(wchar, wcrtomb_start_state) {
Dan Albert9f78c512023-08-03 19:49:09 +0000129 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
130 uselocale(LC_GLOBAL_LOCALE);
131
Calin Juravle15a63102014-05-08 14:38:35 +0100132 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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700139 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100140
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) {
Dan Albert9f78c512023-08-03 19:49:09 +0000155 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
156 uselocale(LC_GLOBAL_LOCALE);
157
Elliott Hughes1b836ee2014-04-18 13:32:33 -0700158 const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
Elliott Hughesf83e6442014-05-01 17:14:59 -0700159 const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
Elliott Hughes05493712014-04-17 17:30:03 -0700160 const wchar_t* src;
161 char bytes[BUFSIZ];
162
163 // Given a NULL destination, these functions count valid characters.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700164 EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
165 EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
166 EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700167 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700168 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, 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, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700172 EXPECT_EQ(&chars[0], src);
173 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700174 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700175 EXPECT_EQ(&chars[0], src);
176
177 // An unrepresentable char just returns an error from wcstombs...
178 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700179 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700180 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700181 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700182 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
Elliott Hughes95646e62023-09-21 14:11:19 -0700183 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700184
185 // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
186 // to actually convert anything...
187 errno = 0;
188 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700189 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700190 EXPECT_EQ(&bad_chars[0], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700191 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700192 errno = 0;
193 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700195 EXPECT_EQ(&bad_chars[0], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700196 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700197
198 // Okay, now let's test actually converting something...
199 memset(bytes, 'x', sizeof(bytes));
200 EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
201 memset(bytes, 'x', sizeof(bytes));
202 EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
203 bytes[5] = 0;
204 EXPECT_STREQ("hellx", bytes);
205 memset(bytes, 'x', sizeof(bytes));
206 EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
207 EXPECT_STREQ("hello", bytes);
208 memset(bytes, 'x', sizeof(bytes));
209 EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
210 EXPECT_STREQ("hello", bytes);
211 errno = 0;
212 memset(bytes, 'x', sizeof(bytes));
213 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
Elliott Hughes95646e62023-09-21 14:11:19 -0700214 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700215 bytes[3] = 0;
216 EXPECT_STREQ("hix", bytes);
217
218 // wcsrtombs is a bit more informative...
219 memset(bytes, 'x', sizeof(bytes));
220 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700221 EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700222 EXPECT_EQ(&chars[0], src); // No input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700223 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700224
225 memset(bytes, 'x', sizeof(bytes));
226 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700227 EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700228 EXPECT_EQ(&chars[4], src); // Some input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700229 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700230 bytes[5] = 0;
231 EXPECT_STREQ("hellx", bytes);
232
233 memset(bytes, 'x', sizeof(bytes));
234 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700235 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
236 EXPECT_EQ(nullptr, src); // All input consumed!
Elliott Hughes95646e62023-09-21 14:11:19 -0700237 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700238 EXPECT_STREQ("hello", bytes);
239
240 memset(bytes, 'x', sizeof(bytes));
241 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700242 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
243 EXPECT_EQ(nullptr, src); // All input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700244 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700245 EXPECT_STREQ("hello", bytes);
246
247 memset(bytes, 'x', sizeof(bytes));
248 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700249 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700250 EXPECT_EQ(&bad_chars[2], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700251 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700252 bytes[3] = 0;
253 EXPECT_STREQ("hix", bytes);
Calin Juravle15a63102014-05-08 14:38:35 +0100254
255 // Any non-initial state is invalid when calling wcsrtombs.
256 mbstate_t ps;
257 src = chars;
258 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700259 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
260 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700261 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700262}
Elliott Hughes83c07b52014-04-21 18:09:46 -0700263
264TEST(wchar, limits) {
265 ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
266}
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700267
Dan Albert001f8f02014-06-04 09:53:06 -0700268TEST(wchar, wcsstr) {
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700269 const wchar_t* haystack = L"big daddy/giant haystacks!";
270 const wchar_t* empty_haystack = L"";
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700271
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700272 // The empty needle is a special case.
273 ASSERT_EQ(haystack, wcsstr(haystack, L""));
274 ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
275
276 ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
277 ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
278 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
279 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
280 ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
281 ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
282
283 ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
284 ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700285}
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700286
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800287TEST(wchar, wcsstr_80199) {
288 // https://code.google.com/p/android/issues/detail?id=80199
Yi Kong32bc0fc2018-08-02 17:31:13 -0700289 ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800290}
291
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700292TEST(wchar, mbtowc) {
293 wchar_t out[8];
294
Dan Albert16007d52023-07-20 23:38:57 +0000295 // mbtowc and all the mbrto* APIs behave slightly differently when n is 0:
Dan Albert512469a2023-08-03 19:34:42 +0000296 //
Dan Albert16007d52023-07-20 23:38:57 +0000297 // mbrtowc returns 0 "if the next n or fewer bytes complete the multibyte
298 // character that corresponds to the null wide character"
Dan Albert512469a2023-08-03 19:34:42 +0000299 //
Dan Albertac243732023-10-25 20:31:48 +0000300 // mbrtoc (C23 7.24.7.2.4) says:
Dan Albert16007d52023-07-20 23:38:57 +0000301 //
Dan Albertac243732023-10-25 20:31:48 +0000302 // If s is not a null pointer, the mbtowc function either returns 0 (if s
303 // points to the null character), or returns the number of bytes that are
304 // contained in the converted multibyte character (if the next n or fewer
305 // bytes form a valid multibyte character), or returns -1 (if they do not
306 // form a valid multibyte character).
Dan Albert16007d52023-07-20 23:38:57 +0000307 //
Dan Albertac243732023-10-25 20:31:48 +0000308 // glibc's interpretation differs from all the BSDs (including macOS) and
309 // bionic (by way of openbsd). glibc returns 0 since s does point to the null
310 // character, whereas the BSDs return -1 because the next 0 bytes do not form
311 // a valid multibyte chatacter. glibc's interpretation is probably more
312 // correct from a strict interpretation of the spec, but considering the other
313 // APIs behave more like the BSD interpretation that may be a bug in the spec.
Dan Albert16007d52023-07-20 23:38:57 +0000314#ifdef __GLIBC__
315 int expected_result_for_zero_length_empty_string = 0;
Dan Albert512469a2023-08-03 19:34:42 +0000316#else
Dan Albert16007d52023-07-20 23:38:57 +0000317 int expected_result_for_zero_length_empty_string = -1;
Dan Albert512469a2023-08-03 19:34:42 +0000318#endif
319
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700320 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000321 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000322 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700323
Dan Albert16007d52023-07-20 23:38:57 +0000324 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
325 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(out, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000326 EXPECT_EQ(1, mbtowc(out, "hello", 1));
327 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700328
Dan Albert16007d52023-07-20 23:38:57 +0000329 EXPECT_EQ(-1, mbtowc(nullptr, "hello", 0));
330 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(nullptr, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000331 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700332
Dan Albert512469a2023-08-03 19:34:42 +0000333 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700334}
335
336TEST(wchar, mbrtowc) {
337 wchar_t out[8];
338
339 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000340 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
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(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
344 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000345 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
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(static_cast<size_t>(-2), mbrtowc(nullptr, "hello", 0, nullptr));
349 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000350 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700351
Dan Albert512469a2023-08-03 19:34:42 +0000352 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700353
Dan Albert512469a2023-08-03 19:34:42 +0000354 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700355 uselocale(LC_GLOBAL_LOCALE);
356
357 // 1-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000358 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
359 EXPECT_EQ(L'a', out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700360 // 2-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000361 EXPECT_EQ(2U, mbrtowc(out,
362 "\xc2\xa2"
363 "cdef",
364 6, nullptr));
365 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700366 // 3-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000367 EXPECT_EQ(3U, mbrtowc(out,
368 "\xe2\x82\xac"
369 "def",
370 6, nullptr));
371 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700372 // 4-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000373 EXPECT_EQ(4U, mbrtowc(out,
374 "\xf0\xa4\xad\xa2"
375 "ef",
376 6, nullptr));
377 EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
Elliott Hughes063525c2014-05-13 11:19:57 -0700378#if defined(__BIONIC__) // glibc allows this.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700379 // Illegal 5-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000380 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
381 "\xf8\xa1\xa2\xa3\xa4"
382 "f",
383 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700384 EXPECT_ERRNO(EILSEQ);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700385#endif
386 // Illegal over-long sequence.
Dan Albert512469a2023-08-03 19:34:42 +0000387 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
388 "\xf0\x82\x82\xac"
389 "ef",
390 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700391 EXPECT_ERRNO(EILSEQ);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700392}
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700393
Elliott Hughes402c7622018-07-06 17:18:05 -0700394TEST(wchar, mbrtowc_valid_non_characters) {
395 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
396 uselocale(LC_GLOBAL_LOCALE);
397
398 wchar_t out[8] = {};
399
400 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
401 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
402 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
403 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
404}
405
406TEST(wchar, mbrtowc_out_of_range) {
407 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
408 uselocale(LC_GLOBAL_LOCALE);
409
410 wchar_t out[8] = {};
411 errno = 0;
Dan Albert53256532023-08-03 19:39:39 +0000412 auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
413 if (kLibcRejectsOverLongUtf8Sequences) {
414 ASSERT_EQ(static_cast<size_t>(-1), result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700415 ASSERT_ERRNO(EILSEQ);
Dan Albert53256532023-08-03 19:39:39 +0000416 } else {
417 ASSERT_EQ(4U, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700418 ASSERT_ERRNO(0);
Dan Albert53256532023-08-03 19:39:39 +0000419 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700420}
421
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700422static void test_mbrtowc_incomplete(mbstate_t* ps) {
Calin Juravle15a63102014-05-08 14:38:35 +0100423 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
424 uselocale(LC_GLOBAL_LOCALE);
425
426 wchar_t out;
427 // 2-byte UTF-8.
428 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
429 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700430 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100431 ASSERT_TRUE(mbsinit(ps));
432 // 3-byte UTF-8.
433 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
434 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
435 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700436 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100437 ASSERT_TRUE(mbsinit(ps));
438 // 4-byte UTF-8.
439 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
440 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
441 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700442 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100443 ASSERT_TRUE(mbsinit(ps));
444
445 // Invalid 2-byte
446 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
447 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700448 ASSERT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100449}
450
451TEST(wchar, mbrtowc_incomplete) {
452 mbstate_t ps;
453 memset(&ps, 0, sizeof(ps));
454
455 test_mbrtowc_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700456 test_mbrtowc_incomplete(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100457}
458
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700459static void test_mbsrtowcs(mbstate_t* ps) {
460 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
461 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
462 constexpr const char* INCOMPLETE = "A" "\xc2";
Calin Juravle15a63102014-05-08 14:38:35 +0100463 wchar_t out[4];
464
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700465 const char* valid = VALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100466 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
467 ASSERT_EQ(L'A', out[0]);
Ben Chengcaff5f22014-05-19 14:27:31 -0700468 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
469 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
470 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
Dan Albert6b55ba52014-07-20 11:51:26 -0700471 // Check that valid has advanced to the next unread character.
Calin Juravle15a63102014-05-08 14:38:35 +0100472 ASSERT_EQ('e', *valid);
473
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800474 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
Dan Albert6b55ba52014-07-20 11:51:26 -0700475 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
476 ASSERT_EQ(L'e', out[0]);
477 ASSERT_EQ(L'f', out[1]);
478 ASSERT_EQ(L'\0', out[2]);
479 // Check that we didn't clobber the rest of out.
480 ASSERT_EQ(L'x', out[3]);
481 // Check that valid has advanced to the end of the string.
Dan Albertb6cc8e02014-07-31 11:31:03 -0700482 ASSERT_EQ(nullptr, valid);
Dan Albert6b55ba52014-07-20 11:51:26 -0700483
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700484 const char* invalid = INVALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100485 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700486 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100487 ASSERT_EQ('\xc2', *invalid);
488
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700489 const char* incomplete = INCOMPLETE;
Calin Juravle15a63102014-05-08 14:38:35 +0100490 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700491 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100492 ASSERT_EQ('\xc2', *incomplete);
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700493
494 // If dst is null, *src shouldn't be updated.
495 // https://code.google.com/p/android/issues/detail?id=166381
496 const char* mbs = VALID;
497 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
498 EXPECT_EQ(VALID, mbs);
499 mbs = INVALID;
500 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
501 EXPECT_EQ(INVALID, mbs);
502 mbs = INCOMPLETE;
503 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
504 EXPECT_EQ(INCOMPLETE, mbs);
Calin Juravle15a63102014-05-08 14:38:35 +0100505}
506
507TEST(wchar, mbsrtowcs) {
508 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
509 uselocale(LC_GLOBAL_LOCALE);
510
511 mbstate_t ps;
512 memset(&ps, 0, sizeof(ps));
513 test_mbsrtowcs(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700514 test_mbsrtowcs(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100515
516 // Invalid multi byte continuation.
517 const char* invalid = "\x20";
518 wchar_t out;
519 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
520 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700521 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100522 ASSERT_EQ('\x20', *invalid);
523}
524
Dan Albert6805c2d2017-08-09 14:55:27 -0700525template <typename T>
526using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
527
528template <typename T>
529void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
530 T expected_value, ptrdiff_t expected_len) {
531 wchar_t* p;
Dan Alberta40159f2023-08-03 19:49:37 +0000532 EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
533 EXPECT_EQ(expected_len, p - str) << str << " " << base;
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700534}
535
Dan Albert6805c2d2017-08-09 14:55:27 -0700536template <typename T>
537void TestWcsToInt(WcsToIntFn<T> fn) {
538 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
539 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
540 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
541 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
542 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
543 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
544 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
545 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
Dan Albert9f30c6b2023-08-03 19:50:28 +0000546 if (kLibcSupportsParsingBinaryLiterals) {
547 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
548 }
Dan Albert6805c2d2017-08-09 14:55:27 -0700549}
550
551template <typename T>
552void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
553 const wchar_t* max_str) {
554 if (std::is_signed<T>::value) {
555 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
556 } else {
557 // If the subject sequence begins with a <hyphen-minus>, the value resulting
558 // from the conversion shall be negated.
Elliott Hughes5f3b8e02024-08-14 14:50:59 +0000559 // https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/strtoul.html
Dan Albert6805c2d2017-08-09 14:55:27 -0700560 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
561 }
562 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
563}
564
565TEST(wchar, wcstol) {
566 TestWcsToInt(wcstol);
567}
568
569TEST(wchar, wcstol_limits) {
570 if (sizeof(long) == 8) {
571 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
572 } else {
573 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
574 }
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700575}
576
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700577TEST(wchar, wcstoul) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700578 TestWcsToInt(wcstoul);
579}
580
581TEST(wchar, wcstoul_limits) {
582 if (sizeof(long) == 8) {
583 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
584 } else {
585 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
586 }
587}
588
589TEST(wchar, wcstoll) {
590 TestWcsToInt(wcstoll);
591}
592
593TEST(wchar, wcstoll_limits) {
594 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700595}
596
597TEST(wchar, wcstoull) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700598 TestWcsToInt(wcstoull);
599}
600
601TEST(wchar, wcstoull_limits) {
602 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
603}
604
605TEST(wchar, wcstoimax) {
606 TestWcsToInt(wcstoimax);
607}
608
609TEST(wchar, wcstoimax_limits) {
610 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
611 L"9223372036854775808");
612}
613
614TEST(wchar, wcstoumax) {
615 TestWcsToInt(wcstoumax);
616}
617
618TEST(wchar, wcstoumax_limits) {
619 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700620}
621
622TEST(wchar, mbsnrtowcs) {
623 wchar_t dst[128];
624 const char* s = "hello, world!";
625 const char* src;
626
627 memset(dst, 0, sizeof(dst));
628 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700629 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700630
631 memset(dst, 0, sizeof(dst));
632 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700633 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700634 ASSERT_EQ(L'h', dst[0]);
635 ASSERT_EQ(L'e', dst[1]);
636 ASSERT_EQ(&s[2], src);
637
638 memset(dst, 0, sizeof(dst));
639 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700640 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700641 ASSERT_EQ(L'h', dst[0]);
642 ASSERT_EQ(L'e', dst[1]);
643 ASSERT_EQ(L'l', dst[2]);
644 ASSERT_EQ(&s[3], src);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700645
646 memset(dst, 0, sizeof(dst));
647 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
648 src = incomplete;
649 errno = 0;
650 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700651 ASSERT_ERRNO(EILSEQ);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700652
653 src = incomplete;
654 errno = 0;
655 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700656 ASSERT_ERRNO(EILSEQ);
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700657}
Elliott Hughesefaa4612014-05-02 15:53:03 -0700658
Elliott Hughes3376c232018-02-13 23:14:12 -0800659TEST(wchar, wcsftime__wcsftime_l) {
Elliott Hughesefaa4612014-05-02 15:53:03 -0700660 setenv("TZ", "UTC", 1);
661
662 struct tm t;
663 memset(&t, 0, sizeof(tm));
664 t.tm_year = 200;
665 t.tm_mon = 2;
666 t.tm_mday = 10;
667
668 wchar_t buf[64];
669
670 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
671 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Dan Albert686e67d2023-08-03 19:00:09 +0000672 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -0800673 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Elliott Hughesefaa4612014-05-02 15:53:03 -0700674}
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200675
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800676TEST(wchar, wmemmove_smoke) {
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200677 const wchar_t const_wstr[] = L"This is a test of something or other.....";
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800678 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200679
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800680 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200681 EXPECT_STREQ(const_wstr, wstr);
682
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800683 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
Christopher Ferris8bf50d52014-05-29 15:42:42 -0700684 EXPECT_STREQ(L"This This is a test of something or other", wstr);
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200685}
Elliott Hughes69f05d22014-06-05 20:10:09 -0700686
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800687TEST(wchar, wmemcpy_smoke) {
688 const wchar_t src[] = L"Source string";
689 wchar_t dst[NUM_WCHARS(sizeof(src))];
690
691 EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
692 EXPECT_STREQ(dst, src);
693}
694
695TEST(wchar, wcpcpy_smoke) {
696 const wchar_t src[] = L"Source string";
697 wchar_t dst[NUM_WCHARS(sizeof(src))];
698
699 EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
700 EXPECT_STREQ(dst, src);
701}
702
703TEST(wchar, wcpncpy_smoke) {
704 const wchar_t src[] = L"Source string";
705 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
706
707 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
708 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
709 EXPECT_STREQ(dst, src);
710
711 EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
712 dst[6] = L'\0';
713 EXPECT_STREQ(dst, L"Source");
714
715 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
716 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
717 EXPECT_STREQ(dst, src);
718 EXPECT_EQ(dst[src_len], L'\0');
719 EXPECT_EQ(dst[src_len+1], L'\0');
720 EXPECT_EQ(dst[src_len+2], L'\0');
721 EXPECT_EQ(dst[src_len+3], L'\0');
722 EXPECT_EQ(dst[src_len+4], L'x');
723}
724
725TEST(wchar, wcscpy_smoke) {
726 const wchar_t src[] = L"Source string";
727 wchar_t dst[NUM_WCHARS(sizeof(src))];
728
729 EXPECT_EQ(dst, wcscpy(dst, src));
730 EXPECT_STREQ(src, dst);
731}
732
733TEST(wchar, wcsncpy_smoke) {
734 const wchar_t src[] = L"Source string";
735 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
736
737 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
738 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
739 EXPECT_STREQ(dst, src);
740
741 EXPECT_EQ(dst, wcsncpy(dst, src, 6));
742 dst[6] = L'\0';
743 EXPECT_STREQ(dst, L"Source");
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700744 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
745 EXPECT_STREQ(dst, L"Source");
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800746
747 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
748 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
749 EXPECT_STREQ(dst, src);
750 EXPECT_EQ(dst[src_len], L'\0');
751 EXPECT_EQ(dst[src_len+1], L'\0');
752 EXPECT_EQ(dst[src_len+2], L'\0');
753 EXPECT_EQ(dst[src_len+3], L'\0');
754 EXPECT_EQ(dst[src_len+4], L'x');
755}
756
Elliott Hughes69f05d22014-06-05 20:10:09 -0700757TEST(wchar, mbrtowc_15439554) {
758 // http://b/15439554
759 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
760 uselocale(LC_GLOBAL_LOCALE);
761
762 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
763 ASSERT_GE(MB_CUR_MAX, 4U);
764
765 wchar_t wc;
766 size_t n;
767
768 // 1-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700769 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700770 EXPECT_EQ(1U, n);
771 EXPECT_EQ(L'x', wc);
772 // 2-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700773 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700774 EXPECT_EQ(2U, n);
775 EXPECT_EQ(L'¢', wc);
776 // 3-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700777 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700778 EXPECT_EQ(3U, n);
779 EXPECT_EQ(L'€', wc);
780 // 4-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700781 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700782 EXPECT_EQ(4U, n);
783 EXPECT_EQ(L'𤭢', wc);
784}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700785
786TEST(wchar, open_wmemstream) {
787 wchar_t* p = nullptr;
788 size_t size = 0;
789 FILE* fp = open_wmemstream(&p, &size);
790 ASSERT_NE(EOF, fputws(L"hello, world!", fp));
791 fclose(fp);
792
793 ASSERT_STREQ(L"hello, world!", p);
794 ASSERT_EQ(wcslen(L"hello, world!"), size);
795 free(p);
796}
797
798TEST(stdio, open_wmemstream_EINVAL) {
799#if defined(__BIONIC__)
800 wchar_t* p;
801 size_t size;
zijunzhao7ce2f952023-04-03 23:13:57 +0000802#pragma clang diagnostic push
803#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes6b841db2014-08-20 16:10:49 -0700804 // Invalid buffer.
805 errno = 0;
806 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
Elliott Hughes95646e62023-09-21 14:11:19 -0700807 ASSERT_ERRNO(EINVAL);
Elliott Hughes6b841db2014-08-20 16:10:49 -0700808
809 // Invalid size.
810 errno = 0;
811 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700812 ASSERT_ERRNO(EINVAL);
zijunzhao7ce2f952023-04-03 23:13:57 +0000813#pragma clang diagnostic pop
Elliott Hughes6b841db2014-08-20 16:10:49 -0700814#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800815 GTEST_SKIP() << "This test is bionic-specific";
Elliott Hughes6b841db2014-08-20 16:10:49 -0700816#endif
817}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700818
819TEST(wchar, wcstol_EINVAL) {
820 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700821 wcstol(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700822 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700823 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700824 wcstol(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700825 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700826 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700827 wcstol(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700828 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700829}
830
831TEST(wchar, wcstoll_EINVAL) {
832 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700833 wcstoll(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 wcstoll(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 wcstoll(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, wcstoul_EINVAL) {
844 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700845 wcstoul(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 wcstoul(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 wcstoul(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, wcstoull_EINVAL) {
856 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700857 wcstoull(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 wcstoull(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 wcstoull(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, wcstoll_l_EINVAL) {
868 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000869 wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700870 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700871 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000872 wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700873 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700874 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000875 wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700876 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700877}
878
879TEST(wchar, wcstoull_l_EINVAL) {
880 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000881 wcstoull_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 wcstoull_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 wcstoull_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}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800890
891TEST(wchar, wmempcpy) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700892#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800893 wchar_t dst[6];
894 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
Colin Cross7da20342021-07-28 11:18:11 -0700895#else
896 GTEST_SKIP() << "musl doesn't have wmempcpy";
897#endif
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800898}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700899
900template <typename T>
Dan Albert6805c2d2017-08-09 14:55:27 -0700901using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700902
Dan Albert6805c2d2017-08-09 14:55:27 -0700903template <typename T>
904void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
905 T expected_value, ptrdiff_t expected_len) {
Dan Albertf6346552016-12-02 12:02:03 -0800906 wchar_t* p;
Dan Albert6805c2d2017-08-09 14:55:27 -0700907 ASSERT_EQ(expected_value, fn(str, &p));
908 ASSERT_EQ(expected_len, p - str);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700909}
910
Dan Albert6805c2d2017-08-09 14:55:27 -0700911template <typename T>
912void TestWcsToFloat(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800913 TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
914 TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
915 TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
916 TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
917 TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
918 TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
Dan Albert6805c2d2017-08-09 14:55:27 -0700919}
920
921template <typename T>
922void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800923 TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
924 TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
925 TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
926 TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
Dan Albert6805c2d2017-08-09 14:55:27 -0700927}
928
929template <typename T>
930void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
931 ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
932 ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
933 ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
934
935 ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
936 ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
937 ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
938
939 wchar_t* p;
940 ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
941 ASSERT_STREQ(L"ny", p);
942 ASSERT_TRUE(isnan(fn(L"nanny", &p)));
943 ASSERT_STREQ(L"ny", p);
944 ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
945 ASSERT_STREQ(L"ny", p);
946
947 ASSERT_EQ(0, fn(L"muppet", &p));
948 ASSERT_STREQ(L"muppet", p);
949 ASSERT_EQ(0, fn(L" muppet", &p));
950 ASSERT_STREQ(L" muppet", p);
951
952 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
953 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
954 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
955
956 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
957 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
958 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
959
960 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
961 ASSERT_STREQ(L"initude", p);
962 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
963 ASSERT_STREQ(L"initude", p);
964 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
965 ASSERT_STREQ(L"initude", p);
966
967 // Check case-insensitivity.
968 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
969 ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700970}
971
972TEST(wchar, wcstof) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700973 TestWcsToFloat(wcstof);
974}
975
976TEST(wchar, wcstof_hex_floats) {
977 TestWcsToFloatHexFloats(wcstof);
978}
979
980TEST(wchar, wcstof_hex_inf_nan) {
981 TestWcsToFloatInfNan(wcstof);
982}
983
984TEST(wchar, wcstod) {
985 TestWcsToFloat(wcstod);
986}
987
988TEST(wchar, wcstod_hex_floats) {
989 TestWcsToFloatHexFloats(wcstod);
990}
991
992TEST(wchar, wcstod_hex_inf_nan) {
993 TestWcsToFloatInfNan(wcstod);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700994}
995
996TEST(wchar, wcstold) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700997 TestWcsToFloat(wcstold);
998}
999
1000TEST(wchar, wcstold_hex_floats) {
1001 TestWcsToFloatHexFloats(wcstold);
1002}
1003
1004TEST(wchar, wcstold_hex_inf_nan) {
1005 TestWcsToFloatInfNan(wcstold);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001006}
Elliott Hughesc41b5602017-07-27 17:08:08 -07001007
Elliott Hughes3376c232018-02-13 23:14:12 -08001008TEST(wchar, wcstod_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001009#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001010 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001011#else
1012 GTEST_SKIP() << "musl doesn't have wcstod_l";
1013#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001014}
1015
1016TEST(wchar, wcstof_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001017#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001018 EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001019#else
1020 GTEST_SKIP() << "musl doesn't have wcstof_l";
1021#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001022}
1023
1024TEST(wchar, wcstol_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001025#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001026 EXPECT_EQ(123L, wcstol_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 wcstol_l";
1029#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001030}
1031
1032TEST(wchar, wcstold_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001033 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001034}
1035
1036TEST(wchar, wcstoll_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001037 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001038}
1039
1040TEST(wchar, wcstoul_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001041#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001042 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001043#else
1044 GTEST_SKIP() << "musl doesn't have wcstoul_l";
1045#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001046}
1047
1048TEST(wchar, wcstoull_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001049 EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001050}
1051
Elliott Hughesc41b5602017-07-27 17:08:08 -07001052static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
1053 for (wchar_t i = begin; i < end; ++i) {
1054 EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
1055 }
1056}
1057
1058TEST(wchar, wcwidth_NUL) {
1059 // NUL is defined to return 0 rather than -1, despite being a C0 control.
1060 EXPECT_EQ(0, wcwidth(0));
1061}
1062
1063TEST(wchar, wcwidth_ascii) {
1064 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1065}
1066
1067TEST(wchar, wcwidth_controls) {
1068 AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
1069 EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
1070 AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
1071}
1072
1073TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001074 if (!have_dl()) return;
1075
Elliott Hughesc41b5602017-07-27 17:08:08 -07001076 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1077 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
Elliott Hughesc41b5602017-07-27 17:08:08 -07001078 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1079}
1080
Elliott Hughes20a9f992024-05-29 21:45:51 +00001081TEST(wchar, wcwidth_non_spacing_special_cases) {
1082 if (!have_dl()) return;
1083
1084 // U+00AD is a soft hyphen, which normally shouldn't be rendered at all.
1085 // I think the assumption here is that you elide the soft hyphen character
1086 // completely in that case, and never call wcwidth() if you don't want to
1087 // render it as an actual hyphen. Whereas if you do want to render it,
1088 // you call wcwidth(), and 1 is the right answer. This is what Markus Kuhn's
1089 // original https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c did,
1090 // and glibc and iOS do the same.
1091 // See also: https://en.wikipedia.org/wiki/Soft_hyphen#Text_to_be_formatted_by_the_recipient
1092 EXPECT_EQ(1, wcwidth(0x00ad)); // Soft hyphen (SHY).
1093
1094 // U+115F is the Hangeul choseong filler (for a degenerate composed
1095 // character missing an initial consonant (as opposed to one with a
1096 // leading ieung). Since the code points for combining jungseong (medial
1097 // vowels) and jongseong (trailing consonants) have width 0, the choseong
1098 // (initial consonant) has width 2 to cover the entire syllable. So unless
1099 // U+115f has width 2, a degenerate composed "syllable" without an initial
1100 // consonant or ieung would have a total width of 0, which is silly.
1101 // The following sequence is effectively "약" without the leading ieung...
1102 EXPECT_EQ(2, wcwidth(0x115f)); // Hangeul choseong filler.
1103 EXPECT_EQ(0, wcwidth(0x1163)); // Hangeul jungseong "ya".
1104 EXPECT_EQ(0, wcwidth(0x11a8)); // Hangeul jongseong "kiyeok".
1105
1106 // U+1160, the jungseong filler, has width 0 because it must have been
1107 // preceded by either a choseong or choseong filler.
1108 EXPECT_EQ(0, wcwidth(0x1160));
1109}
1110
Elliott Hughesc41b5602017-07-27 17:08:08 -07001111TEST(wchar, wcwidth_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001112 if (!have_dl()) return;
1113
Elliott Hughesc41b5602017-07-27 17:08:08 -07001114 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1115 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1116 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1117 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1118 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1119 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1120}
1121
1122TEST(wchar, wcwidth_korean_combining_jamo) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001123 if (!have_dl()) return;
1124
Elliott Hughesc41b5602017-07-27 17:08:08 -07001125 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1126 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1127 EXPECT_EQ(0, wcwidth(0xd7cb));
1128}
1129
1130TEST(wchar, wcwidth_korean_jeongeul_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001131 if (!have_dl()) return;
1132
Elliott Hughesc41b5602017-07-27 17:08:08 -07001133 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
Elliott Hughes20a9f992024-05-29 21:45:51 +00001134 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points as of Unicode 15.
1135
1136 // Undefined characters at the end of the block currently have width 1,
1137 // but since they're undefined, we don't test that.
Elliott Hughesc41b5602017-07-27 17:08:08 -07001138}
1139
1140TEST(wchar, wcwidth_kana) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001141 if (!have_dl()) return;
1142
Elliott Hughesc41b5602017-07-27 17:08:08 -07001143 // 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) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001150 if (!have_dl()) return;
1151
Elliott Hughesc41b5602017-07-27 17:08:08 -07001152 // Circled two-digit CJK "speed sign" numbers are wide,
1153 // though EastAsianWidth is ambiguous.
1154 AssertWcwidthRange(0x3248, 0x3250, 2);
1155}
1156
1157TEST(wchar, wcwidth_hexagrams) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001158 if (!have_dl()) return;
1159
Elliott Hughesc41b5602017-07-27 17:08:08 -07001160 // Hexagrams are wide, though EastAsianWidth is neutral.
1161 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1162}
1163
1164TEST(wchar, wcwidth_default_ignorables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001165 if (!have_dl()) return;
1166
Elliott Hughesc41b5602017-07-27 17:08:08 -07001167 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1168 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1169}
1170
Elliott Hughes20a9f992024-05-29 21:45:51 +00001171TEST(wchar, wcwidth_hangeul_compatibility_jamo) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001172 if (!have_dl()) return;
1173
Elliott Hughes20a9f992024-05-29 21:45:51 +00001174 // These are actually the *compatibility* jamo code points, *not* the regular
1175 // jamo code points (U+1100-U+11FF) using a jungseong filler. If you use the
1176 // Android IME to type any of these, you get these code points.
1177
1178 // (Half of) the Korean "crying" emoticon "ㅠㅠ".
1179 // Actually U+3160 "Hangeul Letter Yu" from Hangeul Compatibility Jamo.
1180 EXPECT_EQ(2, wcwidth(L'ㅠ'));
1181 // The two halves of the Korean internet shorthand "ㄱㅅ" (short for 감사).
1182 // Actually U+3131 "Hangeul Letter Kiyeok" and U+3145 "Hangeul Letter Sios"
1183 // from Hangeul Compatibility Jamo.
1184 EXPECT_EQ(2, wcwidth(L'ㄱ'));
1185 EXPECT_EQ(2, wcwidth(L'ㅅ'));
Elliott Hughesc41b5602017-07-27 17:08:08 -07001186}
Elliott Hughesfe50a0c2021-04-09 08:51:09 -07001187
1188TEST(wchar, wcswidth) {
1189 EXPECT_EQ(2, wcswidth(L"abc", 2));
1190 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1191 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1192}
1193
1194TEST(wchar, wcslcpy) {
1195#if defined(__BIONIC__)
1196 wchar_t dst[32];
1197 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1198 ASSERT_STREQ(L"he", dst);
1199 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1200 ASSERT_STREQ(L"hello world", dst);
1201#else
1202 GTEST_SKIP() << "no wcslcpy in glibc";
1203#endif
1204}
1205
1206TEST(wchar, wcscat) {
1207 wchar_t dst[32];
1208 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1209 ASSERT_STREQ(dst, L"hello");
1210 ASSERT_EQ(dst, wcscat(dst, L" world"));
1211 ASSERT_STREQ(dst, L"hello world");
1212}
1213
1214TEST(wchar, wcscpy) {
1215 wchar_t dst[32];
1216 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1217 ASSERT_STREQ(dst, L"hello");
1218 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1219 ASSERT_STREQ(dst, L"world");
1220}
1221
1222TEST(wchar, wcscasecmp) {
1223 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1224 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1225 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1226 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1227 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1228}
1229
1230TEST(wchar, wcscspn) {
1231 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1232 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1233 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1234}
1235
1236TEST(wchar, wcsspn) {
1237 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1238 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1239 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1240}
1241
1242TEST(wchar, wcsdup) {
1243 wchar_t* s = wcsdup(L"hello");
1244 ASSERT_STREQ(s, L"hello");
1245 free(s);
1246}
1247
1248TEST(wchar, wcslcat) {
1249#if defined(__BIONIC__)
1250 wchar_t dst[4] = {};
1251 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1252 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1253 ASSERT_STREQ(dst, L"abc");
1254#else
1255 GTEST_SKIP() << "no wcslcpy in glibc";
1256#endif
1257}
1258
1259TEST(wchar, wcsncasecmp) {
1260 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1261
1262 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1263 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1264 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1265 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1266 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1267}
1268
1269TEST(wchar, wcsncat) {
1270 wchar_t dst[32];
1271 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1272 ASSERT_STREQ(dst, L"hello");
1273 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1274 ASSERT_STREQ(dst, L"hello");
1275 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1276 ASSERT_STREQ(dst, L"hello, world!");
1277}
1278
1279TEST(wchar, wcsncmp) {
1280 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1281 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1282 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1283 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1284}
1285
1286TEST(wchar, wcsnlen) {
1287 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1288 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1289 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1290}
1291
1292TEST(wchar, wcspbrk) {
1293 const wchar_t* s = L"hello, world!";
1294 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1295 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1296 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1297 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1298}
1299
1300TEST(wchar, wcstok) {
1301 wchar_t s[] = L"this is\ta\nstring";
1302 wchar_t* p;
1303 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1304 ASSERT_STREQ(s, L"this");
1305 ASSERT_STREQ(p, L"is\ta\nstring");
1306 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1307 ASSERT_STREQ(s + 5, L"is");
1308 ASSERT_STREQ(p, L"a\nstring");
1309 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1310 ASSERT_STREQ(s + 8, L"a");
1311 ASSERT_STREQ(p, L"string");
1312 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1313 ASSERT_STREQ(s + 10, L"string");
1314 ASSERT_EQ(nullptr, p);
1315}
1316
1317TEST(wchar, wmemchr) {
1318 const wchar_t* s = L"hello, world!";
1319 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1320 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1321 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1322 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1323}
1324
1325TEST(wchar, wmemcmp) {
1326 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1327 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1328 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1329}
1330
1331TEST(wchar, wmemcpy) {
1332 wchar_t dst[32] = {};
1333 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1334 ASSERT_STREQ(dst, L"hello");
1335}
1336
1337TEST(wchar, wmemmove) {
1338 wchar_t dst[32] = {};
1339 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1340 ASSERT_STREQ(dst, L"hello");
1341}
1342
1343TEST(wchar, wmemset) {
1344 wchar_t dst[4] = {};
1345 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1346 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1347 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1348 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1349 ASSERT_EQ(dst[3], wchar_t(0));
1350 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1351 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1352}