• 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 <wchar.h>
26 
27 #include "utils.h"
28 
29 #define NUM_WCHARS(num_bytes) ((num_bytes)/sizeof(wchar_t))
30 
TEST(wchar,sizeof_wchar_t)31 TEST(wchar, sizeof_wchar_t) {
32   EXPECT_EQ(4U, sizeof(wchar_t));
33   EXPECT_EQ(4U, sizeof(wint_t));
34 }
35 
TEST(wchar,mbrlen)36 TEST(wchar, mbrlen) {
37   char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
38   EXPECT_EQ(0U, mbrlen(&bytes[0], 0, nullptr));
39   EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
40 
41   EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
42   EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
43 }
44 
TEST(wchar,wctomb_wcrtomb)45 TEST(wchar, wctomb_wcrtomb) {
46   // wctomb and wcrtomb behave differently when s == NULL.
47   EXPECT_EQ(0, wctomb(nullptr, L'h'));
48   EXPECT_EQ(0, wctomb(nullptr, L'\0'));
49   EXPECT_EQ(1U, wcrtomb(nullptr, L'\0', nullptr));
50   EXPECT_EQ(1U, wcrtomb(nullptr, L'h', nullptr));
51 
52   char bytes[MB_LEN_MAX];
53 
54   // wctomb and wcrtomb behave similarly for the null wide character.
55   EXPECT_EQ(1, wctomb(bytes, L'\0'));
56   EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
57 
58   // ...and for regular characters.
59   memset(bytes, 0, sizeof(bytes));
60   EXPECT_EQ(1, wctomb(bytes, L'h'));
61   EXPECT_EQ('h', bytes[0]);
62   memset(bytes, 0, sizeof(bytes));
63   EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
64   EXPECT_EQ('h', bytes[0]);
65 
66   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
67   uselocale(LC_GLOBAL_LOCALE);
68 
69   // 1-byte UTF-8.
70   memset(bytes, 0, sizeof(bytes));
71   EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
72   EXPECT_EQ('h', bytes[0]);
73   // 2-byte UTF-8.
74   memset(bytes, 0, sizeof(bytes));
75   EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
76   EXPECT_EQ('\xc2', bytes[0]);
77   EXPECT_EQ('\xa2', bytes[1]);
78   // 3-byte UTF-8.
79   memset(bytes, 0, sizeof(bytes));
80   EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
81   EXPECT_EQ('\xe2', bytes[0]);
82   EXPECT_EQ('\x82', bytes[1]);
83   EXPECT_EQ('\xac', bytes[2]);
84   // 4-byte UTF-8.
85   memset(bytes, 0, sizeof(bytes));
86   EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
87   EXPECT_EQ('\xf0', bytes[0]);
88   EXPECT_EQ('\xa4', bytes[1]);
89   EXPECT_EQ('\xad', bytes[2]);
90   EXPECT_EQ('\xa2', bytes[3]);
91   // Invalid code point.
92   EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
93   EXPECT_EQ(EILSEQ, errno);
94 }
95 
TEST(wchar,wcrtomb_start_state)96 TEST(wchar, wcrtomb_start_state) {
97   char out[MB_LEN_MAX];
98   mbstate_t ps;
99 
100   // Any non-initial state is invalid when calling wcrtomb.
101   memset(&ps, 0, sizeof(ps));
102   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
103   EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
104   EXPECT_EQ(EILSEQ, errno);
105 
106   // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
107   // state should be reset.
108   memset(&ps, 0, sizeof(ps));
109   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
110   EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
111   EXPECT_TRUE(mbsinit(&ps));
112 
113   memset(&ps, 0, sizeof(ps));
114   EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
115   EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
116   EXPECT_TRUE(mbsinit(&ps));
117 }
118 
TEST(wchar,wcstombs_wcrtombs)119 TEST(wchar, wcstombs_wcrtombs) {
120   const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
121   const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
122   const wchar_t* src;
123   char bytes[BUFSIZ];
124 
125   // Given a NULL destination, these functions count valid characters.
126   EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
127   EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
128   EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
129   src = chars;
130   EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
131   EXPECT_EQ(&chars[0], src);
132   src = chars;
133   EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
134   EXPECT_EQ(&chars[0], src);
135   src = chars;
136   EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
137   EXPECT_EQ(&chars[0], src);
138 
139   // An unrepresentable char just returns an error from wcstombs...
140   errno = 0;
141   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
142   EXPECT_EQ(EILSEQ, errno);
143   errno = 0;
144   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
145   EXPECT_EQ(EILSEQ, errno);
146 
147   // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
148   // to actually convert anything...
149   errno = 0;
150   src = bad_chars;
151   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
152   EXPECT_EQ(&bad_chars[0], src);
153   EXPECT_EQ(EILSEQ, errno);
154   errno = 0;
155   src = bad_chars;
156   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
157   EXPECT_EQ(&bad_chars[0], src);
158   EXPECT_EQ(EILSEQ, errno);
159 
160   // Okay, now let's test actually converting something...
161   memset(bytes, 'x', sizeof(bytes));
162   EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
163   memset(bytes, 'x', sizeof(bytes));
164   EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
165   bytes[5] = 0;
166   EXPECT_STREQ("hellx", bytes);
167   memset(bytes, 'x', sizeof(bytes));
168   EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
169   EXPECT_STREQ("hello", bytes);
170   memset(bytes, 'x', sizeof(bytes));
171   EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
172   EXPECT_STREQ("hello", bytes);
173   errno = 0;
174   memset(bytes, 'x', sizeof(bytes));
175   EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
176   EXPECT_EQ(EILSEQ, errno);
177   bytes[3] = 0;
178   EXPECT_STREQ("hix", bytes);
179 
180   // wcsrtombs is a bit more informative...
181   memset(bytes, 'x', sizeof(bytes));
182   src = chars;
183   EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
184   EXPECT_EQ(&chars[0], src); // No input consumed.
185   EXPECT_EQ(EILSEQ, errno);
186 
187   memset(bytes, 'x', sizeof(bytes));
188   src = chars;
189   EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
190   EXPECT_EQ(&chars[4], src); // Some input consumed.
191   EXPECT_EQ(EILSEQ, errno);
192   bytes[5] = 0;
193   EXPECT_STREQ("hellx", bytes);
194 
195   memset(bytes, 'x', sizeof(bytes));
196   src = chars;
197   EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
198   EXPECT_EQ(nullptr, src); // All input consumed!
199   EXPECT_EQ(EILSEQ, errno);
200   EXPECT_STREQ("hello", bytes);
201 
202   memset(bytes, 'x', sizeof(bytes));
203   src = chars;
204   EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
205   EXPECT_EQ(nullptr, src); // All input consumed.
206   EXPECT_EQ(EILSEQ, errno);
207   EXPECT_STREQ("hello", bytes);
208 
209   memset(bytes, 'x', sizeof(bytes));
210   src = bad_chars;
211   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
212   EXPECT_EQ(&bad_chars[2], src);
213   EXPECT_EQ(EILSEQ, errno);
214   bytes[3] = 0;
215   EXPECT_STREQ("hix", bytes);
216 
217   // Any non-initial state is invalid when calling wcsrtombs.
218   mbstate_t ps;
219   src = chars;
220   memset(&ps, 0, sizeof(ps));
221   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
222   EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
223   EXPECT_EQ(EILSEQ, errno);
224 }
225 
TEST(wchar,limits)226 TEST(wchar, limits) {
227   ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
228 }
229 
TEST(wchar,wcsstr)230 TEST(wchar, wcsstr) {
231   const wchar_t* haystack = L"matches hello world, not the second hello world";
232   const wchar_t* empty_needle = L"";
233   const wchar_t* good_needle = L"ll";
234   const wchar_t* bad_needle = L"wort";
235 
236   ASSERT_EQ(haystack, wcsstr(haystack, empty_needle));
237   ASSERT_EQ(&haystack[10], wcsstr(haystack, good_needle));
238   ASSERT_EQ(nullptr, wcsstr(haystack, bad_needle));
239 }
240 
TEST(wchar,wcsstr_80199)241 TEST(wchar, wcsstr_80199) {
242   // https://code.google.com/p/android/issues/detail?id=80199
243   ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
244 }
245 
TEST(wchar,mbtowc)246 TEST(wchar, mbtowc) {
247   wchar_t out[8];
248 
249   out[0] = 'x';
250   ASSERT_EQ(0, mbtowc(out, "hello", 0));
251   ASSERT_EQ('x', out[0]);
252 
253   ASSERT_EQ(0, mbtowc(out, "hello", 0));
254   ASSERT_EQ(0, mbtowc(out, "", 0));
255   ASSERT_EQ(1, mbtowc(out, "hello", 1));
256   ASSERT_EQ(L'h', out[0]);
257 
258   ASSERT_EQ(0, mbtowc(nullptr, "hello", 0));
259   ASSERT_EQ(0, mbtowc(nullptr, "", 0));
260   ASSERT_EQ(1, mbtowc(nullptr, "hello", 1));
261 
262   ASSERT_EQ(0, mbtowc(nullptr, nullptr, 0));
263 }
264 
TEST(wchar,mbrtowc)265 TEST(wchar, mbrtowc) {
266   wchar_t out[8];
267 
268   out[0] = 'x';
269   ASSERT_EQ(0U, mbrtowc(out, "hello", 0, nullptr));
270   ASSERT_EQ('x', out[0]);
271 
272   ASSERT_EQ(0U, mbrtowc(out, "hello", 0, nullptr));
273   ASSERT_EQ(0U, mbrtowc(out, "", 0, nullptr));
274   ASSERT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
275   ASSERT_EQ(L'h', out[0]);
276 
277   ASSERT_EQ(0U, mbrtowc(nullptr, "hello", 0, nullptr));
278   ASSERT_EQ(0U, mbrtowc(nullptr, "", 0, nullptr));
279   ASSERT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
280 
281   ASSERT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
282 
283   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
284   uselocale(LC_GLOBAL_LOCALE);
285 
286   // 1-byte UTF-8.
287   ASSERT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
288   ASSERT_EQ(L'a', out[0]);
289   // 2-byte UTF-8.
290   ASSERT_EQ(2U, mbrtowc(out, "\xc2\xa2" "cdef", 6, nullptr));
291   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
292   // 3-byte UTF-8.
293   ASSERT_EQ(3U, mbrtowc(out, "\xe2\x82\xac" "def", 6, nullptr));
294   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
295   // 4-byte UTF-8.
296   ASSERT_EQ(4U, mbrtowc(out, "\xf0\xa4\xad\xa2" "ef", 6, nullptr));
297   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
298 #if defined(__BIONIC__) // glibc allows this.
299   // Illegal 5-byte UTF-8.
300   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf8\xa1\xa2\xa3\xa4" "f", 6, nullptr));
301   ASSERT_EQ(EILSEQ, errno);
302 #endif
303   // Illegal over-long sequence.
304   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf0\x82\x82\xac" "ef", 6, nullptr));
305   ASSERT_EQ(EILSEQ, errno);
306 }
307 
TEST(wchar,mbrtowc_valid_non_characters)308 TEST(wchar, mbrtowc_valid_non_characters) {
309   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
310   uselocale(LC_GLOBAL_LOCALE);
311 
312   wchar_t out[8] = {};
313 
314   ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
315   ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
316   ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
317   ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
318 }
319 
TEST(wchar,mbrtowc_out_of_range)320 TEST(wchar, mbrtowc_out_of_range) {
321   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
322   uselocale(LC_GLOBAL_LOCALE);
323 
324   wchar_t out[8] = {};
325   errno = 0;
326   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr));
327   ASSERT_EQ(EILSEQ, errno);
328 }
329 
test_mbrtowc_incomplete(mbstate_t * ps)330 static void test_mbrtowc_incomplete(mbstate_t* ps) {
331   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
332   uselocale(LC_GLOBAL_LOCALE);
333 
334   wchar_t out;
335   // 2-byte UTF-8.
336   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
337   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
338   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
339   ASSERT_TRUE(mbsinit(ps));
340   // 3-byte UTF-8.
341   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
342   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
343   ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
344   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
345   ASSERT_TRUE(mbsinit(ps));
346   // 4-byte UTF-8.
347   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
348   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
349   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
350   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
351   ASSERT_TRUE(mbsinit(ps));
352 
353   // Invalid 2-byte
354   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
355   ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
356   ASSERT_EQ(EILSEQ, errno);
357 }
358 
TEST(wchar,mbrtowc_incomplete)359 TEST(wchar, mbrtowc_incomplete) {
360   mbstate_t ps;
361   memset(&ps, 0, sizeof(ps));
362 
363   test_mbrtowc_incomplete(&ps);
364   test_mbrtowc_incomplete(nullptr);
365 }
366 
test_mbsrtowcs(mbstate_t * ps)367 static void test_mbsrtowcs(mbstate_t* ps) {
368   constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
369   constexpr const char* INVALID = "A" "\xc2\x20" "ef";
370   constexpr const char* INCOMPLETE = "A" "\xc2";
371   wchar_t out[4];
372 
373   const char* valid = VALID;
374   ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
375   ASSERT_EQ(L'A', out[0]);
376   ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
377   ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
378   ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
379   // Check that valid has advanced to the next unread character.
380   ASSERT_EQ('e', *valid);
381 
382   wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
383   ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
384   ASSERT_EQ(L'e', out[0]);
385   ASSERT_EQ(L'f', out[1]);
386   ASSERT_EQ(L'\0', out[2]);
387   // Check that we didn't clobber the rest of out.
388   ASSERT_EQ(L'x', out[3]);
389   // Check that valid has advanced to the end of the string.
390   ASSERT_EQ(nullptr, valid);
391 
392   const char* invalid = INVALID;
393   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
394   EXPECT_EQ(EILSEQ, errno);
395   ASSERT_EQ('\xc2', *invalid);
396 
397   const char* incomplete = INCOMPLETE;
398   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
399   EXPECT_EQ(EILSEQ, errno);
400   ASSERT_EQ('\xc2', *incomplete);
401 
402   // If dst is null, *src shouldn't be updated.
403   // https://code.google.com/p/android/issues/detail?id=166381
404   const char* mbs = VALID;
405   EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
406   EXPECT_EQ(VALID, mbs);
407   mbs = INVALID;
408   EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
409   EXPECT_EQ(INVALID, mbs);
410   mbs = INCOMPLETE;
411   EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
412   EXPECT_EQ(INCOMPLETE, mbs);
413 }
414 
TEST(wchar,mbsrtowcs)415 TEST(wchar, mbsrtowcs) {
416   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
417   uselocale(LC_GLOBAL_LOCALE);
418 
419   mbstate_t ps;
420   memset(&ps, 0, sizeof(ps));
421   test_mbsrtowcs(&ps);
422   test_mbsrtowcs(nullptr);
423 
424   // Invalid multi byte continuation.
425   const char* invalid = "\x20";
426   wchar_t out;
427   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
428   ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
429   EXPECT_EQ(EILSEQ, errno);
430   ASSERT_EQ('\x20', *invalid);
431 }
432 
433 template <typename T>
434 using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
435 
436 template <typename T>
TestSingleWcsToInt(WcsToIntFn<T> fn,const wchar_t * str,int base,T expected_value,ptrdiff_t expected_len)437 void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
438                         T expected_value, ptrdiff_t expected_len) {
439   wchar_t* p;
440   ASSERT_EQ(expected_value, fn(str, &p, base));
441   ASSERT_EQ(expected_len, p - str) << str;
442 }
443 
444 template <typename T>
TestWcsToInt(WcsToIntFn<T> fn)445 void TestWcsToInt(WcsToIntFn<T> fn) {
446   TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
447   TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
448   TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
449   TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
450   TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
451   TestSingleWcsToInt(fn, L"   123 45", 0, static_cast<T>(123), 6);
452   TestSingleWcsToInt(fn, L"  -123", 0, static_cast<T>(-123), 6);
453   TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
454 }
455 
456 template <typename T>
TestWcsToIntLimits(WcsToIntFn<T> fn,const wchar_t * min_str,const wchar_t * max_str)457 void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
458                         const wchar_t* max_str) {
459   if (std::is_signed<T>::value) {
460     ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
461   } else {
462     // If the subject sequence begins with a <hyphen-minus>, the value resulting
463     // from the conversion shall be negated.
464     // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
465     ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
466   }
467   ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
468 }
469 
TEST(wchar,wcstol)470 TEST(wchar, wcstol) {
471   TestWcsToInt(wcstol);
472 }
473 
TEST(wchar,wcstol_limits)474 TEST(wchar, wcstol_limits) {
475   if (sizeof(long) == 8) {
476     TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
477   } else {
478     TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
479   }
480 }
481 
TEST(wchar,wcstoul)482 TEST(wchar, wcstoul) {
483   TestWcsToInt(wcstoul);
484 }
485 
TEST(wchar,wcstoul_limits)486 TEST(wchar, wcstoul_limits) {
487   if (sizeof(long) == 8) {
488     TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
489   } else {
490     TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
491   }
492 }
493 
TEST(wchar,wcstoll)494 TEST(wchar, wcstoll) {
495   TestWcsToInt(wcstoll);
496 }
497 
TEST(wchar,wcstoll_limits)498 TEST(wchar, wcstoll_limits) {
499   TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
500 }
501 
TEST(wchar,wcstoull)502 TEST(wchar, wcstoull) {
503   TestWcsToInt(wcstoull);
504 }
505 
TEST(wchar,wcstoull_limits)506 TEST(wchar, wcstoull_limits) {
507   TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
508 }
509 
TEST(wchar,wcstoimax)510 TEST(wchar, wcstoimax) {
511   TestWcsToInt(wcstoimax);
512 }
513 
TEST(wchar,wcstoimax_limits)514 TEST(wchar, wcstoimax_limits) {
515   TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
516                      L"9223372036854775808");
517 }
518 
TEST(wchar,wcstoumax)519 TEST(wchar, wcstoumax) {
520   TestWcsToInt(wcstoumax);
521 }
522 
TEST(wchar,wcstoumax_limits)523 TEST(wchar, wcstoumax_limits) {
524   TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
525 }
526 
TEST(wchar,mbsnrtowcs)527 TEST(wchar, mbsnrtowcs) {
528   wchar_t dst[128];
529   const char* s = "hello, world!";
530   const char* src;
531 
532   memset(dst, 0, sizeof(dst));
533   src = s;
534   ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
535 
536   memset(dst, 0, sizeof(dst));
537   src = s;
538   ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
539   ASSERT_EQ(L'h', dst[0]);
540   ASSERT_EQ(L'e', dst[1]);
541   ASSERT_EQ(&s[2], src);
542 
543   memset(dst, 0, sizeof(dst));
544   src = s;
545   ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
546   ASSERT_EQ(L'h', dst[0]);
547   ASSERT_EQ(L'e', dst[1]);
548   ASSERT_EQ(L'l', dst[2]);
549   ASSERT_EQ(&s[3], src);
550 
551   memset(dst, 0, sizeof(dst));
552   const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
553   src = incomplete;
554   errno = 0;
555   ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
556   ASSERT_EQ(EILSEQ, errno);
557 
558   src = incomplete;
559   errno = 0;
560   ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
561   ASSERT_EQ(EILSEQ, errno);
562 }
563 
TEST(wchar,wcsftime__wcsftime_l)564 TEST(wchar, wcsftime__wcsftime_l) {
565   setenv("TZ", "UTC", 1);
566 
567   struct tm t;
568   memset(&t, 0, sizeof(tm));
569   t.tm_year = 200;
570   t.tm_mon = 2;
571   t.tm_mday = 10;
572 
573   wchar_t buf[64];
574 
575   EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
576   EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
577   EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, LC_GLOBAL_LOCALE));
578   EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
579 }
580 
TEST(wchar,wmemmove_smoke)581 TEST(wchar, wmemmove_smoke) {
582   const wchar_t const_wstr[] = L"This is a test of something or other.....";
583   wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
584 
585   EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
586   EXPECT_STREQ(const_wstr, wstr);
587 
588   EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
589   EXPECT_STREQ(L"This This is a test of something or other", wstr);
590 }
591 
TEST(wchar,wmemcpy_smoke)592 TEST(wchar, wmemcpy_smoke) {
593   const wchar_t src[] = L"Source string";
594   wchar_t dst[NUM_WCHARS(sizeof(src))];
595 
596   EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
597   EXPECT_STREQ(dst, src);
598 }
599 
TEST(wchar,wcpcpy_smoke)600 TEST(wchar, wcpcpy_smoke) {
601   const wchar_t src[] = L"Source string";
602   wchar_t dst[NUM_WCHARS(sizeof(src))];
603 
604   EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
605   EXPECT_STREQ(dst, src);
606 }
607 
TEST(wchar,wcpncpy_smoke)608 TEST(wchar, wcpncpy_smoke) {
609   const wchar_t src[] = L"Source string";
610   wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
611 
612   size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
613   EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
614   EXPECT_STREQ(dst, src);
615 
616   EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
617   dst[6] = L'\0';
618   EXPECT_STREQ(dst, L"Source");
619 
620   wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
621   EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
622   EXPECT_STREQ(dst, src);
623   EXPECT_EQ(dst[src_len], L'\0');
624   EXPECT_EQ(dst[src_len+1], L'\0');
625   EXPECT_EQ(dst[src_len+2], L'\0');
626   EXPECT_EQ(dst[src_len+3], L'\0');
627   EXPECT_EQ(dst[src_len+4], L'x');
628 }
629 
TEST(wchar,wcscpy_smoke)630 TEST(wchar, wcscpy_smoke) {
631   const wchar_t src[] = L"Source string";
632   wchar_t dst[NUM_WCHARS(sizeof(src))];
633 
634   EXPECT_EQ(dst, wcscpy(dst, src));
635   EXPECT_STREQ(src, dst);
636 }
637 
TEST(wchar,wcsncpy_smoke)638 TEST(wchar, wcsncpy_smoke) {
639   const wchar_t src[] = L"Source string";
640   wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
641 
642   size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
643   EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
644   EXPECT_STREQ(dst, src);
645 
646   EXPECT_EQ(dst, wcsncpy(dst, src, 6));
647   dst[6] = L'\0';
648   EXPECT_STREQ(dst, L"Source");
649 
650   wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
651   EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
652   EXPECT_STREQ(dst, src);
653   EXPECT_EQ(dst[src_len], L'\0');
654   EXPECT_EQ(dst[src_len+1], L'\0');
655   EXPECT_EQ(dst[src_len+2], L'\0');
656   EXPECT_EQ(dst[src_len+3], L'\0');
657   EXPECT_EQ(dst[src_len+4], L'x');
658 }
659 
TEST(wchar,mbrtowc_15439554)660 TEST(wchar, mbrtowc_15439554) {
661   // http://b/15439554
662   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
663   uselocale(LC_GLOBAL_LOCALE);
664 
665   ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
666   ASSERT_GE(MB_CUR_MAX, 4U);
667 
668   wchar_t wc;
669   size_t n;
670 
671   // 1-byte character.
672   n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
673   EXPECT_EQ(1U, n);
674   EXPECT_EQ(L'x', wc);
675   // 2-byte character.
676   n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
677   EXPECT_EQ(2U, n);
678   EXPECT_EQ(L'¢', wc);
679   // 3-byte character.
680   n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
681   EXPECT_EQ(3U, n);
682   EXPECT_EQ(L'€', wc);
683   // 4-byte character.
684   n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
685   EXPECT_EQ(4U, n);
686   EXPECT_EQ(L'��', wc);
687 }
688 
TEST(wchar,open_wmemstream)689 TEST(wchar, open_wmemstream) {
690   wchar_t* p = nullptr;
691   size_t size = 0;
692   FILE* fp = open_wmemstream(&p, &size);
693   ASSERT_NE(EOF, fputws(L"hello, world!", fp));
694   fclose(fp);
695 
696   ASSERT_STREQ(L"hello, world!", p);
697   ASSERT_EQ(wcslen(L"hello, world!"), size);
698   free(p);
699 }
700 
TEST(stdio,open_wmemstream_EINVAL)701 TEST(stdio, open_wmemstream_EINVAL) {
702 #if defined(__BIONIC__)
703   wchar_t* p;
704   size_t size;
705 
706   // Invalid buffer.
707   errno = 0;
708   ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
709   ASSERT_EQ(EINVAL, errno);
710 
711   // Invalid size.
712   errno = 0;
713   ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
714   ASSERT_EQ(EINVAL, errno);
715 #else
716   GTEST_SKIP() << "This test is bionic-specific";
717 #endif
718 }
719 
TEST(wchar,wcstol_EINVAL)720 TEST(wchar, wcstol_EINVAL) {
721   errno = 0;
722   wcstol(L"123", nullptr, -1);
723   ASSERT_EQ(EINVAL, errno);
724   errno = 0;
725   wcstol(L"123", nullptr, 1);
726   ASSERT_EQ(EINVAL, errno);
727   errno = 0;
728   wcstol(L"123", nullptr, 37);
729   ASSERT_EQ(EINVAL, errno);
730 }
731 
TEST(wchar,wcstoll_EINVAL)732 TEST(wchar, wcstoll_EINVAL) {
733   errno = 0;
734   wcstoll(L"123", nullptr, -1);
735   ASSERT_EQ(EINVAL, errno);
736   errno = 0;
737   wcstoll(L"123", nullptr, 1);
738   ASSERT_EQ(EINVAL, errno);
739   errno = 0;
740   wcstoll(L"123", nullptr, 37);
741   ASSERT_EQ(EINVAL, errno);
742 }
743 
TEST(wchar,wcstoul_EINVAL)744 TEST(wchar, wcstoul_EINVAL) {
745   errno = 0;
746   wcstoul(L"123", nullptr, -1);
747   ASSERT_EQ(EINVAL, errno);
748   errno = 0;
749   wcstoul(L"123", nullptr, 1);
750   ASSERT_EQ(EINVAL, errno);
751   errno = 0;
752   wcstoul(L"123", nullptr, 37);
753   ASSERT_EQ(EINVAL, errno);
754 }
755 
TEST(wchar,wcstoull_EINVAL)756 TEST(wchar, wcstoull_EINVAL) {
757   errno = 0;
758   wcstoull(L"123", nullptr, -1);
759   ASSERT_EQ(EINVAL, errno);
760   errno = 0;
761   wcstoull(L"123", nullptr, 1);
762   ASSERT_EQ(EINVAL, errno);
763   errno = 0;
764   wcstoull(L"123", nullptr, 37);
765   ASSERT_EQ(EINVAL, errno);
766 }
767 
TEST(wchar,wcstoll_l_EINVAL)768 TEST(wchar, wcstoll_l_EINVAL) {
769   errno = 0;
770   wcstoll_l(L"123", nullptr, -1, LC_GLOBAL_LOCALE);
771   ASSERT_EQ(EINVAL, errno);
772   errno = 0;
773   wcstoll_l(L"123", nullptr, 1, LC_GLOBAL_LOCALE);
774   ASSERT_EQ(EINVAL, errno);
775   errno = 0;
776   wcstoll_l(L"123", nullptr, 37, LC_GLOBAL_LOCALE);
777   ASSERT_EQ(EINVAL, errno);
778 }
779 
TEST(wchar,wcstoull_l_EINVAL)780 TEST(wchar, wcstoull_l_EINVAL) {
781   errno = 0;
782   wcstoull_l(L"123", nullptr, -1, LC_GLOBAL_LOCALE);
783   ASSERT_EQ(EINVAL, errno);
784   errno = 0;
785   wcstoull_l(L"123", nullptr, 1, LC_GLOBAL_LOCALE);
786   ASSERT_EQ(EINVAL, errno);
787   errno = 0;
788   wcstoull_l(L"123", nullptr, 37, LC_GLOBAL_LOCALE);
789   ASSERT_EQ(EINVAL, errno);
790 }
791 
TEST(wchar,wmempcpy)792 TEST(wchar, wmempcpy) {
793   wchar_t dst[6];
794   ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
795 }
796 
797 template <typename T>
798 using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
799 
800 template <typename T>
TestSingleWcsToFloat(WcsToFloatFn<T> fn,const wchar_t * str,T expected_value,ptrdiff_t expected_len)801 void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
802                           T expected_value, ptrdiff_t expected_len) {
803   wchar_t* p;
804   ASSERT_EQ(expected_value, fn(str, &p));
805   ASSERT_EQ(expected_len, p - str);
806 }
807 
808 template <typename T>
TestWcsToFloat(WcsToFloatFn<T> fn)809 void TestWcsToFloat(WcsToFloatFn<T> fn) {
810   TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
811   TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
812   TestSingleWcsToFloat(fn, L"   123 45", static_cast<T>(123.0L), 6);
813   TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
814   TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
815   TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
816 }
817 
818 template <typename T>
TestWcsToFloatHexFloats(WcsToFloatFn<T> fn)819 void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
820   TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
821   TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
822   TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
823   TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
824 }
825 
826 template <typename T>
TestWcsToFloatInfNan(WcsToFloatFn<T> fn)827 void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
828   ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
829   ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
830   ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
831 
832   ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
833   ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
834   ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
835 
836   wchar_t* p;
837   ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
838   ASSERT_STREQ(L"ny", p);
839   ASSERT_TRUE(isnan(fn(L"nanny", &p)));
840   ASSERT_STREQ(L"ny", p);
841   ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
842   ASSERT_STREQ(L"ny", p);
843 
844   ASSERT_EQ(0, fn(L"muppet", &p));
845   ASSERT_STREQ(L"muppet", p);
846   ASSERT_EQ(0, fn(L"  muppet", &p));
847   ASSERT_STREQ(L"  muppet", p);
848 
849   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
850   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
851   ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
852 
853   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
854   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
855   ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
856 
857   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
858   ASSERT_STREQ(L"initude", p);
859   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
860   ASSERT_STREQ(L"initude", p);
861   ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
862   ASSERT_STREQ(L"initude", p);
863 
864   // Check case-insensitivity.
865   ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
866   ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
867 }
868 
TEST(wchar,wcstof)869 TEST(wchar, wcstof) {
870   TestWcsToFloat(wcstof);
871 }
872 
TEST(wchar,wcstof_hex_floats)873 TEST(wchar, wcstof_hex_floats) {
874   TestWcsToFloatHexFloats(wcstof);
875 }
876 
TEST(wchar,wcstof_hex_inf_nan)877 TEST(wchar, wcstof_hex_inf_nan) {
878   TestWcsToFloatInfNan(wcstof);
879 }
880 
TEST(wchar,wcstod)881 TEST(wchar, wcstod) {
882   TestWcsToFloat(wcstod);
883 }
884 
TEST(wchar,wcstod_hex_floats)885 TEST(wchar, wcstod_hex_floats) {
886   TestWcsToFloatHexFloats(wcstod);
887 }
888 
TEST(wchar,wcstod_hex_inf_nan)889 TEST(wchar, wcstod_hex_inf_nan) {
890   TestWcsToFloatInfNan(wcstod);
891 }
892 
TEST(wchar,wcstold)893 TEST(wchar, wcstold) {
894   TestWcsToFloat(wcstold);
895 }
896 
TEST(wchar,wcstold_hex_floats)897 TEST(wchar, wcstold_hex_floats) {
898   TestWcsToFloatHexFloats(wcstold);
899 }
900 
TEST(wchar,wcstold_hex_inf_nan)901 TEST(wchar, wcstold_hex_inf_nan) {
902   TestWcsToFloatInfNan(wcstold);
903 }
904 
TEST(wchar,wcstod_l)905 TEST(wchar, wcstod_l) {
906   EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, LC_GLOBAL_LOCALE));
907 }
908 
TEST(wchar,wcstof_l)909 TEST(wchar, wcstof_l) {
910   EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, LC_GLOBAL_LOCALE));
911 }
912 
TEST(wchar,wcstol_l)913 TEST(wchar, wcstol_l) {
914   EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
915 }
916 
TEST(wchar,wcstold_l)917 TEST(wchar, wcstold_l) {
918   EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, LC_GLOBAL_LOCALE));
919 }
920 
TEST(wchar,wcstoll_l)921 TEST(wchar, wcstoll_l) {
922   EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
923 }
924 
TEST(wchar,wcstoul_l)925 TEST(wchar, wcstoul_l) {
926   EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
927 }
928 
TEST(wchar,wcstoull_l)929 TEST(wchar, wcstoull_l) {
930   EXPECT_EQ(123ULL, wcstoul_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
931 }
932 
AssertWcwidthRange(wchar_t begin,wchar_t end,int expected)933 static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
934   for (wchar_t i = begin; i < end; ++i) {
935     EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
936   }
937 }
938 
TEST(wchar,wcwidth_NUL)939 TEST(wchar, wcwidth_NUL) {
940   // NUL is defined to return 0 rather than -1, despite being a C0 control.
941   EXPECT_EQ(0, wcwidth(0));
942 }
943 
TEST(wchar,wcwidth_ascii)944 TEST(wchar, wcwidth_ascii) {
945   AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
946 }
947 
TEST(wchar,wcwidth_controls)948 TEST(wchar, wcwidth_controls) {
949   AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
950   EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
951   AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
952 }
953 
TEST(wchar,wcwidth_non_spacing_and_enclosing_marks_and_format)954 TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
955   if (!have_dl()) return;
956 
957   EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
958   EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
959   EXPECT_EQ(0, wcwidth(0x00ad)); // Soft hyphen (SHY).
960   EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
961 }
962 
TEST(wchar,wcwidth_cjk)963 TEST(wchar, wcwidth_cjk) {
964   if (!have_dl()) return;
965 
966   EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
967   EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
968   EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
969   EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
970   EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
971   EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
972 }
973 
TEST(wchar,wcwidth_korean_combining_jamo)974 TEST(wchar, wcwidth_korean_combining_jamo) {
975   if (!have_dl()) return;
976 
977   AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
978   EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
979   EXPECT_EQ(0, wcwidth(0xd7cb));
980 }
981 
TEST(wchar,wcwidth_korean_jeongeul_syllables)982 TEST(wchar, wcwidth_korean_jeongeul_syllables) {
983   if (!have_dl()) return;
984 
985   EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
986   EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points in Unicode 7.
987   // Undefined characters at the end of the block have width 1.
988 }
989 
TEST(wchar,wcwidth_kana)990 TEST(wchar, wcwidth_kana) {
991   if (!have_dl()) return;
992 
993   // Hiragana (most, not undefined).
994   AssertWcwidthRange(0x3041, 0x3097, 2);
995   // Katakana.
996   AssertWcwidthRange(0x30a0, 0x3100, 2);
997 }
998 
TEST(wchar,wcwidth_circled_two_digit_cjk)999 TEST(wchar, wcwidth_circled_two_digit_cjk) {
1000   if (!have_dl()) return;
1001 
1002   // Circled two-digit CJK "speed sign" numbers are wide,
1003   // though EastAsianWidth is ambiguous.
1004   AssertWcwidthRange(0x3248, 0x3250, 2);
1005 }
1006 
TEST(wchar,wcwidth_hexagrams)1007 TEST(wchar, wcwidth_hexagrams) {
1008   if (!have_dl()) return;
1009 
1010   // Hexagrams are wide, though EastAsianWidth is neutral.
1011   AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1012 }
1013 
TEST(wchar,wcwidth_default_ignorables)1014 TEST(wchar, wcwidth_default_ignorables) {
1015   if (!have_dl()) return;
1016 
1017   AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1018   EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1019 }
1020 
TEST(wchar,wcwidth_korean_common_non_syllables)1021 TEST(wchar, wcwidth_korean_common_non_syllables) {
1022   if (!have_dl()) return;
1023 
1024   EXPECT_EQ(2, wcwidth(L'ㅜ')); // Korean "crying" emoticon.
1025   EXPECT_EQ(2, wcwidth(L'ㅋ')); // Korean "laughing" emoticon.
1026 }
1027