1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "core/include/fxcrt/fx_basic.h" 6 #include "testing/fx_string_testhelpers.h" 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 TEST(fxcrt, WideStringOperatorSubscript) { 10 // CFX_WideString includes the NUL terminator for non-empty strings. 11 CFX_WideString abc(L"abc"); 12 EXPECT_EQ(L'a', abc[0]); 13 EXPECT_EQ(L'b', abc[1]); 14 EXPECT_EQ(L'c', abc[2]); 15 EXPECT_EQ(L'\0', abc[3]); 16 } 17 18 TEST(fxcrt, WideStringOperatorLT) { 19 CFX_WideString empty; 20 CFX_WideString a(L"a"); 21 CFX_WideString abc(L"\x0110qq"); // Comes before despite endianness. 22 CFX_WideString def(L"\x1001qq"); // Comes after despite endianness. 23 24 EXPECT_FALSE(empty < empty); 25 EXPECT_FALSE(a < a); 26 EXPECT_FALSE(abc < abc); 27 EXPECT_FALSE(def < def); 28 29 EXPECT_TRUE(empty < a); 30 EXPECT_FALSE(a < empty); 31 32 EXPECT_TRUE(empty < abc); 33 EXPECT_FALSE(abc < empty); 34 35 EXPECT_TRUE(empty < def); 36 EXPECT_FALSE(def < empty); 37 38 EXPECT_TRUE(a < abc); 39 EXPECT_FALSE(abc < a); 40 41 EXPECT_TRUE(a < def); 42 EXPECT_FALSE(def < a); 43 44 EXPECT_TRUE(abc < def); 45 EXPECT_FALSE(def < abc); 46 } 47 48 TEST(fxcrt, WideStringOperatorEQ) { 49 CFX_WideString null_string; 50 EXPECT_TRUE(null_string == null_string); 51 52 CFX_WideString empty_string(L""); 53 EXPECT_TRUE(empty_string == empty_string); 54 EXPECT_TRUE(empty_string == null_string); 55 EXPECT_TRUE(null_string == empty_string); 56 57 CFX_WideString deleted_string(L"hello"); 58 deleted_string.Delete(0, 5); 59 EXPECT_TRUE(deleted_string == deleted_string); 60 EXPECT_TRUE(deleted_string == null_string); 61 EXPECT_TRUE(deleted_string == empty_string); 62 EXPECT_TRUE(null_string == deleted_string); 63 EXPECT_TRUE(null_string == empty_string); 64 65 CFX_WideString wide_string(L"hello"); 66 EXPECT_TRUE(wide_string == wide_string); 67 EXPECT_FALSE(wide_string == null_string); 68 EXPECT_FALSE(wide_string == empty_string); 69 EXPECT_FALSE(wide_string == deleted_string); 70 EXPECT_FALSE(null_string == wide_string); 71 EXPECT_FALSE(empty_string == wide_string); 72 EXPECT_FALSE(deleted_string == wide_string); 73 74 CFX_WideString wide_string_same1(L"hello"); 75 EXPECT_TRUE(wide_string == wide_string_same1); 76 EXPECT_TRUE(wide_string_same1 == wide_string); 77 78 CFX_WideString wide_string_same2(wide_string); 79 EXPECT_TRUE(wide_string == wide_string_same2); 80 EXPECT_TRUE(wide_string_same2 == wide_string); 81 82 CFX_WideString wide_string1(L"he"); 83 CFX_WideString wide_string2(L"hellp"); 84 CFX_WideString wide_string3(L"hellod"); 85 EXPECT_FALSE(wide_string == wide_string1); 86 EXPECT_FALSE(wide_string == wide_string2); 87 EXPECT_FALSE(wide_string == wide_string3); 88 EXPECT_FALSE(wide_string1 == wide_string); 89 EXPECT_FALSE(wide_string2 == wide_string); 90 EXPECT_FALSE(wide_string3 == wide_string); 91 92 CFX_WideStringC null_string_c; 93 CFX_WideStringC empty_string_c(L""); 94 EXPECT_TRUE(null_string == null_string_c); 95 EXPECT_TRUE(null_string == empty_string_c); 96 EXPECT_TRUE(empty_string == null_string_c); 97 EXPECT_TRUE(empty_string == empty_string_c); 98 EXPECT_TRUE(deleted_string == null_string_c); 99 EXPECT_TRUE(deleted_string == empty_string_c); 100 EXPECT_TRUE(null_string_c == null_string); 101 EXPECT_TRUE(empty_string_c == null_string); 102 EXPECT_TRUE(null_string_c == empty_string); 103 EXPECT_TRUE(empty_string_c == empty_string); 104 EXPECT_TRUE(null_string_c == deleted_string); 105 EXPECT_TRUE(empty_string_c == deleted_string); 106 107 CFX_WideStringC wide_string_c_same1(L"hello"); 108 EXPECT_TRUE(wide_string == wide_string_c_same1); 109 EXPECT_TRUE(wide_string_c_same1 == wide_string); 110 111 CFX_WideStringC wide_string_c1(L"he"); 112 CFX_WideStringC wide_string_c2(L"hellp"); 113 CFX_WideStringC wide_string_c3(L"hellod"); 114 EXPECT_FALSE(wide_string == wide_string_c1); 115 EXPECT_FALSE(wide_string == wide_string_c2); 116 EXPECT_FALSE(wide_string == wide_string_c3); 117 EXPECT_FALSE(wide_string_c1 == wide_string); 118 EXPECT_FALSE(wide_string_c2 == wide_string); 119 EXPECT_FALSE(wide_string_c3 == wide_string); 120 121 const wchar_t* c_null_string = nullptr; 122 const wchar_t* c_empty_string = L""; 123 EXPECT_TRUE(null_string == c_null_string); 124 EXPECT_TRUE(null_string == c_empty_string); 125 EXPECT_TRUE(empty_string == c_null_string); 126 EXPECT_TRUE(empty_string == c_empty_string); 127 EXPECT_TRUE(deleted_string == c_null_string); 128 EXPECT_TRUE(deleted_string == c_empty_string); 129 EXPECT_TRUE(c_null_string == null_string); 130 EXPECT_TRUE(c_empty_string == null_string); 131 EXPECT_TRUE(c_null_string == empty_string); 132 EXPECT_TRUE(c_empty_string == empty_string); 133 EXPECT_TRUE(c_null_string == deleted_string); 134 EXPECT_TRUE(c_empty_string == deleted_string); 135 136 const wchar_t* c_string_same1 = L"hello"; 137 EXPECT_TRUE(wide_string == c_string_same1); 138 EXPECT_TRUE(c_string_same1 == wide_string); 139 140 const wchar_t* c_string1 = L"he"; 141 const wchar_t* c_string2 = L"hellp"; 142 const wchar_t* c_string3 = L"hellod"; 143 EXPECT_FALSE(wide_string == c_string1); 144 EXPECT_FALSE(wide_string == c_string2); 145 EXPECT_FALSE(wide_string == c_string3); 146 EXPECT_FALSE(c_string1 == wide_string); 147 EXPECT_FALSE(c_string2 == wide_string); 148 EXPECT_FALSE(c_string3 == wide_string); 149 } 150 151 TEST(fxcrt, WideStringOperatorNE) { 152 CFX_WideString null_string; 153 EXPECT_FALSE(null_string != null_string); 154 155 CFX_WideString empty_string(L""); 156 EXPECT_FALSE(empty_string != empty_string); 157 EXPECT_FALSE(empty_string != null_string); 158 EXPECT_FALSE(null_string != empty_string); 159 160 CFX_WideString deleted_string(L"hello"); 161 deleted_string.Delete(0, 5); 162 EXPECT_FALSE(deleted_string != deleted_string); 163 EXPECT_FALSE(deleted_string != null_string); 164 EXPECT_FALSE(deleted_string != empty_string); 165 EXPECT_FALSE(null_string != deleted_string); 166 EXPECT_FALSE(null_string != empty_string); 167 168 CFX_WideString wide_string(L"hello"); 169 EXPECT_FALSE(wide_string != wide_string); 170 EXPECT_TRUE(wide_string != null_string); 171 EXPECT_TRUE(wide_string != empty_string); 172 EXPECT_TRUE(wide_string != deleted_string); 173 EXPECT_TRUE(null_string != wide_string); 174 EXPECT_TRUE(empty_string != wide_string); 175 EXPECT_TRUE(deleted_string != wide_string); 176 177 CFX_WideString wide_string_same1(L"hello"); 178 EXPECT_FALSE(wide_string != wide_string_same1); 179 EXPECT_FALSE(wide_string_same1 != wide_string); 180 181 CFX_WideString wide_string_same2(wide_string); 182 EXPECT_FALSE(wide_string != wide_string_same2); 183 EXPECT_FALSE(wide_string_same2 != wide_string); 184 185 CFX_WideString wide_string1(L"he"); 186 CFX_WideString wide_string2(L"hellp"); 187 CFX_WideString wide_string3(L"hellod"); 188 EXPECT_TRUE(wide_string != wide_string1); 189 EXPECT_TRUE(wide_string != wide_string2); 190 EXPECT_TRUE(wide_string != wide_string3); 191 EXPECT_TRUE(wide_string1 != wide_string); 192 EXPECT_TRUE(wide_string2 != wide_string); 193 EXPECT_TRUE(wide_string3 != wide_string); 194 195 CFX_WideStringC null_string_c; 196 CFX_WideStringC empty_string_c(L""); 197 EXPECT_FALSE(null_string != null_string_c); 198 EXPECT_FALSE(null_string != empty_string_c); 199 EXPECT_FALSE(empty_string != null_string_c); 200 EXPECT_FALSE(empty_string != empty_string_c); 201 EXPECT_FALSE(deleted_string != null_string_c); 202 EXPECT_FALSE(deleted_string != empty_string_c); 203 EXPECT_FALSE(null_string_c != null_string); 204 EXPECT_FALSE(empty_string_c != null_string); 205 EXPECT_FALSE(null_string_c != empty_string); 206 EXPECT_FALSE(empty_string_c != empty_string); 207 208 CFX_WideStringC wide_string_c_same1(L"hello"); 209 EXPECT_FALSE(wide_string != wide_string_c_same1); 210 EXPECT_FALSE(wide_string_c_same1 != wide_string); 211 212 CFX_WideStringC wide_string_c1(L"he"); 213 CFX_WideStringC wide_string_c2(L"hellp"); 214 CFX_WideStringC wide_string_c3(L"hellod"); 215 EXPECT_TRUE(wide_string != wide_string_c1); 216 EXPECT_TRUE(wide_string != wide_string_c2); 217 EXPECT_TRUE(wide_string != wide_string_c3); 218 EXPECT_TRUE(wide_string_c1 != wide_string); 219 EXPECT_TRUE(wide_string_c2 != wide_string); 220 EXPECT_TRUE(wide_string_c3 != wide_string); 221 222 const wchar_t* c_null_string = nullptr; 223 const wchar_t* c_empty_string = L""; 224 EXPECT_FALSE(null_string != c_null_string); 225 EXPECT_FALSE(null_string != c_empty_string); 226 EXPECT_FALSE(empty_string != c_null_string); 227 EXPECT_FALSE(empty_string != c_empty_string); 228 EXPECT_FALSE(deleted_string != c_null_string); 229 EXPECT_FALSE(deleted_string != c_empty_string); 230 EXPECT_FALSE(c_null_string != null_string); 231 EXPECT_FALSE(c_empty_string != null_string); 232 EXPECT_FALSE(c_null_string != empty_string); 233 EXPECT_FALSE(c_empty_string != empty_string); 234 EXPECT_FALSE(c_null_string != deleted_string); 235 EXPECT_FALSE(c_empty_string != deleted_string); 236 237 const wchar_t* c_string_same1 = L"hello"; 238 EXPECT_FALSE(wide_string != c_string_same1); 239 EXPECT_FALSE(c_string_same1 != wide_string); 240 241 const wchar_t* c_string1 = L"he"; 242 const wchar_t* c_string2 = L"hellp"; 243 const wchar_t* c_string3 = L"hellod"; 244 EXPECT_TRUE(wide_string != c_string1); 245 EXPECT_TRUE(wide_string != c_string2); 246 EXPECT_TRUE(wide_string != c_string3); 247 EXPECT_TRUE(c_string1 != wide_string); 248 EXPECT_TRUE(c_string2 != wide_string); 249 EXPECT_TRUE(c_string3 != wide_string); 250 } 251 252 TEST(fxcrt, WideStringConcatInPlace) { 253 CFX_WideString fred; 254 fred.ConcatInPlace(4, L"FRED"); 255 EXPECT_EQ(L"FRED", fred); 256 257 fred.ConcatInPlace(2, L"DY"); 258 EXPECT_EQ(L"FREDDY", fred); 259 260 fred.Delete(3, 3); 261 EXPECT_EQ(L"FRE", fred); 262 263 fred.ConcatInPlace(1, L"D"); 264 EXPECT_EQ(L"FRED", fred); 265 266 CFX_WideString copy = fred; 267 fred.ConcatInPlace(2, L"DY"); 268 EXPECT_EQ(L"FREDDY", fred); 269 EXPECT_EQ(L"FRED", copy); 270 271 // Test invalid arguments. 272 copy = fred; 273 fred.ConcatInPlace(-6, L"freddy"); 274 CFX_WideString not_aliased(L"xxxxxx"); 275 EXPECT_EQ(L"FREDDY", fred); 276 EXPECT_EQ(L"xxxxxx", not_aliased); 277 } 278 279 TEST(fxcrt, WideStringUTF16LE_Encode) { 280 struct UTF16LEEncodeCase { 281 CFX_WideString ws; 282 CFX_ByteString bs; 283 } utf16le_encode_cases[] = { 284 {L"", CFX_ByteString("\0\0", 2)}, 285 {L"abc", CFX_ByteString("a\0b\0c\0\0\0", 8)}, 286 {L"abcdef", CFX_ByteString("a\0b\0c\0d\0e\0f\0\0\0", 14)}, 287 {L"abc\0def", CFX_ByteString("a\0b\0c\0\0\0", 8)}, 288 {L"\xaabb\xccdd", CFX_ByteString("\xbb\xaa\xdd\xcc\0\0", 6)}, 289 {L"\x3132\x6162", CFX_ByteString("\x32\x31\x62\x61\0\0", 6)}, 290 }; 291 292 for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) { 293 EXPECT_EQ(utf16le_encode_cases[i].bs, 294 utf16le_encode_cases[i].ws.UTF16LE_Encode()) 295 << " for case number " << i; 296 } 297 } 298 299 TEST(fxcrt, WideStringCOperatorSubscript) { 300 // CFX_WideStringC includes the NUL terminator for non-empty strings. 301 CFX_WideStringC abc(L"abc"); 302 EXPECT_EQ(L'a', abc[0]); 303 EXPECT_EQ(L'b', abc[1]); 304 EXPECT_EQ(L'c', abc[2]); 305 EXPECT_EQ(L'\0', abc[3]); 306 } 307 308 TEST(fxcrt, WideStringCOperatorLT) { 309 CFX_WideStringC empty; 310 CFX_WideStringC a(L"a"); 311 CFX_WideStringC abc(L"\x0110qq"); // Comes before despite endianness. 312 CFX_WideStringC def(L"\x1001qq"); // Comes after despite endianness. 313 314 EXPECT_FALSE(empty < empty); 315 EXPECT_FALSE(a < a); 316 EXPECT_FALSE(abc < abc); 317 EXPECT_FALSE(def < def); 318 319 EXPECT_TRUE(empty < a); 320 EXPECT_FALSE(a < empty); 321 322 EXPECT_TRUE(empty < abc); 323 EXPECT_FALSE(abc < empty); 324 325 EXPECT_TRUE(empty < def); 326 EXPECT_FALSE(def < empty); 327 328 EXPECT_TRUE(a < abc); 329 EXPECT_FALSE(abc < a); 330 331 EXPECT_TRUE(a < def); 332 EXPECT_FALSE(def < a); 333 334 EXPECT_TRUE(abc < def); 335 EXPECT_FALSE(def < abc); 336 } 337 338 TEST(fxcrt, WideStringCOperatorEQ) { 339 CFX_WideStringC wide_string_c(L"hello"); 340 EXPECT_TRUE(wide_string_c == wide_string_c); 341 342 CFX_WideStringC wide_string_c_same1(L"hello"); 343 EXPECT_TRUE(wide_string_c == wide_string_c_same1); 344 EXPECT_TRUE(wide_string_c_same1 == wide_string_c); 345 346 CFX_WideStringC wide_string_c_same2(wide_string_c); 347 EXPECT_TRUE(wide_string_c == wide_string_c_same2); 348 EXPECT_TRUE(wide_string_c_same2 == wide_string_c); 349 350 CFX_WideStringC wide_string_c1(L"he"); 351 CFX_WideStringC wide_string_c2(L"hellp"); 352 CFX_WideStringC wide_string_c3(L"hellod"); 353 EXPECT_FALSE(wide_string_c == wide_string_c1); 354 EXPECT_FALSE(wide_string_c == wide_string_c2); 355 EXPECT_FALSE(wide_string_c == wide_string_c3); 356 EXPECT_FALSE(wide_string_c1 == wide_string_c); 357 EXPECT_FALSE(wide_string_c2 == wide_string_c); 358 EXPECT_FALSE(wide_string_c3 == wide_string_c); 359 360 CFX_WideString wide_string_same1(L"hello"); 361 EXPECT_TRUE(wide_string_c == wide_string_same1); 362 EXPECT_TRUE(wide_string_same1 == wide_string_c); 363 364 CFX_WideString wide_string1(L"he"); 365 CFX_WideString wide_string2(L"hellp"); 366 CFX_WideString wide_string3(L"hellod"); 367 EXPECT_FALSE(wide_string_c == wide_string1); 368 EXPECT_FALSE(wide_string_c == wide_string2); 369 EXPECT_FALSE(wide_string_c == wide_string3); 370 EXPECT_FALSE(wide_string1 == wide_string_c); 371 EXPECT_FALSE(wide_string2 == wide_string_c); 372 EXPECT_FALSE(wide_string3 == wide_string_c); 373 374 const wchar_t* c_string_same1 = L"hello"; 375 EXPECT_TRUE(wide_string_c == c_string_same1); 376 EXPECT_TRUE(c_string_same1 == wide_string_c); 377 378 const wchar_t* c_string1 = L"he"; 379 const wchar_t* c_string2 = L"hellp"; 380 const wchar_t* c_string3 = L"hellod"; 381 EXPECT_FALSE(wide_string_c == c_string1); 382 EXPECT_FALSE(wide_string_c == c_string2); 383 EXPECT_FALSE(wide_string_c == c_string3); 384 385 EXPECT_FALSE(c_string1 == wide_string_c); 386 EXPECT_FALSE(c_string2 == wide_string_c); 387 EXPECT_FALSE(c_string3 == wide_string_c); 388 } 389 390 TEST(fxcrt, WideStringCOperatorNE) { 391 CFX_WideStringC wide_string_c(L"hello"); 392 EXPECT_FALSE(wide_string_c != wide_string_c); 393 394 CFX_WideStringC wide_string_c_same1(L"hello"); 395 EXPECT_FALSE(wide_string_c != wide_string_c_same1); 396 EXPECT_FALSE(wide_string_c_same1 != wide_string_c); 397 398 CFX_WideStringC wide_string_c_same2(wide_string_c); 399 EXPECT_FALSE(wide_string_c != wide_string_c_same2); 400 EXPECT_FALSE(wide_string_c_same2 != wide_string_c); 401 402 CFX_WideStringC wide_string_c1(L"he"); 403 CFX_WideStringC wide_string_c2(L"hellp"); 404 CFX_WideStringC wide_string_c3(L"hellod"); 405 EXPECT_TRUE(wide_string_c != wide_string_c1); 406 EXPECT_TRUE(wide_string_c != wide_string_c2); 407 EXPECT_TRUE(wide_string_c != wide_string_c3); 408 EXPECT_TRUE(wide_string_c1 != wide_string_c); 409 EXPECT_TRUE(wide_string_c2 != wide_string_c); 410 EXPECT_TRUE(wide_string_c3 != wide_string_c); 411 412 CFX_WideString wide_string_same1(L"hello"); 413 EXPECT_FALSE(wide_string_c != wide_string_same1); 414 EXPECT_FALSE(wide_string_same1 != wide_string_c); 415 416 CFX_WideString wide_string1(L"he"); 417 CFX_WideString wide_string2(L"hellp"); 418 CFX_WideString wide_string3(L"hellod"); 419 EXPECT_TRUE(wide_string_c != wide_string1); 420 EXPECT_TRUE(wide_string_c != wide_string2); 421 EXPECT_TRUE(wide_string_c != wide_string3); 422 EXPECT_TRUE(wide_string1 != wide_string_c); 423 EXPECT_TRUE(wide_string2 != wide_string_c); 424 EXPECT_TRUE(wide_string3 != wide_string_c); 425 426 const wchar_t* c_string_same1 = L"hello"; 427 EXPECT_FALSE(wide_string_c != c_string_same1); 428 EXPECT_FALSE(c_string_same1 != wide_string_c); 429 430 const wchar_t* c_string1 = L"he"; 431 const wchar_t* c_string2 = L"hellp"; 432 const wchar_t* c_string3 = L"hellod"; 433 EXPECT_TRUE(wide_string_c != c_string1); 434 EXPECT_TRUE(wide_string_c != c_string2); 435 EXPECT_TRUE(wide_string_c != c_string3); 436 437 EXPECT_TRUE(c_string1 != wide_string_c); 438 EXPECT_TRUE(c_string2 != wide_string_c); 439 EXPECT_TRUE(c_string3 != wide_string_c); 440 } 441 442 TEST(fxcrt, WideStringFormatWidth) { 443 { 444 CFX_WideString str; 445 str.Format(L"%5d", 1); 446 EXPECT_EQ(L" 1", str); 447 } 448 449 { 450 CFX_WideString str; 451 str.Format(L"%d", 1); 452 EXPECT_EQ(L"1", str); 453 } 454 455 { 456 CFX_WideString str; 457 str.Format(L"%*d", 5, 1); 458 EXPECT_EQ(L" 1", str); 459 } 460 461 { 462 CFX_WideString str; 463 str.Format(L"%-1d", 1); 464 EXPECT_EQ(L"1", str); 465 } 466 467 { 468 CFX_WideString str; 469 str.Format(L"%0d", 1); 470 EXPECT_EQ(L"1", str); 471 } 472 473 { 474 CFX_WideString str; 475 str.Format(L"%1048576d", 1); 476 EXPECT_EQ(L"Bad width", str); 477 } 478 } 479 480 TEST(fxcrt, WideStringFormatPrecision) { 481 { 482 CFX_WideString str; 483 str.Format(L"%.2f", 1.12345); 484 EXPECT_EQ(L"1.12", str); 485 } 486 487 { 488 CFX_WideString str; 489 str.Format(L"%.*f", 3, 1.12345); 490 EXPECT_EQ(L"1.123", str); 491 } 492 493 { 494 CFX_WideString str; 495 str.Format(L"%f", 1.12345); 496 EXPECT_EQ(L"1.123450", str); 497 } 498 499 { 500 CFX_WideString str; 501 str.Format(L"%-1f", 1.12345); 502 EXPECT_EQ(L"1.123450", str); 503 } 504 505 { 506 CFX_WideString str; 507 str.Format(L"%0f", 1.12345); 508 EXPECT_EQ(L"1.123450", str); 509 } 510 511 { 512 CFX_WideString str; 513 str.Format(L"%.1048576f", 1.2); 514 EXPECT_EQ(L"Bad precision", str); 515 } 516 } 517 518 TEST(fxcrt, EmptyWideString) { 519 CFX_WideString empty_str; 520 EXPECT_TRUE(empty_str.IsEmpty()); 521 EXPECT_EQ(0, empty_str.GetLength()); 522 const FX_WCHAR* cstr = empty_str.c_str(); 523 EXPECT_EQ(0, FXSYS_wcslen(cstr)); 524 } 525