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