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

Side by Side Diff: base/string_util_unittest.cc

Issue 6877008: Add a unittest case of ReplaceStringPlaceholders() (Closed)
Patch Set: Prevent overrun of string parsing Created 9 years, 6 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/string_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <math.h> 5 #include <math.h>
6 #include <stdarg.h> 6 #include <stdarg.h>
7 7
8 #include <limits> 8 #include <limits>
9 #include <sstream> 9 #include <sstream>
10 10
(...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 857
858 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $2. Your number is $1."), 858 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $2. Your number is $1."),
859 subst, 859 subst,
860 &offsets); 860 &offsets);
861 EXPECT_EQ(2U, offsets.size()); 861 EXPECT_EQ(2U, offsets.size());
862 EXPECT_EQ(25U, offsets[0]); 862 EXPECT_EQ(25U, offsets[0]);
863 EXPECT_EQ(7U, offsets[1]); 863 EXPECT_EQ(7U, offsets[1]);
864 offsets.clear(); 864 offsets.clear();
865 } 865 }
866 866
867 TEST(StringUtilTest, ReplaceStringPlaceholdersTooFew) {
868 // Test whether replacestringplaceholders works as expected when there
869 // are fewer inputs than outputs.
870 std::vector<string16> subst;
871 subst.push_back(ASCIIToUTF16("9a"));
872 subst.push_back(ASCIIToUTF16("8b"));
873 subst.push_back(ASCIIToUTF16("7c"));
874
875 string16 formatted =
876 ReplaceStringPlaceholders(
877 ASCIIToUTF16("$1a,$2b,$3c,$4d,$5e,$6f,$1g,$2h,$3i"), subst, NULL);
878
879 EXPECT_EQ(formatted, ASCIIToUTF16("9aa,8bb,7cc,d,e,f,9ag,8bh,7ci"));
880 }
881
867 TEST(StringUtilTest, ReplaceStringPlaceholders) { 882 TEST(StringUtilTest, ReplaceStringPlaceholders) {
868 std::vector<string16> subst; 883 std::vector<string16> subst;
869 subst.push_back(ASCIIToUTF16("9a")); 884 subst.push_back(ASCIIToUTF16("9a"));
870 subst.push_back(ASCIIToUTF16("8b")); 885 subst.push_back(ASCIIToUTF16("8b"));
871 subst.push_back(ASCIIToUTF16("7c")); 886 subst.push_back(ASCIIToUTF16("7c"));
872 subst.push_back(ASCIIToUTF16("6d")); 887 subst.push_back(ASCIIToUTF16("6d"));
873 subst.push_back(ASCIIToUTF16("5e")); 888 subst.push_back(ASCIIToUTF16("5e"));
874 subst.push_back(ASCIIToUTF16("4f")); 889 subst.push_back(ASCIIToUTF16("4f"));
875 subst.push_back(ASCIIToUTF16("3g")); 890 subst.push_back(ASCIIToUTF16("3g"));
876 subst.push_back(ASCIIToUTF16("2h")); 891 subst.push_back(ASCIIToUTF16("2h"));
877 subst.push_back(ASCIIToUTF16("1i")); 892 subst.push_back(ASCIIToUTF16("1i"));
878 893
879 string16 formatted = 894 string16 formatted =
880 ReplaceStringPlaceholders( 895 ReplaceStringPlaceholders(
881 ASCIIToUTF16("$1a,$2b,$3c,$4d,$5e,$6f,$7g,$8h,$9i"), subst, NULL); 896 ASCIIToUTF16("$1a,$2b,$3c,$4d,$5e,$6f,$7g,$8h,$9i"), subst, NULL);
882 897
883 EXPECT_EQ(formatted, ASCIIToUTF16("9aa,8bb,7cc,6dd,5ee,4ff,3gg,2hh,1ii")); 898 EXPECT_EQ(formatted, ASCIIToUTF16("9aa,8bb,7cc,6dd,5ee,4ff,3gg,2hh,1ii"));
884 } 899 }
885 900
886 TEST(StringUtilTest, ReplaceStringPlaceholdersTooFew) { 901 TEST(StringUtilTest, ReplaceStringPlaceholdersMoreThan9Replacements) {
887 // Test whether replacestringplaceholders works as expected when there
888 // are fewer inputs than outputs.
889 std::vector<string16> subst; 902 std::vector<string16> subst;
890 subst.push_back(ASCIIToUTF16("9a")); 903 subst.push_back(ASCIIToUTF16("9a"));
891 subst.push_back(ASCIIToUTF16("8b")); 904 subst.push_back(ASCIIToUTF16("8b"));
892 subst.push_back(ASCIIToUTF16("7c")); 905 subst.push_back(ASCIIToUTF16("7c"));
906 subst.push_back(ASCIIToUTF16("6d"));
907 subst.push_back(ASCIIToUTF16("5e"));
908 subst.push_back(ASCIIToUTF16("4f"));
909 subst.push_back(ASCIIToUTF16("3g"));
910 subst.push_back(ASCIIToUTF16("2h"));
911 subst.push_back(ASCIIToUTF16("1i"));
912 subst.push_back(ASCIIToUTF16("0j"));
913 subst.push_back(ASCIIToUTF16("-1k"));
914 subst.push_back(ASCIIToUTF16("-2l"));
915 subst.push_back(ASCIIToUTF16("-3m"));
916 subst.push_back(ASCIIToUTF16("-4n"));
893 917
894 string16 formatted = 918 string16 formatted =
895 ReplaceStringPlaceholders( 919 ReplaceStringPlaceholders(
896 ASCIIToUTF16("$1a,$2b,$3c,$4d,$5e,$6f,$1g,$2h,$3i"), subst, NULL); 920 ASCIIToUTF16("$1a,$2b,$3c,$4d,$5e,$6f,$7g,$8h,$9i,"
921 "$10j,$11k,$12l,$13m,$14n,$1"), subst, NULL);
897 922
898 EXPECT_EQ(formatted, ASCIIToUTF16("9aa,8bb,7cc,d,e,f,9ag,8bh,7ci")); 923 EXPECT_EQ(formatted, ASCIIToUTF16("9aa,8bb,7cc,6dd,5ee,4ff,3gg,2hh,"
924 "1ii,0jj,-1kk,-2ll,-3mm,-4nn,9a"));
899 } 925 }
900 926
901 TEST(StringUtilTest, StdStringReplaceStringPlaceholders) { 927 TEST(StringUtilTest, StdStringReplaceStringPlaceholders) {
902 std::vector<std::string> subst; 928 std::vector<std::string> subst;
903 subst.push_back("9a"); 929 subst.push_back("9a");
904 subst.push_back("8b"); 930 subst.push_back("8b");
905 subst.push_back("7c"); 931 subst.push_back("7c");
906 subst.push_back("6d"); 932 subst.push_back("6d");
907 subst.push_back("5e"); 933 subst.push_back("5e");
908 subst.push_back("4f"); 934 subst.push_back("4f");
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 EXPECT_FALSE(ContainsOnlyChars("Hello", "")); 1106 EXPECT_FALSE(ContainsOnlyChars("Hello", ""));
1081 1107
1082 EXPECT_TRUE(ContainsOnlyChars("", "1234")); 1108 EXPECT_TRUE(ContainsOnlyChars("", "1234"));
1083 EXPECT_TRUE(ContainsOnlyChars("1", "1234")); 1109 EXPECT_TRUE(ContainsOnlyChars("1", "1234"));
1084 EXPECT_TRUE(ContainsOnlyChars("1", "4321")); 1110 EXPECT_TRUE(ContainsOnlyChars("1", "4321"));
1085 EXPECT_TRUE(ContainsOnlyChars("123", "4321")); 1111 EXPECT_TRUE(ContainsOnlyChars("123", "4321"));
1086 EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); 1112 EXPECT_FALSE(ContainsOnlyChars("123a", "4321"));
1087 } 1113 }
1088 1114
1089 } // namespace base 1115 } // namespace base
OLDNEW
« no previous file with comments | « base/string_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698