wchar_test.cpp revision 69f05d291d848de654c72e5278de8ca06fbf5d2f
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 ASSERT_EQ('e', *valid); 344 345 const char* invalid = "A" "\xc2\x20" "ef"; 346 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps)); 347 EXPECT_EQ(EILSEQ, errno); 348 ASSERT_EQ('\xc2', *invalid); 349 350 const char* incomplete = "A" "\xc2"; 351 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps)); 352 EXPECT_EQ(EILSEQ, errno); 353 ASSERT_EQ('\xc2', *incomplete); 354} 355 356TEST(wchar, mbsrtowcs) { 357 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); 358 uselocale(LC_GLOBAL_LOCALE); 359 360 mbstate_t ps; 361 memset(&ps, 0, sizeof(ps)); 362 test_mbsrtowcs(&ps); 363 test_mbsrtowcs(NULL); 364 365 // Invalid multi byte continuation. 366 const char* invalid = "\x20"; 367 wchar_t out; 368 ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps)); 369 ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps)); 370 EXPECT_EQ(EILSEQ, errno); 371 ASSERT_EQ('\x20', *invalid); 372} 373 374TEST(wchar, wcstod) { 375 ASSERT_DOUBLE_EQ(1.23, wcstod(L"1.23", NULL)); 376} 377 378TEST(wchar, wcstof) { 379 ASSERT_FLOAT_EQ(1.23f, wcstof(L"1.23", NULL)); 380} 381 382TEST(wchar, wcstol) { 383 ASSERT_EQ(123L, wcstol(L"123", NULL, 0)); 384} 385 386TEST(wchar, wcstoll) { 387 ASSERT_EQ(123LL, wcstol(L"123", NULL, 0)); 388} 389 390TEST(wchar, wcstold) { 391 ASSERT_DOUBLE_EQ(1.23L, wcstold(L"1.23", NULL)); 392} 393 394TEST(wchar, wcstoul) { 395 ASSERT_EQ(123UL, wcstoul(L"123", NULL, 0)); 396} 397 398TEST(wchar, wcstoull) { 399 ASSERT_EQ(123ULL, wcstoul(L"123", NULL, 0)); 400} 401 402TEST(wchar, mbsnrtowcs) { 403 wchar_t dst[128]; 404 const char* s = "hello, world!"; 405 const char* src; 406 407 memset(dst, 0, sizeof(dst)); 408 src = s; 409 ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, NULL)); 410 411 memset(dst, 0, sizeof(dst)); 412 src = s; 413 ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, NULL)); // glibc chokes on SIZE_MAX here. 414 ASSERT_EQ(L'h', dst[0]); 415 ASSERT_EQ(L'e', dst[1]); 416 ASSERT_EQ(&s[2], src); 417 418 memset(dst, 0, sizeof(dst)); 419 src = s; 420 ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, NULL)); 421 ASSERT_EQ(L'h', dst[0]); 422 ASSERT_EQ(L'e', dst[1]); 423 ASSERT_EQ(L'l', dst[2]); 424 ASSERT_EQ(&s[3], src); 425} 426 427TEST(wchar, wcsftime) { 428 setenv("TZ", "UTC", 1); 429 430 struct tm t; 431 memset(&t, 0, sizeof(tm)); 432 t.tm_year = 200; 433 t.tm_mon = 2; 434 t.tm_mday = 10; 435 436 wchar_t buf[64]; 437 438 EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t)); 439 EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf); 440} 441 442TEST(wchar, wmemmove) { 443 const wchar_t const_wstr[] = L"This is a test of something or other....."; 444 wchar_t* wstr = new wchar_t[sizeof(const_wstr)]; 445 446 wmemmove(wstr, const_wstr, sizeof(const_wstr)/sizeof(wchar_t)); 447 EXPECT_STREQ(const_wstr, wstr); 448 449 wmemmove(wstr+5, wstr, sizeof(const_wstr)/sizeof(wchar_t) - 6); 450 EXPECT_STREQ(L"This This is a test of something or other", wstr); 451} 452 453TEST(wchar, mbrtowc_15439554) { 454 // http://b/15439554 455 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); 456 uselocale(LC_GLOBAL_LOCALE); 457 458 ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX); 459 ASSERT_GE(MB_CUR_MAX, 4U); 460 461 wchar_t wc; 462 size_t n; 463 464 // 1-byte character. 465 n = mbrtowc(&wc, "x", MB_CUR_MAX, NULL); 466 EXPECT_EQ(1U, n); 467 EXPECT_EQ(L'x', wc); 468 // 2-byte character. 469 n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, NULL); 470 EXPECT_EQ(2U, n); 471 EXPECT_EQ(L'¢', wc); 472 // 3-byte character. 473 n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, NULL); 474 EXPECT_EQ(3U, n); 475 EXPECT_EQ(L'€', wc); 476 // 4-byte character. 477 n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, NULL); 478 EXPECT_EQ(4U, n); 479 EXPECT_EQ(L'', wc); 480} 481