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"big daddy/giant haystacks!";
232 const wchar_t* empty_haystack = L"";
233
234 // The empty needle is a special case.
235 ASSERT_EQ(haystack, wcsstr(haystack, L""));
236 ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
237
238 ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
239 ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
240 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
241 ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
242 ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
243 ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
244
245 ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
246 ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
247 }
248
TEST(wchar,wcsstr_80199)249 TEST(wchar, wcsstr_80199) {
250 // https://code.google.com/p/android/issues/detail?id=80199
251 ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
252 }
253
TEST(wchar,mbtowc)254 TEST(wchar, mbtowc) {
255 wchar_t out[8];
256
257 out[0] = 'x';
258 ASSERT_EQ(0, mbtowc(out, "hello", 0));
259 ASSERT_EQ('x', out[0]);
260
261 ASSERT_EQ(0, mbtowc(out, "hello", 0));
262 ASSERT_EQ(0, mbtowc(out, "", 0));
263 ASSERT_EQ(1, mbtowc(out, "hello", 1));
264 ASSERT_EQ(L'h', out[0]);
265
266 ASSERT_EQ(0, mbtowc(nullptr, "hello", 0));
267 ASSERT_EQ(0, mbtowc(nullptr, "", 0));
268 ASSERT_EQ(1, mbtowc(nullptr, "hello", 1));
269
270 ASSERT_EQ(0, mbtowc(nullptr, nullptr, 0));
271 }
272
TEST(wchar,mbrtowc)273 TEST(wchar, mbrtowc) {
274 wchar_t out[8];
275
276 out[0] = 'x';
277 ASSERT_EQ(0U, mbrtowc(out, "hello", 0, nullptr));
278 ASSERT_EQ('x', out[0]);
279
280 ASSERT_EQ(0U, mbrtowc(out, "hello", 0, nullptr));
281 ASSERT_EQ(0U, mbrtowc(out, "", 0, nullptr));
282 ASSERT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
283 ASSERT_EQ(L'h', out[0]);
284
285 ASSERT_EQ(0U, mbrtowc(nullptr, "hello", 0, nullptr));
286 ASSERT_EQ(0U, mbrtowc(nullptr, "", 0, nullptr));
287 ASSERT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
288
289 ASSERT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
290
291 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
292 uselocale(LC_GLOBAL_LOCALE);
293
294 // 1-byte UTF-8.
295 ASSERT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
296 ASSERT_EQ(L'a', out[0]);
297 // 2-byte UTF-8.
298 ASSERT_EQ(2U, mbrtowc(out, "\xc2\xa2" "cdef", 6, nullptr));
299 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
300 // 3-byte UTF-8.
301 ASSERT_EQ(3U, mbrtowc(out, "\xe2\x82\xac" "def", 6, nullptr));
302 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
303 // 4-byte UTF-8.
304 ASSERT_EQ(4U, mbrtowc(out, "\xf0\xa4\xad\xa2" "ef", 6, nullptr));
305 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
306 #if defined(__BIONIC__) // glibc allows this.
307 // Illegal 5-byte UTF-8.
308 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf8\xa1\xa2\xa3\xa4" "f", 6, nullptr));
309 ASSERT_EQ(EILSEQ, errno);
310 #endif
311 // Illegal over-long sequence.
312 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf0\x82\x82\xac" "ef", 6, nullptr));
313 ASSERT_EQ(EILSEQ, errno);
314 }
315
TEST(wchar,mbrtowc_valid_non_characters)316 TEST(wchar, mbrtowc_valid_non_characters) {
317 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
318 uselocale(LC_GLOBAL_LOCALE);
319
320 wchar_t out[8] = {};
321
322 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
323 ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
324 ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
325 ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
326 }
327
TEST(wchar,mbrtowc_out_of_range)328 TEST(wchar, mbrtowc_out_of_range) {
329 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
330 uselocale(LC_GLOBAL_LOCALE);
331
332 wchar_t out[8] = {};
333 errno = 0;
334 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr));
335 ASSERT_EQ(EILSEQ, errno);
336 }
337
test_mbrtowc_incomplete(mbstate_t * ps)338 static void test_mbrtowc_incomplete(mbstate_t* ps) {
339 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
340 uselocale(LC_GLOBAL_LOCALE);
341
342 wchar_t out;
343 // 2-byte UTF-8.
344 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
345 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
346 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
347 ASSERT_TRUE(mbsinit(ps));
348 // 3-byte UTF-8.
349 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
350 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
351 ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
352 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
353 ASSERT_TRUE(mbsinit(ps));
354 // 4-byte UTF-8.
355 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
356 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
357 ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
358 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
359 ASSERT_TRUE(mbsinit(ps));
360
361 // Invalid 2-byte
362 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
363 ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
364 ASSERT_EQ(EILSEQ, errno);
365 }
366
TEST(wchar,mbrtowc_incomplete)367 TEST(wchar, mbrtowc_incomplete) {
368 mbstate_t ps;
369 memset(&ps, 0, sizeof(ps));
370
371 test_mbrtowc_incomplete(&ps);
372 test_mbrtowc_incomplete(nullptr);
373 }
374
test_mbsrtowcs(mbstate_t * ps)375 static void test_mbsrtowcs(mbstate_t* ps) {
376 constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
377 constexpr const char* INVALID = "A" "\xc2\x20" "ef";
378 constexpr const char* INCOMPLETE = "A" "\xc2";
379 wchar_t out[4];
380
381 const char* valid = VALID;
382 ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
383 ASSERT_EQ(L'A', out[0]);
384 ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
385 ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
386 ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
387 // Check that valid has advanced to the next unread character.
388 ASSERT_EQ('e', *valid);
389
390 wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
391 ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
392 ASSERT_EQ(L'e', out[0]);
393 ASSERT_EQ(L'f', out[1]);
394 ASSERT_EQ(L'\0', out[2]);
395 // Check that we didn't clobber the rest of out.
396 ASSERT_EQ(L'x', out[3]);
397 // Check that valid has advanced to the end of the string.
398 ASSERT_EQ(nullptr, valid);
399
400 const char* invalid = INVALID;
401 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
402 EXPECT_EQ(EILSEQ, errno);
403 ASSERT_EQ('\xc2', *invalid);
404
405 const char* incomplete = INCOMPLETE;
406 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
407 EXPECT_EQ(EILSEQ, errno);
408 ASSERT_EQ('\xc2', *incomplete);
409
410 // If dst is null, *src shouldn't be updated.
411 // https://code.google.com/p/android/issues/detail?id=166381
412 const char* mbs = VALID;
413 EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
414 EXPECT_EQ(VALID, mbs);
415 mbs = INVALID;
416 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
417 EXPECT_EQ(INVALID, mbs);
418 mbs = INCOMPLETE;
419 EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
420 EXPECT_EQ(INCOMPLETE, mbs);
421 }
422
TEST(wchar,mbsrtowcs)423 TEST(wchar, mbsrtowcs) {
424 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
425 uselocale(LC_GLOBAL_LOCALE);
426
427 mbstate_t ps;
428 memset(&ps, 0, sizeof(ps));
429 test_mbsrtowcs(&ps);
430 test_mbsrtowcs(nullptr);
431
432 // Invalid multi byte continuation.
433 const char* invalid = "\x20";
434 wchar_t out;
435 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
436 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
437 EXPECT_EQ(EILSEQ, errno);
438 ASSERT_EQ('\x20', *invalid);
439 }
440
441 template <typename T>
442 using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
443
444 template <typename T>
TestSingleWcsToInt(WcsToIntFn<T> fn,const wchar_t * str,int base,T expected_value,ptrdiff_t expected_len)445 void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
446 T expected_value, ptrdiff_t expected_len) {
447 wchar_t* p;
448 ASSERT_EQ(expected_value, fn(str, &p, base));
449 ASSERT_EQ(expected_len, p - str) << str;
450 }
451
452 template <typename T>
TestWcsToInt(WcsToIntFn<T> fn)453 void TestWcsToInt(WcsToIntFn<T> fn) {
454 TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
455 TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
456 TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
457 TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
458 TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
459 TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
460 TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
461 TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
462 }
463
464 template <typename T>
TestWcsToIntLimits(WcsToIntFn<T> fn,const wchar_t * min_str,const wchar_t * max_str)465 void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
466 const wchar_t* max_str) {
467 if (std::is_signed<T>::value) {
468 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
469 } else {
470 // If the subject sequence begins with a <hyphen-minus>, the value resulting
471 // from the conversion shall be negated.
472 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
473 ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
474 }
475 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
476 }
477
TEST(wchar,wcstol)478 TEST(wchar, wcstol) {
479 TestWcsToInt(wcstol);
480 }
481
TEST(wchar,wcstol_limits)482 TEST(wchar, wcstol_limits) {
483 if (sizeof(long) == 8) {
484 TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
485 } else {
486 TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
487 }
488 }
489
TEST(wchar,wcstoul)490 TEST(wchar, wcstoul) {
491 TestWcsToInt(wcstoul);
492 }
493
TEST(wchar,wcstoul_limits)494 TEST(wchar, wcstoul_limits) {
495 if (sizeof(long) == 8) {
496 TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
497 } else {
498 TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
499 }
500 }
501
TEST(wchar,wcstoll)502 TEST(wchar, wcstoll) {
503 TestWcsToInt(wcstoll);
504 }
505
TEST(wchar,wcstoll_limits)506 TEST(wchar, wcstoll_limits) {
507 TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
508 }
509
TEST(wchar,wcstoull)510 TEST(wchar, wcstoull) {
511 TestWcsToInt(wcstoull);
512 }
513
TEST(wchar,wcstoull_limits)514 TEST(wchar, wcstoull_limits) {
515 TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
516 }
517
TEST(wchar,wcstoimax)518 TEST(wchar, wcstoimax) {
519 TestWcsToInt(wcstoimax);
520 }
521
TEST(wchar,wcstoimax_limits)522 TEST(wchar, wcstoimax_limits) {
523 TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
524 L"9223372036854775808");
525 }
526
TEST(wchar,wcstoumax)527 TEST(wchar, wcstoumax) {
528 TestWcsToInt(wcstoumax);
529 }
530
TEST(wchar,wcstoumax_limits)531 TEST(wchar, wcstoumax_limits) {
532 TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
533 }
534
TEST(wchar,mbsnrtowcs)535 TEST(wchar, mbsnrtowcs) {
536 wchar_t dst[128];
537 const char* s = "hello, world!";
538 const char* src;
539
540 memset(dst, 0, sizeof(dst));
541 src = s;
542 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
543
544 memset(dst, 0, sizeof(dst));
545 src = s;
546 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
547 ASSERT_EQ(L'h', dst[0]);
548 ASSERT_EQ(L'e', dst[1]);
549 ASSERT_EQ(&s[2], src);
550
551 memset(dst, 0, sizeof(dst));
552 src = s;
553 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
554 ASSERT_EQ(L'h', dst[0]);
555 ASSERT_EQ(L'e', dst[1]);
556 ASSERT_EQ(L'l', dst[2]);
557 ASSERT_EQ(&s[3], src);
558
559 memset(dst, 0, sizeof(dst));
560 const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
561 src = incomplete;
562 errno = 0;
563 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
564 ASSERT_EQ(EILSEQ, errno);
565
566 src = incomplete;
567 errno = 0;
568 ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
569 ASSERT_EQ(EILSEQ, errno);
570 }
571
TEST(wchar,wcsftime__wcsftime_l)572 TEST(wchar, wcsftime__wcsftime_l) {
573 setenv("TZ", "UTC", 1);
574
575 struct tm t;
576 memset(&t, 0, sizeof(tm));
577 t.tm_year = 200;
578 t.tm_mon = 2;
579 t.tm_mday = 10;
580
581 wchar_t buf[64];
582
583 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
584 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
585 EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, LC_GLOBAL_LOCALE));
586 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
587 }
588
TEST(wchar,wmemmove_smoke)589 TEST(wchar, wmemmove_smoke) {
590 const wchar_t const_wstr[] = L"This is a test of something or other.....";
591 wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
592
593 EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
594 EXPECT_STREQ(const_wstr, wstr);
595
596 EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
597 EXPECT_STREQ(L"This This is a test of something or other", wstr);
598 }
599
TEST(wchar,wmemcpy_smoke)600 TEST(wchar, wmemcpy_smoke) {
601 const wchar_t src[] = L"Source string";
602 wchar_t dst[NUM_WCHARS(sizeof(src))];
603
604 EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
605 EXPECT_STREQ(dst, src);
606 }
607
TEST(wchar,wcpcpy_smoke)608 TEST(wchar, wcpcpy_smoke) {
609 const wchar_t src[] = L"Source string";
610 wchar_t dst[NUM_WCHARS(sizeof(src))];
611
612 EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
613 EXPECT_STREQ(dst, src);
614 }
615
TEST(wchar,wcpncpy_smoke)616 TEST(wchar, wcpncpy_smoke) {
617 const wchar_t src[] = L"Source string";
618 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
619
620 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
621 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
622 EXPECT_STREQ(dst, src);
623
624 EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
625 dst[6] = L'\0';
626 EXPECT_STREQ(dst, L"Source");
627
628 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
629 EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
630 EXPECT_STREQ(dst, src);
631 EXPECT_EQ(dst[src_len], L'\0');
632 EXPECT_EQ(dst[src_len+1], L'\0');
633 EXPECT_EQ(dst[src_len+2], L'\0');
634 EXPECT_EQ(dst[src_len+3], L'\0');
635 EXPECT_EQ(dst[src_len+4], L'x');
636 }
637
TEST(wchar,wcscpy_smoke)638 TEST(wchar, wcscpy_smoke) {
639 const wchar_t src[] = L"Source string";
640 wchar_t dst[NUM_WCHARS(sizeof(src))];
641
642 EXPECT_EQ(dst, wcscpy(dst, src));
643 EXPECT_STREQ(src, dst);
644 }
645
TEST(wchar,wcsncpy_smoke)646 TEST(wchar, wcsncpy_smoke) {
647 const wchar_t src[] = L"Source string";
648 wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
649
650 size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
651 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
652 EXPECT_STREQ(dst, src);
653
654 EXPECT_EQ(dst, wcsncpy(dst, src, 6));
655 dst[6] = L'\0';
656 EXPECT_STREQ(dst, L"Source");
657 EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
658 EXPECT_STREQ(dst, L"Source");
659
660 wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
661 EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
662 EXPECT_STREQ(dst, src);
663 EXPECT_EQ(dst[src_len], L'\0');
664 EXPECT_EQ(dst[src_len+1], L'\0');
665 EXPECT_EQ(dst[src_len+2], L'\0');
666 EXPECT_EQ(dst[src_len+3], L'\0');
667 EXPECT_EQ(dst[src_len+4], L'x');
668 }
669
TEST(wchar,mbrtowc_15439554)670 TEST(wchar, mbrtowc_15439554) {
671 // http://b/15439554
672 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
673 uselocale(LC_GLOBAL_LOCALE);
674
675 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
676 ASSERT_GE(MB_CUR_MAX, 4U);
677
678 wchar_t wc;
679 size_t n;
680
681 // 1-byte character.
682 n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
683 EXPECT_EQ(1U, n);
684 EXPECT_EQ(L'x', wc);
685 // 2-byte character.
686 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
687 EXPECT_EQ(2U, n);
688 EXPECT_EQ(L'¢', wc);
689 // 3-byte character.
690 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
691 EXPECT_EQ(3U, n);
692 EXPECT_EQ(L'€', wc);
693 // 4-byte character.
694 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
695 EXPECT_EQ(4U, n);
696 EXPECT_EQ(L'', wc);
697 }
698
TEST(wchar,open_wmemstream)699 TEST(wchar, open_wmemstream) {
700 wchar_t* p = nullptr;
701 size_t size = 0;
702 FILE* fp = open_wmemstream(&p, &size);
703 ASSERT_NE(EOF, fputws(L"hello, world!", fp));
704 fclose(fp);
705
706 ASSERT_STREQ(L"hello, world!", p);
707 ASSERT_EQ(wcslen(L"hello, world!"), size);
708 free(p);
709 }
710
TEST(stdio,open_wmemstream_EINVAL)711 TEST(stdio, open_wmemstream_EINVAL) {
712 #if defined(__BIONIC__)
713 wchar_t* p;
714 size_t size;
715
716 // Invalid buffer.
717 errno = 0;
718 ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
719 ASSERT_EQ(EINVAL, errno);
720
721 // Invalid size.
722 errno = 0;
723 ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
724 ASSERT_EQ(EINVAL, errno);
725 #else
726 GTEST_SKIP() << "This test is bionic-specific";
727 #endif
728 }
729
TEST(wchar,wcstol_EINVAL)730 TEST(wchar, wcstol_EINVAL) {
731 errno = 0;
732 wcstol(L"123", nullptr, -1);
733 ASSERT_EQ(EINVAL, errno);
734 errno = 0;
735 wcstol(L"123", nullptr, 1);
736 ASSERT_EQ(EINVAL, errno);
737 errno = 0;
738 wcstol(L"123", nullptr, 37);
739 ASSERT_EQ(EINVAL, errno);
740 }
741
TEST(wchar,wcstoll_EINVAL)742 TEST(wchar, wcstoll_EINVAL) {
743 errno = 0;
744 wcstoll(L"123", nullptr, -1);
745 ASSERT_EQ(EINVAL, errno);
746 errno = 0;
747 wcstoll(L"123", nullptr, 1);
748 ASSERT_EQ(EINVAL, errno);
749 errno = 0;
750 wcstoll(L"123", nullptr, 37);
751 ASSERT_EQ(EINVAL, errno);
752 }
753
TEST(wchar,wcstoul_EINVAL)754 TEST(wchar, wcstoul_EINVAL) {
755 errno = 0;
756 wcstoul(L"123", nullptr, -1);
757 ASSERT_EQ(EINVAL, errno);
758 errno = 0;
759 wcstoul(L"123", nullptr, 1);
760 ASSERT_EQ(EINVAL, errno);
761 errno = 0;
762 wcstoul(L"123", nullptr, 37);
763 ASSERT_EQ(EINVAL, errno);
764 }
765
TEST(wchar,wcstoull_EINVAL)766 TEST(wchar, wcstoull_EINVAL) {
767 errno = 0;
768 wcstoull(L"123", nullptr, -1);
769 ASSERT_EQ(EINVAL, errno);
770 errno = 0;
771 wcstoull(L"123", nullptr, 1);
772 ASSERT_EQ(EINVAL, errno);
773 errno = 0;
774 wcstoull(L"123", nullptr, 37);
775 ASSERT_EQ(EINVAL, errno);
776 }
777
TEST(wchar,wcstoll_l_EINVAL)778 TEST(wchar, wcstoll_l_EINVAL) {
779 errno = 0;
780 wcstoll_l(L"123", nullptr, -1, LC_GLOBAL_LOCALE);
781 ASSERT_EQ(EINVAL, errno);
782 errno = 0;
783 wcstoll_l(L"123", nullptr, 1, LC_GLOBAL_LOCALE);
784 ASSERT_EQ(EINVAL, errno);
785 errno = 0;
786 wcstoll_l(L"123", nullptr, 37, LC_GLOBAL_LOCALE);
787 ASSERT_EQ(EINVAL, errno);
788 }
789
TEST(wchar,wcstoull_l_EINVAL)790 TEST(wchar, wcstoull_l_EINVAL) {
791 errno = 0;
792 wcstoull_l(L"123", nullptr, -1, LC_GLOBAL_LOCALE);
793 ASSERT_EQ(EINVAL, errno);
794 errno = 0;
795 wcstoull_l(L"123", nullptr, 1, LC_GLOBAL_LOCALE);
796 ASSERT_EQ(EINVAL, errno);
797 errno = 0;
798 wcstoull_l(L"123", nullptr, 37, LC_GLOBAL_LOCALE);
799 ASSERT_EQ(EINVAL, errno);
800 }
801
TEST(wchar,wmempcpy)802 TEST(wchar, wmempcpy) {
803 wchar_t dst[6];
804 ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
805 }
806
807 template <typename T>
808 using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
809
810 template <typename T>
TestSingleWcsToFloat(WcsToFloatFn<T> fn,const wchar_t * str,T expected_value,ptrdiff_t expected_len)811 void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
812 T expected_value, ptrdiff_t expected_len) {
813 wchar_t* p;
814 ASSERT_EQ(expected_value, fn(str, &p));
815 ASSERT_EQ(expected_len, p - str);
816 }
817
818 template <typename T>
TestWcsToFloat(WcsToFloatFn<T> fn)819 void TestWcsToFloat(WcsToFloatFn<T> fn) {
820 TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
821 TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
822 TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
823 TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
824 TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
825 TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
826 }
827
828 template <typename T>
TestWcsToFloatHexFloats(WcsToFloatFn<T> fn)829 void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
830 TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
831 TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
832 TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
833 TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
834 }
835
836 template <typename T>
TestWcsToFloatInfNan(WcsToFloatFn<T> fn)837 void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
838 ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
839 ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
840 ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
841
842 ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
843 ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
844 ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
845
846 wchar_t* p;
847 ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
848 ASSERT_STREQ(L"ny", p);
849 ASSERT_TRUE(isnan(fn(L"nanny", &p)));
850 ASSERT_STREQ(L"ny", p);
851 ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
852 ASSERT_STREQ(L"ny", p);
853
854 ASSERT_EQ(0, fn(L"muppet", &p));
855 ASSERT_STREQ(L"muppet", p);
856 ASSERT_EQ(0, fn(L" muppet", &p));
857 ASSERT_STREQ(L" muppet", p);
858
859 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
860 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
861 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
862
863 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
864 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
865 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
866
867 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
868 ASSERT_STREQ(L"initude", p);
869 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
870 ASSERT_STREQ(L"initude", p);
871 ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
872 ASSERT_STREQ(L"initude", p);
873
874 // Check case-insensitivity.
875 ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
876 ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
877 }
878
TEST(wchar,wcstof)879 TEST(wchar, wcstof) {
880 TestWcsToFloat(wcstof);
881 }
882
TEST(wchar,wcstof_hex_floats)883 TEST(wchar, wcstof_hex_floats) {
884 TestWcsToFloatHexFloats(wcstof);
885 }
886
TEST(wchar,wcstof_hex_inf_nan)887 TEST(wchar, wcstof_hex_inf_nan) {
888 TestWcsToFloatInfNan(wcstof);
889 }
890
TEST(wchar,wcstod)891 TEST(wchar, wcstod) {
892 TestWcsToFloat(wcstod);
893 }
894
TEST(wchar,wcstod_hex_floats)895 TEST(wchar, wcstod_hex_floats) {
896 TestWcsToFloatHexFloats(wcstod);
897 }
898
TEST(wchar,wcstod_hex_inf_nan)899 TEST(wchar, wcstod_hex_inf_nan) {
900 TestWcsToFloatInfNan(wcstod);
901 }
902
TEST(wchar,wcstold)903 TEST(wchar, wcstold) {
904 TestWcsToFloat(wcstold);
905 }
906
TEST(wchar,wcstold_hex_floats)907 TEST(wchar, wcstold_hex_floats) {
908 TestWcsToFloatHexFloats(wcstold);
909 }
910
TEST(wchar,wcstold_hex_inf_nan)911 TEST(wchar, wcstold_hex_inf_nan) {
912 TestWcsToFloatInfNan(wcstold);
913 }
914
TEST(wchar,wcstod_l)915 TEST(wchar, wcstod_l) {
916 EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, LC_GLOBAL_LOCALE));
917 }
918
TEST(wchar,wcstof_l)919 TEST(wchar, wcstof_l) {
920 EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, LC_GLOBAL_LOCALE));
921 }
922
TEST(wchar,wcstol_l)923 TEST(wchar, wcstol_l) {
924 EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
925 }
926
TEST(wchar,wcstold_l)927 TEST(wchar, wcstold_l) {
928 EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, LC_GLOBAL_LOCALE));
929 }
930
TEST(wchar,wcstoll_l)931 TEST(wchar, wcstoll_l) {
932 EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
933 }
934
TEST(wchar,wcstoul_l)935 TEST(wchar, wcstoul_l) {
936 EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
937 }
938
TEST(wchar,wcstoull_l)939 TEST(wchar, wcstoull_l) {
940 EXPECT_EQ(123ULL, wcstoul_l(L"123", nullptr, 10, LC_GLOBAL_LOCALE));
941 }
942
AssertWcwidthRange(wchar_t begin,wchar_t end,int expected)943 static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
944 for (wchar_t i = begin; i < end; ++i) {
945 EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
946 }
947 }
948
TEST(wchar,wcwidth_NUL)949 TEST(wchar, wcwidth_NUL) {
950 // NUL is defined to return 0 rather than -1, despite being a C0 control.
951 EXPECT_EQ(0, wcwidth(0));
952 }
953
TEST(wchar,wcwidth_ascii)954 TEST(wchar, wcwidth_ascii) {
955 AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
956 }
957
TEST(wchar,wcwidth_controls)958 TEST(wchar, wcwidth_controls) {
959 AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
960 EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
961 AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
962 }
963
TEST(wchar,wcwidth_non_spacing_and_enclosing_marks_and_format)964 TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
965 if (!have_dl()) return;
966
967 EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
968 EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
969 EXPECT_EQ(0, wcwidth(0x00ad)); // Soft hyphen (SHY).
970 EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
971 }
972
TEST(wchar,wcwidth_cjk)973 TEST(wchar, wcwidth_cjk) {
974 if (!have_dl()) return;
975
976 EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
977 EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
978 EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
979 EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
980 EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
981 EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
982 }
983
TEST(wchar,wcwidth_korean_combining_jamo)984 TEST(wchar, wcwidth_korean_combining_jamo) {
985 if (!have_dl()) return;
986
987 AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
988 EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
989 EXPECT_EQ(0, wcwidth(0xd7cb));
990 }
991
TEST(wchar,wcwidth_korean_jeongeul_syllables)992 TEST(wchar, wcwidth_korean_jeongeul_syllables) {
993 if (!have_dl()) return;
994
995 EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
996 EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points in Unicode 7.
997 // Undefined characters at the end of the block have width 1.
998 }
999
TEST(wchar,wcwidth_kana)1000 TEST(wchar, wcwidth_kana) {
1001 if (!have_dl()) return;
1002
1003 // Hiragana (most, not undefined).
1004 AssertWcwidthRange(0x3041, 0x3097, 2);
1005 // Katakana.
1006 AssertWcwidthRange(0x30a0, 0x3100, 2);
1007 }
1008
TEST(wchar,wcwidth_circled_two_digit_cjk)1009 TEST(wchar, wcwidth_circled_two_digit_cjk) {
1010 if (!have_dl()) return;
1011
1012 // Circled two-digit CJK "speed sign" numbers are wide,
1013 // though EastAsianWidth is ambiguous.
1014 AssertWcwidthRange(0x3248, 0x3250, 2);
1015 }
1016
TEST(wchar,wcwidth_hexagrams)1017 TEST(wchar, wcwidth_hexagrams) {
1018 if (!have_dl()) return;
1019
1020 // Hexagrams are wide, though EastAsianWidth is neutral.
1021 AssertWcwidthRange(0x4dc0, 0x4e00, 2);
1022 }
1023
TEST(wchar,wcwidth_default_ignorables)1024 TEST(wchar, wcwidth_default_ignorables) {
1025 if (!have_dl()) return;
1026
1027 AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
1028 EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
1029 }
1030
TEST(wchar,wcwidth_korean_common_non_syllables)1031 TEST(wchar, wcwidth_korean_common_non_syllables) {
1032 if (!have_dl()) return;
1033
1034 EXPECT_EQ(2, wcwidth(L'ㅜ')); // Korean "crying" emoticon.
1035 EXPECT_EQ(2, wcwidth(L'ㅋ')); // Korean "laughing" emoticon.
1036 }
1037
TEST(wchar,wcswidth)1038 TEST(wchar, wcswidth) {
1039 EXPECT_EQ(2, wcswidth(L"abc", 2));
1040 EXPECT_EQ(2, wcswidth(L"ab\t", 2));
1041 EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
1042 }
1043
TEST(wchar,wcslcpy)1044 TEST(wchar, wcslcpy) {
1045 #if defined(__BIONIC__)
1046 wchar_t dst[32];
1047 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
1048 ASSERT_STREQ(L"he", dst);
1049 ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
1050 ASSERT_STREQ(L"hello world", dst);
1051 #else
1052 GTEST_SKIP() << "no wcslcpy in glibc";
1053 #endif
1054 }
1055
TEST(wchar,wcscat)1056 TEST(wchar, wcscat) {
1057 wchar_t dst[32];
1058 ASSERT_EQ(dst, wcscat(dst, L"hello"));
1059 ASSERT_STREQ(dst, L"hello");
1060 ASSERT_EQ(dst, wcscat(dst, L" world"));
1061 ASSERT_STREQ(dst, L"hello world");
1062 }
1063
TEST(wchar,wcscpy)1064 TEST(wchar, wcscpy) {
1065 wchar_t dst[32];
1066 ASSERT_EQ(dst, wcscpy(dst, L"hello"));
1067 ASSERT_STREQ(dst, L"hello");
1068 ASSERT_EQ(dst, wcscpy(dst, L"world"));
1069 ASSERT_STREQ(dst, L"world");
1070 }
1071
TEST(wchar,wcscasecmp)1072 TEST(wchar, wcscasecmp) {
1073 ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
1074 ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
1075 ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
1076 ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
1077 ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
1078 }
1079
TEST(wchar,wcscspn)1080 TEST(wchar, wcscspn) {
1081 ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1082 ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
1083 ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
1084 }
1085
TEST(wchar,wcsspn)1086 TEST(wchar, wcsspn) {
1087 ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
1088 ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
1089 ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
1090 }
1091
TEST(wchar,wcsdup)1092 TEST(wchar, wcsdup) {
1093 wchar_t* s = wcsdup(L"hello");
1094 ASSERT_STREQ(s, L"hello");
1095 free(s);
1096 }
1097
TEST(wchar,wcslcat)1098 TEST(wchar, wcslcat) {
1099 #if defined(__BIONIC__)
1100 wchar_t dst[4] = {};
1101 ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
1102 ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
1103 ASSERT_STREQ(dst, L"abc");
1104 #else
1105 GTEST_SKIP() << "no wcslcpy in glibc";
1106 #endif
1107 }
1108
TEST(wchar,wcsncasecmp)1109 TEST(wchar, wcsncasecmp) {
1110 ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
1111
1112 ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
1113 ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
1114 ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
1115 ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
1116 ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
1117 }
1118
TEST(wchar,wcsncat)1119 TEST(wchar, wcsncat) {
1120 wchar_t dst[32];
1121 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
1122 ASSERT_STREQ(dst, L"hello");
1123 ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
1124 ASSERT_STREQ(dst, L"hello");
1125 ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
1126 ASSERT_STREQ(dst, L"hello, world!");
1127 }
1128
TEST(wchar,wcsncmp)1129 TEST(wchar, wcsncmp) {
1130 ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
1131 ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
1132 ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
1133 ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
1134 }
1135
TEST(wchar,wcsnlen)1136 TEST(wchar, wcsnlen) {
1137 ASSERT_EQ(2U, wcsnlen(L"hello", 2));
1138 ASSERT_EQ(5U, wcsnlen(L"hello", 5));
1139 ASSERT_EQ(5U, wcsnlen(L"hello", 666));
1140 }
1141
TEST(wchar,wcspbrk)1142 TEST(wchar, wcspbrk) {
1143 const wchar_t* s = L"hello, world!";
1144 ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
1145 ASSERT_EQ(s, wcspbrk(s, L"abch"));
1146 ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
1147 ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
1148 }
1149
TEST(wchar,wcstok)1150 TEST(wchar, wcstok) {
1151 wchar_t s[] = L"this is\ta\nstring";
1152 wchar_t* p;
1153 ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
1154 ASSERT_STREQ(s, L"this");
1155 ASSERT_STREQ(p, L"is\ta\nstring");
1156 ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
1157 ASSERT_STREQ(s + 5, L"is");
1158 ASSERT_STREQ(p, L"a\nstring");
1159 ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
1160 ASSERT_STREQ(s + 8, L"a");
1161 ASSERT_STREQ(p, L"string");
1162 ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
1163 ASSERT_STREQ(s + 10, L"string");
1164 ASSERT_EQ(nullptr, p);
1165 }
1166
TEST(wchar,wmemchr)1167 TEST(wchar, wmemchr) {
1168 const wchar_t* s = L"hello, world!";
1169 ASSERT_EQ(s, wmemchr(s, L'h', 13));
1170 ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
1171 ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
1172 ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
1173 }
1174
TEST(wchar,wmemcmp)1175 TEST(wchar, wmemcmp) {
1176 ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
1177 ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
1178 ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
1179 }
1180
TEST(wchar,wmemcpy)1181 TEST(wchar, wmemcpy) {
1182 wchar_t dst[32] = {};
1183 ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
1184 ASSERT_STREQ(dst, L"hello");
1185 }
1186
TEST(wchar,wmemmove)1187 TEST(wchar, wmemmove) {
1188 wchar_t dst[32] = {};
1189 ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
1190 ASSERT_STREQ(dst, L"hello");
1191 }
1192
TEST(wchar,wmemset)1193 TEST(wchar, wmemset) {
1194 wchar_t dst[4] = {};
1195 ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
1196 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1197 ASSERT_EQ(dst[1], wchar_t(0x12345678));
1198 ASSERT_EQ(dst[2], wchar_t(0x12345678));
1199 ASSERT_EQ(dst[3], wchar_t(0));
1200 ASSERT_EQ(dst, wmemset(dst, L'y', 0));
1201 ASSERT_EQ(dst[0], wchar_t(0x12345678));
1202 }
1203