1 /*
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
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <limits.h>
22 #include <locale.h>
23 #include <math.h>
24 #include <stdint.h>
25 #include <sys/cdefs.h>
26 #include <wchar.h>
27
28 #include "utils.h"
29
30 #define NUM_WCHARS(num_bytes) ((num_bytes)/sizeof(wchar_t))
31
32 #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.
36 static locale_t SAFE_LC_GLOBAL_LOCALE = duplocale(LC_GLOBAL_LOCALE);
37 #else
38 static locale_t SAFE_LC_GLOBAL_LOCALE = LC_GLOBAL_LOCALE;
39 #endif
40
41 // 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)
50 constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
51 #elif defined(__GLIBC__)
52 constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
53 #else
54 #error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
55 #endif
56
57 #if defined(__GLIBC__)
58 constexpr bool kLibcSupportsParsingBinaryLiterals = __GLIBC_PREREQ(2, 38);
59 #else
60 constexpr bool kLibcSupportsParsingBinaryLiterals = true;
61 #endif
62
TEST(wchar,sizeof_wchar_t)63 TEST(wchar, sizeof_wchar_t) {
64 EXPECT_EQ(4U, sizeof(wchar_t));
65 EXPECT_EQ(4U, sizeof(wint_t));
66 }
67
TEST(wchar,mbrlen)68 TEST(wchar, mbrlen) {
69 char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
70 EXPECT_EQ(static_cast<size_t>(-2), mbrlen(&bytes[0], 0, nullptr));
71 EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
72
73 EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
74 EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
75 }
76
TEST(wchar,wctomb_wcrtomb)77 TEST(wchar, wctomb_wcrtomb) {
78 // wctomb and wcrtomb behave differently when s == NULL.
79 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));
83
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'));
88 EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
89
90 // ...and for regular characters.
91 memset(bytes, 0, sizeof(bytes));
92 EXPECT_EQ(1, wctomb(bytes, L'h'));
93 EXPECT_EQ('h', bytes[0]);
94 memset(bytes, 0, sizeof(bytes));
95 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
96 EXPECT_EQ('h', bytes[0]);
97
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));
103 EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
104 EXPECT_EQ('h', bytes[0]);
105 // 2-byte UTF-8.
106 memset(bytes, 0, sizeof(bytes));
107 EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
108 EXPECT_EQ('\xc2', bytes[0]);
109 EXPECT_EQ('\xa2', bytes[1]);
110 // 3-byte UTF-8.
111 memset(bytes, 0, sizeof(bytes));
112 EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
113 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));
118 EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
119 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.
124 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
125 EXPECT_ERRNO(EILSEQ);
126 }
127
TEST(wchar,wcrtomb_start_state)128 TEST(wchar, wcrtomb_start_state) {
129 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
130 uselocale(LC_GLOBAL_LOCALE);
131
132 char out[MB_LEN_MAX];
133 mbstate_t ps = {};
134
135 // Any non-initial state is invalid when calling wcrtomb.
136 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
137 EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
138 EXPECT_ERRNO(EILSEQ);
139
140 // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
141 // state should be reset.
142 ps = {};
143 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
144 EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
145 EXPECT_TRUE(mbsinit(&ps));
146
147 ps = {};
148 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
149 EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
150 EXPECT_TRUE(mbsinit(&ps));
151 }
152
TEST(wchar,wcstombs_wcrtombs)153 TEST(wchar, wcstombs_wcrtombs) {
154 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
155 uselocale(LC_GLOBAL_LOCALE);
156
157 const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
158 const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
159 const wchar_t* src;
160 char bytes[BUFSIZ];
161
162 // Given a NULL destination, these functions count valid characters.
163 EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
164 EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
165 EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
166 src = chars;
167 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
168 EXPECT_EQ(&chars[0], src);
169 src = chars;
170 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
171 EXPECT_EQ(&chars[0], src);
172 src = chars;
173 EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
174 EXPECT_EQ(&chars[0], src);
175
176 // An unrepresentable char just returns an error from wcstombs...
177 errno = 0;
178 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
179 EXPECT_ERRNO(EILSEQ);
180 errno = 0;
181 EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
182 EXPECT_ERRNO(EILSEQ);
183
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;
188 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
189 EXPECT_EQ(&bad_chars[0], src);
190 EXPECT_ERRNO(EILSEQ);
191 errno = 0;
192 src = bad_chars;
193 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
194 EXPECT_EQ(&bad_chars[0], src);
195 EXPECT_ERRNO(EILSEQ);
196
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));
213 EXPECT_ERRNO(EILSEQ);
214 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;
220 EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
221 EXPECT_EQ(&chars[0], src); // No input consumed.
222 EXPECT_ERRNO(EILSEQ);
223
224 memset(bytes, 'x', sizeof(bytes));
225 src = chars;
226 EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
227 EXPECT_EQ(&chars[4], src); // Some input consumed.
228 EXPECT_ERRNO(EILSEQ);
229 bytes[5] = 0;
230 EXPECT_STREQ("hellx", bytes);
231
232 memset(bytes, 'x', sizeof(bytes));
233 src = chars;
234 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
235 EXPECT_EQ(nullptr, src); // All input consumed!
236 EXPECT_ERRNO(EILSEQ);
237 EXPECT_STREQ("hello", bytes);
238
239 memset(bytes, 'x', sizeof(bytes));
240 src = chars;
241 EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
242 EXPECT_EQ(nullptr, src); // All input consumed.
243 EXPECT_ERRNO(EILSEQ);
244 EXPECT_STREQ("hello", bytes);
245
246 memset(bytes, 'x', sizeof(bytes));
247 src = bad_chars;
248 EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
249 EXPECT_EQ(&bad_chars[2], src);
250 EXPECT_ERRNO(EILSEQ);
251 bytes[3] = 0;
252 EXPECT_STREQ("hix", bytes);
253
254 // Any non-initial state is invalid when calling wcsrtombs.
255 mbstate_t ps = {};
256 src = chars;
257 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));
259 EXPECT_ERRNO(EILSEQ);
260 }
261
TEST(wchar,limits)262 TEST(wchar, limits) {
263 ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
264 }
265
TEST(wchar,wcsstr)266 TEST(wchar, wcsstr) {
267 const wchar_t* haystack = L"big daddy/giant haystacks!";
268 const wchar_t* empty_haystack = L"";
269
270 // 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"));
283 }
284
TEST(wchar,wcsstr_80199)285 TEST(wchar, wcsstr_80199) {
286 // https://code.google.com/p/android/issues/detail?id=80199
287 ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
288 }
289
TEST(wchar,mbtowc)290 TEST(wchar, mbtowc) {
291 wchar_t out[8];
292
293 // mbtowc and all the mbrto* APIs behave slightly differently when n is 0:
294 //
295 // mbrtowc returns 0 "if the next n or fewer bytes complete the multibyte
296 // character that corresponds to the null wide character"
297 //
298 // mbrtoc (C23 7.24.7.2.4) says:
299 //
300 // 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).
305 //
306 // 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.
312 #ifdef __GLIBC__
313 int expected_result_for_zero_length_empty_string = 0;
314 #else
315 int expected_result_for_zero_length_empty_string = -1;
316 #endif
317
318 out[0] = 'x';
319 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
320 EXPECT_EQ('x', out[0]);
321
322 EXPECT_EQ(-1, mbtowc(out, "hello", 0));
323 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(out, "", 0));
324 EXPECT_EQ(1, mbtowc(out, "hello", 1));
325 EXPECT_EQ(L'h', out[0]);
326
327 EXPECT_EQ(-1, mbtowc(nullptr, "hello", 0));
328 EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(nullptr, "", 0));
329 EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
330
331 EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
332 }
333
TEST(wchar,mbrtowc)334 TEST(wchar, mbrtowc) {
335 wchar_t out[8];
336
337 out[0] = 'x';
338 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
339 EXPECT_EQ('x', out[0]);
340
341 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
342 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "", 0, nullptr));
343 EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
344 EXPECT_EQ(L'h', out[0]);
345
346 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "hello", 0, nullptr));
347 EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "", 0, nullptr));
348 EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
349
350 EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
351
352 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
353 uselocale(LC_GLOBAL_LOCALE);
354
355 // 1-byte UTF-8.
356 EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
357 EXPECT_EQ(L'a', out[0]);
358 // 2-byte UTF-8.
359 EXPECT_EQ(2U, mbrtowc(out,
360 "\xc2\xa2"
361 "cdef",
362 6, nullptr));
363 EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
364 // 3-byte UTF-8.
365 EXPECT_EQ(3U, mbrtowc(out,
366 "\xe2\x82\xac"
367 "def",
368 6, nullptr));
369 EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
370 // 4-byte UTF-8.
371 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]);
376 #if defined(__BIONIC__) // glibc allows this.
377 // Illegal 5-byte UTF-8.
378 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
379 "\xf8\xa1\xa2\xa3\xa4"
380 "f",
381 6, nullptr));
382 EXPECT_ERRNO(EILSEQ);
383 #endif
384 // Illegal over-long sequence.
385 EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
386 "\xf0\x82\x82\xac"
387 "ef",
388 6, nullptr));
389 EXPECT_ERRNO(EILSEQ);
390 }
391
TEST(wchar,mbrtowc_valid_non_characters)392 TEST(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
TEST(wchar,mbrtowc_out_of_range)404 TEST(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;
410 auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
411 if (kLibcRejectsOverLongUtf8Sequences) {
412 ASSERT_EQ(static_cast<size_t>(-1), result);
413 ASSERT_ERRNO(EILSEQ);
414 } else {
415 ASSERT_EQ(4U, result);
416 ASSERT_ERRNO(0);
417 }
418 }
419
test_mbrtowc_incomplete(mbstate_t * ps)420 static void test_mbrtowc_incomplete(mbstate_t* ps) {
421 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));
428 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
429 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));
434 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
435 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));
440 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
441 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));
446 ASSERT_ERRNO(EILSEQ);
447 }
448
TEST(wchar,mbrtowc_incomplete)449 TEST(wchar, mbrtowc_incomplete) {
450 mbstate_t ps = {};
451
452 test_mbrtowc_incomplete(&ps);
453 test_mbrtowc_incomplete(nullptr);
454 }
455
test_mbsrtowcs(mbstate_t * ps)456 static 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";
460 wchar_t out[4];
461
462 const char* valid = VALID;
463 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
464 ASSERT_EQ(L'A', out[0]);
465 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]);
468 // Check that valid has advanced to the next unread character.
469 ASSERT_EQ('e', *valid);
470
471 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
472 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.
479 ASSERT_EQ(nullptr, valid);
480
481 const char* invalid = INVALID;
482 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
483 EXPECT_ERRNO(EILSEQ);
484 ASSERT_EQ('\xc2', *invalid);
485
486 const char* incomplete = INCOMPLETE;
487 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
488 EXPECT_ERRNO(EILSEQ);
489 ASSERT_EQ('\xc2', *incomplete);
490
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);
502 }
503
TEST(wchar,mbsrtowcs)504 TEST(wchar, mbsrtowcs) {
505 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
506 uselocale(LC_GLOBAL_LOCALE);
507
508 mbstate_t ps = {};
509 test_mbsrtowcs(&ps);
510 test_mbsrtowcs(nullptr);
511
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));
517 EXPECT_ERRNO(EILSEQ);
518 ASSERT_EQ('\x20', *invalid);
519 }
520
521 template <typename T>
522 using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
523
524 template <typename T>
TestSingleWcsToInt(WcsToIntFn<T> fn,const wchar_t * str,int base,T expected_value,ptrdiff_t expected_len)525 void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
526 T expected_value, ptrdiff_t expected_len) {
527 wchar_t* p;
528 EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
529 EXPECT_EQ(expected_len, p - str) << str << " " << base;
530 }
531
532 template <typename T>
TestWcsToInt(WcsToIntFn<T> fn)533 void 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);
542 if (kLibcSupportsParsingBinaryLiterals) {
543 TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
544 }
545 }
546
547 template <typename T>
TestWcsToIntLimits(WcsToIntFn<T> fn,const wchar_t * min_str,const wchar_t * max_str)548 void 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.
555 // https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/strtoul.html
556 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
TEST(wchar,wcstol)561 TEST(wchar, wcstol) {
562 TestWcsToInt(wcstol);
563 }
564
TEST(wchar,wcstol_limits)565 TEST(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 }
571 }
572
TEST(wchar,wcstoul)573 TEST(wchar, wcstoul) {
574 TestWcsToInt(wcstoul);
575 }
576
TEST(wchar,wcstoul_limits)577 TEST(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
TEST(wchar,wcstoll)585 TEST(wchar, wcstoll) {
586 TestWcsToInt(wcstoll);
587 }
588
TEST(wchar,wcstoll_limits)589 TEST(wchar, wcstoll_limits) {
590 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
591 }
592
TEST(wchar,wcstoull)593 TEST(wchar, wcstoull) {
594 TestWcsToInt(wcstoull);
595 }
596
TEST(wchar,wcstoull_limits)597 TEST(wchar, wcstoull_limits) {
598 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
599 }
600
TEST(wchar,wcstoimax)601 TEST(wchar, wcstoimax) {
602 TestWcsToInt(wcstoimax);
603 }
604
TEST(wchar,wcstoimax_limits)605 TEST(wchar, wcstoimax_limits) {
606 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
607 L"9223372036854775808");
608 }
609
TEST(wchar,wcstoumax)610 TEST(wchar, wcstoumax) {
611 TestWcsToInt(wcstoumax);
612 }
613
TEST(wchar,wcstoumax_limits)614 TEST(wchar, wcstoumax_limits) {
615 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
616 }
617
TEST(wchar,mbsnrtowcs)618 TEST(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;
625 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
626
627 memset(dst, 0, sizeof(dst));
628 src = s;
629 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
630 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;
636 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
637 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);
641
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));
647 ASSERT_ERRNO(EILSEQ);
648
649 src = incomplete;
650 errno = 0;
651 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
652 ASSERT_ERRNO(EILSEQ);
653 }
654
TEST(wchar,wcsftime__wcsftime_l)655 TEST(wchar, wcsftime__wcsftime_l) {
656 setenv("TZ", "UTC", 1);
657
658 struct tm t = {.tm_year = 200, .tm_mon = 2, .tm_mday = 10};
659 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);
663 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
664 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
665 }
666
TEST(wchar,wmemmove_smoke)667 TEST(wchar, wmemmove_smoke) {
668 const wchar_t const_wstr[] = L"This is a test of something or other.....";
669 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
670
671 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
672 EXPECT_STREQ(const_wstr, wstr);
673
674 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
675 EXPECT_STREQ(L"This This is a test of something or other", wstr);
676 }
677
TEST(wchar,wmemcpy_smoke)678 TEST(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
TEST(wchar,wcpcpy_smoke)686 TEST(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
TEST(wchar,wcpncpy_smoke)694 TEST(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
TEST(wchar,wcscpy_smoke)716 TEST(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
TEST(wchar,wcsncpy_smoke)724 TEST(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");
735 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
736 EXPECT_STREQ(dst, L"Source");
737
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
TEST(wchar,mbrtowc_15439554)748 TEST(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.
760 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
761 EXPECT_EQ(1U, n);
762 EXPECT_EQ(L'x', wc);
763 // 2-byte character.
764 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
765 EXPECT_EQ(2U, n);
766 EXPECT_EQ(L'¢', wc);
767 // 3-byte character.
768 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
769 EXPECT_EQ(3U, n);
770 EXPECT_EQ(L'€', wc);
771 // 4-byte character.
772 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
773 EXPECT_EQ(4U, n);
774 EXPECT_EQ(L'', wc);
775 }
776
TEST(wchar,open_wmemstream)777 TEST(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
TEST(stdio,open_wmemstream_EINVAL)789 TEST(stdio, open_wmemstream_EINVAL) {
790 #if defined(__BIONIC__)
791 wchar_t* p;
792 size_t size;
793 #pragma clang diagnostic push
794 #pragma clang diagnostic ignored "-Wnonnull"
795 // Invalid buffer.
796 errno = 0;
797 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
798 ASSERT_ERRNO(EINVAL);
799
800 // Invalid size.
801 errno = 0;
802 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
803 ASSERT_ERRNO(EINVAL);
804 #pragma clang diagnostic pop
805 #else
806 GTEST_SKIP() << "This test is bionic-specific";
807 #endif
808 }
809
TEST(wchar,wcstol_EINVAL)810 TEST(wchar, wcstol_EINVAL) {
811 errno = 0;
812 wcstol(L"123", nullptr, -1);
813 ASSERT_ERRNO(EINVAL);
814 errno = 0;
815 wcstol(L"123", nullptr, 1);
816 ASSERT_ERRNO(EINVAL);
817 errno = 0;
818 wcstol(L"123", nullptr, 37);
819 ASSERT_ERRNO(EINVAL);
820 }
821
TEST(wchar,wcstoll_EINVAL)822 TEST(wchar, wcstoll_EINVAL) {
823 errno = 0;
824 wcstoll(L"123", nullptr, -1);
825 ASSERT_ERRNO(EINVAL);
826 errno = 0;
827 wcstoll(L"123", nullptr, 1);
828 ASSERT_ERRNO(EINVAL);
829 errno = 0;
830 wcstoll(L"123", nullptr, 37);
831 ASSERT_ERRNO(EINVAL);
832 }
833
TEST(wchar,wcstoul_EINVAL)834 TEST(wchar, wcstoul_EINVAL) {
835 errno = 0;
836 wcstoul(L"123", nullptr, -1);
837 ASSERT_ERRNO(EINVAL);
838 errno = 0;
839 wcstoul(L"123", nullptr, 1);
840 ASSERT_ERRNO(EINVAL);
841 errno = 0;
842 wcstoul(L"123", nullptr, 37);
843 ASSERT_ERRNO(EINVAL);
844 }
845
TEST(wchar,wcstoull_EINVAL)846 TEST(wchar, wcstoull_EINVAL) {
847 errno = 0;
848 wcstoull(L"123", nullptr, -1);
849 ASSERT_ERRNO(EINVAL);
850 errno = 0;
851 wcstoull(L"123", nullptr, 1);
852 ASSERT_ERRNO(EINVAL);
853 errno = 0;
854 wcstoull(L"123", nullptr, 37);
855 ASSERT_ERRNO(EINVAL);
856 }
857
TEST(wchar,wcstoll_l_EINVAL)858 TEST(wchar, wcstoll_l_EINVAL) {
859 errno = 0;
860 wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
861 ASSERT_ERRNO(EINVAL);
862 errno = 0;
863 wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
864 ASSERT_ERRNO(EINVAL);
865 errno = 0;
866 wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
867 ASSERT_ERRNO(EINVAL);
868 }
869
TEST(wchar,wcstoull_l_EINVAL)870 TEST(wchar, wcstoull_l_EINVAL) {
871 errno = 0;
872 wcstoull_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
873 ASSERT_ERRNO(EINVAL);
874 errno = 0;
875 wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
876 ASSERT_ERRNO(EINVAL);
877 errno = 0;
878 wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
879 ASSERT_ERRNO(EINVAL);
880 }
881
TEST(wchar,wmempcpy)882 TEST(wchar, wmempcpy) {
883 #if !defined(ANDROID_HOST_MUSL)
884 wchar_t dst[6];
885 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
886 #else
887 GTEST_SKIP() << "musl doesn't have wmempcpy";
888 #endif
889 }
890
891 template <typename T>
892 using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
893
894 template <typename T>
TestSingleWcsToFloat(WcsToFloatFn<T> fn,const wchar_t * str,T expected_value,ptrdiff_t expected_len)895 void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
896 T expected_value, ptrdiff_t expected_len) {
897 wchar_t* p;
898 ASSERT_EQ(expected_value, fn(str, &p));
899 ASSERT_EQ(expected_len, p - str);
900 }
901
902 template <typename T>
TestWcsToFloat(WcsToFloatFn<T> fn)903 void TestWcsToFloat(WcsToFloatFn<T> fn) {
904 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);
910 }
911
912 template <typename T>
TestWcsToFloatHexFloats(WcsToFloatFn<T> fn)913 void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
914 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);
918 }
919
920 template <typename T>
TestWcsToFloatInfNan(WcsToFloatFn<T> fn)921 void 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)));
961 }
962
TEST(wchar,wcstof)963 TEST(wchar, wcstof) {
964 TestWcsToFloat(wcstof);
965 }
966
TEST(wchar,wcstof_hex_floats)967 TEST(wchar, wcstof_hex_floats) {
968 TestWcsToFloatHexFloats(wcstof);
969 }
970
TEST(wchar,wcstof_hex_inf_nan)971 TEST(wchar, wcstof_hex_inf_nan) {
972 TestWcsToFloatInfNan(wcstof);
973 }
974
TEST(wchar,wcstod)975 TEST(wchar, wcstod) {
976 TestWcsToFloat(wcstod);
977 }
978
TEST(wchar,wcstod_hex_floats)979 TEST(wchar, wcstod_hex_floats) {
980 TestWcsToFloatHexFloats(wcstod);
981 }
982
TEST(wchar,wcstod_hex_inf_nan)983 TEST(wchar, wcstod_hex_inf_nan) {
984 TestWcsToFloatInfNan(wcstod);
985 }
986
TEST(wchar,wcstold)987 TEST(wchar, wcstold) {
988 TestWcsToFloat(wcstold);
989 }
990
TEST(wchar,wcstold_hex_floats)991 TEST(wchar, wcstold_hex_floats) {
992 TestWcsToFloatHexFloats(wcstold);
993 }
994
TEST(wchar,wcstold_hex_inf_nan)995 TEST(wchar, wcstold_hex_inf_nan) {
996 TestWcsToFloatInfNan(wcstold);
997 }
998
TEST(wchar,wcstod_l)999 TEST(wchar, wcstod_l) {
1000 #if !defined(ANDROID_HOST_MUSL)
1001 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
1002 #else
1003 GTEST_SKIP() << "musl doesn't have wcstod_l";
1004 #endif
1005 }
1006
TEST(wchar,wcstof_l)1007 TEST(wchar, wcstof_l) {
1008 #if !defined(ANDROID_HOST_MUSL)
1009 EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
1010 #else
1011 GTEST_SKIP() << "musl doesn't have wcstof_l";
1012 #endif
1013 }
1014
TEST(wchar,wcstol_l)1015 TEST(wchar, wcstol_l) {
1016 #if !defined(ANDROID_HOST_MUSL)
1017 EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1018 #else
1019 GTEST_SKIP() << "musl doesn't have wcstol_l";
1020 #endif
1021 }
1022
TEST(wchar,wcstold_l)1023 TEST(wchar, wcstold_l) {
1024 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
1025 }
1026
TEST(wchar,wcstoll_l)1027 TEST(wchar, wcstoll_l) {
1028 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1029 }
1030
TEST(wchar,wcstoul_l)1031 TEST(wchar, wcstoul_l) {
1032 #if !defined(ANDROID_HOST_MUSL)
1033 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1034 #else
1035 GTEST_SKIP() << "musl doesn't have wcstoul_l";
1036 #endif
1037 }
1038
TEST(wchar,wcstoull_l)1039 TEST(wchar, wcstoull_l) {
1040 EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1041 }
1042
AssertWcwidthRange(wchar_t begin,wchar_t end,int expected)1043 static 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
TEST(wchar,wcwidth_NUL)1049 TEST(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
TEST(wchar,wcwidth_ascii)1054 TEST(wchar, wcwidth_ascii) {
1055 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1056 }
1057
TEST(wchar,wcwidth_controls)1058 TEST(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
TEST(wchar,wcwidth_non_spacing_and_enclosing_marks_and_format)1064 TEST(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.
1067 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1068 }
1069
TEST(wchar,wcwidth_non_spacing_special_cases)1070 TEST(wchar, wcwidth_non_spacing_special_cases) {
1071 // 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
TEST(wchar,wcwidth_cjk)1098 TEST(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
TEST(wchar,wcwidth_korean_combining_jamo)1107 TEST(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
TEST(wchar,wcwidth_korean_jeongeul_syllables)1113 TEST(wchar, wcwidth_korean_jeongeul_syllables) {
1114 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
1115 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.
1119 }
1120
TEST(wchar,wcwidth_kana)1121 TEST(wchar, wcwidth_kana) {
1122 // Hiragana (most, not undefined).
1123 AssertWcwidthRange(0x3041, 0x3097, 2);
1124 // Katakana.
1125 AssertWcwidthRange(0x30a0, 0x3100, 2);
1126 }
1127
TEST(wchar,wcwidth_circled_two_digit_cjk)1128 TEST(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
TEST(wchar,wcwidth_hexagrams)1134 TEST(wchar, wcwidth_hexagrams) {
1135 // Hexagrams are wide, though EastAsianWidth is neutral.
1136 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1137 }
1138
TEST(wchar,wcwidth_default_ignorables)1139 TEST(wchar, wcwidth_default_ignorables) {
1140 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1141 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1142 }
1143
TEST(wchar,wcwidth_hangeul_compatibility_jamo)1144 TEST(wchar, wcwidth_hangeul_compatibility_jamo) {
1145 // 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'ㅅ'));
1157 }
1158
TEST(wchar,wcswidth)1159 TEST(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
TEST(wchar,wcslcpy)1165 TEST(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
TEST(wchar,wcscat)1177 TEST(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
TEST(wchar,wcscpy)1185 TEST(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
TEST(wchar,wcscasecmp)1193 TEST(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
TEST(wchar,wcscspn)1201 TEST(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
TEST(wchar,wcsspn)1207 TEST(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
TEST(wchar,wcsdup)1213 TEST(wchar, wcsdup) {
1214 wchar_t* s = wcsdup(L"hello");
1215 ASSERT_STREQ(s, L"hello");
1216 free(s);
1217 }
1218
TEST(wchar,wcslcat)1219 TEST(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
TEST(wchar,wcsncasecmp)1230 TEST(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
TEST(wchar,wcsncat)1240 TEST(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
TEST(wchar,wcsncmp)1250 TEST(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
TEST(wchar,wcsnlen)1257 TEST(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
TEST(wchar,wcspbrk)1263 TEST(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
TEST(wchar,wcstok)1271 TEST(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
TEST(wchar,wmemchr)1288 TEST(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
TEST(wchar,wmemcmp)1296 TEST(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
TEST(wchar,wmemcpy)1302 TEST(wchar, wmemcpy) {
1303 wchar_t dst[32] = {};
1304 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1305 ASSERT_STREQ(dst, L"hello");
1306 }
1307
TEST(wchar,wmemmove)1308 TEST(wchar, wmemmove) {
1309 wchar_t dst[32] = {};
1310 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1311 ASSERT_STREQ(dst, L"hello");
1312 }
1313
TEST(wchar,wmemset)1314 TEST(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 }
1324