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 <limits.h>
21 #include <locale.h>
22 #include <stdint.h>
23 #include <wchar.h>
24 
25 #define NUM_WCHARS(num_bytes) (num_bytes/sizeof(wchar_t))
26 
TEST(wchar,sizeof_wchar_t)27 TEST(wchar, sizeof_wchar_t) {
28   EXPECT_EQ(4U, sizeof(wchar_t));
29   EXPECT_EQ(4U, sizeof(wint_t));
30 }
31 
TEST(wchar,mbrlen)32 TEST(wchar, mbrlen) {
33   char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
34   EXPECT_EQ(0U, mbrlen(&bytes[0], 0, NULL));
35   EXPECT_EQ(1U, mbrlen(&bytes[0], 1, NULL));
36 
37   EXPECT_EQ(1U, mbrlen(&bytes[4], 1, NULL));
38   EXPECT_EQ(0U, mbrlen(&bytes[5], 1, NULL));
39 }
40 
TEST(wchar,wctomb_wcrtomb)41 TEST(wchar, wctomb_wcrtomb) {
42   // wctomb and wcrtomb behave differently when s == NULL.
43   EXPECT_EQ(0, wctomb(NULL, L'h'));
44   EXPECT_EQ(0, wctomb(NULL, L'\0'));
45   EXPECT_EQ(1U, wcrtomb(NULL, L'\0', NULL));
46   EXPECT_EQ(1U, wcrtomb(NULL, L'h', NULL));
47 
48   char bytes[MB_LEN_MAX];
49 
50   // wctomb and wcrtomb behave similarly for the null wide character.
51   EXPECT_EQ(1, wctomb(bytes, L'\0'));
52   EXPECT_EQ(1U, wcrtomb(bytes, L'\0', NULL));
53 
54   // ...and for regular characters.
55   memset(bytes, 0, sizeof(bytes));
56   EXPECT_EQ(1, wctomb(bytes, L'h'));
57   EXPECT_EQ('h', bytes[0]);
58   memset(bytes, 0, sizeof(bytes));
59   EXPECT_EQ(1U, wcrtomb(bytes, L'h', NULL));
60   EXPECT_EQ('h', bytes[0]);
61 
62   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
63   uselocale(LC_GLOBAL_LOCALE);
64 
65   // 1-byte UTF-8.
66   memset(bytes, 0, sizeof(bytes));
67   EXPECT_EQ(1U, wcrtomb(bytes, L'h', NULL));
68   EXPECT_EQ('h', bytes[0]);
69   // 2-byte UTF-8.
70   memset(bytes, 0, sizeof(bytes));
71   EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, NULL));
72   EXPECT_EQ('\xc2', bytes[0]);
73   EXPECT_EQ('\xa2', bytes[1]);
74   // 3-byte UTF-8.
75   memset(bytes, 0, sizeof(bytes));
76   EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, NULL));
77   EXPECT_EQ('\xe2', bytes[0]);
78   EXPECT_EQ('\x82', bytes[1]);
79   EXPECT_EQ('\xac', bytes[2]);
80   // 4-byte UTF-8.
81   memset(bytes, 0, sizeof(bytes));
82   EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, NULL));
83   EXPECT_EQ('\xf0', bytes[0]);
84   EXPECT_EQ('\xa4', bytes[1]);
85   EXPECT_EQ('\xad', bytes[2]);
86   EXPECT_EQ('\xa2', bytes[3]);
87   // Invalid code point.
88   EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, NULL));
89   EXPECT_EQ(EILSEQ, errno);
90 }
91 
TEST(wchar,wcrtomb_start_state)92 TEST(wchar, wcrtomb_start_state) {
93   char out[MB_LEN_MAX];
94   mbstate_t ps;
95 
96   // Any non-initial state is invalid when calling wcrtomb.
97   memset(&ps, 0, sizeof(ps));
98   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xc2", 1, &ps));
99   EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
100   EXPECT_EQ(EILSEQ, errno);
101 
102   // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
103   // state should be reset.
104   memset(&ps, 0, sizeof(ps));
105   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xc2", 1, &ps));
106   EXPECT_EQ(1U, wcrtomb(NULL, 0x00a2, &ps));
107   EXPECT_TRUE(mbsinit(&ps));
108 
109   memset(&ps, 0, sizeof(ps));
110   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xf0\xa4", 1, &ps));
111   EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
112   EXPECT_TRUE(mbsinit(&ps));
113 }
114 
TEST(wchar,wcstombs_wcrtombs)115 TEST(wchar, wcstombs_wcrtombs) {
116   const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
117   const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
118   const wchar_t* src;
119   char bytes[BUFSIZ];
120 
121   // Given a NULL destination, these functions count valid characters.
122   EXPECT_EQ(5U, wcstombs(NULL, chars, 0));
123   EXPECT_EQ(5U, wcstombs(NULL, chars, 4));
124   EXPECT_EQ(5U, wcstombs(NULL, chars, 256));
125   src = chars;
126   EXPECT_EQ(5U, wcsrtombs(NULL, &src, 0, NULL));
127   EXPECT_EQ(&chars[0], src);
128   src = chars;
129   EXPECT_EQ(5U, wcsrtombs(NULL, &src, 4, NULL));
130   EXPECT_EQ(&chars[0], src);
131   src = chars;
132   EXPECT_EQ(5U, wcsrtombs(NULL, &src, 256, NULL));
133   EXPECT_EQ(&chars[0], src);
134 
135   // An unrepresentable char just returns an error from wcstombs...
136   errno = 0;
137   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(NULL, bad_chars, 0));
138   EXPECT_EQ(EILSEQ, errno);
139   errno = 0;
140   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(NULL, bad_chars, 256));
141   EXPECT_EQ(EILSEQ, errno);
142 
143   // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
144   // to actually convert anything...
145   errno = 0;
146   src = bad_chars;
147   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 0, NULL));
148   EXPECT_EQ(&bad_chars[0], src);
149   EXPECT_EQ(EILSEQ, errno);
150   errno = 0;
151   src = bad_chars;
152   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 256, NULL));
153   EXPECT_EQ(&bad_chars[0], src);
154   EXPECT_EQ(EILSEQ, errno);
155 
156   // Okay, now let's test actually converting something...
157   memset(bytes, 'x', sizeof(bytes));
158   EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
159   memset(bytes, 'x', sizeof(bytes));
160   EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
161   bytes[5] = 0;
162   EXPECT_STREQ("hellx", bytes);
163   memset(bytes, 'x', sizeof(bytes));
164   EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
165   EXPECT_STREQ("hello", bytes);
166   memset(bytes, 'x', sizeof(bytes));
167   EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
168   EXPECT_STREQ("hello", bytes);
169   errno = 0;
170   memset(bytes, 'x', sizeof(bytes));
171   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
172   EXPECT_EQ(EILSEQ, errno);
173   bytes[3] = 0;
174   EXPECT_STREQ("hix", bytes);
175 
176   // wcsrtombs is a bit more informative...
177   memset(bytes, 'x', sizeof(bytes));
178   src = chars;
179   EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, NULL));
180   EXPECT_EQ(&chars[0], src); // No input consumed.
181   EXPECT_EQ(EILSEQ, errno);
182 
183   memset(bytes, 'x', sizeof(bytes));
184   src = chars;
185   EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, NULL));
186   EXPECT_EQ(&chars[4], src); // Some input consumed.
187   EXPECT_EQ(EILSEQ, errno);
188   bytes[5] = 0;
189   EXPECT_STREQ("hellx", bytes);
190 
191   memset(bytes, 'x', sizeof(bytes));
192   src = chars;
193   EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, NULL));
194   EXPECT_EQ(NULL, src); // All input consumed!
195   EXPECT_EQ(EILSEQ, errno);
196   EXPECT_STREQ("hello", bytes);
197 
198   memset(bytes, 'x', sizeof(bytes));
199   src = chars;
200   EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, NULL));
201   EXPECT_EQ(NULL, src); // All input consumed.
202   EXPECT_EQ(EILSEQ, errno);
203   EXPECT_STREQ("hello", bytes);
204 
205   memset(bytes, 'x', sizeof(bytes));
206   src = bad_chars;
207   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, NULL));
208   EXPECT_EQ(&bad_chars[2], src);
209   EXPECT_EQ(EILSEQ, errno);
210   bytes[3] = 0;
211   EXPECT_STREQ("hix", bytes);
212 
213   // Any non-initial state is invalid when calling wcsrtombs.
214   mbstate_t ps;
215   src = chars;
216   memset(&ps, 0, sizeof(ps));
217   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xc2", 1, &ps));
218   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 0, &ps));
219   EXPECT_EQ(EILSEQ, errno);
220 }
221 
TEST(wchar,limits)222 TEST(wchar, limits) {
223   ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
224 }
225 
TEST(wchar,wcsstr)226 TEST(wchar, wcsstr) {
227   const wchar_t* haystack = L"matches hello world, not the second hello world";
228   const wchar_t* empty_needle = L"";
229   const wchar_t* good_needle = L"ll";
230   const wchar_t* bad_needle = L"wort";
231 
232   ASSERT_EQ(haystack, wcsstr(haystack, empty_needle));
233   ASSERT_EQ(&haystack[10], wcsstr(haystack, good_needle));
234   ASSERT_EQ(NULL, wcsstr(haystack, bad_needle));
235 }
236 
TEST(wchar,wcsstr_80199)237 TEST(wchar, wcsstr_80199) {
238   // https://code.google.com/p/android/issues/detail?id=80199
239   ASSERT_TRUE(wcsstr(L"romrom", L"rom") != NULL);
240 }
241 
TEST(wchar,mbtowc)242 TEST(wchar, mbtowc) {
243   wchar_t out[8];
244 
245   out[0] = 'x';
246   ASSERT_EQ(0, mbtowc(out, "hello", 0));
247   ASSERT_EQ('x', out[0]);
248 
249   ASSERT_EQ(0, mbtowc(out, "hello", 0));
250   ASSERT_EQ(0, mbtowc(out, "", 0));
251   ASSERT_EQ(1, mbtowc(out, "hello", 1));
252   ASSERT_EQ(L'h', out[0]);
253 
254   ASSERT_EQ(0, mbtowc(NULL, "hello", 0));
255   ASSERT_EQ(0, mbtowc(NULL, "", 0));
256   ASSERT_EQ(1, mbtowc(NULL, "hello", 1));
257 
258   ASSERT_EQ(0, mbtowc(NULL, NULL, 0));
259 }
260 
TEST(wchar,mbrtowc)261 TEST(wchar, mbrtowc) {
262   wchar_t out[8];
263 
264   out[0] = 'x';
265   ASSERT_EQ(0U, mbrtowc(out, "hello", 0, NULL));
266   ASSERT_EQ('x', out[0]);
267 
268   ASSERT_EQ(0U, mbrtowc(out, "hello", 0, NULL));
269   ASSERT_EQ(0U, mbrtowc(out, "", 0, NULL));
270   ASSERT_EQ(1U, mbrtowc(out, "hello", 1, NULL));
271   ASSERT_EQ(L'h', out[0]);
272 
273   ASSERT_EQ(0U, mbrtowc(NULL, "hello", 0, NULL));
274   ASSERT_EQ(0U, mbrtowc(NULL, "", 0, NULL));
275   ASSERT_EQ(1U, mbrtowc(NULL, "hello", 1, NULL));
276 
277   ASSERT_EQ(0U, mbrtowc(NULL, NULL, 0, NULL));
278 
279   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
280   uselocale(LC_GLOBAL_LOCALE);
281 
282   // 1-byte UTF-8.
283   ASSERT_EQ(1U, mbrtowc(out, "abcdef", 6, NULL));
284   ASSERT_EQ(L'a', out[0]);
285   // 2-byte UTF-8.
286   ASSERT_EQ(2U, mbrtowc(out, "\xc2\xa2" "cdef", 6, NULL));
287   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
288   // 3-byte UTF-8.
289   ASSERT_EQ(3U, mbrtowc(out, "\xe2\x82\xac" "def", 6, NULL));
290   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
291   // 4-byte UTF-8.
292   ASSERT_EQ(4U, mbrtowc(out, "\xf0\xa4\xad\xa2" "ef", 6, NULL));
293   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
294 #if defined(__BIONIC__) // glibc allows this.
295   // Illegal 5-byte UTF-8.
296   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf8\xa1\xa2\xa3\xa4" "f", 6, NULL));
297   ASSERT_EQ(EILSEQ, errno);
298 #endif
299   // Illegal over-long sequence.
300   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf0\x82\x82\xac" "ef", 6, NULL));
301   ASSERT_EQ(EILSEQ, errno);
302 }
303 
test_mbrtowc_incomplete(mbstate_t * ps)304 void test_mbrtowc_incomplete(mbstate_t* ps) {
305   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
306   uselocale(LC_GLOBAL_LOCALE);
307 
308   wchar_t out;
309   // 2-byte UTF-8.
310   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
311   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
312   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
313   ASSERT_TRUE(mbsinit(ps));
314   // 3-byte UTF-8.
315   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
316   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
317   ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
318   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
319   ASSERT_TRUE(mbsinit(ps));
320   // 4-byte UTF-8.
321   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
322   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
323   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
324   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
325   ASSERT_TRUE(mbsinit(ps));
326 
327   // Invalid 2-byte
328   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
329   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
330   ASSERT_EQ(EILSEQ, errno);
331 }
332 
TEST(wchar,mbrtowc_incomplete)333 TEST(wchar, mbrtowc_incomplete) {
334   mbstate_t ps;
335   memset(&ps, 0, sizeof(ps));
336 
337   test_mbrtowc_incomplete(&ps);
338   test_mbrtowc_incomplete(NULL);
339 }
340 
test_mbsrtowcs(mbstate_t * ps)341 void test_mbsrtowcs(mbstate_t* ps) {
342   wchar_t out[4];
343 
344   const char* valid = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
345   ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
346   ASSERT_EQ(L'A', out[0]);
347   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
348   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
349   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
350   // Check that valid has advanced to the next unread character.
351   ASSERT_EQ('e', *valid);
352 
353   wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
354   ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
355   ASSERT_EQ(L'e', out[0]);
356   ASSERT_EQ(L'f', out[1]);
357   ASSERT_EQ(L'\0', out[2]);
358   // Check that we didn't clobber the rest of out.
359   ASSERT_EQ(L'x', out[3]);
360   // Check that valid has advanced to the end of the string.
361   ASSERT_EQ(nullptr, valid);
362 
363   const char* invalid = "A" "\xc2\x20" "ef";
364   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
365   EXPECT_EQ(EILSEQ, errno);
366   ASSERT_EQ('\xc2', *invalid);
367 
368   const char* incomplete = "A" "\xc2";
369   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
370   EXPECT_EQ(EILSEQ, errno);
371   ASSERT_EQ('\xc2', *incomplete);
372 }
373 
TEST(wchar,mbsrtowcs)374 TEST(wchar, mbsrtowcs) {
375   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
376   uselocale(LC_GLOBAL_LOCALE);
377 
378   mbstate_t ps;
379   memset(&ps, 0, sizeof(ps));
380   test_mbsrtowcs(&ps);
381   test_mbsrtowcs(NULL);
382 
383   // Invalid multi byte continuation.
384   const char* invalid = "\x20";
385   wchar_t out;
386   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
387   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
388   EXPECT_EQ(EILSEQ, errno);
389   ASSERT_EQ('\x20', *invalid);
390 }
391 
TEST(wchar,wcstod)392 TEST(wchar, wcstod) {
393   ASSERT_DOUBLE_EQ(1.23, wcstod(L"1.23", NULL));
394 }
395 
TEST(wchar,wcstof)396 TEST(wchar, wcstof) {
397   ASSERT_FLOAT_EQ(1.23f, wcstof(L"1.23", NULL));
398 }
399 
TEST(wchar,wcstol)400 TEST(wchar, wcstol) {
401   ASSERT_EQ(123L, wcstol(L"123", NULL, 0));
402 }
403 
TEST(wchar,wcstoll)404 TEST(wchar, wcstoll) {
405   ASSERT_EQ(123LL, wcstol(L"123", NULL, 0));
406 }
407 
TEST(wchar,wcstold)408 TEST(wchar, wcstold) {
409   ASSERT_DOUBLE_EQ(1.23L, wcstold(L"1.23", NULL));
410 }
411 
TEST(wchar,wcstoul)412 TEST(wchar, wcstoul) {
413   ASSERT_EQ(123UL, wcstoul(L"123", NULL, 0));
414 }
415 
TEST(wchar,wcstoull)416 TEST(wchar, wcstoull) {
417   ASSERT_EQ(123ULL, wcstoul(L"123", NULL, 0));
418 }
419 
TEST(wchar,mbsnrtowcs)420 TEST(wchar, mbsnrtowcs) {
421   wchar_t dst[128];
422   const char* s = "hello, world!";
423   const char* src;
424 
425   memset(dst, 0, sizeof(dst));
426   src = s;
427   ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, NULL));
428 
429   memset(dst, 0, sizeof(dst));
430   src = s;
431   ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, NULL)); // glibc chokes on SIZE_MAX here.
432   ASSERT_EQ(L'h', dst[0]);
433   ASSERT_EQ(L'e', dst[1]);
434   ASSERT_EQ(&s[2], src);
435 
436   memset(dst, 0, sizeof(dst));
437   src = s;
438   ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, NULL));
439   ASSERT_EQ(L'h', dst[0]);
440   ASSERT_EQ(L'e', dst[1]);
441   ASSERT_EQ(L'l', dst[2]);
442   ASSERT_EQ(&s[3], src);
443 }
444 
TEST(wchar,wcsftime)445 TEST(wchar, wcsftime) {
446   setenv("TZ", "UTC", 1);
447 
448   struct tm t;
449   memset(&t, 0, sizeof(tm));
450   t.tm_year = 200;
451   t.tm_mon = 2;
452   t.tm_mday = 10;
453 
454   wchar_t buf[64];
455 
456   EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
457   EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
458 }
459 
TEST(wchar,wmemmove_smoke)460 TEST(wchar, wmemmove_smoke) {
461   const wchar_t const_wstr[] = L"This is a test of something or other.....";
462   wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
463 
464   EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
465   EXPECT_STREQ(const_wstr, wstr);
466 
467   EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
468   EXPECT_STREQ(L"This This is a test of something or other", wstr);
469 }
470 
TEST(wchar,wmemcpy_smoke)471 TEST(wchar, wmemcpy_smoke) {
472   const wchar_t src[] = L"Source string";
473   wchar_t dst[NUM_WCHARS(sizeof(src))];
474 
475   EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
476   EXPECT_STREQ(dst, src);
477 }
478 
TEST(wchar,wcpcpy_smoke)479 TEST(wchar, wcpcpy_smoke) {
480   const wchar_t src[] = L"Source string";
481   wchar_t dst[NUM_WCHARS(sizeof(src))];
482 
483   EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
484   EXPECT_STREQ(dst, src);
485 }
486 
TEST(wchar,wcpncpy_smoke)487 TEST(wchar, wcpncpy_smoke) {
488   const wchar_t src[] = L"Source string";
489   wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
490 
491   size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
492   EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
493   EXPECT_STREQ(dst, src);
494 
495   EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
496   dst[6] = L'\0';
497   EXPECT_STREQ(dst, L"Source");
498 
499   wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
500   EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
501   EXPECT_STREQ(dst, src);
502   EXPECT_EQ(dst[src_len], L'\0');
503   EXPECT_EQ(dst[src_len+1], L'\0');
504   EXPECT_EQ(dst[src_len+2], L'\0');
505   EXPECT_EQ(dst[src_len+3], L'\0');
506   EXPECT_EQ(dst[src_len+4], L'x');
507 }
508 
TEST(wchar,wcscpy_smoke)509 TEST(wchar, wcscpy_smoke) {
510   const wchar_t src[] = L"Source string";
511   wchar_t dst[NUM_WCHARS(sizeof(src))];
512 
513   EXPECT_EQ(dst, wcscpy(dst, src));
514   EXPECT_STREQ(src, dst);
515 }
516 
TEST(wchar,wcsncpy_smoke)517 TEST(wchar, wcsncpy_smoke) {
518   const wchar_t src[] = L"Source string";
519   wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
520 
521   size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
522   EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
523   EXPECT_STREQ(dst, src);
524 
525   EXPECT_EQ(dst, wcsncpy(dst, src, 6));
526   dst[6] = L'\0';
527   EXPECT_STREQ(dst, L"Source");
528 
529   wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
530   EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
531   EXPECT_STREQ(dst, src);
532   EXPECT_EQ(dst[src_len], L'\0');
533   EXPECT_EQ(dst[src_len+1], L'\0');
534   EXPECT_EQ(dst[src_len+2], L'\0');
535   EXPECT_EQ(dst[src_len+3], L'\0');
536   EXPECT_EQ(dst[src_len+4], L'x');
537 }
538 
TEST(wchar,mbrtowc_15439554)539 TEST(wchar, mbrtowc_15439554) {
540   // http://b/15439554
541   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
542   uselocale(LC_GLOBAL_LOCALE);
543 
544   ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
545   ASSERT_GE(MB_CUR_MAX, 4U);
546 
547   wchar_t wc;
548   size_t n;
549 
550   // 1-byte character.
551   n = mbrtowc(&wc, "x", MB_CUR_MAX, NULL);
552   EXPECT_EQ(1U, n);
553   EXPECT_EQ(L'x', wc);
554   // 2-byte character.
555   n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, NULL);
556   EXPECT_EQ(2U, n);
557   EXPECT_EQ(L'¢', wc);
558   // 3-byte character.
559   n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, NULL);
560   EXPECT_EQ(3U, n);
561   EXPECT_EQ(L'€', wc);
562   // 4-byte character.
563   n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, NULL);
564   EXPECT_EQ(4U, n);
565   EXPECT_EQ(L'��', wc);
566 }
567 
TEST(wchar,open_wmemstream)568 TEST(wchar, open_wmemstream) {
569   wchar_t* p = nullptr;
570   size_t size = 0;
571   FILE* fp = open_wmemstream(&p, &size);
572   ASSERT_NE(EOF, fputws(L"hello, world!", fp));
573   fclose(fp);
574 
575   ASSERT_STREQ(L"hello, world!", p);
576   ASSERT_EQ(wcslen(L"hello, world!"), size);
577   free(p);
578 }
579 
TEST(stdio,open_wmemstream_EINVAL)580 TEST(stdio, open_wmemstream_EINVAL) {
581 #if defined(__BIONIC__)
582   wchar_t* p;
583   size_t size;
584 
585   // Invalid buffer.
586   errno = 0;
587   ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
588   ASSERT_EQ(EINVAL, errno);
589 
590   // Invalid size.
591   errno = 0;
592   ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
593   ASSERT_EQ(EINVAL, errno);
594 #else
595   GTEST_LOG_(INFO) << "This test does nothing.\n";
596 #endif
597 }
598 
TEST(wchar,wcstol_EINVAL)599 TEST(wchar, wcstol_EINVAL) {
600   errno = 0;
601   wcstol(L"123", NULL, -1);
602   ASSERT_EQ(EINVAL, errno);
603   errno = 0;
604   wcstol(L"123", NULL, 1);
605   ASSERT_EQ(EINVAL, errno);
606   errno = 0;
607   wcstol(L"123", NULL, 37);
608   ASSERT_EQ(EINVAL, errno);
609 }
610 
TEST(wchar,wcstoll_EINVAL)611 TEST(wchar, wcstoll_EINVAL) {
612   errno = 0;
613   wcstoll(L"123", NULL, -1);
614   ASSERT_EQ(EINVAL, errno);
615   errno = 0;
616   wcstoll(L"123", NULL, 1);
617   ASSERT_EQ(EINVAL, errno);
618   errno = 0;
619   wcstoll(L"123", NULL, 37);
620   ASSERT_EQ(EINVAL, errno);
621 }
622 
TEST(wchar,wcstoul_EINVAL)623 TEST(wchar, wcstoul_EINVAL) {
624   errno = 0;
625   wcstoul(L"123", NULL, -1);
626   ASSERT_EQ(EINVAL, errno);
627   errno = 0;
628   wcstoul(L"123", NULL, 1);
629   ASSERT_EQ(EINVAL, errno);
630   errno = 0;
631   wcstoul(L"123", NULL, 37);
632   ASSERT_EQ(EINVAL, errno);
633 }
634 
TEST(wchar,wcstoull_EINVAL)635 TEST(wchar, wcstoull_EINVAL) {
636   errno = 0;
637   wcstoull(L"123", NULL, -1);
638   ASSERT_EQ(EINVAL, errno);
639   errno = 0;
640   wcstoull(L"123", NULL, 1);
641   ASSERT_EQ(EINVAL, errno);
642   errno = 0;
643   wcstoull(L"123", NULL, 37);
644   ASSERT_EQ(EINVAL, errno);
645 }
646 
TEST(wchar,wcstoll_l_EINVAL)647 TEST(wchar, wcstoll_l_EINVAL) {
648   errno = 0;
649   wcstoll_l(L"123", NULL, -1, LC_GLOBAL_LOCALE);
650   ASSERT_EQ(EINVAL, errno);
651   errno = 0;
652   wcstoll_l(L"123", NULL, 1, LC_GLOBAL_LOCALE);
653   ASSERT_EQ(EINVAL, errno);
654   errno = 0;
655   wcstoll_l(L"123", NULL, 37, LC_GLOBAL_LOCALE);
656   ASSERT_EQ(EINVAL, errno);
657 }
658 
TEST(wchar,wcstoull_l_EINVAL)659 TEST(wchar, wcstoull_l_EINVAL) {
660   errno = 0;
661   wcstoull_l(L"123", NULL, -1, LC_GLOBAL_LOCALE);
662   ASSERT_EQ(EINVAL, errno);
663   errno = 0;
664   wcstoull_l(L"123", NULL, 1, LC_GLOBAL_LOCALE);
665   ASSERT_EQ(EINVAL, errno);
666   errno = 0;
667   wcstoull_l(L"123", NULL, 37, LC_GLOBAL_LOCALE);
668   ASSERT_EQ(EINVAL, errno);
669 }
670 
TEST(wchar,wmempcpy)671 TEST(wchar, wmempcpy) {
672   wchar_t dst[6];
673   ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
674 }
675