OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/string_piece.h" | 7 #include "base/string_piece.h" |
| 8 #include "base/utf_string_conversions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace base { | 11 namespace base { |
11 | 12 |
12 TEST(StringPieceTest, CheckComparisonOperators) { | 13 TEST(StringPieceTest, CheckComparisonOperators) { |
13 #define CMP_Y(op, x, y) \ | 14 #define CMP_Y(op, x, y) \ |
14 ASSERT_TRUE( (StringPiece((x)) op StringPiece((y)))); \ | 15 ASSERT_TRUE( (StringPiece((x)) op StringPiece((y)))); \ |
15 ASSERT_TRUE( (StringPiece((x)).compare(StringPiece((y))) op 0)) | 16 ASSERT_TRUE( (StringPiece((x)).compare(StringPiece((y))) op 0)) |
16 | 17 |
17 #define CMP_N(op, x, y) \ | 18 #define CMP_N(op, x, y) \ |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 TEST(StringPieceTest, StringCompareNotAmbiguous) { | 534 TEST(StringPieceTest, StringCompareNotAmbiguous) { |
534 ASSERT_TRUE("hello" == std::string("hello")); | 535 ASSERT_TRUE("hello" == std::string("hello")); |
535 ASSERT_TRUE("hello" < std::string("world")); | 536 ASSERT_TRUE("hello" < std::string("world")); |
536 } | 537 } |
537 | 538 |
538 TEST(StringPieceTest, HeterogenousStringPieceEquals) { | 539 TEST(StringPieceTest, HeterogenousStringPieceEquals) { |
539 ASSERT_TRUE(StringPiece("hello") == std::string("hello")); | 540 ASSERT_TRUE(StringPiece("hello") == std::string("hello")); |
540 ASSERT_TRUE("hello" == StringPiece("hello")); | 541 ASSERT_TRUE("hello" == StringPiece("hello")); |
541 } | 542 } |
542 | 543 |
| 544 TEST(StringPiece16Test, CheckComparisonOperators) { |
| 545 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("")) == |
| 546 StringPiece16(ASCIIToUTF16(""))); |
| 547 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) == |
| 548 StringPiece16(ASCIIToUTF16("a"))); |
| 549 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) == |
| 550 StringPiece16(ASCIIToUTF16("aa"))); |
| 551 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) == |
| 552 StringPiece16(ASCIIToUTF16(""))); |
| 553 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("")) == |
| 554 StringPiece16(ASCIIToUTF16("a"))); |
| 555 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) == |
| 556 StringPiece16(ASCIIToUTF16("b"))); |
| 557 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) == |
| 558 StringPiece16(ASCIIToUTF16("aa"))); |
| 559 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) == |
| 560 StringPiece16(ASCIIToUTF16("a"))); |
| 561 |
| 562 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("")) != |
| 563 StringPiece16(ASCIIToUTF16(""))); |
| 564 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) != |
| 565 StringPiece16(ASCIIToUTF16("a"))); |
| 566 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) != |
| 567 StringPiece16(ASCIIToUTF16("aa"))); |
| 568 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) != |
| 569 StringPiece16(ASCIIToUTF16(""))); |
| 570 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("")) != |
| 571 StringPiece16(ASCIIToUTF16("a"))); |
| 572 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) != |
| 573 StringPiece16(ASCIIToUTF16("b"))); |
| 574 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) != |
| 575 StringPiece16(ASCIIToUTF16("aa"))); |
| 576 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) != |
| 577 StringPiece16(ASCIIToUTF16("a"))); |
| 578 |
| 579 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) < |
| 580 StringPiece16(ASCIIToUTF16("b"))); |
| 581 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) < |
| 582 StringPiece16(ASCIIToUTF16("aa"))); |
| 583 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) < |
| 584 StringPiece16(ASCIIToUTF16("b"))); |
| 585 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) < |
| 586 StringPiece16(ASCIIToUTF16("bb"))); |
| 587 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) < |
| 588 StringPiece16(ASCIIToUTF16("a"))); |
| 589 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) < |
| 590 StringPiece16(ASCIIToUTF16("a"))); |
| 591 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) < |
| 592 StringPiece16(ASCIIToUTF16("a"))); |
| 593 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) < |
| 594 StringPiece16(ASCIIToUTF16("aa"))); |
| 595 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) < |
| 596 StringPiece16(ASCIIToUTF16("aa"))); |
| 597 |
| 598 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <= |
| 599 StringPiece16(ASCIIToUTF16("a"))); |
| 600 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <= |
| 601 StringPiece16(ASCIIToUTF16("b"))); |
| 602 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <= |
| 603 StringPiece16(ASCIIToUTF16("aa"))); |
| 604 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <= |
| 605 StringPiece16(ASCIIToUTF16("b"))); |
| 606 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <= |
| 607 StringPiece16(ASCIIToUTF16("bb"))); |
| 608 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <= |
| 609 StringPiece16(ASCIIToUTF16("a"))); |
| 610 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <= |
| 611 StringPiece16(ASCIIToUTF16("a"))); |
| 612 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <= |
| 613 StringPiece16(ASCIIToUTF16("aa"))); |
| 614 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <= |
| 615 StringPiece16(ASCIIToUTF16("aa"))); |
| 616 |
| 617 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <= |
| 618 StringPiece16(ASCIIToUTF16("a"))); |
| 619 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <= |
| 620 StringPiece16(ASCIIToUTF16("b"))); |
| 621 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <= |
| 622 StringPiece16(ASCIIToUTF16("aa"))); |
| 623 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <= |
| 624 StringPiece16(ASCIIToUTF16("b"))); |
| 625 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <= |
| 626 StringPiece16(ASCIIToUTF16("bb"))); |
| 627 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <= |
| 628 StringPiece16(ASCIIToUTF16("a"))); |
| 629 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <= |
| 630 StringPiece16(ASCIIToUTF16("a"))); |
| 631 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <= |
| 632 StringPiece16(ASCIIToUTF16("aa"))); |
| 633 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <= |
| 634 StringPiece16(ASCIIToUTF16("aa"))); |
| 635 |
| 636 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >= |
| 637 StringPiece16(ASCIIToUTF16("b"))); |
| 638 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >= |
| 639 StringPiece16(ASCIIToUTF16("aa"))); |
| 640 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >= |
| 641 StringPiece16(ASCIIToUTF16("b"))); |
| 642 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >= |
| 643 StringPiece16(ASCIIToUTF16("bb"))); |
| 644 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) >= |
| 645 StringPiece16(ASCIIToUTF16("a"))); |
| 646 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >= |
| 647 StringPiece16(ASCIIToUTF16("a"))); |
| 648 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) >= |
| 649 StringPiece16(ASCIIToUTF16("a"))); |
| 650 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >= |
| 651 StringPiece16(ASCIIToUTF16("aa"))); |
| 652 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("bb")) >= |
| 653 StringPiece16(ASCIIToUTF16("aa"))); |
| 654 |
| 655 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) > |
| 656 StringPiece16(ASCIIToUTF16("a"))); |
| 657 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) > |
| 658 StringPiece16(ASCIIToUTF16("b"))); |
| 659 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) > |
| 660 StringPiece16(ASCIIToUTF16("aa"))); |
| 661 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) > |
| 662 StringPiece16(ASCIIToUTF16("b"))); |
| 663 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) > |
| 664 StringPiece16(ASCIIToUTF16("bb"))); |
| 665 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) > |
| 666 StringPiece16(ASCIIToUTF16("a"))); |
| 667 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) > |
| 668 StringPiece16(ASCIIToUTF16("a"))); |
| 669 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) > |
| 670 StringPiece16(ASCIIToUTF16("aa"))); |
| 671 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("bb")) > |
| 672 StringPiece16(ASCIIToUTF16("aa"))); |
| 673 |
| 674 string16 x; |
| 675 for (int i = 0; i < 256; i++) { |
| 676 x += 'a'; |
| 677 string16 y = x; |
| 678 ASSERT_EQ(StringPiece16(x), StringPiece16(y)); |
| 679 for (int j = 0; j < i; j++) { |
| 680 string16 z = x; |
| 681 z[j] = 'b'; // Differs in position 'j' |
| 682 ASSERT_NE(StringPiece16(x), StringPiece16(z)); |
| 683 } |
| 684 } |
| 685 } |
| 686 |
| 687 TEST(StringPiece16Test, CheckSTL) { |
| 688 string16 first = ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz"); |
| 689 StringPiece16 a(first); |
| 690 string16 second = ASCIIToUTF16("abc"); |
| 691 StringPiece16 b(second.c_str()); |
| 692 string16 third = ASCIIToUTF16("xyz"); |
| 693 StringPiece16 c(third.c_str(), third.size()); |
| 694 string16 fourth = ASCIIToUTF16("foobarrandomstuff"); |
| 695 StringPiece16 d(fourth.c_str(), 6); |
| 696 StringPiece16 e; |
| 697 // Check some non-ascii characters. |
| 698 string16 fifth(ASCIIToUTF16("123")); |
| 699 fifth.push_back(0x0000); |
| 700 fifth.push_back(0xd8c5); |
| 701 fifth.push_back(0xdffe); |
| 702 StringPiece16 f(fifth); |
| 703 |
| 704 ASSERT_EQ(a[6], 'g'); |
| 705 ASSERT_EQ(b[0], 'a'); |
| 706 ASSERT_EQ(c[2], 'z'); |
| 707 ASSERT_EQ(f[3], '\0'); |
| 708 ASSERT_EQ(f[5], static_cast<char16>(0xdffe)); |
| 709 |
| 710 ASSERT_EQ(*d.data(), 'f'); |
| 711 ASSERT_EQ(d.data()[5], 'r'); |
| 712 ASSERT_TRUE(e.data() == NULL); |
| 713 |
| 714 ASSERT_EQ(*a.begin(), 'a'); |
| 715 ASSERT_EQ(*(b.begin() + 2), 'c'); |
| 716 ASSERT_EQ(*(c.end() - 1), 'z'); |
| 717 |
| 718 ASSERT_EQ(*a.rbegin(), 'z'); |
| 719 ASSERT_EQ(*(b.rbegin() + 2), 'a'); |
| 720 ASSERT_EQ(*(c.rend() - 1), 'x'); |
| 721 ASSERT_TRUE(a.rbegin() + 26 == a.rend()); |
| 722 |
| 723 ASSERT_EQ(a.size(), 26U); |
| 724 ASSERT_EQ(b.size(), 3U); |
| 725 ASSERT_EQ(c.size(), 3U); |
| 726 ASSERT_EQ(d.size(), 6U); |
| 727 ASSERT_EQ(e.size(), 0U); |
| 728 ASSERT_EQ(f.size(), 6U); |
| 729 |
| 730 ASSERT_TRUE(!d.empty()); |
| 731 ASSERT_TRUE(d.begin() != d.end()); |
| 732 ASSERT_TRUE(d.begin() + 6 == d.end()); |
| 733 |
| 734 ASSERT_TRUE(e.empty()); |
| 735 ASSERT_TRUE(e.begin() == e.end()); |
| 736 |
| 737 d.clear(); |
| 738 ASSERT_EQ(d.size(), 0U); |
| 739 ASSERT_TRUE(d.empty()); |
| 740 ASSERT_TRUE(d.data() == NULL); |
| 741 ASSERT_TRUE(d.begin() == d.end()); |
| 742 |
| 743 ASSERT_GE(a.max_size(), a.capacity()); |
| 744 ASSERT_GE(a.capacity(), a.size()); |
| 745 } |
| 746 |
| 747 TEST(StringPiece16Test, CheckNULL) { |
| 748 StringPiece16 s(NULL); |
| 749 ASSERT_EQ(s.data(), (const char16*)NULL); |
| 750 ASSERT_EQ(s.size(), 0U); |
| 751 |
| 752 s.set(NULL); |
| 753 ASSERT_EQ(s.data(), (const char16*)NULL); |
| 754 ASSERT_EQ(s.size(), 0U); |
| 755 |
| 756 string16 str = s.as_string16(); |
| 757 ASSERT_EQ(s.data(), (const char16*)NULL); |
| 758 ASSERT_EQ(s.size(), 0U); |
| 759 } |
| 760 |
| 761 TEST(StringPiece16Test, HeterogenousStringPieceEquals) { |
| 762 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("hello")) == ASCIIToUTF16("hello")); |
| 763 } |
| 764 |
| 765 TEST(StringPiece16Test, CheckConversion) { |
| 766 // Make sure that we can convert from UTF8 to UTF16 and back. We use a two |
| 767 // byte character (G clef) to test this. |
| 768 ASSERT_EQ( |
| 769 UTF16ToUTF8( |
| 770 StringPiece16(UTF8ToUTF16("\xf0\x9d\x84\x9e")).as_string16()), |
| 771 "\xf0\x9d\x84\x9e"); |
| 772 } |
| 773 |
543 } // namespace base | 774 } // namespace base |
OLD | NEW |