Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1281)

Side by Side Diff: base/strings/string_util_unittest.cc

Issue 1641513004: Update //base to chromium 9659b08ea5a34f889dc4166217f438095ddc10d2 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/strings/string_util_posix.h ('k') | base/strings/string_util_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/strings/string_util.h" 5 #include "base/strings/string_util.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <stdarg.h> 8 #include <stdarg.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 662
663 // Verify the lower case as well. 663 // Verify the lower case as well.
664 EXPECT_EQ(10, HexDigitToInt('a')); 664 EXPECT_EQ(10, HexDigitToInt('a'));
665 EXPECT_EQ(11, HexDigitToInt('b')); 665 EXPECT_EQ(11, HexDigitToInt('b'));
666 EXPECT_EQ(12, HexDigitToInt('c')); 666 EXPECT_EQ(12, HexDigitToInt('c'));
667 EXPECT_EQ(13, HexDigitToInt('d')); 667 EXPECT_EQ(13, HexDigitToInt('d'));
668 EXPECT_EQ(14, HexDigitToInt('e')); 668 EXPECT_EQ(14, HexDigitToInt('e'));
669 EXPECT_EQ(15, HexDigitToInt('f')); 669 EXPECT_EQ(15, HexDigitToInt('f'));
670 } 670 }
671 671
672 // Test for Tokenize
673 template <typename STR>
674 void TokenizeTest() {
675 std::vector<STR> r;
676 size_t size;
677
678 size = Tokenize(STR("This is a string"), STR(" "), &r);
679 EXPECT_EQ(4U, size);
680 ASSERT_EQ(4U, r.size());
681 EXPECT_EQ(r[0], STR("This"));
682 EXPECT_EQ(r[1], STR("is"));
683 EXPECT_EQ(r[2], STR("a"));
684 EXPECT_EQ(r[3], STR("string"));
685 r.clear();
686
687 size = Tokenize(STR("one,two,three"), STR(","), &r);
688 EXPECT_EQ(3U, size);
689 ASSERT_EQ(3U, r.size());
690 EXPECT_EQ(r[0], STR("one"));
691 EXPECT_EQ(r[1], STR("two"));
692 EXPECT_EQ(r[2], STR("three"));
693 r.clear();
694
695 size = Tokenize(STR("one,two:three;four"), STR(",:"), &r);
696 EXPECT_EQ(3U, size);
697 ASSERT_EQ(3U, r.size());
698 EXPECT_EQ(r[0], STR("one"));
699 EXPECT_EQ(r[1], STR("two"));
700 EXPECT_EQ(r[2], STR("three;four"));
701 r.clear();
702
703 size = Tokenize(STR("one,two:three;four"), STR(";,:"), &r);
704 EXPECT_EQ(4U, size);
705 ASSERT_EQ(4U, r.size());
706 EXPECT_EQ(r[0], STR("one"));
707 EXPECT_EQ(r[1], STR("two"));
708 EXPECT_EQ(r[2], STR("three"));
709 EXPECT_EQ(r[3], STR("four"));
710 r.clear();
711
712 size = Tokenize(STR("one, two, three"), STR(","), &r);
713 EXPECT_EQ(3U, size);
714 ASSERT_EQ(3U, r.size());
715 EXPECT_EQ(r[0], STR("one"));
716 EXPECT_EQ(r[1], STR(" two"));
717 EXPECT_EQ(r[2], STR(" three"));
718 r.clear();
719
720 size = Tokenize(STR("one, two, three, "), STR(","), &r);
721 EXPECT_EQ(4U, size);
722 ASSERT_EQ(4U, r.size());
723 EXPECT_EQ(r[0], STR("one"));
724 EXPECT_EQ(r[1], STR(" two"));
725 EXPECT_EQ(r[2], STR(" three"));
726 EXPECT_EQ(r[3], STR(" "));
727 r.clear();
728
729 size = Tokenize(STR("one, two, three,"), STR(","), &r);
730 EXPECT_EQ(3U, size);
731 ASSERT_EQ(3U, r.size());
732 EXPECT_EQ(r[0], STR("one"));
733 EXPECT_EQ(r[1], STR(" two"));
734 EXPECT_EQ(r[2], STR(" three"));
735 r.clear();
736
737 size = Tokenize(STR(), STR(","), &r);
738 EXPECT_EQ(0U, size);
739 ASSERT_EQ(0U, r.size());
740 r.clear();
741
742 size = Tokenize(STR(","), STR(","), &r);
743 EXPECT_EQ(0U, size);
744 ASSERT_EQ(0U, r.size());
745 r.clear();
746
747 size = Tokenize(STR(",;:."), STR(".:;,"), &r);
748 EXPECT_EQ(0U, size);
749 ASSERT_EQ(0U, r.size());
750 r.clear();
751
752 size = Tokenize(STR("\t\ta\t"), STR("\t"), &r);
753 EXPECT_EQ(1U, size);
754 ASSERT_EQ(1U, r.size());
755 EXPECT_EQ(r[0], STR("a"));
756 r.clear();
757
758 size = Tokenize(STR("\ta\t\nb\tcc"), STR("\n"), &r);
759 EXPECT_EQ(2U, size);
760 ASSERT_EQ(2U, r.size());
761 EXPECT_EQ(r[0], STR("\ta\t"));
762 EXPECT_EQ(r[1], STR("b\tcc"));
763 r.clear();
764 }
765
766 TEST(StringUtilTest, TokenizeStdString) {
767 TokenizeTest<std::string>();
768 }
769
770 TEST(StringUtilTest, TokenizeStringPiece) {
771 TokenizeTest<StringPiece>();
772 }
773
774 // Test for JoinString
775 TEST(StringUtilTest, JoinString) { 672 TEST(StringUtilTest, JoinString) {
776 std::vector<std::string> in;
777 EXPECT_EQ("", JoinString(in, ','));
778
779 in.push_back("a");
780 EXPECT_EQ("a", JoinString(in, ','));
781
782 in.push_back("b");
783 in.push_back("c");
784 EXPECT_EQ("a,b,c", JoinString(in, ','));
785
786 in.push_back(std::string());
787 EXPECT_EQ("a,b,c,", JoinString(in, ','));
788 in.push_back(" ");
789 EXPECT_EQ("a|b|c|| ", JoinString(in, '|'));
790 }
791
792 // Test for JoinString overloaded with std::string separator
793 TEST(StringUtilTest, JoinStringWithString) {
794 std::string separator(", "); 673 std::string separator(", ");
795 std::vector<std::string> parts; 674 std::vector<std::string> parts;
796 EXPECT_EQ(std::string(), JoinString(parts, separator)); 675 EXPECT_EQ(std::string(), JoinString(parts, separator));
797 676
798 parts.push_back("a"); 677 parts.push_back("a");
799 EXPECT_EQ("a", JoinString(parts, separator)); 678 EXPECT_EQ("a", JoinString(parts, separator));
800 679
801 parts.push_back("b"); 680 parts.push_back("b");
802 parts.push_back("c"); 681 parts.push_back("c");
803 EXPECT_EQ("a, b, c", JoinString(parts, separator)); 682 EXPECT_EQ("a, b, c", JoinString(parts, separator));
804 683
805 parts.push_back(std::string()); 684 parts.push_back(std::string());
806 EXPECT_EQ("a, b, c, ", JoinString(parts, separator)); 685 EXPECT_EQ("a, b, c, ", JoinString(parts, separator));
807 parts.push_back(" "); 686 parts.push_back(" ");
808 EXPECT_EQ("a|b|c|| ", JoinString(parts, "|")); 687 EXPECT_EQ("a|b|c|| ", JoinString(parts, "|"));
809 } 688 }
810 689
811 // Test for JoinString overloaded with string16 separator 690 TEST(StringUtilTest, JoinString16) {
812 TEST(StringUtilTest, JoinStringWithString16) {
813 string16 separator = ASCIIToUTF16(", "); 691 string16 separator = ASCIIToUTF16(", ");
814 std::vector<string16> parts; 692 std::vector<string16> parts;
815 EXPECT_EQ(string16(), JoinString(parts, separator)); 693 EXPECT_EQ(string16(), JoinString(parts, separator));
816 694
817 parts.push_back(ASCIIToUTF16("a")); 695 parts.push_back(ASCIIToUTF16("a"));
818 EXPECT_EQ(ASCIIToUTF16("a"), JoinString(parts, separator)); 696 EXPECT_EQ(ASCIIToUTF16("a"), JoinString(parts, separator));
819 697
820 parts.push_back(ASCIIToUTF16("b")); 698 parts.push_back(ASCIIToUTF16("b"));
821 parts.push_back(ASCIIToUTF16("c")); 699 parts.push_back(ASCIIToUTF16("c"));
822 EXPECT_EQ(ASCIIToUTF16("a, b, c"), JoinString(parts, separator)); 700 EXPECT_EQ(ASCIIToUTF16("a, b, c"), JoinString(parts, separator));
823 701
824 parts.push_back(ASCIIToUTF16("")); 702 parts.push_back(ASCIIToUTF16(""));
825 EXPECT_EQ(ASCIIToUTF16("a, b, c, "), JoinString(parts, separator)); 703 EXPECT_EQ(ASCIIToUTF16("a, b, c, "), JoinString(parts, separator));
826 parts.push_back(ASCIIToUTF16(" ")); 704 parts.push_back(ASCIIToUTF16(" "));
827 EXPECT_EQ(ASCIIToUTF16("a|b|c|| "), JoinString(parts, ASCIIToUTF16("|"))); 705 EXPECT_EQ(ASCIIToUTF16("a|b|c|| "), JoinString(parts, ASCIIToUTF16("|")));
828 } 706 }
829 707
830 TEST(StringUtilTest, StartsWith) { 708 TEST(StringUtilTest, StartsWith) {
831 EXPECT_TRUE(StartsWithASCII("javascript:url", "javascript", true)); 709 EXPECT_TRUE(
832 EXPECT_FALSE(StartsWithASCII("JavaScript:url", "javascript", true)); 710 StartsWith("javascript:url", "javascript", base::CompareCase::SENSITIVE));
833 EXPECT_TRUE(StartsWithASCII("javascript:url", "javascript", false)); 711 EXPECT_FALSE(
834 EXPECT_TRUE(StartsWithASCII("JavaScript:url", "javascript", false)); 712 StartsWith("JavaScript:url", "javascript", base::CompareCase::SENSITIVE));
835 EXPECT_FALSE(StartsWithASCII("java", "javascript", true)); 713 EXPECT_TRUE(StartsWith("javascript:url", "javascript",
836 EXPECT_FALSE(StartsWithASCII("java", "javascript", false)); 714 base::CompareCase::INSENSITIVE_ASCII));
837 EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", false)); 715 EXPECT_TRUE(StartsWith("JavaScript:url", "javascript",
838 EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", true)); 716 base::CompareCase::INSENSITIVE_ASCII));
839 EXPECT_TRUE(StartsWithASCII("java", std::string(), false)); 717 EXPECT_FALSE(StartsWith("java", "javascript", base::CompareCase::SENSITIVE));
840 EXPECT_TRUE(StartsWithASCII("java", std::string(), true)); 718 EXPECT_FALSE(
719 StartsWith("java", "javascript", base::CompareCase::INSENSITIVE_ASCII));
720 EXPECT_FALSE(StartsWith(std::string(), "javascript",
721 base::CompareCase::INSENSITIVE_ASCII));
722 EXPECT_FALSE(
723 StartsWith(std::string(), "javascript", base::CompareCase::SENSITIVE));
724 EXPECT_TRUE(
725 StartsWith("java", std::string(), base::CompareCase::INSENSITIVE_ASCII));
726 EXPECT_TRUE(StartsWith("java", std::string(), base::CompareCase::SENSITIVE));
841 727
842 EXPECT_TRUE(StartsWith(ASCIIToUTF16("javascript:url"), 728 EXPECT_TRUE(StartsWith(ASCIIToUTF16("javascript:url"),
843 ASCIIToUTF16("javascript"), true)); 729 ASCIIToUTF16("javascript"),
730 base::CompareCase::SENSITIVE));
844 EXPECT_FALSE(StartsWith(ASCIIToUTF16("JavaScript:url"), 731 EXPECT_FALSE(StartsWith(ASCIIToUTF16("JavaScript:url"),
845 ASCIIToUTF16("javascript"), true)); 732 ASCIIToUTF16("javascript"),
733 base::CompareCase::SENSITIVE));
846 EXPECT_TRUE(StartsWith(ASCIIToUTF16("javascript:url"), 734 EXPECT_TRUE(StartsWith(ASCIIToUTF16("javascript:url"),
847 ASCIIToUTF16("javascript"), false)); 735 ASCIIToUTF16("javascript"),
736 base::CompareCase::INSENSITIVE_ASCII));
848 EXPECT_TRUE(StartsWith(ASCIIToUTF16("JavaScript:url"), 737 EXPECT_TRUE(StartsWith(ASCIIToUTF16("JavaScript:url"),
849 ASCIIToUTF16("javascript"), false)); 738 ASCIIToUTF16("javascript"),
850 EXPECT_FALSE(StartsWith(ASCIIToUTF16("java"), 739 base::CompareCase::INSENSITIVE_ASCII));
851 ASCIIToUTF16("javascript"), true)); 740 EXPECT_FALSE(StartsWith(ASCIIToUTF16("java"), ASCIIToUTF16("javascript"),
852 EXPECT_FALSE(StartsWith(ASCIIToUTF16("java"), 741 base::CompareCase::SENSITIVE));
853 ASCIIToUTF16("javascript"), false)); 742 EXPECT_FALSE(StartsWith(ASCIIToUTF16("java"), ASCIIToUTF16("javascript"),
854 EXPECT_FALSE(StartsWith(string16(), ASCIIToUTF16("javascript"), false)); 743 base::CompareCase::INSENSITIVE_ASCII));
855 EXPECT_FALSE(StartsWith(string16(), ASCIIToUTF16("javascript"), true)); 744 EXPECT_FALSE(StartsWith(string16(), ASCIIToUTF16("javascript"),
856 EXPECT_TRUE(StartsWith(ASCIIToUTF16("java"), string16(), false)); 745 base::CompareCase::INSENSITIVE_ASCII));
857 EXPECT_TRUE(StartsWith(ASCIIToUTF16("java"), string16(), true)); 746 EXPECT_FALSE(StartsWith(string16(), ASCIIToUTF16("javascript"),
747 base::CompareCase::SENSITIVE));
748 EXPECT_TRUE(StartsWith(ASCIIToUTF16("java"), string16(),
749 base::CompareCase::INSENSITIVE_ASCII));
750 EXPECT_TRUE(StartsWith(ASCIIToUTF16("java"), string16(),
751 base::CompareCase::SENSITIVE));
858 } 752 }
859 753
860 TEST(StringUtilTest, EndsWith) { 754 TEST(StringUtilTest, EndsWith) {
861 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), 755 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), ASCIIToUTF16(".plugin"),
862 ASCIIToUTF16(".plugin"), true)); 756 base::CompareCase::SENSITIVE));
863 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.Plugin"), 757 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.Plugin"), ASCIIToUTF16(".plugin"),
864 ASCIIToUTF16(".plugin"), true)); 758 base::CompareCase::SENSITIVE));
865 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), 759 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), ASCIIToUTF16(".plugin"),
866 ASCIIToUTF16(".plugin"), false)); 760 base::CompareCase::INSENSITIVE_ASCII));
867 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.Plugin"), 761 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.Plugin"), ASCIIToUTF16(".plugin"),
868 ASCIIToUTF16(".plugin"), false)); 762 base::CompareCase::INSENSITIVE_ASCII));
869 EXPECT_FALSE(EndsWith(ASCIIToUTF16(".plug"), ASCIIToUTF16(".plugin"), true)); 763 EXPECT_FALSE(EndsWith(ASCIIToUTF16(".plug"), ASCIIToUTF16(".plugin"),
870 EXPECT_FALSE(EndsWith(ASCIIToUTF16(".plug"), ASCIIToUTF16(".plugin"), false)); 764 base::CompareCase::SENSITIVE));
871 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.plugin Bar"), 765 EXPECT_FALSE(EndsWith(ASCIIToUTF16(".plug"), ASCIIToUTF16(".plugin"),
872 ASCIIToUTF16(".plugin"), true)); 766 base::CompareCase::INSENSITIVE_ASCII));
873 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.plugin Bar"), 767 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.plugin Bar"), ASCIIToUTF16(".plugin"),
874 ASCIIToUTF16(".plugin"), false)); 768 base::CompareCase::SENSITIVE));
875 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"), false)); 769 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.plugin Bar"), ASCIIToUTF16(".plugin"),
876 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"), true)); 770 base::CompareCase::INSENSITIVE_ASCII));
877 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), false)); 771 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"),
878 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), true)); 772 base::CompareCase::INSENSITIVE_ASCII));
879 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), 773 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"),
880 ASCIIToUTF16(".plugin"), false)); 774 base::CompareCase::SENSITIVE));
881 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), ASCIIToUTF16(".plugin"), true)); 775 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(),
882 EXPECT_TRUE(EndsWith(string16(), string16(), false)); 776 base::CompareCase::INSENSITIVE_ASCII));
883 EXPECT_TRUE(EndsWith(string16(), string16(), true)); 777 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(),
778 base::CompareCase::SENSITIVE));
779 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), ASCIIToUTF16(".plugin"),
780 base::CompareCase::INSENSITIVE_ASCII));
781 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), ASCIIToUTF16(".plugin"),
782 base::CompareCase::SENSITIVE));
783 EXPECT_TRUE(
784 EndsWith(string16(), string16(), base::CompareCase::INSENSITIVE_ASCII));
785 EXPECT_TRUE(EndsWith(string16(), string16(), base::CompareCase::SENSITIVE));
884 } 786 }
885 787
886 TEST(StringUtilTest, GetStringFWithOffsets) { 788 TEST(StringUtilTest, GetStringFWithOffsets) {
887 std::vector<string16> subst; 789 std::vector<string16> subst;
888 subst.push_back(ASCIIToUTF16("1")); 790 subst.push_back(ASCIIToUTF16("1"));
889 subst.push_back(ASCIIToUTF16("2")); 791 subst.push_back(ASCIIToUTF16("2"));
890 std::vector<size_t> offsets; 792 std::vector<size_t> offsets;
891 793
892 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $1. Your number is $2."), 794 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $1. Your number is $2."),
893 subst, 795 subst,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
987 889
988 TEST(StringUtilTest, ReplaceStringPlaceholdersConsecutiveDollarSigns) { 890 TEST(StringUtilTest, ReplaceStringPlaceholdersConsecutiveDollarSigns) {
989 std::vector<std::string> subst; 891 std::vector<std::string> subst;
990 subst.push_back("a"); 892 subst.push_back("a");
991 subst.push_back("b"); 893 subst.push_back("b");
992 subst.push_back("c"); 894 subst.push_back("c");
993 EXPECT_EQ(ReplaceStringPlaceholders("$$1 $$$2 $$$$3", subst, NULL), 895 EXPECT_EQ(ReplaceStringPlaceholders("$$1 $$$2 $$$$3", subst, NULL),
994 "$1 $$2 $$$3"); 896 "$1 $$2 $$$3");
995 } 897 }
996 898
997 TEST(StringUtilTest, MatchPatternTest) {
998 EXPECT_TRUE(MatchPattern("www.google.com", "*.com"));
999 EXPECT_TRUE(MatchPattern("www.google.com", "*"));
1000 EXPECT_FALSE(MatchPattern("www.google.com", "www*.g*.org"));
1001 EXPECT_TRUE(MatchPattern("Hello", "H?l?o"));
1002 EXPECT_FALSE(MatchPattern("www.google.com", "http://*)"));
1003 EXPECT_FALSE(MatchPattern("www.msn.com", "*.COM"));
1004 EXPECT_TRUE(MatchPattern("Hello*1234", "He??o\\*1*"));
1005 EXPECT_FALSE(MatchPattern("", "*.*"));
1006 EXPECT_TRUE(MatchPattern("", "*"));
1007 EXPECT_TRUE(MatchPattern("", "?"));
1008 EXPECT_TRUE(MatchPattern("", ""));
1009 EXPECT_FALSE(MatchPattern("Hello", ""));
1010 EXPECT_TRUE(MatchPattern("Hello*", "Hello*"));
1011 // Stop after a certain recursion depth.
1012 EXPECT_FALSE(MatchPattern("123456789012345678", "?????????????????*"));
1013
1014 // Test UTF8 matching.
1015 EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0", "*\xe2\x99\xa0"));
1016 EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0.", "heart: ?."));
1017 EXPECT_TRUE(MatchPattern("hearts: \xe2\x99\xa0\xe2\x99\xa0", "*"));
1018 // Invalid sequences should be handled as a single invalid character.
1019 EXPECT_TRUE(MatchPattern("invalid: \xef\xbf\xbe", "invalid: ?"));
1020 // If the pattern has invalid characters, it shouldn't match anything.
1021 EXPECT_FALSE(MatchPattern("\xf4\x90\x80\x80", "\xf4\x90\x80\x80"));
1022
1023 // Test UTF16 character matching.
1024 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("www.google.com"),
1025 UTF8ToUTF16("*.com")));
1026 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello*1234"),
1027 UTF8ToUTF16("He??o\\*1*")));
1028
1029 // This test verifies that consecutive wild cards are collapsed into 1
1030 // wildcard (when this doesn't occur, MatchPattern reaches it's maximum
1031 // recursion depth).
1032 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello"),
1033 UTF8ToUTF16("He********************************o")));
1034 }
1035
1036 TEST(StringUtilTest, LcpyTest) { 899 TEST(StringUtilTest, LcpyTest) {
1037 // Test the normal case where we fit in our buffer. 900 // Test the normal case where we fit in our buffer.
1038 { 901 {
1039 char dst[10]; 902 char dst[10];
1040 wchar_t wdst[10]; 903 wchar_t wdst[10];
1041 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst))); 904 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst)));
1042 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); 905 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8));
1043 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst))); 906 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst)));
1044 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); 907 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8));
1045 } 908 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 1053
1191 EXPECT_TRUE(ContainsOnlyChars(string16(), kWhitespaceUTF16)); 1054 EXPECT_TRUE(ContainsOnlyChars(string16(), kWhitespaceUTF16));
1192 EXPECT_TRUE(ContainsOnlyChars(ASCIIToUTF16(" "), kWhitespaceUTF16)); 1055 EXPECT_TRUE(ContainsOnlyChars(ASCIIToUTF16(" "), kWhitespaceUTF16));
1193 EXPECT_TRUE(ContainsOnlyChars(ASCIIToUTF16("\t"), kWhitespaceUTF16)); 1056 EXPECT_TRUE(ContainsOnlyChars(ASCIIToUTF16("\t"), kWhitespaceUTF16));
1194 EXPECT_TRUE(ContainsOnlyChars(ASCIIToUTF16("\t \r \n "), kWhitespaceUTF16)); 1057 EXPECT_TRUE(ContainsOnlyChars(ASCIIToUTF16("\t \r \n "), kWhitespaceUTF16));
1195 EXPECT_FALSE(ContainsOnlyChars(ASCIIToUTF16("a"), kWhitespaceUTF16)); 1058 EXPECT_FALSE(ContainsOnlyChars(ASCIIToUTF16("a"), kWhitespaceUTF16));
1196 EXPECT_FALSE(ContainsOnlyChars(ASCIIToUTF16("\thello\r \n "), 1059 EXPECT_FALSE(ContainsOnlyChars(ASCIIToUTF16("\thello\r \n "),
1197 kWhitespaceUTF16)); 1060 kWhitespaceUTF16));
1198 } 1061 }
1199 1062
1063 TEST(StringUtilTest, CompareCaseInsensitiveASCII) {
1064 EXPECT_EQ(0, CompareCaseInsensitiveASCII("", ""));
1065 EXPECT_EQ(0, CompareCaseInsensitiveASCII("Asdf", "aSDf"));
1066
1067 // Differing lengths.
1068 EXPECT_EQ(-1, CompareCaseInsensitiveASCII("Asdf", "aSDfA"));
1069 EXPECT_EQ(1, CompareCaseInsensitiveASCII("AsdfA", "aSDf"));
1070
1071 // Differing values.
1072 EXPECT_EQ(-1, CompareCaseInsensitiveASCII("AsdfA", "aSDfb"));
1073 EXPECT_EQ(1, CompareCaseInsensitiveASCII("Asdfb", "aSDfA"));
1074 }
1075
1076 TEST(StringUtilTest, EqualsCaseInsensitiveASCII) {
1077 EXPECT_TRUE(EqualsCaseInsensitiveASCII("", ""));
1078 EXPECT_TRUE(EqualsCaseInsensitiveASCII("Asdf", "aSDF"));
1079 EXPECT_FALSE(EqualsCaseInsensitiveASCII("bsdf", "aSDF"));
1080 EXPECT_FALSE(EqualsCaseInsensitiveASCII("Asdf", "aSDFz"));
1081 }
1082
1200 class WriteIntoTest : public testing::Test { 1083 class WriteIntoTest : public testing::Test {
1201 protected: 1084 protected:
1202 static void WritesCorrectly(size_t num_chars) { 1085 static void WritesCorrectly(size_t num_chars) {
1203 std::string buffer; 1086 std::string buffer;
1204 char kOriginal[] = "supercali"; 1087 char kOriginal[] = "supercali";
1205 strncpy(WriteInto(&buffer, num_chars + 1), kOriginal, num_chars); 1088 strncpy(WriteInto(&buffer, num_chars + 1), kOriginal, num_chars);
1206 // Using std::string(buffer.c_str()) instead of |buffer| truncates the 1089 // Using std::string(buffer.c_str()) instead of |buffer| truncates the
1207 // string at the first \0. 1090 // string at the first \0.
1208 EXPECT_EQ(std::string(kOriginal, 1091 EXPECT_EQ(std::string(kOriginal,
1209 std::min(num_chars, arraysize(kOriginal) - 1)), 1092 std::min(num_chars, arraysize(kOriginal) - 1)),
(...skipping 16 matching lines...) Expand all
1226 const std::string live = kLive; 1109 const std::string live = kLive;
1227 std::string dead = live; 1110 std::string dead = live;
1228 strncpy(WriteInto(&dead, 5), kDead, 4); 1111 strncpy(WriteInto(&dead, 5), kDead, 4);
1229 EXPECT_EQ(kDead, dead); 1112 EXPECT_EQ(kDead, dead);
1230 EXPECT_EQ(4u, dead.size()); 1113 EXPECT_EQ(4u, dead.size());
1231 EXPECT_EQ(kLive, live); 1114 EXPECT_EQ(kLive, live);
1232 EXPECT_EQ(4u, live.size()); 1115 EXPECT_EQ(4u, live.size());
1233 } 1116 }
1234 1117
1235 } // namespace base 1118 } // namespace base
OLDNEW
« no previous file with comments | « base/strings/string_util_posix.h ('k') | base/strings/string_util_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698