blob: c4cc0bd95f1b418549ddf620ca351a402e184f2a [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 Albert16007d52023-07-20 23:38:57 +0000300 // mbrtoc says: "If s is not a null pointer, the mbtowc function either
301 // returns 0 (if s points to the null character)..."
302 //
303 // So mbrtowc will not provide the correct mbtowc return value for "" and
304 // n = 0.
305 //
306 // glibc gets this right, but all the BSDs (including macOS) and bionic (by
307 // way of openbsd) return -1 instead of 0.
308#ifdef __GLIBC__
309 int expected_result_for_zero_length_empty_string = 0;
Dan Albert512469a2023-08-03 19:34:42 +0000310#else
Dan Albert16007d52023-07-20 23:38:57 +0000311 int expected_result_for_zero_length_empty_string = -1;
Dan Albert512469a2023-08-03 19:34:42 +0000312#endif
313
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700314 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000315 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000316 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700317
Dan Albert16007d52023-07-20 23:38:57 +0000318 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
319 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(out, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000320 EXPECT_EQ(1, mbtowc(out, "hello", 1));
321 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700322
Dan Albert16007d52023-07-20 23:38:57 +0000323 EXPECT_EQ(-1, mbtowc(nullptr, "hello", 0));
324 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(nullptr, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000325 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700326
Dan Albert512469a2023-08-03 19:34:42 +0000327 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700328}
329
330TEST(wchar, mbrtowc) {
331 wchar_t out[8];
332
333 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000334 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000335 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700336
Dan Albert16007d52023-07-20 23:38:57 +0000337 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
338 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000339 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
340 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700341
Dan Albert16007d52023-07-20 23:38:57 +0000342 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "hello", 0, nullptr));
343 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000344 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700345
Dan Albert512469a2023-08-03 19:34:42 +0000346 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700347
Dan Albert512469a2023-08-03 19:34:42 +0000348 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700349 uselocale(LC_GLOBAL_LOCALE);
350
351 // 1-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000352 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
353 EXPECT_EQ(L'a', out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700354 // 2-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000355 EXPECT_EQ(2U, mbrtowc(out,
356 "\xc2\xa2"
357 "cdef",
358 6, nullptr));
359 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700360 // 3-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000361 EXPECT_EQ(3U, mbrtowc(out,
362 "\xe2\x82\xac"
363 "def",
364 6, nullptr));
365 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700366 // 4-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000367 EXPECT_EQ(4U, mbrtowc(out,
368 "\xf0\xa4\xad\xa2"
369 "ef",
370 6, nullptr));
371 EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
Elliott Hughes063525c2014-05-13 11:19:57 -0700372#if defined(__BIONIC__) // glibc allows this.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700373 // Illegal 5-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000374 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
375 "\xf8\xa1\xa2\xa3\xa4"
376 "f",
377 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700378 EXPECT_ERRNO(EILSEQ);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700379#endif
380 // Illegal over-long sequence.
Dan Albert512469a2023-08-03 19:34:42 +0000381 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
382 "\xf0\x82\x82\xac"
383 "ef",
384 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700385 EXPECT_ERRNO(EILSEQ);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700386}
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700387
Elliott Hughes402c7622018-07-06 17:18:05 -0700388TEST(wchar, mbrtowc_valid_non_characters) {
389 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
390 uselocale(LC_GLOBAL_LOCALE);
391
392 wchar_t out[8] = {};
393
394 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
395 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
396 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
397 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
398}
399
400TEST(wchar, mbrtowc_out_of_range) {
401 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
402 uselocale(LC_GLOBAL_LOCALE);
403
404 wchar_t out[8] = {};
405 errno = 0;
Dan Albert53256532023-08-03 19:39:39 +0000406 auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
407 if (kLibcRejectsOverLongUtf8Sequences) {
408 ASSERT_EQ(static_cast<size_t>(-1), result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700409 ASSERT_ERRNO(EILSEQ);
Dan Albert53256532023-08-03 19:39:39 +0000410 } else {
411 ASSERT_EQ(4U, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700412 ASSERT_ERRNO(0);
Dan Albert53256532023-08-03 19:39:39 +0000413 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700414}
415
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700416static void test_mbrtowc_incomplete(mbstate_t* ps) {
Calin Juravle15a63102014-05-08 14:38:35 +0100417 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
418 uselocale(LC_GLOBAL_LOCALE);
419
420 wchar_t out;
421 // 2-byte UTF-8.
422 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
423 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700424 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100425 ASSERT_TRUE(mbsinit(ps));
426 // 3-byte UTF-8.
427 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
428 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
429 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700430 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100431 ASSERT_TRUE(mbsinit(ps));
432 // 4-byte UTF-8.
433 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
434 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
435 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700436 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100437 ASSERT_TRUE(mbsinit(ps));
438
439 // Invalid 2-byte
440 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
441 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700442 ASSERT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100443}
444
445TEST(wchar, mbrtowc_incomplete) {
446 mbstate_t ps;
447 memset(&ps, 0, sizeof(ps));
448
449 test_mbrtowc_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700450 test_mbrtowc_incomplete(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100451}
452
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700453static void test_mbsrtowcs(mbstate_t* ps) {
454 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
455 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
456 constexpr const char* INCOMPLETE = "A" "\xc2";
Calin Juravle15a63102014-05-08 14:38:35 +0100457 wchar_t out[4];
458
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700459 const char* valid = VALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100460 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
461 ASSERT_EQ(L'A', out[0]);
Ben Chengcaff5f22014-05-19 14:27:31 -0700462 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
463 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
464 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
Dan Albert6b55ba52014-07-20 11:51:26 -0700465 // Check that valid has advanced to the next unread character.
Calin Juravle15a63102014-05-08 14:38:35 +0100466 ASSERT_EQ('e', *valid);
467
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800468 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
Dan Albert6b55ba52014-07-20 11:51:26 -0700469 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
470 ASSERT_EQ(L'e', out[0]);
471 ASSERT_EQ(L'f', out[1]);
472 ASSERT_EQ(L'\0', out[2]);
473 // Check that we didn't clobber the rest of out.
474 ASSERT_EQ(L'x', out[3]);
475 // Check that valid has advanced to the end of the string.
Dan Albertb6cc8e02014-07-31 11:31:03 -0700476 ASSERT_EQ(nullptr, valid);
Dan Albert6b55ba52014-07-20 11:51:26 -0700477
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700478 const char* invalid = INVALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100479 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700480 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100481 ASSERT_EQ('\xc2', *invalid);
482
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700483 const char* incomplete = INCOMPLETE;
Calin Juravle15a63102014-05-08 14:38:35 +0100484 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700485 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100486 ASSERT_EQ('\xc2', *incomplete);
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700487
488 // If dst is null, *src shouldn't be updated.
489 // https://code.google.com/p/android/issues/detail?id=166381
490 const char* mbs = VALID;
491 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
492 EXPECT_EQ(VALID, mbs);
493 mbs = INVALID;
494 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
495 EXPECT_EQ(INVALID, mbs);
496 mbs = INCOMPLETE;
497 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
498 EXPECT_EQ(INCOMPLETE, mbs);
Calin Juravle15a63102014-05-08 14:38:35 +0100499}
500
501TEST(wchar, mbsrtowcs) {
502 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
503 uselocale(LC_GLOBAL_LOCALE);
504
505 mbstate_t ps;
506 memset(&ps, 0, sizeof(ps));
507 test_mbsrtowcs(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700508 test_mbsrtowcs(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100509
510 // Invalid multi byte continuation.
511 const char* invalid = "\x20";
512 wchar_t out;
513 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
514 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700515 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100516 ASSERT_EQ('\x20', *invalid);
517}
518
Dan Albert6805c2d2017-08-09 14:55:27 -0700519template <typename T>
520using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
521
522template <typename T>
523void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
524 T expected_value, ptrdiff_t expected_len) {
525 wchar_t* p;
Dan Alberta40159f2023-08-03 19:49:37 +0000526 EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
527 EXPECT_EQ(expected_len, p - str) << str << " " << base;
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700528}
529
Dan Albert6805c2d2017-08-09 14:55:27 -0700530template <typename T>
531void TestWcsToInt(WcsToIntFn<T> fn) {
532 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
533 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
534 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
535 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
536 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
537 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
538 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
539 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
Dan Albert9f30c6b2023-08-03 19:50:28 +0000540 if (kLibcSupportsParsingBinaryLiterals) {
541 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
542 }
Dan Albert6805c2d2017-08-09 14:55:27 -0700543}
544
545template <typename T>
546void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
547 const wchar_t* max_str) {
548 if (std::is_signed<T>::value) {
549 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
550 } else {
551 // If the subject sequence begins with a <hyphen-minus>, the value resulting
552 // from the conversion shall be negated.
553 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
554 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
555 }
556 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
557}
558
559TEST(wchar, wcstol) {
560 TestWcsToInt(wcstol);
561}
562
563TEST(wchar, wcstol_limits) {
564 if (sizeof(long) == 8) {
565 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
566 } else {
567 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
568 }
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700569}
570
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700571TEST(wchar, wcstoul) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700572 TestWcsToInt(wcstoul);
573}
574
575TEST(wchar, wcstoul_limits) {
576 if (sizeof(long) == 8) {
577 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
578 } else {
579 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
580 }
581}
582
583TEST(wchar, wcstoll) {
584 TestWcsToInt(wcstoll);
585}
586
587TEST(wchar, wcstoll_limits) {
588 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700589}
590
591TEST(wchar, wcstoull) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700592 TestWcsToInt(wcstoull);
593}
594
595TEST(wchar, wcstoull_limits) {
596 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
597}
598
599TEST(wchar, wcstoimax) {
600 TestWcsToInt(wcstoimax);
601}
602
603TEST(wchar, wcstoimax_limits) {
604 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
605 L"9223372036854775808");
606}
607
608TEST(wchar, wcstoumax) {
609 TestWcsToInt(wcstoumax);
610}
611
612TEST(wchar, wcstoumax_limits) {
613 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700614}
615
616TEST(wchar, mbsnrtowcs) {
617 wchar_t dst[128];
618 const char* s = "hello, world!";
619 const char* src;
620
621 memset(dst, 0, sizeof(dst));
622 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700623 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700624
625 memset(dst, 0, sizeof(dst));
626 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700627 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700628 ASSERT_EQ(L'h', dst[0]);
629 ASSERT_EQ(L'e', dst[1]);
630 ASSERT_EQ(&s[2], src);
631
632 memset(dst, 0, sizeof(dst));
633 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700634 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700635 ASSERT_EQ(L'h', dst[0]);
636 ASSERT_EQ(L'e', dst[1]);
637 ASSERT_EQ(L'l', dst[2]);
638 ASSERT_EQ(&s[3], src);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700639
640 memset(dst, 0, sizeof(dst));
641 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
642 src = incomplete;
643 errno = 0;
644 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700645 ASSERT_ERRNO(EILSEQ);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700646
647 src = incomplete;
648 errno = 0;
649 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700650 ASSERT_ERRNO(EILSEQ);
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700651}
Elliott Hughesefaa4612014-05-02 15:53:03 -0700652
Elliott Hughes3376c232018-02-13 23:14:12 -0800653TEST(wchar, wcsftime__wcsftime_l) {
Elliott Hughesefaa4612014-05-02 15:53:03 -0700654 setenv("TZ", "UTC", 1);
655
656 struct tm t;
657 memset(&t, 0, sizeof(tm));
658 t.tm_year = 200;
659 t.tm_mon = 2;
660 t.tm_mday = 10;
661
662 wchar_t buf[64];
663
664 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
665 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Dan Albert686e67d2023-08-03 19:00:09 +0000666 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -0800667 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Elliott Hughesefaa4612014-05-02 15:53:03 -0700668}
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200669
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800670TEST(wchar, wmemmove_smoke) {
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200671 const wchar_t const_wstr[] = L"This is a test of something or other.....";
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800672 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200673
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800674 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200675 EXPECT_STREQ(const_wstr, wstr);
676
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800677 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
Christopher Ferris8bf50d52014-05-29 15:42:42 -0700678 EXPECT_STREQ(L"This This is a test of something or other", wstr);
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200679}
Elliott Hughes69f05d22014-06-05 20:10:09 -0700680
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800681TEST(wchar, wmemcpy_smoke) {
682 const wchar_t src[] = L"Source string";
683 wchar_t dst[NUM_WCHARS(sizeof(src))];
684
685 EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
686 EXPECT_STREQ(dst, src);
687}
688
689TEST(wchar, wcpcpy_smoke) {
690 const wchar_t src[] = L"Source string";
691 wchar_t dst[NUM_WCHARS(sizeof(src))];
692
693 EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
694 EXPECT_STREQ(dst, src);
695}
696
697TEST(wchar, wcpncpy_smoke) {
698 const wchar_t src[] = L"Source string";
699 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
700
701 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
702 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
703 EXPECT_STREQ(dst, src);
704
705 EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
706 dst[6] = L'\0';
707 EXPECT_STREQ(dst, L"Source");
708
709 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
710 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
711 EXPECT_STREQ(dst, src);
712 EXPECT_EQ(dst[src_len], L'\0');
713 EXPECT_EQ(dst[src_len+1], L'\0');
714 EXPECT_EQ(dst[src_len+2], L'\0');
715 EXPECT_EQ(dst[src_len+3], L'\0');
716 EXPECT_EQ(dst[src_len+4], L'x');
717}
718
719TEST(wchar, wcscpy_smoke) {
720 const wchar_t src[] = L"Source string";
721 wchar_t dst[NUM_WCHARS(sizeof(src))];
722
723 EXPECT_EQ(dst, wcscpy(dst, src));
724 EXPECT_STREQ(src, dst);
725}
726
727TEST(wchar, wcsncpy_smoke) {
728 const wchar_t src[] = L"Source string";
729 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
730
731 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
732 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
733 EXPECT_STREQ(dst, src);
734
735 EXPECT_EQ(dst, wcsncpy(dst, src, 6));
736 dst[6] = L'\0';
737 EXPECT_STREQ(dst, L"Source");
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700738 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
739 EXPECT_STREQ(dst, L"Source");
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800740
741 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
742 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
743 EXPECT_STREQ(dst, src);
744 EXPECT_EQ(dst[src_len], L'\0');
745 EXPECT_EQ(dst[src_len+1], L'\0');
746 EXPECT_EQ(dst[src_len+2], L'\0');
747 EXPECT_EQ(dst[src_len+3], L'\0');
748 EXPECT_EQ(dst[src_len+4], L'x');
749}
750
Elliott Hughes69f05d22014-06-05 20:10:09 -0700751TEST(wchar, mbrtowc_15439554) {
752 // http://b/15439554
753 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
754 uselocale(LC_GLOBAL_LOCALE);
755
756 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
757 ASSERT_GE(MB_CUR_MAX, 4U);
758
759 wchar_t wc;
760 size_t n;
761
762 // 1-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700763 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700764 EXPECT_EQ(1U, n);
765 EXPECT_EQ(L'x', wc);
766 // 2-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700767 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700768 EXPECT_EQ(2U, n);
769 EXPECT_EQ(L'¢', wc);
770 // 3-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700771 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700772 EXPECT_EQ(3U, n);
773 EXPECT_EQ(L'€', wc);
774 // 4-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700775 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700776 EXPECT_EQ(4U, n);
777 EXPECT_EQ(L'𤭢', wc);
778}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700779
780TEST(wchar, open_wmemstream) {
781 wchar_t* p = nullptr;
782 size_t size = 0;
783 FILE* fp = open_wmemstream(&p, &size);
784 ASSERT_NE(EOF, fputws(L"hello, world!", fp));
785 fclose(fp);
786
787 ASSERT_STREQ(L"hello, world!", p);
788 ASSERT_EQ(wcslen(L"hello, world!"), size);
789 free(p);
790}
791
792TEST(stdio, open_wmemstream_EINVAL) {
793#if defined(__BIONIC__)
794 wchar_t* p;
795 size_t size;
zijunzhao7ce2f952023-04-03 23:13:57 +0000796#pragma clang diagnostic push
797#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes6b841db2014-08-20 16:10:49 -0700798 // Invalid buffer.
799 errno = 0;
800 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
Elliott Hughes95646e62023-09-21 14:11:19 -0700801 ASSERT_ERRNO(EINVAL);
Elliott Hughes6b841db2014-08-20 16:10:49 -0700802
803 // Invalid size.
804 errno = 0;
805 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700806 ASSERT_ERRNO(EINVAL);
zijunzhao7ce2f952023-04-03 23:13:57 +0000807#pragma clang diagnostic pop
Elliott Hughes6b841db2014-08-20 16:10:49 -0700808#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800809 GTEST_SKIP() << "This test is bionic-specific";
Elliott Hughes6b841db2014-08-20 16:10:49 -0700810#endif
811}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700812
813TEST(wchar, wcstol_EINVAL) {
814 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700815 wcstol(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700816 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700817 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700818 wcstol(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700819 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700820 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700821 wcstol(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700822 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700823}
824
825TEST(wchar, wcstoll_EINVAL) {
826 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700827 wcstoll(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700828 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700829 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700830 wcstoll(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700831 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700832 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700833 wcstoll(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700834 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700835}
836
837TEST(wchar, wcstoul_EINVAL) {
838 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700839 wcstoul(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700840 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700841 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700842 wcstoul(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700843 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700844 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700845 wcstoul(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700846 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700847}
848
849TEST(wchar, wcstoull_EINVAL) {
850 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700851 wcstoull(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700852 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700853 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700854 wcstoull(L"123", nullptr, 1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700855 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700856 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700857 wcstoull(L"123", nullptr, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700858 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700859}
860
861TEST(wchar, wcstoll_l_EINVAL) {
862 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000863 wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700864 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700865 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000866 wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700867 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700868 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000869 wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700870 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700871}
872
873TEST(wchar, wcstoull_l_EINVAL) {
874 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000875 wcstoull_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700876 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700877 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000878 wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700879 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700880 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000881 wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700882 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700883}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800884
885TEST(wchar, wmempcpy) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700886#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800887 wchar_t dst[6];
888 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
Colin Cross7da20342021-07-28 11:18:11 -0700889#else
890 GTEST_SKIP() << "musl doesn't have wmempcpy";
891#endif
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800892}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700893
894template <typename T>
Dan Albert6805c2d2017-08-09 14:55:27 -0700895using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700896
Dan Albert6805c2d2017-08-09 14:55:27 -0700897template <typename T>
898void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
899 T expected_value, ptrdiff_t expected_len) {
Dan Albertf6346552016-12-02 12:02:03 -0800900 wchar_t* p;
Dan Albert6805c2d2017-08-09 14:55:27 -0700901 ASSERT_EQ(expected_value, fn(str, &p));
902 ASSERT_EQ(expected_len, p - str);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700903}
904
Dan Albert6805c2d2017-08-09 14:55:27 -0700905template <typename T>
906void TestWcsToFloat(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800907 TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
908 TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
909 TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
910 TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
911 TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
912 TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
Dan Albert6805c2d2017-08-09 14:55:27 -0700913}
914
915template <typename T>
916void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800917 TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
918 TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
919 TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
920 TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
Dan Albert6805c2d2017-08-09 14:55:27 -0700921}
922
923template <typename T>
924void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
925 ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
926 ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
927 ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
928
929 ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
930 ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
931 ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
932
933 wchar_t* p;
934 ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
935 ASSERT_STREQ(L"ny", p);
936 ASSERT_TRUE(isnan(fn(L"nanny", &p)));
937 ASSERT_STREQ(L"ny", p);
938 ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
939 ASSERT_STREQ(L"ny", p);
940
941 ASSERT_EQ(0, fn(L"muppet", &p));
942 ASSERT_STREQ(L"muppet", p);
943 ASSERT_EQ(0, fn(L" muppet", &p));
944 ASSERT_STREQ(L" muppet", p);
945
946 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
947 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
948 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
949
950 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
951 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
952 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
953
954 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
955 ASSERT_STREQ(L"initude", p);
956 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
957 ASSERT_STREQ(L"initude", p);
958 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
959 ASSERT_STREQ(L"initude", p);
960
961 // Check case-insensitivity.
962 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
963 ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700964}
965
966TEST(wchar, wcstof) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700967 TestWcsToFloat(wcstof);
968}
969
970TEST(wchar, wcstof_hex_floats) {
971 TestWcsToFloatHexFloats(wcstof);
972}
973
974TEST(wchar, wcstof_hex_inf_nan) {
975 TestWcsToFloatInfNan(wcstof);
976}
977
978TEST(wchar, wcstod) {
979 TestWcsToFloat(wcstod);
980}
981
982TEST(wchar, wcstod_hex_floats) {
983 TestWcsToFloatHexFloats(wcstod);
984}
985
986TEST(wchar, wcstod_hex_inf_nan) {
987 TestWcsToFloatInfNan(wcstod);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700988}
989
990TEST(wchar, wcstold) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700991 TestWcsToFloat(wcstold);
992}
993
994TEST(wchar, wcstold_hex_floats) {
995 TestWcsToFloatHexFloats(wcstold);
996}
997
998TEST(wchar, wcstold_hex_inf_nan) {
999 TestWcsToFloatInfNan(wcstold);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001000}
Elliott Hughesc41b5602017-07-27 17:08:08 -07001001
Elliott Hughes3376c232018-02-13 23:14:12 -08001002TEST(wchar, wcstod_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001003#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001004 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001005#else
1006 GTEST_SKIP() << "musl doesn't have wcstod_l";
1007#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001008}
1009
1010TEST(wchar, wcstof_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001011#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001012 EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001013#else
1014 GTEST_SKIP() << "musl doesn't have wcstof_l";
1015#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001016}
1017
1018TEST(wchar, wcstol_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001019#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001020 EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001021#else
1022 GTEST_SKIP() << "musl doesn't have wcstol_l";
1023#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001024}
1025
1026TEST(wchar, wcstold_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001027 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001028}
1029
1030TEST(wchar, wcstoll_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001031 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001032}
1033
1034TEST(wchar, wcstoul_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001035#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001036 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001037#else
1038 GTEST_SKIP() << "musl doesn't have wcstoul_l";
1039#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001040}
1041
1042TEST(wchar, wcstoull_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001043 EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001044}
1045
Elliott Hughesc41b5602017-07-27 17:08:08 -07001046static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
1047 for (wchar_t i = begin; i < end; ++i) {
1048 EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
1049 }
1050}
1051
1052TEST(wchar, wcwidth_NUL) {
1053 // NUL is defined to return 0 rather than -1, despite being a C0 control.
1054 EXPECT_EQ(0, wcwidth(0));
1055}
1056
1057TEST(wchar, wcwidth_ascii) {
1058 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1059}
1060
1061TEST(wchar, wcwidth_controls) {
1062 AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
1063 EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
1064 AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
1065}
1066
1067TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001068 if (!have_dl()) return;
1069
Elliott Hughesc41b5602017-07-27 17:08:08 -07001070 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1071 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
1072 EXPECT_EQ(0, wcwidth(0x00ad)); // Soft hyphen (SHY).
1073 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1074}
1075
1076TEST(wchar, wcwidth_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001077 if (!have_dl()) return;
1078
Elliott Hughesc41b5602017-07-27 17:08:08 -07001079 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1080 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1081 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1082 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1083 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1084 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1085}
1086
1087TEST(wchar, wcwidth_korean_combining_jamo) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001088 if (!have_dl()) return;
1089
Elliott Hughesc41b5602017-07-27 17:08:08 -07001090 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1091 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1092 EXPECT_EQ(0, wcwidth(0xd7cb));
1093}
1094
1095TEST(wchar, wcwidth_korean_jeongeul_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001096 if (!have_dl()) return;
1097
Elliott Hughesc41b5602017-07-27 17:08:08 -07001098 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
1099 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points in Unicode 7.
1100 // Undefined characters at the end of the block have width 1.
1101}
1102
1103TEST(wchar, wcwidth_kana) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001104 if (!have_dl()) return;
1105
Elliott Hughesc41b5602017-07-27 17:08:08 -07001106 // Hiragana (most, not undefined).
1107 AssertWcwidthRange(0x3041, 0x3097, 2);
1108 // Katakana.
1109 AssertWcwidthRange(0x30a0, 0x3100, 2);
1110}
1111
1112TEST(wchar, wcwidth_circled_two_digit_cjk) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001113 if (!have_dl()) return;
1114
Elliott Hughesc41b5602017-07-27 17:08:08 -07001115 // Circled two-digit CJK "speed sign" numbers are wide,
1116 // though EastAsianWidth is ambiguous.
1117 AssertWcwidthRange(0x3248, 0x3250, 2);
1118}
1119
1120TEST(wchar, wcwidth_hexagrams) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001121 if (!have_dl()) return;
1122
Elliott Hughesc41b5602017-07-27 17:08:08 -07001123 // Hexagrams are wide, though EastAsianWidth is neutral.
1124 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1125}
1126
1127TEST(wchar, wcwidth_default_ignorables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001128 if (!have_dl()) return;
1129
Elliott Hughesc41b5602017-07-27 17:08:08 -07001130 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1131 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1132}
1133
1134TEST(wchar, wcwidth_korean_common_non_syllables) {
Elliott Hughes966d8a32017-08-29 11:29:28 -07001135 if (!have_dl()) return;
1136
Elliott Hughesc41b5602017-07-27 17:08:08 -07001137 EXPECT_EQ(2, wcwidth(L'ㅜ')); // Korean "crying" emoticon.
1138 EXPECT_EQ(2, wcwidth(L'ã…‹')); // Korean "laughing" emoticon.
1139}
Elliott Hughesfe50a0c2021-04-09 08:51:09 -07001140
1141TEST(wchar, wcswidth) {
1142 EXPECT_EQ(2, wcswidth(L"abc", 2));
1143 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1144 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1145}
1146
1147TEST(wchar, wcslcpy) {
1148#if defined(__BIONIC__)
1149 wchar_t dst[32];
1150 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1151 ASSERT_STREQ(L"he", dst);
1152 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1153 ASSERT_STREQ(L"hello world", dst);
1154#else
1155 GTEST_SKIP() << "no wcslcpy in glibc";
1156#endif
1157}
1158
1159TEST(wchar, wcscat) {
1160 wchar_t dst[32];
1161 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1162 ASSERT_STREQ(dst, L"hello");
1163 ASSERT_EQ(dst, wcscat(dst, L" world"));
1164 ASSERT_STREQ(dst, L"hello world");
1165}
1166
1167TEST(wchar, wcscpy) {
1168 wchar_t dst[32];
1169 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1170 ASSERT_STREQ(dst, L"hello");
1171 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1172 ASSERT_STREQ(dst, L"world");
1173}
1174
1175TEST(wchar, wcscasecmp) {
1176 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1177 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1178 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1179 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1180 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1181}
1182
1183TEST(wchar, wcscspn) {
1184 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1185 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1186 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1187}
1188
1189TEST(wchar, wcsspn) {
1190 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1191 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1192 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1193}
1194
1195TEST(wchar, wcsdup) {
1196 wchar_t* s = wcsdup(L"hello");
1197 ASSERT_STREQ(s, L"hello");
1198 free(s);
1199}
1200
1201TEST(wchar, wcslcat) {
1202#if defined(__BIONIC__)
1203 wchar_t dst[4] = {};
1204 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1205 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1206 ASSERT_STREQ(dst, L"abc");
1207#else
1208 GTEST_SKIP() << "no wcslcpy in glibc";
1209#endif
1210}
1211
1212TEST(wchar, wcsncasecmp) {
1213 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1214
1215 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1216 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1217 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1218 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1219 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1220}
1221
1222TEST(wchar, wcsncat) {
1223 wchar_t dst[32];
1224 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1225 ASSERT_STREQ(dst, L"hello");
1226 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1227 ASSERT_STREQ(dst, L"hello");
1228 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1229 ASSERT_STREQ(dst, L"hello, world!");
1230}
1231
1232TEST(wchar, wcsncmp) {
1233 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1234 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1235 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1236 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1237}
1238
1239TEST(wchar, wcsnlen) {
1240 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1241 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1242 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1243}
1244
1245TEST(wchar, wcspbrk) {
1246 const wchar_t* s = L"hello, world!";
1247 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1248 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1249 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1250 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1251}
1252
1253TEST(wchar, wcstok) {
1254 wchar_t s[] = L"this is\ta\nstring";
1255 wchar_t* p;
1256 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1257 ASSERT_STREQ(s, L"this");
1258 ASSERT_STREQ(p, L"is\ta\nstring");
1259 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1260 ASSERT_STREQ(s + 5, L"is");
1261 ASSERT_STREQ(p, L"a\nstring");
1262 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1263 ASSERT_STREQ(s + 8, L"a");
1264 ASSERT_STREQ(p, L"string");
1265 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1266 ASSERT_STREQ(s + 10, L"string");
1267 ASSERT_EQ(nullptr, p);
1268}
1269
1270TEST(wchar, wmemchr) {
1271 const wchar_t* s = L"hello, world!";
1272 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1273 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1274 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1275 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1276}
1277
1278TEST(wchar, wmemcmp) {
1279 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1280 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1281 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1282}
1283
1284TEST(wchar, wmemcpy) {
1285 wchar_t dst[32] = {};
1286 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1287 ASSERT_STREQ(dst, L"hello");
1288}
1289
1290TEST(wchar, wmemmove) {
1291 wchar_t dst[32] = {};
1292 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1293 ASSERT_STREQ(dst, L"hello");
1294}
1295
1296TEST(wchar, wmemset) {
1297 wchar_t dst[4] = {};
1298 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1299 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1300 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1301 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1302 ASSERT_EQ(dst[3], wchar_t(0));
1303 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1304 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1305}