• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   memset(&ps, 0, sizeof(ps));
137   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
138   EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
139   EXPECT_ERRNO(EILSEQ);
140 
141   // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
142   // state should be reset.
143   memset(&ps, 0, sizeof(ps));
144   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
145   EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
146   EXPECT_TRUE(mbsinit(&ps));
147 
148   memset(&ps, 0, sizeof(ps));
149   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
150   EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
151   EXPECT_TRUE(mbsinit(&ps));
152 }
153 
TEST(wchar,wcstombs_wcrtombs)154 TEST(wchar, wcstombs_wcrtombs) {
155   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
156   uselocale(LC_GLOBAL_LOCALE);
157 
158   const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
159   const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
160   const wchar_t* src;
161   char bytes[BUFSIZ];
162 
163   // Given a NULL destination, these functions count valid characters.
164   EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
165   EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
166   EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
167   src = chars;
168   EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
169   EXPECT_EQ(&chars[0], src);
170   src = chars;
171   EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
172   EXPECT_EQ(&chars[0], src);
173   src = chars;
174   EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
175   EXPECT_EQ(&chars[0], src);
176 
177   // An unrepresentable char just returns an error from wcstombs...
178   errno = 0;
179   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
180   EXPECT_ERRNO(EILSEQ);
181   errno = 0;
182   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
183   EXPECT_ERRNO(EILSEQ);
184 
185   // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
186   // to actually convert anything...
187   errno = 0;
188   src = bad_chars;
189   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
190   EXPECT_EQ(&bad_chars[0], src);
191   EXPECT_ERRNO(EILSEQ);
192   errno = 0;
193   src = bad_chars;
194   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
195   EXPECT_EQ(&bad_chars[0], src);
196   EXPECT_ERRNO(EILSEQ);
197 
198   // Okay, now let's test actually converting something...
199   memset(bytes, 'x', sizeof(bytes));
200   EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
201   memset(bytes, 'x', sizeof(bytes));
202   EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
203   bytes[5] = 0;
204   EXPECT_STREQ("hellx", bytes);
205   memset(bytes, 'x', sizeof(bytes));
206   EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
207   EXPECT_STREQ("hello", bytes);
208   memset(bytes, 'x', sizeof(bytes));
209   EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
210   EXPECT_STREQ("hello", bytes);
211   errno = 0;
212   memset(bytes, 'x', sizeof(bytes));
213   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
214   EXPECT_ERRNO(EILSEQ);
215   bytes[3] = 0;
216   EXPECT_STREQ("hix", bytes);
217 
218   // wcsrtombs is a bit more informative...
219   memset(bytes, 'x', sizeof(bytes));
220   src = chars;
221   EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
222   EXPECT_EQ(&chars[0], src); // No input consumed.
223   EXPECT_ERRNO(EILSEQ);
224 
225   memset(bytes, 'x', sizeof(bytes));
226   src = chars;
227   EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
228   EXPECT_EQ(&chars[4], src); // Some input consumed.
229   EXPECT_ERRNO(EILSEQ);
230   bytes[5] = 0;
231   EXPECT_STREQ("hellx", bytes);
232 
233   memset(bytes, 'x', sizeof(bytes));
234   src = chars;
235   EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
236   EXPECT_EQ(nullptr, src); // All input consumed!
237   EXPECT_ERRNO(EILSEQ);
238   EXPECT_STREQ("hello", bytes);
239 
240   memset(bytes, 'x', sizeof(bytes));
241   src = chars;
242   EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
243   EXPECT_EQ(nullptr, src); // All input consumed.
244   EXPECT_ERRNO(EILSEQ);
245   EXPECT_STREQ("hello", bytes);
246 
247   memset(bytes, 'x', sizeof(bytes));
248   src = bad_chars;
249   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
250   EXPECT_EQ(&bad_chars[2], src);
251   EXPECT_ERRNO(EILSEQ);
252   bytes[3] = 0;
253   EXPECT_STREQ("hix", bytes);
254 
255   // Any non-initial state is invalid when calling wcsrtombs.
256   mbstate_t ps;
257   src = chars;
258   memset(&ps, 0, sizeof(ps));
259   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
260   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
261   EXPECT_ERRNO(EILSEQ);
262 }
263 
TEST(wchar,limits)264 TEST(wchar, limits) {
265   ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
266 }
267 
TEST(wchar,wcsstr)268 TEST(wchar, wcsstr) {
269   const wchar_t* haystack = L"big daddy/giant haystacks!";
270   const wchar_t* empty_haystack = L"";
271 
272   // The empty needle is a special case.
273   ASSERT_EQ(haystack, wcsstr(haystack, L""));
274   ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
275 
276   ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
277   ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
278   ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
279   ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
280   ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
281   ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
282 
283   ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
284   ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
285 }
286 
TEST(wchar,wcsstr_80199)287 TEST(wchar, wcsstr_80199) {
288   // https://code.google.com/p/android/issues/detail?id=80199
289   ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
290 }
291 
TEST(wchar,mbtowc)292 TEST(wchar, mbtowc) {
293   wchar_t out[8];
294 
295   // mbtowc and all the mbrto* APIs behave slightly differently when n is 0:
296   //
297   // mbrtowc returns 0 "if the next n or fewer bytes complete the multibyte
298   // character that corresponds to the null wide character"
299   //
300   // mbrtoc (C23 7.24.7.2.4) says:
301   //
302   //     If s is not a null pointer, the mbtowc function either returns 0 (if s
303   //     points to the null character), or returns the number of bytes that are
304   //     contained in the converted multibyte character (if the next n or fewer
305   //     bytes form a valid multibyte character), or returns -1 (if they do not
306   //     form a valid multibyte character).
307   //
308   // glibc's interpretation differs from all the BSDs (including macOS) and
309   // bionic (by way of openbsd). glibc returns 0 since s does point to the null
310   // character, whereas the BSDs return -1 because the next 0 bytes do not form
311   // a valid multibyte chatacter. glibc's interpretation is probably more
312   // correct from a strict interpretation of the spec, but considering the other
313   // APIs behave more like the BSD interpretation that may be a bug in the spec.
314 #ifdef __GLIBC__
315   int expected_result_for_zero_length_empty_string = 0;
316 #else
317   int expected_result_for_zero_length_empty_string = -1;
318 #endif
319 
320   out[0] = 'x';
321   EXPECT_EQ(-1, mbtowc(out, "hello", 0));
322   EXPECT_EQ('x', out[0]);
323 
324   EXPECT_EQ(-1, mbtowc(out, "hello", 0));
325   EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(out, "", 0));
326   EXPECT_EQ(1, mbtowc(out, "hello", 1));
327   EXPECT_EQ(L'h', out[0]);
328 
329   EXPECT_EQ(-1, mbtowc(nullptr, "hello", 0));
330   EXPECT_EQ(expected_result_for_zero_length_empty_string, mbtowc(nullptr, "", 0));
331   EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
332 
333   EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
334 }
335 
TEST(wchar,mbrtowc)336 TEST(wchar, mbrtowc) {
337   wchar_t out[8];
338 
339   out[0] = 'x';
340   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
341   EXPECT_EQ('x', out[0]);
342 
343   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "hello", 0, nullptr));
344   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(out, "", 0, nullptr));
345   EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
346   EXPECT_EQ(L'h', out[0]);
347 
348   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "hello", 0, nullptr));
349   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "", 0, nullptr));
350   EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
351 
352   EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
353 
354   EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
355   uselocale(LC_GLOBAL_LOCALE);
356 
357   // 1-byte UTF-8.
358   EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
359   EXPECT_EQ(L'a', out[0]);
360   // 2-byte UTF-8.
361   EXPECT_EQ(2U, mbrtowc(out,
362                         "\xc2\xa2"
363                         "cdef",
364                         6, nullptr));
365   EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
366   // 3-byte UTF-8.
367   EXPECT_EQ(3U, mbrtowc(out,
368                         "\xe2\x82\xac"
369                         "def",
370                         6, nullptr));
371   EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
372   // 4-byte UTF-8.
373   EXPECT_EQ(4U, mbrtowc(out,
374                         "\xf0\xa4\xad\xa2"
375                         "ef",
376                         6, nullptr));
377   EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
378 #if defined(__BIONIC__) // glibc allows this.
379   // Illegal 5-byte UTF-8.
380   EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
381                                              "\xf8\xa1\xa2\xa3\xa4"
382                                              "f",
383                                              6, nullptr));
384   EXPECT_ERRNO(EILSEQ);
385 #endif
386   // Illegal over-long sequence.
387   EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
388                                              "\xf0\x82\x82\xac"
389                                              "ef",
390                                              6, nullptr));
391   EXPECT_ERRNO(EILSEQ);
392 }
393 
TEST(wchar,mbrtowc_valid_non_characters)394 TEST(wchar, mbrtowc_valid_non_characters) {
395   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
396   uselocale(LC_GLOBAL_LOCALE);
397 
398   wchar_t out[8] = {};
399 
400   ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
401   ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
402   ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
403   ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
404 }
405 
TEST(wchar,mbrtowc_out_of_range)406 TEST(wchar, mbrtowc_out_of_range) {
407   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
408   uselocale(LC_GLOBAL_LOCALE);
409 
410   wchar_t out[8] = {};
411   errno = 0;
412   auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
413   if (kLibcRejectsOverLongUtf8Sequences) {
414     ASSERT_EQ(static_cast<size_t>(-1), result);
415     ASSERT_ERRNO(EILSEQ);
416   } else {
417     ASSERT_EQ(4U, result);
418     ASSERT_ERRNO(0);
419   }
420 }
421 
test_mbrtowc_incomplete(mbstate_t * ps)422 static void test_mbrtowc_incomplete(mbstate_t* ps) {
423   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
424   uselocale(LC_GLOBAL_LOCALE);
425 
426   wchar_t out;
427   // 2-byte UTF-8.
428   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
429   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
430   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
431   ASSERT_TRUE(mbsinit(ps));
432   // 3-byte UTF-8.
433   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
434   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
435   ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
436   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
437   ASSERT_TRUE(mbsinit(ps));
438   // 4-byte UTF-8.
439   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
440   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
441   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
442   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
443   ASSERT_TRUE(mbsinit(ps));
444 
445   // Invalid 2-byte
446   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
447   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
448   ASSERT_ERRNO(EILSEQ);
449 }
450 
TEST(wchar,mbrtowc_incomplete)451 TEST(wchar, mbrtowc_incomplete) {
452   mbstate_t ps;
453   memset(&ps, 0, sizeof(ps));
454 
455   test_mbrtowc_incomplete(&ps);
456   test_mbrtowc_incomplete(nullptr);
457 }
458 
test_mbsrtowcs(mbstate_t * ps)459 static void test_mbsrtowcs(mbstate_t* ps) {
460   constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
461   constexpr const char* INVALID = "A" "\xc2\x20" "ef";
462   constexpr const char* INCOMPLETE = "A" "\xc2";
463   wchar_t out[4];
464 
465   const char* valid = VALID;
466   ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
467   ASSERT_EQ(L'A', out[0]);
468   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
469   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
470   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
471   // Check that valid has advanced to the next unread character.
472   ASSERT_EQ('e', *valid);
473 
474   wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
475   ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
476   ASSERT_EQ(L'e', out[0]);
477   ASSERT_EQ(L'f', out[1]);
478   ASSERT_EQ(L'\0', out[2]);
479   // Check that we didn't clobber the rest of out.
480   ASSERT_EQ(L'x', out[3]);
481   // Check that valid has advanced to the end of the string.
482   ASSERT_EQ(nullptr, valid);
483 
484   const char* invalid = INVALID;
485   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
486   EXPECT_ERRNO(EILSEQ);
487   ASSERT_EQ('\xc2', *invalid);
488 
489   const char* incomplete = INCOMPLETE;
490   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
491   EXPECT_ERRNO(EILSEQ);
492   ASSERT_EQ('\xc2', *incomplete);
493 
494   // If dst is null, *src shouldn't be updated.
495   // https://code.google.com/p/android/issues/detail?id=166381
496   const char* mbs = VALID;
497   EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
498   EXPECT_EQ(VALID, mbs);
499   mbs = INVALID;
500   EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
501   EXPECT_EQ(INVALID, mbs);
502   mbs = INCOMPLETE;
503   EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
504   EXPECT_EQ(INCOMPLETE, mbs);
505 }
506 
TEST(wchar,mbsrtowcs)507 TEST(wchar, mbsrtowcs) {
508   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
509   uselocale(LC_GLOBAL_LOCALE);
510 
511   mbstate_t ps;
512   memset(&ps, 0, sizeof(ps));
513   test_mbsrtowcs(&ps);
514   test_mbsrtowcs(nullptr);
515 
516   // Invalid multi byte continuation.
517   const char* invalid = "\x20";
518   wchar_t out;
519   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
520   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
521   EXPECT_ERRNO(EILSEQ);
522   ASSERT_EQ('\x20', *invalid);
523 }
524 
525 template <typename T>
526 using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
527 
528 template <typename T>
TestSingleWcsToInt(WcsToIntFn<T> fn,const wchar_t * str,int base,T expected_value,ptrdiff_t expected_len)529 void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
530                         T expected_value, ptrdiff_t expected_len) {
531   wchar_t* p;
532   EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
533   EXPECT_EQ(expected_len, p - str) << str << " " << base;
534 }
535 
536 template <typename T>
TestWcsToInt(WcsToIntFn<T> fn)537 void TestWcsToInt(WcsToIntFn<T> fn) {
538   TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
539   TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
540   TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
541   TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
542   TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
543   TestSingleWcsToInt(fn, L"   123 45", 0, static_cast<T>(123), 6);
544   TestSingleWcsToInt(fn, L"  -123", 0, static_cast<T>(-123), 6);
545   TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
546   if (kLibcSupportsParsingBinaryLiterals) {
547     TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
548   }
549 }
550 
551 template <typename T>
TestWcsToIntLimits(WcsToIntFn<T> fn,const wchar_t * min_str,const wchar_t * max_str)552 void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
553                         const wchar_t* max_str) {
554   if (std::is_signed<T>::value) {
555     ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
556   } else {
557     // If the subject sequence begins with a <hyphen-minus>, the value resulting
558     // from the conversion shall be negated.
559     // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
560     ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
561   }
562   ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
563 }
564 
TEST(wchar,wcstol)565 TEST(wchar, wcstol) {
566   TestWcsToInt(wcstol);
567 }
568 
TEST(wchar,wcstol_limits)569 TEST(wchar, wcstol_limits) {
570   if (sizeof(long) == 8) {
571     TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
572   } else {
573     TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
574   }
575 }
576 
TEST(wchar,wcstoul)577 TEST(wchar, wcstoul) {
578   TestWcsToInt(wcstoul);
579 }
580 
TEST(wchar,wcstoul_limits)581 TEST(wchar, wcstoul_limits) {
582   if (sizeof(long) == 8) {
583     TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
584   } else {
585     TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
586   }
587 }
588 
TEST(wchar,wcstoll)589 TEST(wchar, wcstoll) {
590   TestWcsToInt(wcstoll);
591 }
592 
TEST(wchar,wcstoll_limits)593 TEST(wchar, wcstoll_limits) {
594   TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
595 }
596 
TEST(wchar,wcstoull)597 TEST(wchar, wcstoull) {
598   TestWcsToInt(wcstoull);
599 }
600 
TEST(wchar,wcstoull_limits)601 TEST(wchar, wcstoull_limits) {
602   TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
603 }
604 
TEST(wchar,wcstoimax)605 TEST(wchar, wcstoimax) {
606   TestWcsToInt(wcstoimax);
607 }
608 
TEST(wchar,wcstoimax_limits)609 TEST(wchar, wcstoimax_limits) {
610   TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
611                      L"9223372036854775808");
612 }
613 
TEST(wchar,wcstoumax)614 TEST(wchar, wcstoumax) {
615   TestWcsToInt(wcstoumax);
616 }
617 
TEST(wchar,wcstoumax_limits)618 TEST(wchar, wcstoumax_limits) {
619   TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
620 }
621 
TEST(wchar,mbsnrtowcs)622 TEST(wchar, mbsnrtowcs) {
623   wchar_t dst[128];
624   const char* s = "hello, world!";
625   const char* src;
626 
627   memset(dst, 0, sizeof(dst));
628   src = s;
629   ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
630 
631   memset(dst, 0, sizeof(dst));
632   src = s;
633   ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
634   ASSERT_EQ(L'h', dst[0]);
635   ASSERT_EQ(L'e', dst[1]);
636   ASSERT_EQ(&s[2], src);
637 
638   memset(dst, 0, sizeof(dst));
639   src = s;
640   ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
641   ASSERT_EQ(L'h', dst[0]);
642   ASSERT_EQ(L'e', dst[1]);
643   ASSERT_EQ(L'l', dst[2]);
644   ASSERT_EQ(&s[3], src);
645 
646   memset(dst, 0, sizeof(dst));
647   const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
648   src = incomplete;
649   errno = 0;
650   ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
651   ASSERT_ERRNO(EILSEQ);
652 
653   src = incomplete;
654   errno = 0;
655   ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
656   ASSERT_ERRNO(EILSEQ);
657 }
658 
TEST(wchar,wcsftime__wcsftime_l)659 TEST(wchar, wcsftime__wcsftime_l) {
660   setenv("TZ", "UTC", 1);
661 
662   struct tm t;
663   memset(&t, 0, sizeof(tm));
664   t.tm_year = 200;
665   t.tm_mon = 2;
666   t.tm_mday = 10;
667 
668   wchar_t buf[64];
669 
670   EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
671   EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
672   EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
673   EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
674 }
675 
TEST(wchar,wmemmove_smoke)676 TEST(wchar, wmemmove_smoke) {
677   const wchar_t const_wstr[] = L"This is a test of something or other.....";
678   wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
679 
680   EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
681   EXPECT_STREQ(const_wstr, wstr);
682 
683   EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
684   EXPECT_STREQ(L"This This is a test of something or other", wstr);
685 }
686 
TEST(wchar,wmemcpy_smoke)687 TEST(wchar, wmemcpy_smoke) {
688   const wchar_t src[] = L"Source string";
689   wchar_t dst[NUM_WCHARS(sizeof(src))];
690 
691   EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
692   EXPECT_STREQ(dst, src);
693 }
694 
TEST(wchar,wcpcpy_smoke)695 TEST(wchar, wcpcpy_smoke) {
696   const wchar_t src[] = L"Source string";
697   wchar_t dst[NUM_WCHARS(sizeof(src))];
698 
699   EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
700   EXPECT_STREQ(dst, src);
701 }
702 
TEST(wchar,wcpncpy_smoke)703 TEST(wchar, wcpncpy_smoke) {
704   const wchar_t src[] = L"Source string";
705   wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
706 
707   size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
708   EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
709   EXPECT_STREQ(dst, src);
710 
711   EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
712   dst[6] = L'\0';
713   EXPECT_STREQ(dst, L"Source");
714 
715   wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
716   EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
717   EXPECT_STREQ(dst, src);
718   EXPECT_EQ(dst[src_len], L'\0');
719   EXPECT_EQ(dst[src_len+1], L'\0');
720   EXPECT_EQ(dst[src_len+2], L'\0');
721   EXPECT_EQ(dst[src_len+3], L'\0');
722   EXPECT_EQ(dst[src_len+4], L'x');
723 }
724 
TEST(wchar,wcscpy_smoke)725 TEST(wchar, wcscpy_smoke) {
726   const wchar_t src[] = L"Source string";
727   wchar_t dst[NUM_WCHARS(sizeof(src))];
728 
729   EXPECT_EQ(dst, wcscpy(dst, src));
730   EXPECT_STREQ(src, dst);
731 }
732 
TEST(wchar,wcsncpy_smoke)733 TEST(wchar, wcsncpy_smoke) {
734   const wchar_t src[] = L"Source string";
735   wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
736 
737   size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
738   EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
739   EXPECT_STREQ(dst, src);
740 
741   EXPECT_EQ(dst, wcsncpy(dst, src, 6));
742   dst[6] = L'\0';
743   EXPECT_STREQ(dst, L"Source");
744   EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
745   EXPECT_STREQ(dst, L"Source");
746 
747   wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
748   EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
749   EXPECT_STREQ(dst, src);
750   EXPECT_EQ(dst[src_len], L'\0');
751   EXPECT_EQ(dst[src_len+1], L'\0');
752   EXPECT_EQ(dst[src_len+2], L'\0');
753   EXPECT_EQ(dst[src_len+3], L'\0');
754   EXPECT_EQ(dst[src_len+4], L'x');
755 }
756 
TEST(wchar,mbrtowc_15439554)757 TEST(wchar, mbrtowc_15439554) {
758   // http://b/15439554
759   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
760   uselocale(LC_GLOBAL_LOCALE);
761 
762   ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
763   ASSERT_GE(MB_CUR_MAX, 4U);
764 
765   wchar_t wc;
766   size_t n;
767 
768   // 1-byte character.
769   n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
770   EXPECT_EQ(1U, n);
771   EXPECT_EQ(L'x', wc);
772   // 2-byte character.
773   n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
774   EXPECT_EQ(2U, n);
775   EXPECT_EQ(L'¢', wc);
776   // 3-byte character.
777   n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
778   EXPECT_EQ(3U, n);
779   EXPECT_EQ(L'€', wc);
780   // 4-byte character.
781   n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
782   EXPECT_EQ(4U, n);
783   EXPECT_EQ(L'��', wc);
784 }
785 
TEST(wchar,open_wmemstream)786 TEST(wchar, open_wmemstream) {
787   wchar_t* p = nullptr;
788   size_t size = 0;
789   FILE* fp = open_wmemstream(&p, &size);
790   ASSERT_NE(EOF, fputws(L"hello, world!", fp));
791   fclose(fp);
792 
793   ASSERT_STREQ(L"hello, world!", p);
794   ASSERT_EQ(wcslen(L"hello, world!"), size);
795   free(p);
796 }
797 
TEST(stdio,open_wmemstream_EINVAL)798 TEST(stdio, open_wmemstream_EINVAL) {
799 #if defined(__BIONIC__)
800   wchar_t* p;
801   size_t size;
802 #pragma clang diagnostic push
803 #pragma clang diagnostic ignored "-Wnonnull"
804   // Invalid buffer.
805   errno = 0;
806   ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
807   ASSERT_ERRNO(EINVAL);
808 
809   // Invalid size.
810   errno = 0;
811   ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
812   ASSERT_ERRNO(EINVAL);
813 #pragma clang diagnostic pop
814 #else
815   GTEST_SKIP() << "This test is bionic-specific";
816 #endif
817 }
818 
TEST(wchar,wcstol_EINVAL)819 TEST(wchar, wcstol_EINVAL) {
820   errno = 0;
821   wcstol(L"123", nullptr, -1);
822   ASSERT_ERRNO(EINVAL);
823   errno = 0;
824   wcstol(L"123", nullptr, 1);
825   ASSERT_ERRNO(EINVAL);
826   errno = 0;
827   wcstol(L"123", nullptr, 37);
828   ASSERT_ERRNO(EINVAL);
829 }
830 
TEST(wchar,wcstoll_EINVAL)831 TEST(wchar, wcstoll_EINVAL) {
832   errno = 0;
833   wcstoll(L"123", nullptr, -1);
834   ASSERT_ERRNO(EINVAL);
835   errno = 0;
836   wcstoll(L"123", nullptr, 1);
837   ASSERT_ERRNO(EINVAL);
838   errno = 0;
839   wcstoll(L"123", nullptr, 37);
840   ASSERT_ERRNO(EINVAL);
841 }
842 
TEST(wchar,wcstoul_EINVAL)843 TEST(wchar, wcstoul_EINVAL) {
844   errno = 0;
845   wcstoul(L"123", nullptr, -1);
846   ASSERT_ERRNO(EINVAL);
847   errno = 0;
848   wcstoul(L"123", nullptr, 1);
849   ASSERT_ERRNO(EINVAL);
850   errno = 0;
851   wcstoul(L"123", nullptr, 37);
852   ASSERT_ERRNO(EINVAL);
853 }
854 
TEST(wchar,wcstoull_EINVAL)855 TEST(wchar, wcstoull_EINVAL) {
856   errno = 0;
857   wcstoull(L"123", nullptr, -1);
858   ASSERT_ERRNO(EINVAL);
859   errno = 0;
860   wcstoull(L"123", nullptr, 1);
861   ASSERT_ERRNO(EINVAL);
862   errno = 0;
863   wcstoull(L"123", nullptr, 37);
864   ASSERT_ERRNO(EINVAL);
865 }
866 
TEST(wchar,wcstoll_l_EINVAL)867 TEST(wchar, wcstoll_l_EINVAL) {
868   errno = 0;
869   wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
870   ASSERT_ERRNO(EINVAL);
871   errno = 0;
872   wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
873   ASSERT_ERRNO(EINVAL);
874   errno = 0;
875   wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
876   ASSERT_ERRNO(EINVAL);
877 }
878 
TEST(wchar,wcstoull_l_EINVAL)879 TEST(wchar, wcstoull_l_EINVAL) {
880   errno = 0;
881   wcstoull_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
882   ASSERT_ERRNO(EINVAL);
883   errno = 0;
884   wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
885   ASSERT_ERRNO(EINVAL);
886   errno = 0;
887   wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
888   ASSERT_ERRNO(EINVAL);
889 }
890 
TEST(wchar,wmempcpy)891 TEST(wchar, wmempcpy) {
892 #if !defined(ANDROID_HOST_MUSL)
893   wchar_t dst[6];
894   ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
895 #else
896   GTEST_SKIP() << "musl doesn't have wmempcpy";
897 #endif
898 }
899 
900 template <typename T>
901 using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
902 
903 template <typename T>
TestSingleWcsToFloat(WcsToFloatFn<T> fn,const wchar_t * str,T expected_value,ptrdiff_t expected_len)904 void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
905                           T expected_value, ptrdiff_t expected_len) {
906   wchar_t* p;
907   ASSERT_EQ(expected_value, fn(str, &p));
908   ASSERT_EQ(expected_len, p - str);
909 }
910 
911 template <typename T>
TestWcsToFloat(WcsToFloatFn<T> fn)912 void TestWcsToFloat(WcsToFloatFn<T> fn) {
913   TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
914   TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
915   TestSingleWcsToFloat(fn, L"   123 45", static_cast<T>(123.0L), 6);
916   TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
917   TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
918   TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
919 }
920 
921 template <typename T>
TestWcsToFloatHexFloats(WcsToFloatFn<T> fn)922 void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
923   TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
924   TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
925   TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
926   TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
927 }
928 
929 template <typename T>
TestWcsToFloatInfNan(WcsToFloatFn<T> fn)930 void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
931   ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
932   ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
933   ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
934 
935   ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
936   ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
937   ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
938 
939   wchar_t* p;
940   ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
941   ASSERT_STREQ(L"ny", p);
942   ASSERT_TRUE(isnan(fn(L"nanny", &p)));
943   ASSERT_STREQ(L"ny", p);
944   ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
945   ASSERT_STREQ(L"ny", p);
946 
947   ASSERT_EQ(0, fn(L"muppet", &p));
948   ASSERT_STREQ(L"muppet", p);
949   ASSERT_EQ(0, fn(L"  muppet", &p));
950   ASSERT_STREQ(L"  muppet", p);
951 
952   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
953   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
954   ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
955 
956   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
957   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
958   ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
959 
960   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
961   ASSERT_STREQ(L"initude", p);
962   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
963   ASSERT_STREQ(L"initude", p);
964   ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
965   ASSERT_STREQ(L"initude", p);
966 
967   // Check case-insensitivity.
968   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
969   ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
970 }
971 
TEST(wchar,wcstof)972 TEST(wchar, wcstof) {
973   TestWcsToFloat(wcstof);
974 }
975 
TEST(wchar,wcstof_hex_floats)976 TEST(wchar, wcstof_hex_floats) {
977   TestWcsToFloatHexFloats(wcstof);
978 }
979 
TEST(wchar,wcstof_hex_inf_nan)980 TEST(wchar, wcstof_hex_inf_nan) {
981   TestWcsToFloatInfNan(wcstof);
982 }
983 
TEST(wchar,wcstod)984 TEST(wchar, wcstod) {
985   TestWcsToFloat(wcstod);
986 }
987 
TEST(wchar,wcstod_hex_floats)988 TEST(wchar, wcstod_hex_floats) {
989   TestWcsToFloatHexFloats(wcstod);
990 }
991 
TEST(wchar,wcstod_hex_inf_nan)992 TEST(wchar, wcstod_hex_inf_nan) {
993   TestWcsToFloatInfNan(wcstod);
994 }
995 
TEST(wchar,wcstold)996 TEST(wchar, wcstold) {
997   TestWcsToFloat(wcstold);
998 }
999 
TEST(wchar,wcstold_hex_floats)1000 TEST(wchar, wcstold_hex_floats) {
1001   TestWcsToFloatHexFloats(wcstold);
1002 }
1003 
TEST(wchar,wcstold_hex_inf_nan)1004 TEST(wchar, wcstold_hex_inf_nan) {
1005   TestWcsToFloatInfNan(wcstold);
1006 }
1007 
TEST(wchar,wcstod_l)1008 TEST(wchar, wcstod_l) {
1009 #if !defined(ANDROID_HOST_MUSL)
1010   EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
1011 #else
1012   GTEST_SKIP() << "musl doesn't have wcstod_l";
1013 #endif
1014 }
1015 
TEST(wchar,wcstof_l)1016 TEST(wchar, wcstof_l) {
1017 #if !defined(ANDROID_HOST_MUSL)
1018   EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
1019 #else
1020   GTEST_SKIP() << "musl doesn't have wcstof_l";
1021 #endif
1022 }
1023 
TEST(wchar,wcstol_l)1024 TEST(wchar, wcstol_l) {
1025 #if !defined(ANDROID_HOST_MUSL)
1026   EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1027 #else
1028   GTEST_SKIP() << "musl doesn't have wcstol_l";
1029 #endif
1030 }
1031 
TEST(wchar,wcstold_l)1032 TEST(wchar, wcstold_l) {
1033   EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
1034 }
1035 
TEST(wchar,wcstoll_l)1036 TEST(wchar, wcstoll_l) {
1037   EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1038 }
1039 
TEST(wchar,wcstoul_l)1040 TEST(wchar, wcstoul_l) {
1041 #if !defined(ANDROID_HOST_MUSL)
1042   EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1043 #else
1044   GTEST_SKIP() << "musl doesn't have wcstoul_l";
1045 #endif
1046 }
1047 
TEST(wchar,wcstoull_l)1048 TEST(wchar, wcstoull_l) {
1049   EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
1050 }
1051 
AssertWcwidthRange(wchar_t begin,wchar_t end,int expected)1052 static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
1053   for (wchar_t i = begin; i < end; ++i) {
1054     EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
1055   }
1056 }
1057 
TEST(wchar,wcwidth_NUL)1058 TEST(wchar, wcwidth_NUL) {
1059   // NUL is defined to return 0 rather than -1, despite being a C0 control.
1060   EXPECT_EQ(0, wcwidth(0));
1061 }
1062 
TEST(wchar,wcwidth_ascii)1063 TEST(wchar, wcwidth_ascii) {
1064   AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
1065 }
1066 
TEST(wchar,wcwidth_controls)1067 TEST(wchar, wcwidth_controls) {
1068   AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
1069   EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
1070   AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
1071 }
1072 
TEST(wchar,wcwidth_non_spacing_and_enclosing_marks_and_format)1073 TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
1074   if (!have_dl()) return;
1075 
1076   EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
1077   EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
1078   EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
1079 }
1080 
TEST(wchar,wcwidth_non_spacing_special_cases)1081 TEST(wchar, wcwidth_non_spacing_special_cases) {
1082   if (!have_dl()) return;
1083 
1084   // U+00AD is a soft hyphen, which normally shouldn't be rendered at all.
1085   // I think the assumption here is that you elide the soft hyphen character
1086   // completely in that case, and never call wcwidth() if you don't want to
1087   // render it as an actual hyphen. Whereas if you do want to render it,
1088   // you call wcwidth(), and 1 is the right answer. This is what Markus Kuhn's
1089   // original https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c did,
1090   // and glibc and iOS do the same.
1091   // See also: https://en.wikipedia.org/wiki/Soft_hyphen#Text_to_be_formatted_by_the_recipient
1092   EXPECT_EQ(1, wcwidth(0x00ad)); // Soft hyphen (SHY).
1093 
1094   // U+115F is the Hangeul choseong filler (for a degenerate composed
1095   // character missing an initial consonant (as opposed to one with a
1096   // leading ieung). Since the code points for combining jungseong (medial
1097   // vowels) and jongseong (trailing consonants) have width 0, the choseong
1098   // (initial consonant) has width 2 to cover the entire syllable. So unless
1099   // U+115f has width 2, a degenerate composed "syllable" without an initial
1100   // consonant or ieung would have a total width of 0, which is silly.
1101   // The following sequence is effectively "약" without the leading ieung...
1102   EXPECT_EQ(2, wcwidth(0x115f)); // Hangeul choseong filler.
1103   EXPECT_EQ(0, wcwidth(0x1163)); // Hangeul jungseong "ya".
1104   EXPECT_EQ(0, wcwidth(0x11a8)); // Hangeul jongseong "kiyeok".
1105 
1106   // U+1160, the jungseong filler, has width 0 because it must have been
1107   // preceded by either a choseong or choseong filler.
1108   EXPECT_EQ(0, wcwidth(0x1160));
1109 }
1110 
TEST(wchar,wcwidth_cjk)1111 TEST(wchar, wcwidth_cjk) {
1112   if (!have_dl()) return;
1113 
1114   EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
1115   EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
1116   EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
1117   EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
1118   EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
1119   EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
1120 }
1121 
TEST(wchar,wcwidth_korean_combining_jamo)1122 TEST(wchar, wcwidth_korean_combining_jamo) {
1123   if (!have_dl()) return;
1124 
1125   AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
1126   EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
1127   EXPECT_EQ(0, wcwidth(0xd7cb));
1128 }
1129 
TEST(wchar,wcwidth_korean_jeongeul_syllables)1130 TEST(wchar, wcwidth_korean_jeongeul_syllables) {
1131   if (!have_dl()) return;
1132 
1133   EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
1134   EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points as of Unicode 15.
1135 
1136   // Undefined characters at the end of the block currently have width 1,
1137   // but since they're undefined, we don't test that.
1138 }
1139 
TEST(wchar,wcwidth_kana)1140 TEST(wchar, wcwidth_kana) {
1141   if (!have_dl()) return;
1142 
1143   // Hiragana (most, not undefined).
1144   AssertWcwidthRange(0x3041, 0x3097, 2);
1145   // Katakana.
1146   AssertWcwidthRange(0x30a0, 0x3100, 2);
1147 }
1148 
TEST(wchar,wcwidth_circled_two_digit_cjk)1149 TEST(wchar, wcwidth_circled_two_digit_cjk) {
1150   if (!have_dl()) return;
1151 
1152   // Circled two-digit CJK "speed sign" numbers are wide,
1153   // though EastAsianWidth is ambiguous.
1154   AssertWcwidthRange(0x3248, 0x3250, 2);
1155 }
1156 
TEST(wchar,wcwidth_hexagrams)1157 TEST(wchar, wcwidth_hexagrams) {
1158   if (!have_dl()) return;
1159 
1160   // Hexagrams are wide, though EastAsianWidth is neutral.
1161   AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1162 }
1163 
TEST(wchar,wcwidth_default_ignorables)1164 TEST(wchar, wcwidth_default_ignorables) {
1165   if (!have_dl()) return;
1166 
1167   AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1168   EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1169 }
1170 
TEST(wchar,wcwidth_hangeul_compatibility_jamo)1171 TEST(wchar, wcwidth_hangeul_compatibility_jamo) {
1172   if (!have_dl()) return;
1173 
1174   // These are actually the *compatibility* jamo code points, *not* the regular
1175   // jamo code points (U+1100-U+11FF) using a jungseong filler. If you use the
1176   // Android IME to type any of these, you get these code points.
1177 
1178   // (Half of) the Korean "crying" emoticon "ㅠㅠ".
1179   // Actually U+3160 "Hangeul Letter Yu" from Hangeul Compatibility Jamo.
1180   EXPECT_EQ(2, wcwidth(L'ㅠ'));
1181   // The two halves of the Korean internet shorthand "ㄱㅅ" (short for 감사).
1182   // Actually U+3131 "Hangeul Letter Kiyeok" and U+3145 "Hangeul Letter Sios"
1183   // from Hangeul Compatibility Jamo.
1184   EXPECT_EQ(2, wcwidth(L'ㄱ'));
1185   EXPECT_EQ(2, wcwidth(L'ㅅ'));
1186 }
1187 
TEST(wchar,wcswidth)1188 TEST(wchar, wcswidth) {
1189   EXPECT_EQ(2, wcswidth(L"abc", 2));
1190   EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1191   EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1192 }
1193 
TEST(wchar,wcslcpy)1194 TEST(wchar, wcslcpy) {
1195 #if defined(__BIONIC__)
1196   wchar_t dst[32];
1197   ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1198   ASSERT_STREQ(L"he", dst);
1199   ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1200   ASSERT_STREQ(L"hello world", dst);
1201 #else
1202   GTEST_SKIP() << "no wcslcpy in glibc";
1203 #endif
1204 }
1205 
TEST(wchar,wcscat)1206 TEST(wchar, wcscat) {
1207   wchar_t dst[32];
1208   ASSERT_EQ(dst, wcscat(dst, L"hello"));
1209   ASSERT_STREQ(dst, L"hello");
1210   ASSERT_EQ(dst, wcscat(dst, L" world"));
1211   ASSERT_STREQ(dst, L"hello world");
1212 }
1213 
TEST(wchar,wcscpy)1214 TEST(wchar, wcscpy) {
1215   wchar_t dst[32];
1216   ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1217   ASSERT_STREQ(dst, L"hello");
1218   ASSERT_EQ(dst, wcscpy(dst, L"world"));
1219   ASSERT_STREQ(dst, L"world");
1220 }
1221 
TEST(wchar,wcscasecmp)1222 TEST(wchar, wcscasecmp) {
1223   ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1224   ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1225   ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1226   ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1227   ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1228 }
1229 
TEST(wchar,wcscspn)1230 TEST(wchar, wcscspn) {
1231   ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1232   ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1233   ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1234 }
1235 
TEST(wchar,wcsspn)1236 TEST(wchar, wcsspn) {
1237   ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1238   ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1239   ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1240 }
1241 
TEST(wchar,wcsdup)1242 TEST(wchar, wcsdup) {
1243   wchar_t* s = wcsdup(L"hello");
1244   ASSERT_STREQ(s, L"hello");
1245   free(s);
1246 }
1247 
TEST(wchar,wcslcat)1248 TEST(wchar, wcslcat) {
1249 #if defined(__BIONIC__)
1250   wchar_t dst[4] = {};
1251   ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1252   ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1253   ASSERT_STREQ(dst, L"abc");
1254 #else
1255   GTEST_SKIP() << "no wcslcpy in glibc";
1256 #endif
1257 }
1258 
TEST(wchar,wcsncasecmp)1259 TEST(wchar, wcsncasecmp) {
1260   ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1261 
1262   ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1263   ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1264   ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1265   ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1266   ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1267 }
1268 
TEST(wchar,wcsncat)1269 TEST(wchar, wcsncat) {
1270   wchar_t dst[32];
1271   ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1272   ASSERT_STREQ(dst, L"hello");
1273   ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1274   ASSERT_STREQ(dst, L"hello");
1275   ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1276   ASSERT_STREQ(dst, L"hello, world!");
1277 }
1278 
TEST(wchar,wcsncmp)1279 TEST(wchar, wcsncmp) {
1280   ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1281   ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1282   ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1283   ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1284 }
1285 
TEST(wchar,wcsnlen)1286 TEST(wchar, wcsnlen) {
1287   ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1288   ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1289   ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1290 }
1291 
TEST(wchar,wcspbrk)1292 TEST(wchar, wcspbrk) {
1293   const wchar_t* s = L"hello, world!";
1294   ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1295   ASSERT_EQ(s, wcspbrk(s, L"abch"));
1296   ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1297   ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1298 }
1299 
TEST(wchar,wcstok)1300 TEST(wchar, wcstok) {
1301   wchar_t s[] = L"this is\ta\nstring";
1302   wchar_t* p;
1303   ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1304   ASSERT_STREQ(s, L"this");
1305   ASSERT_STREQ(p, L"is\ta\nstring");
1306   ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1307   ASSERT_STREQ(s + 5, L"is");
1308   ASSERT_STREQ(p, L"a\nstring");
1309   ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1310   ASSERT_STREQ(s + 8, L"a");
1311   ASSERT_STREQ(p, L"string");
1312   ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1313   ASSERT_STREQ(s + 10, L"string");
1314   ASSERT_EQ(nullptr, p);
1315 }
1316 
TEST(wchar,wmemchr)1317 TEST(wchar, wmemchr) {
1318   const wchar_t* s = L"hello, world!";
1319   ASSERT_EQ(s, wmemchr(s, L'h', 13));
1320   ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1321   ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1322   ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1323 }
1324 
TEST(wchar,wmemcmp)1325 TEST(wchar, wmemcmp) {
1326   ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1327   ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1328   ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1329 }
1330 
TEST(wchar,wmemcpy)1331 TEST(wchar, wmemcpy) {
1332   wchar_t dst[32] = {};
1333   ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1334   ASSERT_STREQ(dst, L"hello");
1335 }
1336 
TEST(wchar,wmemmove)1337 TEST(wchar, wmemmove) {
1338   wchar_t dst[32] = {};
1339   ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1340   ASSERT_STREQ(dst, L"hello");
1341 }
1342 
TEST(wchar,wmemset)1343 TEST(wchar, wmemset) {
1344   wchar_t dst[4] = {};
1345   ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1346   ASSERT_EQ(dst[0], wchar_t(0x12345678));
1347   ASSERT_EQ(dst[1], wchar_t(0x12345678));
1348   ASSERT_EQ(dst[2], wchar_t(0x12345678));
1349   ASSERT_EQ(dst[3], wchar_t(0));
1350   ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1351   ASSERT_EQ(dst[0], wchar_t(0x12345678));
1352 }
1353