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