blob: c76f8006203c2abd1834c31295c6d44492bd1e9b [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];
Christopher Ferris2a391882024-12-19 13:44:35 -0800133 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100134
135 // Any non-initial state is invalid when calling wcrtomb.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700136 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100137 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700138 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100139
140 // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
141 // state should be reset.
Christopher Ferris2a391882024-12-19 13:44:35 -0800142 ps = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700143 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
144 EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100145 EXPECT_TRUE(mbsinit(&ps));
146
Christopher Ferris2a391882024-12-19 13:44:35 -0800147 ps = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700148 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
Calin Juravle15a63102014-05-08 14:38:35 +0100149 EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
150 EXPECT_TRUE(mbsinit(&ps));
151}
152
Elliott Hughes05493712014-04-17 17:30:03 -0700153TEST(wchar, wcstombs_wcrtombs) {
Dan Albert9f78c512023-08-03 19:49:09 +0000154 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
155 uselocale(LC_GLOBAL_LOCALE);
156
Elliott Hughes1b836ee2014-04-18 13:32:33 -0700157 const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
Elliott Hughesf83e6442014-05-01 17:14:59 -0700158 const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
Elliott Hughes05493712014-04-17 17:30:03 -0700159 const wchar_t* src;
160 char bytes[BUFSIZ];
161
162 // Given a NULL destination, these functions count valid characters.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700163 EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
164 EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
165 EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
Elliott Hughes05493712014-04-17 17:30:03 -0700166 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700167 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700168 EXPECT_EQ(&chars[0], src);
169 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700170 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700171 EXPECT_EQ(&chars[0], src);
172 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700173 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700174 EXPECT_EQ(&chars[0], src);
175
176 // An unrepresentable char just returns an error from wcstombs...
177 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700178 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700179 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700180 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700181 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
Elliott Hughes95646e62023-09-21 14:11:19 -0700182 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700183
184 // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
185 // to actually convert anything...
186 errno = 0;
187 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700188 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700189 EXPECT_EQ(&bad_chars[0], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700190 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700191 errno = 0;
192 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700193 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700194 EXPECT_EQ(&bad_chars[0], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700195 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700196
197 // Okay, now let's test actually converting something...
198 memset(bytes, 'x', sizeof(bytes));
199 EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
200 memset(bytes, 'x', sizeof(bytes));
201 EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
202 bytes[5] = 0;
203 EXPECT_STREQ("hellx", bytes);
204 memset(bytes, 'x', sizeof(bytes));
205 EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
206 EXPECT_STREQ("hello", bytes);
207 memset(bytes, 'x', sizeof(bytes));
208 EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
209 EXPECT_STREQ("hello", bytes);
210 errno = 0;
211 memset(bytes, 'x', sizeof(bytes));
212 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
Elliott Hughes95646e62023-09-21 14:11:19 -0700213 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700214 bytes[3] = 0;
215 EXPECT_STREQ("hix", bytes);
216
217 // wcsrtombs is a bit more informative...
218 memset(bytes, 'x', sizeof(bytes));
219 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700220 EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700221 EXPECT_EQ(&chars[0], src); // No input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700222 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700223
224 memset(bytes, 'x', sizeof(bytes));
225 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700226 EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700227 EXPECT_EQ(&chars[4], src); // Some input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700228 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700229 bytes[5] = 0;
230 EXPECT_STREQ("hellx", bytes);
231
232 memset(bytes, 'x', sizeof(bytes));
233 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700234 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
235 EXPECT_EQ(nullptr, src); // All input consumed!
Elliott Hughes95646e62023-09-21 14:11:19 -0700236 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700237 EXPECT_STREQ("hello", bytes);
238
239 memset(bytes, 'x', sizeof(bytes));
240 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700241 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
242 EXPECT_EQ(nullptr, src); // All input consumed.
Elliott Hughes95646e62023-09-21 14:11:19 -0700243 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700244 EXPECT_STREQ("hello", bytes);
245
246 memset(bytes, 'x', sizeof(bytes));
247 src = bad_chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700248 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
Elliott Hughes05493712014-04-17 17:30:03 -0700249 EXPECT_EQ(&bad_chars[2], src);
Elliott Hughes95646e62023-09-21 14:11:19 -0700250 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700251 bytes[3] = 0;
252 EXPECT_STREQ("hix", bytes);
Calin Juravle15a63102014-05-08 14:38:35 +0100253
254 // Any non-initial state is invalid when calling wcsrtombs.
Christopher Ferris2a391882024-12-19 13:44:35 -0800255 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100256 src = chars;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700257 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
258 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700259 EXPECT_ERRNO(EILSEQ);
Elliott Hughes05493712014-04-17 17:30:03 -0700260}
Elliott Hughes83c07b52014-04-21 18:09:46 -0700261
262TEST(wchar, limits) {
263 ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
264}
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700265
Dan Albert001f8f02014-06-04 09:53:06 -0700266TEST(wchar, wcsstr) {
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700267 const wchar_t* haystack = L"big daddy/giant haystacks!";
268 const wchar_t* empty_haystack = L"";
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700269
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700270 // The empty needle is a special case.
271 ASSERT_EQ(haystack, wcsstr(haystack, L""));
272 ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
273
274 ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
275 ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
276 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
277 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
278 ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
279 ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
280
281 ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
282 ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
Elliott Hughesd299bcf2014-04-28 16:28:51 -0700283}
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700284
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800285TEST(wchar, wcsstr_80199) {
286 // https://code.google.com/p/android/issues/detail?id=80199
Yi Kong32bc0fc2018-08-02 17:31:13 -0700287 ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
Elliott Hughese1f9dda2015-02-14 14:11:50 -0800288}
289
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700290TEST(wchar, mbtowc) {
291 wchar_t out[8];
292
Dan Albert16007d52023-07-20 23:38:57 +0000293 // mbtowc and all the mbrto* APIs behave slightly differently when n is 0:
Dan Albert512469a2023-08-03 19:34:42 +0000294 //
Dan Albert16007d52023-07-20 23:38:57 +0000295 // mbrtowc returns 0 "if the next n or fewer bytes complete the multibyte
296 // character that corresponds to the null wide character"
Dan Albert512469a2023-08-03 19:34:42 +0000297 //
Dan Albertac243732023-10-25 20:31:48 +0000298 // mbrtoc (C23 7.24.7.2.4) says:
Dan Albert16007d52023-07-20 23:38:57 +0000299 //
Dan Albertac243732023-10-25 20:31:48 +0000300 // If s is not a null pointer, the mbtowc function either returns 0 (if s
301 // points to the null character), or returns the number of bytes that are
302 // contained in the converted multibyte character (if the next n or fewer
303 // bytes form a valid multibyte character), or returns -1 (if they do not
304 // form a valid multibyte character).
Dan Albert16007d52023-07-20 23:38:57 +0000305 //
Dan Albertac243732023-10-25 20:31:48 +0000306 // glibc's interpretation differs from all the BSDs (including macOS) and
307 // bionic (by way of openbsd). glibc returns 0 since s does point to the null
308 // character, whereas the BSDs return -1 because the next 0 bytes do not form
309 // a valid multibyte chatacter. glibc's interpretation is probably more
310 // correct from a strict interpretation of the spec, but considering the other
311 // APIs behave more like the BSD interpretation that may be a bug in the spec.
Dan Albert16007d52023-07-20 23:38:57 +0000312#ifdef __GLIBC__
313 int expected_result_for_zero_length_empty_string = 0;
Dan Albert512469a2023-08-03 19:34:42 +0000314#else
Dan Albert16007d52023-07-20 23:38:57 +0000315 int expected_result_for_zero_length_empty_string = -1;
Dan Albert512469a2023-08-03 19:34:42 +0000316#endif
317
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700318 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000319 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000320 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700321
Dan Albert16007d52023-07-20 23:38:57 +0000322 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
323 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(out, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000324 EXPECT_EQ(1, mbtowc(out, "hello", 1));
325 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700326
Dan Albert16007d52023-07-20 23:38:57 +0000327 EXPECT_EQ(-1, mbtowc(nullptr, "hello", 0));
328 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(nullptr, "", 0));
Dan Albert512469a2023-08-03 19:34:42 +0000329 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700330
Dan Albert512469a2023-08-03 19:34:42 +0000331 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700332}
333
334TEST(wchar, mbrtowc) {
335 wchar_t out[8];
336
337 out[0] = 'x';
Dan Albert16007d52023-07-20 23:38:57 +0000338 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000339 EXPECT_EQ('x', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700340
Dan Albert16007d52023-07-20 23:38:57 +0000341 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
342 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000343 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
344 EXPECT_EQ(L'h', out[0]);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700345
Dan Albert16007d52023-07-20 23:38:57 +0000346 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "hello", 0, nullptr));
347 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "", 0, nullptr));
Dan Albert512469a2023-08-03 19:34:42 +0000348 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700349
Dan Albert512469a2023-08-03 19:34:42 +0000350 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700351
Dan Albert512469a2023-08-03 19:34:42 +0000352 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700353 uselocale(LC_GLOBAL_LOCALE);
354
355 // 1-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000356 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
357 EXPECT_EQ(L'a', out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700358 // 2-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000359 EXPECT_EQ(2U, mbrtowc(out,
360 "\xc2\xa2"
361 "cdef",
362 6, nullptr));
363 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700364 // 3-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000365 EXPECT_EQ(3U, mbrtowc(out,
366 "\xe2\x82\xac"
367 "def",
368 6, nullptr));
369 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700370 // 4-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000371 EXPECT_EQ(4U, mbrtowc(out,
372 "\xf0\xa4\xad\xa2"
373 "ef",
374 6, nullptr));
375 EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
Elliott Hughes063525c2014-05-13 11:19:57 -0700376#if defined(__BIONIC__) // glibc allows this.
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700377 // Illegal 5-byte UTF-8.
Dan Albert512469a2023-08-03 19:34:42 +0000378 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
379 "\xf8\xa1\xa2\xa3\xa4"
380 "f",
381 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700382 EXPECT_ERRNO(EILSEQ);
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700383#endif
384 // Illegal over-long sequence.
Dan Albert512469a2023-08-03 19:34:42 +0000385 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
386 "\xf0\x82\x82\xac"
387 "ef",
388 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700389 EXPECT_ERRNO(EILSEQ);
Elliott Hughes0a5e26d2014-04-28 17:51:13 -0700390}
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700391
Elliott Hughes402c7622018-07-06 17:18:05 -0700392TEST(wchar, mbrtowc_valid_non_characters) {
393 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
394 uselocale(LC_GLOBAL_LOCALE);
395
396 wchar_t out[8] = {};
397
398 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
399 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
400 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
401 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
402}
403
404TEST(wchar, mbrtowc_out_of_range) {
405 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
406 uselocale(LC_GLOBAL_LOCALE);
407
408 wchar_t out[8] = {};
409 errno = 0;
Dan Albert53256532023-08-03 19:39:39 +0000410 auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
411 if (kLibcRejectsOverLongUtf8Sequences) {
412 ASSERT_EQ(static_cast<size_t>(-1), result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700413 ASSERT_ERRNO(EILSEQ);
Dan Albert53256532023-08-03 19:39:39 +0000414 } else {
415 ASSERT_EQ(4U, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700416 ASSERT_ERRNO(0);
Dan Albert53256532023-08-03 19:39:39 +0000417 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700418}
419
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700420static void test_mbrtowc_incomplete(mbstate_t* ps) {
Calin Juravle15a63102014-05-08 14:38:35 +0100421 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
422 uselocale(LC_GLOBAL_LOCALE);
423
424 wchar_t out;
425 // 2-byte UTF-8.
426 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
427 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700428 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100429 ASSERT_TRUE(mbsinit(ps));
430 // 3-byte UTF-8.
431 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
432 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
433 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700434 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100435 ASSERT_TRUE(mbsinit(ps));
436 // 4-byte UTF-8.
437 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
438 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
439 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
Ben Chengcaff5f22014-05-19 14:27:31 -0700440 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
Calin Juravle15a63102014-05-08 14:38:35 +0100441 ASSERT_TRUE(mbsinit(ps));
442
443 // Invalid 2-byte
444 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
445 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700446 ASSERT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100447}
448
449TEST(wchar, mbrtowc_incomplete) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800450 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100451
452 test_mbrtowc_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700453 test_mbrtowc_incomplete(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100454}
455
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700456static void test_mbsrtowcs(mbstate_t* ps) {
457 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
458 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
459 constexpr const char* INCOMPLETE = "A" "\xc2";
Calin Juravle15a63102014-05-08 14:38:35 +0100460 wchar_t out[4];
461
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700462 const char* valid = VALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100463 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
464 ASSERT_EQ(L'A', out[0]);
Ben Chengcaff5f22014-05-19 14:27:31 -0700465 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
466 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
467 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
Dan Albert6b55ba52014-07-20 11:51:26 -0700468 // Check that valid has advanced to the next unread character.
Calin Juravle15a63102014-05-08 14:38:35 +0100469 ASSERT_EQ('e', *valid);
470
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800471 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
Dan Albert6b55ba52014-07-20 11:51:26 -0700472 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
473 ASSERT_EQ(L'e', out[0]);
474 ASSERT_EQ(L'f', out[1]);
475 ASSERT_EQ(L'\0', out[2]);
476 // Check that we didn't clobber the rest of out.
477 ASSERT_EQ(L'x', out[3]);
478 // Check that valid has advanced to the end of the string.
Dan Albertb6cc8e02014-07-31 11:31:03 -0700479 ASSERT_EQ(nullptr, valid);
Dan Albert6b55ba52014-07-20 11:51:26 -0700480
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700481 const char* invalid = INVALID;
Calin Juravle15a63102014-05-08 14:38:35 +0100482 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700483 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100484 ASSERT_EQ('\xc2', *invalid);
485
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700486 const char* incomplete = INCOMPLETE;
Calin Juravle15a63102014-05-08 14:38:35 +0100487 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700488 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100489 ASSERT_EQ('\xc2', *incomplete);
Elliott Hughes89e29ee2016-09-29 17:21:43 -0700490
491 // If dst is null, *src shouldn't be updated.
492 // https://code.google.com/p/android/issues/detail?id=166381
493 const char* mbs = VALID;
494 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
495 EXPECT_EQ(VALID, mbs);
496 mbs = INVALID;
497 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
498 EXPECT_EQ(INVALID, mbs);
499 mbs = INCOMPLETE;
500 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
501 EXPECT_EQ(INCOMPLETE, mbs);
Calin Juravle15a63102014-05-08 14:38:35 +0100502}
503
504TEST(wchar, mbsrtowcs) {
505 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
506 uselocale(LC_GLOBAL_LOCALE);
507
Christopher Ferris2a391882024-12-19 13:44:35 -0800508 mbstate_t ps = {};
Calin Juravle15a63102014-05-08 14:38:35 +0100509 test_mbsrtowcs(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700510 test_mbsrtowcs(nullptr);
Calin Juravle15a63102014-05-08 14:38:35 +0100511
512 // Invalid multi byte continuation.
513 const char* invalid = "\x20";
514 wchar_t out;
515 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
516 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700517 EXPECT_ERRNO(EILSEQ);
Calin Juravle15a63102014-05-08 14:38:35 +0100518 ASSERT_EQ('\x20', *invalid);
519}
520
Dan Albert6805c2d2017-08-09 14:55:27 -0700521template <typename T>
522using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
523
524template <typename T>
525void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
526 T expected_value, ptrdiff_t expected_len) {
527 wchar_t* p;
Dan Alberta40159f2023-08-03 19:49:37 +0000528 EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
529 EXPECT_EQ(expected_len, p - str) << str << " " << base;
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700530}
531
Dan Albert6805c2d2017-08-09 14:55:27 -0700532template <typename T>
533void TestWcsToInt(WcsToIntFn<T> fn) {
534 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
535 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
536 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
537 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
538 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
539 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
540 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
541 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
Dan Albert9f30c6b2023-08-03 19:50:28 +0000542 if (kLibcSupportsParsingBinaryLiterals) {
543 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
544 }
Dan Albert6805c2d2017-08-09 14:55:27 -0700545}
546
547template <typename T>
548void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
549 const wchar_t* max_str) {
550 if (std::is_signed<T>::value) {
551 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
552 } else {
553 // If the subject sequence begins with a <hyphen-minus>, the value resulting
554 // from the conversion shall be negated.
Elliott Hughes5f3b8e02024-08-14 14:50:59 +0000555 // https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/strtoul.html
Dan Albert6805c2d2017-08-09 14:55:27 -0700556 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
557 }
558 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
559}
560
561TEST(wchar, wcstol) {
562 TestWcsToInt(wcstol);
563}
564
565TEST(wchar, wcstol_limits) {
566 if (sizeof(long) == 8) {
567 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
568 } else {
569 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
570 }
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700571}
572
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700573TEST(wchar, wcstoul) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700574 TestWcsToInt(wcstoul);
575}
576
577TEST(wchar, wcstoul_limits) {
578 if (sizeof(long) == 8) {
579 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
580 } else {
581 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
582 }
583}
584
585TEST(wchar, wcstoll) {
586 TestWcsToInt(wcstoll);
587}
588
589TEST(wchar, wcstoll_limits) {
590 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700591}
592
593TEST(wchar, wcstoull) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700594 TestWcsToInt(wcstoull);
595}
596
597TEST(wchar, wcstoull_limits) {
598 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
599}
600
601TEST(wchar, wcstoimax) {
602 TestWcsToInt(wcstoimax);
603}
604
605TEST(wchar, wcstoimax_limits) {
606 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
607 L"9223372036854775808");
608}
609
610TEST(wchar, wcstoumax) {
611 TestWcsToInt(wcstoumax);
612}
613
614TEST(wchar, wcstoumax_limits) {
615 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700616}
617
618TEST(wchar, mbsnrtowcs) {
619 wchar_t dst[128];
620 const char* s = "hello, world!";
621 const char* src;
622
623 memset(dst, 0, sizeof(dst));
624 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700625 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700626
627 memset(dst, 0, sizeof(dst));
628 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700629 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700630 ASSERT_EQ(L'h', dst[0]);
631 ASSERT_EQ(L'e', dst[1]);
632 ASSERT_EQ(&s[2], src);
633
634 memset(dst, 0, sizeof(dst));
635 src = s;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700636 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700637 ASSERT_EQ(L'h', dst[0]);
638 ASSERT_EQ(L'e', dst[1]);
639 ASSERT_EQ(L'l', dst[2]);
640 ASSERT_EQ(&s[3], src);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700641
642 memset(dst, 0, sizeof(dst));
643 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
644 src = incomplete;
645 errno = 0;
646 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700647 ASSERT_ERRNO(EILSEQ);
Elliott Hughes697f42a2017-07-14 17:00:05 -0700648
649 src = incomplete;
650 errno = 0;
651 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700652 ASSERT_ERRNO(EILSEQ);
Elliott Hughes3d7a0d92014-04-29 14:46:56 -0700653}
Elliott Hughesefaa4612014-05-02 15:53:03 -0700654
Elliott Hughes3376c232018-02-13 23:14:12 -0800655TEST(wchar, wcsftime__wcsftime_l) {
Elliott Hughesefaa4612014-05-02 15:53:03 -0700656 setenv("TZ", "UTC", 1);
657
Christopher Ferris2a391882024-12-19 13:44:35 -0800658 struct tm t = {.tm_year = 200, .tm_mon = 2, .tm_mday = 10};
Elliott Hughesefaa4612014-05-02 15:53:03 -0700659 wchar_t buf[64];
660
661 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
662 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Dan Albert686e67d2023-08-03 19:00:09 +0000663 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -0800664 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
Elliott Hughesefaa4612014-05-02 15:53:03 -0700665}
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200666
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800667TEST(wchar, wmemmove_smoke) {
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200668 const wchar_t const_wstr[] = L"This is a test of something or other.....";
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800669 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200670
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800671 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200672 EXPECT_STREQ(const_wstr, wstr);
673
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800674 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
Christopher Ferris8bf50d52014-05-29 15:42:42 -0700675 EXPECT_STREQ(L"This This is a test of something or other", wstr);
Bernhard Rosenkraenzer6f2bde32014-05-23 17:44:18 +0200676}
Elliott Hughes69f05d22014-06-05 20:10:09 -0700677
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800678TEST(wchar, wmemcpy_smoke) {
679 const wchar_t src[] = L"Source string";
680 wchar_t dst[NUM_WCHARS(sizeof(src))];
681
682 EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
683 EXPECT_STREQ(dst, src);
684}
685
686TEST(wchar, wcpcpy_smoke) {
687 const wchar_t src[] = L"Source string";
688 wchar_t dst[NUM_WCHARS(sizeof(src))];
689
690 EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
691 EXPECT_STREQ(dst, src);
692}
693
694TEST(wchar, wcpncpy_smoke) {
695 const wchar_t src[] = L"Source string";
696 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
697
698 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
699 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
700 EXPECT_STREQ(dst, src);
701
702 EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
703 dst[6] = L'\0';
704 EXPECT_STREQ(dst, L"Source");
705
706 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
707 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
708 EXPECT_STREQ(dst, src);
709 EXPECT_EQ(dst[src_len], L'\0');
710 EXPECT_EQ(dst[src_len+1], L'\0');
711 EXPECT_EQ(dst[src_len+2], L'\0');
712 EXPECT_EQ(dst[src_len+3], L'\0');
713 EXPECT_EQ(dst[src_len+4], L'x');
714}
715
716TEST(wchar, wcscpy_smoke) {
717 const wchar_t src[] = L"Source string";
718 wchar_t dst[NUM_WCHARS(sizeof(src))];
719
720 EXPECT_EQ(dst, wcscpy(dst, src));
721 EXPECT_STREQ(src, dst);
722}
723
724TEST(wchar, wcsncpy_smoke) {
725 const wchar_t src[] = L"Source string";
726 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
727
728 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
729 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
730 EXPECT_STREQ(dst, src);
731
732 EXPECT_EQ(dst, wcsncpy(dst, src, 6));
733 dst[6] = L'\0';
734 EXPECT_STREQ(dst, L"Source");
Elliott Hughesfe50a0c2021-04-09 08:51:09 -0700735 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
736 EXPECT_STREQ(dst, L"Source");
Christopher Ferris5c7d9582014-11-13 15:48:39 -0800737
738 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
739 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
740 EXPECT_STREQ(dst, src);
741 EXPECT_EQ(dst[src_len], L'\0');
742 EXPECT_EQ(dst[src_len+1], L'\0');
743 EXPECT_EQ(dst[src_len+2], L'\0');
744 EXPECT_EQ(dst[src_len+3], L'\0');
745 EXPECT_EQ(dst[src_len+4], L'x');
746}
747
Elliott Hughes69f05d22014-06-05 20:10:09 -0700748TEST(wchar, mbrtowc_15439554) {
749 // http://b/15439554
750 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
751 uselocale(LC_GLOBAL_LOCALE);
752
753 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
754 ASSERT_GE(MB_CUR_MAX, 4U);
755
756 wchar_t wc;
757 size_t n;
758
759 // 1-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700760 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700761 EXPECT_EQ(1U, n);
762 EXPECT_EQ(L'x', wc);
763 // 2-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700764 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700765 EXPECT_EQ(2U, n);
766 EXPECT_EQ(L'¢', wc);
767 // 3-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700768 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700769 EXPECT_EQ(3U, n);
770 EXPECT_EQ(L'€', wc);
771 // 4-byte character.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700772 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700773 EXPECT_EQ(4U, n);
774 EXPECT_EQ(L'𤭢', wc);
775}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700776
777TEST(wchar, open_wmemstream) {
778 wchar_t* p = nullptr;
779 size_t size = 0;
780 FILE* fp = open_wmemstream(&p, &size);
781 ASSERT_NE(EOF, fputws(L"hello, world!", fp));
782 fclose(fp);
783
784 ASSERT_STREQ(L"hello, world!", p);
785 ASSERT_EQ(wcslen(L"hello, world!"), size);
786 free(p);
787}
788
789TEST(stdio, open_wmemstream_EINVAL) {
790#if defined(__BIONIC__)
791 wchar_t* p;
792 size_t size;
zijunzhao7ce2f952023-04-03 23:13:57 +0000793#pragma clang diagnostic push
794#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes6b841db2014-08-20 16:10:49 -0700795 // Invalid buffer.
796 errno = 0;
797 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
Elliott Hughes95646e62023-09-21 14:11:19 -0700798 ASSERT_ERRNO(EINVAL);
Elliott Hughes6b841db2014-08-20 16:10:49 -0700799
800 // Invalid size.
801 errno = 0;
802 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700803 ASSERT_ERRNO(EINVAL);
zijunzhao7ce2f952023-04-03 23:13:57 +0000804#pragma clang diagnostic pop
Elliott Hughes6b841db2014-08-20 16:10:49 -0700805#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800806 GTEST_SKIP() << "This test is bionic-specific";
Elliott Hughes6b841db2014-08-20 16:10:49 -0700807#endif
808}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700809
810TEST(wchar, wcstol_EINVAL) {
811 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700812 wcstol(L"123", nullptr, -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700813 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700814 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, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700819 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700820}
821
822TEST(wchar, wcstoll_EINVAL) {
823 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700824 wcstoll(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 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, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700831 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700832}
833
834TEST(wchar, wcstoul_EINVAL) {
835 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700836 wcstoul(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 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, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700843 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700844}
845
846TEST(wchar, wcstoull_EINVAL) {
847 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700848 wcstoull(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 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, 37);
Elliott Hughes95646e62023-09-21 14:11:19 -0700855 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700856}
857
858TEST(wchar, wcstoll_l_EINVAL) {
859 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000860 wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700861 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700862 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, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700867 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700868}
869
870TEST(wchar, wcstoull_l_EINVAL) {
871 errno = 0;
Dan Albert686e67d2023-08-03 19:00:09 +0000872 wcstoull_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 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, 37, SAFE_LC_GLOBAL_LOCALE);
Elliott Hughes95646e62023-09-21 14:11:19 -0700879 ASSERT_ERRNO(EINVAL);
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700880}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800881
882TEST(wchar, wmempcpy) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700883#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800884 wchar_t dst[6];
885 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
Colin Cross7da20342021-07-28 11:18:11 -0700886#else
887 GTEST_SKIP() << "musl doesn't have wmempcpy";
888#endif
Elliott Hughes3cfb52a2015-02-18 21:29:13 -0800889}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700890
891template <typename T>
Dan Albert6805c2d2017-08-09 14:55:27 -0700892using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700893
Dan Albert6805c2d2017-08-09 14:55:27 -0700894template <typename T>
895void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
896 T expected_value, ptrdiff_t expected_len) {
Dan Albertf6346552016-12-02 12:02:03 -0800897 wchar_t* p;
Dan Albert6805c2d2017-08-09 14:55:27 -0700898 ASSERT_EQ(expected_value, fn(str, &p));
899 ASSERT_EQ(expected_len, p - str);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700900}
901
Dan Albert6805c2d2017-08-09 14:55:27 -0700902template <typename T>
903void TestWcsToFloat(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800904 TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
905 TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
906 TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
907 TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
908 TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
909 TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
Dan Albert6805c2d2017-08-09 14:55:27 -0700910}
911
912template <typename T>
913void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
Logan Chien0288dbb2017-08-22 17:52:02 +0800914 TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
915 TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
916 TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
917 TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
Dan Albert6805c2d2017-08-09 14:55:27 -0700918}
919
920template <typename T>
921void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
922 ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
923 ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
924 ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
925
926 ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
927 ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
928 ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
929
930 wchar_t* p;
931 ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
932 ASSERT_STREQ(L"ny", p);
933 ASSERT_TRUE(isnan(fn(L"nanny", &p)));
934 ASSERT_STREQ(L"ny", p);
935 ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
936 ASSERT_STREQ(L"ny", p);
937
938 ASSERT_EQ(0, fn(L"muppet", &p));
939 ASSERT_STREQ(L"muppet", p);
940 ASSERT_EQ(0, fn(L" muppet", &p));
941 ASSERT_STREQ(L" muppet", p);
942
943 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
944 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
945 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
946
947 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
948 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
949 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
950
951 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
952 ASSERT_STREQ(L"initude", p);
953 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
954 ASSERT_STREQ(L"initude", p);
955 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
956 ASSERT_STREQ(L"initude", p);
957
958 // Check case-insensitivity.
959 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
960 ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700961}
962
963TEST(wchar, wcstof) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700964 TestWcsToFloat(wcstof);
965}
966
967TEST(wchar, wcstof_hex_floats) {
968 TestWcsToFloatHexFloats(wcstof);
969}
970
971TEST(wchar, wcstof_hex_inf_nan) {
972 TestWcsToFloatInfNan(wcstof);
973}
974
975TEST(wchar, wcstod) {
976 TestWcsToFloat(wcstod);
977}
978
979TEST(wchar, wcstod_hex_floats) {
980 TestWcsToFloatHexFloats(wcstod);
981}
982
983TEST(wchar, wcstod_hex_inf_nan) {
984 TestWcsToFloatInfNan(wcstod);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700985}
986
987TEST(wchar, wcstold) {
Dan Albert6805c2d2017-08-09 14:55:27 -0700988 TestWcsToFloat(wcstold);
989}
990
991TEST(wchar, wcstold_hex_floats) {
992 TestWcsToFloatHexFloats(wcstold);
993}
994
995TEST(wchar, wcstold_hex_inf_nan) {
996 TestWcsToFloatInfNan(wcstold);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700997}
Elliott Hughesc41b5602017-07-27 17:08:08 -0700998
Elliott Hughes3376c232018-02-13 23:14:12 -0800999TEST(wchar, wcstod_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001000#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001001 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001002#else
1003 GTEST_SKIP() << "musl doesn't have wcstod_l";
1004#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001005}
1006
1007TEST(wchar, wcstof_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001008#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001009 EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001010#else
1011 GTEST_SKIP() << "musl doesn't have wcstof_l";
1012#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001013}
1014
1015TEST(wchar, wcstol_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001016#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001017 EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001018#else
1019 GTEST_SKIP() << "musl doesn't have wcstol_l";
1020#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001021}
1022
1023TEST(wchar, wcstold_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001024 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001025}
1026
1027TEST(wchar, wcstoll_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001028 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001029}
1030
1031TEST(wchar, wcstoul_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001032#if !defined(ANDROID_HOST_MUSL)
Dan Albert686e67d2023-08-03 19:00:09 +00001033 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Colin Cross7da20342021-07-28 11:18:11 -07001034#else
1035 GTEST_SKIP() << "musl doesn't have wcstoul_l";
1036#endif
Elliott Hughes3376c232018-02-13 23:14:12 -08001037}
1038
1039TEST(wchar, wcstoull_l) {
Dan Albert686e67d2023-08-03 19:00:09 +00001040 EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
Elliott Hughes3376c232018-02-13 23:14:12 -08001041}
1042
Elliott Hughesc41b5602017-07-27 17:08:08 -07001043static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
1044 for (wchar_t i = begin; i < end; ++i) {
1045 EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
1046 }
1047}
1048
1049TEST(wchar, wcwidth_NUL) {
1050 // NUL is defined to return 0 rather than -1, despite being a C0 control.
1051 EXPECT_EQ(0, wcwidth(0));
1052}
1053
1054TEST(wchar, wcwidth_ascii) {
1055 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1056}
1057
1058TEST(wchar, wcwidth_controls) {
1059 AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
1060 EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
1061 AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
1062}
1063
1064TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
1065 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1066 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
Elliott Hughesc41b5602017-07-27 17:08:08 -07001067 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1068}
1069
Elliott Hughes20a9f992024-05-29 21:45:51 +00001070TEST(wchar, wcwidth_non_spacing_special_cases) {
Elliott Hughes20a9f992024-05-29 21:45:51 +00001071 // U+00AD is a soft hyphen, which normally shouldn't be rendered at all.
1072 // I think the assumption here is that you elide the soft hyphen character
1073 // completely in that case, and never call wcwidth() if you don't want to
1074 // render it as an actual hyphen. Whereas if you do want to render it,
1075 // you call wcwidth(), and 1 is the right answer. This is what Markus Kuhn's
1076 // original https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c did,
1077 // and glibc and iOS do the same.
1078 // See also: https://en.wikipedia.org/wiki/Soft_hyphen#Text_to_be_formatted_by_the_recipient
1079 EXPECT_EQ(1, wcwidth(0x00ad)); // Soft hyphen (SHY).
1080
1081 // U+115F is the Hangeul choseong filler (for a degenerate composed
1082 // character missing an initial consonant (as opposed to one with a
1083 // leading ieung). Since the code points for combining jungseong (medial
1084 // vowels) and jongseong (trailing consonants) have width 0, the choseong
1085 // (initial consonant) has width 2 to cover the entire syllable. So unless
1086 // U+115f has width 2, a degenerate composed "syllable" without an initial
1087 // consonant or ieung would have a total width of 0, which is silly.
1088 // The following sequence is effectively "약" without the leading ieung...
1089 EXPECT_EQ(2, wcwidth(0x115f)); // Hangeul choseong filler.
1090 EXPECT_EQ(0, wcwidth(0x1163)); // Hangeul jungseong "ya".
1091 EXPECT_EQ(0, wcwidth(0x11a8)); // Hangeul jongseong "kiyeok".
1092
1093 // U+1160, the jungseong filler, has width 0 because it must have been
1094 // preceded by either a choseong or choseong filler.
1095 EXPECT_EQ(0, wcwidth(0x1160));
1096}
1097
Elliott Hughesc41b5602017-07-27 17:08:08 -07001098TEST(wchar, wcwidth_cjk) {
1099 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1100 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1101 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1102 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1103 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1104 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1105}
1106
1107TEST(wchar, wcwidth_korean_combining_jamo) {
1108 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1109 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1110 EXPECT_EQ(0, wcwidth(0xd7cb));
1111}
1112
1113TEST(wchar, wcwidth_korean_jeongeul_syllables) {
1114 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
Elliott Hughes20a9f992024-05-29 21:45:51 +00001115 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points as of Unicode 15.
1116
1117 // Undefined characters at the end of the block currently have width 1,
1118 // but since they're undefined, we don't test that.
Elliott Hughesc41b5602017-07-27 17:08:08 -07001119}
1120
1121TEST(wchar, wcwidth_kana) {
1122 // Hiragana (most, not undefined).
1123 AssertWcwidthRange(0x3041, 0x3097, 2);
1124 // Katakana.
1125 AssertWcwidthRange(0x30a0, 0x3100, 2);
1126}
1127
1128TEST(wchar, wcwidth_circled_two_digit_cjk) {
1129 // Circled two-digit CJK "speed sign" numbers are wide,
1130 // though EastAsianWidth is ambiguous.
1131 AssertWcwidthRange(0x3248, 0x3250, 2);
1132}
1133
1134TEST(wchar, wcwidth_hexagrams) {
1135 // Hexagrams are wide, though EastAsianWidth is neutral.
1136 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1137}
1138
1139TEST(wchar, wcwidth_default_ignorables) {
1140 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1141 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1142}
1143
Elliott Hughes20a9f992024-05-29 21:45:51 +00001144TEST(wchar, wcwidth_hangeul_compatibility_jamo) {
Elliott Hughes20a9f992024-05-29 21:45:51 +00001145 // These are actually the *compatibility* jamo code points, *not* the regular
1146 // jamo code points (U+1100-U+11FF) using a jungseong filler. If you use the
1147 // Android IME to type any of these, you get these code points.
1148
1149 // (Half of) the Korean "crying" emoticon "ㅠㅠ".
1150 // Actually U+3160 "Hangeul Letter Yu" from Hangeul Compatibility Jamo.
1151 EXPECT_EQ(2, wcwidth(L'ㅠ'));
1152 // The two halves of the Korean internet shorthand "ㄱㅅ" (short for 감사).
1153 // Actually U+3131 "Hangeul Letter Kiyeok" and U+3145 "Hangeul Letter Sios"
1154 // from Hangeul Compatibility Jamo.
1155 EXPECT_EQ(2, wcwidth(L'ㄱ'));
1156 EXPECT_EQ(2, wcwidth(L'ㅅ'));
Elliott Hughesc41b5602017-07-27 17:08:08 -07001157}
Elliott Hughesfe50a0c2021-04-09 08:51:09 -07001158
1159TEST(wchar, wcswidth) {
1160 EXPECT_EQ(2, wcswidth(L"abc", 2));
1161 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1162 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1163}
1164
1165TEST(wchar, wcslcpy) {
1166#if defined(__BIONIC__)
1167 wchar_t dst[32];
1168 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1169 ASSERT_STREQ(L"he", dst);
1170 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1171 ASSERT_STREQ(L"hello world", dst);
1172#else
1173 GTEST_SKIP() << "no wcslcpy in glibc";
1174#endif
1175}
1176
1177TEST(wchar, wcscat) {
1178 wchar_t dst[32];
1179 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1180 ASSERT_STREQ(dst, L"hello");
1181 ASSERT_EQ(dst, wcscat(dst, L" world"));
1182 ASSERT_STREQ(dst, L"hello world");
1183}
1184
1185TEST(wchar, wcscpy) {
1186 wchar_t dst[32];
1187 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1188 ASSERT_STREQ(dst, L"hello");
1189 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1190 ASSERT_STREQ(dst, L"world");
1191}
1192
1193TEST(wchar, wcscasecmp) {
1194 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1195 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1196 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1197 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1198 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1199}
1200
1201TEST(wchar, wcscspn) {
1202 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1203 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1204 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1205}
1206
1207TEST(wchar, wcsspn) {
1208 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1209 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1210 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1211}
1212
1213TEST(wchar, wcsdup) {
1214 wchar_t* s = wcsdup(L"hello");
1215 ASSERT_STREQ(s, L"hello");
1216 free(s);
1217}
1218
1219TEST(wchar, wcslcat) {
1220#if defined(__BIONIC__)
1221 wchar_t dst[4] = {};
1222 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1223 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1224 ASSERT_STREQ(dst, L"abc");
1225#else
1226 GTEST_SKIP() << "no wcslcpy in glibc";
1227#endif
1228}
1229
1230TEST(wchar, wcsncasecmp) {
1231 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1232
1233 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1234 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1235 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1236 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1237 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1238}
1239
1240TEST(wchar, wcsncat) {
1241 wchar_t dst[32];
1242 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1243 ASSERT_STREQ(dst, L"hello");
1244 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1245 ASSERT_STREQ(dst, L"hello");
1246 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1247 ASSERT_STREQ(dst, L"hello, world!");
1248}
1249
1250TEST(wchar, wcsncmp) {
1251 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1252 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1253 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1254 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1255}
1256
1257TEST(wchar, wcsnlen) {
1258 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1259 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1260 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1261}
1262
1263TEST(wchar, wcspbrk) {
1264 const wchar_t* s = L"hello, world!";
1265 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1266 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1267 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1268 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1269}
1270
1271TEST(wchar, wcstok) {
1272 wchar_t s[] = L"this is\ta\nstring";
1273 wchar_t* p;
1274 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1275 ASSERT_STREQ(s, L"this");
1276 ASSERT_STREQ(p, L"is\ta\nstring");
1277 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1278 ASSERT_STREQ(s + 5, L"is");
1279 ASSERT_STREQ(p, L"a\nstring");
1280 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1281 ASSERT_STREQ(s + 8, L"a");
1282 ASSERT_STREQ(p, L"string");
1283 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1284 ASSERT_STREQ(s + 10, L"string");
1285 ASSERT_EQ(nullptr, p);
1286}
1287
1288TEST(wchar, wmemchr) {
1289 const wchar_t* s = L"hello, world!";
1290 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1291 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1292 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1293 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1294}
1295
1296TEST(wchar, wmemcmp) {
1297 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1298 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1299 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1300}
1301
1302TEST(wchar, wmemcpy) {
1303 wchar_t dst[32] = {};
1304 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1305 ASSERT_STREQ(dst, L"hello");
1306}
1307
1308TEST(wchar, wmemmove) {
1309 wchar_t dst[32] = {};
1310 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1311 ASSERT_STREQ(dst, L"hello");
1312}
1313
1314TEST(wchar, wmemset) {
1315 wchar_t dst[4] = {};
1316 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1317 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1318 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1319 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1320 ASSERT_EQ(dst[3], wchar_t(0));
1321 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1322 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1323}