match_test.cc (11207B)
1 // Copyright 2017 The Abseil Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "absl/strings/match.h" 16 17 #include <string> 18 19 #include "gtest/gtest.h" 20 #include "absl/strings/string_view.h" 21 22 namespace { 23 24 TEST(MatchTest, StartsWith) { 25 const std::string s1("123\0abc", 7); 26 const absl::string_view a("foobar"); 27 const absl::string_view b(s1); 28 const absl::string_view e; 29 EXPECT_TRUE(absl::StartsWith(a, a)); 30 EXPECT_TRUE(absl::StartsWith(a, "foo")); 31 EXPECT_TRUE(absl::StartsWith(a, e)); 32 EXPECT_TRUE(absl::StartsWith(b, s1)); 33 EXPECT_TRUE(absl::StartsWith(b, b)); 34 EXPECT_TRUE(absl::StartsWith(b, e)); 35 EXPECT_TRUE(absl::StartsWith(e, "")); 36 EXPECT_FALSE(absl::StartsWith(a, b)); 37 EXPECT_FALSE(absl::StartsWith(b, a)); 38 EXPECT_FALSE(absl::StartsWith(e, a)); 39 } 40 41 TEST(MatchTest, EndsWith) { 42 const std::string s1("123\0abc", 7); 43 const absl::string_view a("foobar"); 44 const absl::string_view b(s1); 45 const absl::string_view e; 46 EXPECT_TRUE(absl::EndsWith(a, a)); 47 EXPECT_TRUE(absl::EndsWith(a, "bar")); 48 EXPECT_TRUE(absl::EndsWith(a, e)); 49 EXPECT_TRUE(absl::EndsWith(b, s1)); 50 EXPECT_TRUE(absl::EndsWith(b, b)); 51 EXPECT_TRUE(absl::EndsWith(b, e)); 52 EXPECT_TRUE(absl::EndsWith(e, "")); 53 EXPECT_FALSE(absl::EndsWith(a, b)); 54 EXPECT_FALSE(absl::EndsWith(b, a)); 55 EXPECT_FALSE(absl::EndsWith(e, a)); 56 } 57 58 TEST(MatchTest, Contains) { 59 absl::string_view a("abcdefg"); 60 absl::string_view b("abcd"); 61 absl::string_view c("efg"); 62 absl::string_view d("gh"); 63 EXPECT_TRUE(absl::StrContains(a, a)); 64 EXPECT_TRUE(absl::StrContains(a, b)); 65 EXPECT_TRUE(absl::StrContains(a, c)); 66 EXPECT_FALSE(absl::StrContains(a, d)); 67 EXPECT_TRUE(absl::StrContains("", "")); 68 EXPECT_TRUE(absl::StrContains("abc", "")); 69 EXPECT_FALSE(absl::StrContains("", "a")); 70 } 71 72 TEST(MatchTest, ContainsChar) { 73 absl::string_view a("abcdefg"); 74 absl::string_view b("abcd"); 75 EXPECT_TRUE(absl::StrContains(a, 'a')); 76 EXPECT_TRUE(absl::StrContains(a, 'b')); 77 EXPECT_TRUE(absl::StrContains(a, 'e')); 78 EXPECT_FALSE(absl::StrContains(a, 'h')); 79 80 EXPECT_TRUE(absl::StrContains(b, 'a')); 81 EXPECT_TRUE(absl::StrContains(b, 'b')); 82 EXPECT_FALSE(absl::StrContains(b, 'e')); 83 EXPECT_FALSE(absl::StrContains(b, 'h')); 84 85 EXPECT_FALSE(absl::StrContains("", 'a')); 86 EXPECT_FALSE(absl::StrContains("", 'a')); 87 } 88 89 TEST(MatchTest, ContainsNull) { 90 const std::string s = "foo"; 91 const char* cs = "foo"; 92 const absl::string_view sv("foo"); 93 const absl::string_view sv2("foo\0bar", 4); 94 EXPECT_EQ(s, "foo"); 95 EXPECT_EQ(sv, "foo"); 96 EXPECT_NE(sv2, "foo"); 97 EXPECT_TRUE(absl::EndsWith(s, sv)); 98 EXPECT_TRUE(absl::StartsWith(cs, sv)); 99 EXPECT_TRUE(absl::StrContains(cs, sv)); 100 EXPECT_FALSE(absl::StrContains(cs, sv2)); 101 } 102 103 TEST(MatchTest, EqualsIgnoreCase) { 104 std::string text = "the"; 105 absl::string_view data(text); 106 107 EXPECT_TRUE(absl::EqualsIgnoreCase(data, "The")); 108 EXPECT_TRUE(absl::EqualsIgnoreCase(data, "THE")); 109 EXPECT_TRUE(absl::EqualsIgnoreCase(data, "the")); 110 EXPECT_FALSE(absl::EqualsIgnoreCase(data, "Quick")); 111 EXPECT_FALSE(absl::EqualsIgnoreCase(data, "then")); 112 } 113 114 TEST(MatchTest, StartsWithIgnoreCase) { 115 EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "foo")); 116 EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "Fo")); 117 EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "")); 118 EXPECT_FALSE(absl::StartsWithIgnoreCase("foo", "fooo")); 119 EXPECT_FALSE(absl::StartsWithIgnoreCase("", "fo")); 120 } 121 122 TEST(MatchTest, EndsWithIgnoreCase) { 123 EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "foo")); 124 EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "Oo")); 125 EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "")); 126 EXPECT_FALSE(absl::EndsWithIgnoreCase("foo", "fooo")); 127 EXPECT_FALSE(absl::EndsWithIgnoreCase("", "fo")); 128 } 129 130 TEST(MatchTest, ContainsIgnoreCase) { 131 EXPECT_TRUE(absl::StrContainsIgnoreCase("foo", "foo")); 132 EXPECT_TRUE(absl::StrContainsIgnoreCase("FOO", "Foo")); 133 EXPECT_TRUE(absl::StrContainsIgnoreCase("--FOO", "Foo")); 134 EXPECT_TRUE(absl::StrContainsIgnoreCase("FOO--", "Foo")); 135 EXPECT_FALSE(absl::StrContainsIgnoreCase("BAR", "Foo")); 136 EXPECT_FALSE(absl::StrContainsIgnoreCase("BAR", "Foo")); 137 EXPECT_TRUE(absl::StrContainsIgnoreCase("123456", "123456")); 138 EXPECT_TRUE(absl::StrContainsIgnoreCase("123456", "234")); 139 EXPECT_TRUE(absl::StrContainsIgnoreCase("", "")); 140 EXPECT_TRUE(absl::StrContainsIgnoreCase("abc", "")); 141 EXPECT_FALSE(absl::StrContainsIgnoreCase("", "a")); 142 } 143 144 TEST(MatchTest, ContainsCharIgnoreCase) { 145 absl::string_view a("AaBCdefg!"); 146 absl::string_view b("AaBCd!"); 147 EXPECT_TRUE(absl::StrContainsIgnoreCase(a, 'a')); 148 EXPECT_TRUE(absl::StrContainsIgnoreCase(a, 'A')); 149 EXPECT_TRUE(absl::StrContainsIgnoreCase(a, 'b')); 150 EXPECT_TRUE(absl::StrContainsIgnoreCase(a, 'B')); 151 EXPECT_TRUE(absl::StrContainsIgnoreCase(a, 'e')); 152 EXPECT_TRUE(absl::StrContainsIgnoreCase(a, 'E')); 153 EXPECT_FALSE(absl::StrContainsIgnoreCase(a, 'h')); 154 EXPECT_FALSE(absl::StrContainsIgnoreCase(a, 'H')); 155 EXPECT_TRUE(absl::StrContainsIgnoreCase(a, '!')); 156 EXPECT_FALSE(absl::StrContainsIgnoreCase(a, '?')); 157 158 EXPECT_TRUE(absl::StrContainsIgnoreCase(b, 'a')); 159 EXPECT_TRUE(absl::StrContainsIgnoreCase(b, 'A')); 160 EXPECT_TRUE(absl::StrContainsIgnoreCase(b, 'b')); 161 EXPECT_TRUE(absl::StrContainsIgnoreCase(b, 'B')); 162 EXPECT_FALSE(absl::StrContainsIgnoreCase(b, 'e')); 163 EXPECT_FALSE(absl::StrContainsIgnoreCase(b, 'E')); 164 EXPECT_FALSE(absl::StrContainsIgnoreCase(b, 'h')); 165 EXPECT_FALSE(absl::StrContainsIgnoreCase(b, 'H')); 166 EXPECT_TRUE(absl::StrContainsIgnoreCase(b, '!')); 167 EXPECT_FALSE(absl::StrContainsIgnoreCase(b, '?')); 168 169 EXPECT_FALSE(absl::StrContainsIgnoreCase("", 'a')); 170 EXPECT_FALSE(absl::StrContainsIgnoreCase("", 'A')); 171 EXPECT_FALSE(absl::StrContainsIgnoreCase("", '0')); 172 } 173 174 TEST(MatchTest, FindLongestCommonPrefix) { 175 EXPECT_EQ(absl::FindLongestCommonPrefix("", ""), ""); 176 EXPECT_EQ(absl::FindLongestCommonPrefix("", "abc"), ""); 177 EXPECT_EQ(absl::FindLongestCommonPrefix("abc", ""), ""); 178 EXPECT_EQ(absl::FindLongestCommonPrefix("ab", "abc"), "ab"); 179 EXPECT_EQ(absl::FindLongestCommonPrefix("abc", "ab"), "ab"); 180 EXPECT_EQ(absl::FindLongestCommonPrefix("abc", "abd"), "ab"); 181 EXPECT_EQ(absl::FindLongestCommonPrefix("abc", "abcd"), "abc"); 182 EXPECT_EQ(absl::FindLongestCommonPrefix("abcd", "abcd"), "abcd"); 183 EXPECT_EQ(absl::FindLongestCommonPrefix("abcd", "efgh"), ""); 184 185 // "abcde" v. "abc" but in the middle of other data 186 EXPECT_EQ(absl::FindLongestCommonPrefix( 187 absl::string_view("1234 abcdef").substr(5, 5), 188 absl::string_view("5678 abcdef").substr(5, 3)), 189 "abc"); 190 } 191 192 // Since the little-endian implementation involves a bit of if-else and various 193 // return paths, the following tests aims to provide full test coverage of the 194 // implementation. 195 TEST(MatchTest, FindLongestCommonPrefixLoad16Mismatch) { 196 const std::string x1 = "abcdefgh"; 197 const std::string x2 = "abcde_"; 198 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "abcde"); 199 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "abcde"); 200 } 201 202 TEST(MatchTest, FindLongestCommonPrefixLoad16MatchesNoLast) { 203 const std::string x1 = "abcdef"; 204 const std::string x2 = "abcdef"; 205 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "abcdef"); 206 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "abcdef"); 207 } 208 209 TEST(MatchTest, FindLongestCommonPrefixLoad16MatchesLastCharMismatches) { 210 const std::string x1 = "abcdefg"; 211 const std::string x2 = "abcdef_h"; 212 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "abcdef"); 213 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "abcdef"); 214 } 215 216 TEST(MatchTest, FindLongestCommonPrefixLoad16MatchesLastMatches) { 217 const std::string x1 = "abcde"; 218 const std::string x2 = "abcdefgh"; 219 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "abcde"); 220 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "abcde"); 221 } 222 223 TEST(MatchTest, FindLongestCommonPrefixSize8Load64Mismatches) { 224 const std::string x1 = "abcdefghijk"; 225 const std::string x2 = "abcde_g_"; 226 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "abcde"); 227 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "abcde"); 228 } 229 230 TEST(MatchTest, FindLongestCommonPrefixSize8Load64Matches) { 231 const std::string x1 = "abcdefgh"; 232 const std::string x2 = "abcdefgh"; 233 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "abcdefgh"); 234 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "abcdefgh"); 235 } 236 237 TEST(MatchTest, FindLongestCommonPrefixSize15Load64Mismatches) { 238 const std::string x1 = "012345670123456"; 239 const std::string x2 = "0123456701_34_6"; 240 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "0123456701"); 241 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "0123456701"); 242 } 243 244 TEST(MatchTest, FindLongestCommonPrefixSize15Load64Matches) { 245 const std::string x1 = "012345670123456"; 246 const std::string x2 = "0123456701234567"; 247 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "012345670123456"); 248 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "012345670123456"); 249 } 250 251 TEST(MatchTest, FindLongestCommonPrefixSizeFirstByteOfLast8BytesMismatch) { 252 const std::string x1 = "012345670123456701234567"; 253 const std::string x2 = "0123456701234567_1234567"; 254 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), "0123456701234567"); 255 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), "0123456701234567"); 256 } 257 258 TEST(MatchTest, FindLongestCommonPrefixLargeLastCharMismatches) { 259 const std::string x1(300, 'x'); 260 std::string x2 = x1; 261 x2.back() = '#'; 262 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), std::string(299, 'x')); 263 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), std::string(299, 'x')); 264 } 265 266 TEST(MatchTest, FindLongestCommonPrefixLargeFullMatch) { 267 const std::string x1(300, 'x'); 268 const std::string x2 = x1; 269 EXPECT_EQ(absl::FindLongestCommonPrefix(x1, x2), std::string(300, 'x')); 270 EXPECT_EQ(absl::FindLongestCommonPrefix(x2, x1), std::string(300, 'x')); 271 } 272 273 TEST(MatchTest, FindLongestCommonSuffix) { 274 EXPECT_EQ(absl::FindLongestCommonSuffix("", ""), ""); 275 EXPECT_EQ(absl::FindLongestCommonSuffix("", "abc"), ""); 276 EXPECT_EQ(absl::FindLongestCommonSuffix("abc", ""), ""); 277 EXPECT_EQ(absl::FindLongestCommonSuffix("bc", "abc"), "bc"); 278 EXPECT_EQ(absl::FindLongestCommonSuffix("abc", "bc"), "bc"); 279 EXPECT_EQ(absl::FindLongestCommonSuffix("abc", "dbc"), "bc"); 280 EXPECT_EQ(absl::FindLongestCommonSuffix("bcd", "abcd"), "bcd"); 281 EXPECT_EQ(absl::FindLongestCommonSuffix("abcd", "abcd"), "abcd"); 282 EXPECT_EQ(absl::FindLongestCommonSuffix("abcd", "efgh"), ""); 283 284 // "abcde" v. "cde" but in the middle of other data 285 EXPECT_EQ(absl::FindLongestCommonSuffix( 286 absl::string_view("1234 abcdef").substr(5, 5), 287 absl::string_view("5678 abcdef").substr(7, 3)), 288 "cde"); 289 } 290 291 } // namespace