• 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,ContainsNull)69 TEST(MatchTest, ContainsNull) {
70   const std::string s = "foo";
71   const char* cs = "foo";
72   const absl::string_view sv("foo");
73   const absl::string_view sv2("foo\0bar", 4);
74   EXPECT_EQ(s, "foo");
75   EXPECT_EQ(sv, "foo");
76   EXPECT_NE(sv2, "foo");
77   EXPECT_TRUE(absl::EndsWith(s, sv));
78   EXPECT_TRUE(absl::StartsWith(cs, sv));
79   EXPECT_TRUE(absl::StrContains(cs, sv));
80   EXPECT_FALSE(absl::StrContains(cs, sv2));
81 }
82 
TEST(MatchTest,EqualsIgnoreCase)83 TEST(MatchTest, EqualsIgnoreCase) {
84   std::string text = "the";
85   absl::string_view data(text);
86 
87   EXPECT_TRUE(absl::EqualsIgnoreCase(data, "The"));
88   EXPECT_TRUE(absl::EqualsIgnoreCase(data, "THE"));
89   EXPECT_TRUE(absl::EqualsIgnoreCase(data, "the"));
90   EXPECT_FALSE(absl::EqualsIgnoreCase(data, "Quick"));
91   EXPECT_FALSE(absl::EqualsIgnoreCase(data, "then"));
92 }
93 
TEST(MatchTest,StartsWithIgnoreCase)94 TEST(MatchTest, StartsWithIgnoreCase) {
95   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "foo"));
96   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "Fo"));
97   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", ""));
98   EXPECT_FALSE(absl::StartsWithIgnoreCase("foo", "fooo"));
99   EXPECT_FALSE(absl::StartsWithIgnoreCase("", "fo"));
100 }
101 
TEST(MatchTest,EndsWithIgnoreCase)102 TEST(MatchTest, EndsWithIgnoreCase) {
103   EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "foo"));
104   EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "Oo"));
105   EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", ""));
106   EXPECT_FALSE(absl::EndsWithIgnoreCase("foo", "fooo"));
107   EXPECT_FALSE(absl::EndsWithIgnoreCase("", "fo"));
108 }
109 
110 }  // namespace
111