match_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/match.h"
00016 
00017 #include "gtest/gtest.h"
00018 
00019 namespace {
00020 
00021 TEST(MatchTest, StartsWith) {
00022   const std::string s1("123\0abc", 7);
00023   const absl::string_view a("foobar");
00024   const absl::string_view b(s1);
00025   const absl::string_view e;
00026   EXPECT_TRUE(absl::StartsWith(a, a));
00027   EXPECT_TRUE(absl::StartsWith(a, "foo"));
00028   EXPECT_TRUE(absl::StartsWith(a, e));
00029   EXPECT_TRUE(absl::StartsWith(b, s1));
00030   EXPECT_TRUE(absl::StartsWith(b, b));
00031   EXPECT_TRUE(absl::StartsWith(b, e));
00032   EXPECT_TRUE(absl::StartsWith(e, ""));
00033   EXPECT_FALSE(absl::StartsWith(a, b));
00034   EXPECT_FALSE(absl::StartsWith(b, a));
00035   EXPECT_FALSE(absl::StartsWith(e, a));
00036 }
00037 
00038 TEST(MatchTest, EndsWith) {
00039   const std::string s1("123\0abc", 7);
00040   const absl::string_view a("foobar");
00041   const absl::string_view b(s1);
00042   const absl::string_view e;
00043   EXPECT_TRUE(absl::EndsWith(a, a));
00044   EXPECT_TRUE(absl::EndsWith(a, "bar"));
00045   EXPECT_TRUE(absl::EndsWith(a, e));
00046   EXPECT_TRUE(absl::EndsWith(b, s1));
00047   EXPECT_TRUE(absl::EndsWith(b, b));
00048   EXPECT_TRUE(absl::EndsWith(b, e));
00049   EXPECT_TRUE(absl::EndsWith(e, ""));
00050   EXPECT_FALSE(absl::EndsWith(a, b));
00051   EXPECT_FALSE(absl::EndsWith(b, a));
00052   EXPECT_FALSE(absl::EndsWith(e, a));
00053 }
00054 
00055 TEST(MatchTest, Contains) {
00056   absl::string_view a("abcdefg");
00057   absl::string_view b("abcd");
00058   absl::string_view c("efg");
00059   absl::string_view d("gh");
00060   EXPECT_TRUE(absl::StrContains(a, a));
00061   EXPECT_TRUE(absl::StrContains(a, b));
00062   EXPECT_TRUE(absl::StrContains(a, c));
00063   EXPECT_FALSE(absl::StrContains(a, d));
00064   EXPECT_TRUE(absl::StrContains("", ""));
00065   EXPECT_TRUE(absl::StrContains("abc", ""));
00066   EXPECT_FALSE(absl::StrContains("", "a"));
00067 }
00068 
00069 TEST(MatchTest, ContainsNull) {
00070   const std::string s = "foo";
00071   const char* cs = "foo";
00072   const absl::string_view sv("foo");
00073   const absl::string_view sv2("foo\0bar", 4);
00074   EXPECT_EQ(s, "foo");
00075   EXPECT_EQ(sv, "foo");
00076   EXPECT_NE(sv2, "foo");
00077   EXPECT_TRUE(absl::EndsWith(s, sv));
00078   EXPECT_TRUE(absl::StartsWith(cs, sv));
00079   EXPECT_TRUE(absl::StrContains(cs, sv));
00080   EXPECT_FALSE(absl::StrContains(cs, sv2));
00081 }
00082 
00083 TEST(MatchTest, EqualsIgnoreCase) {
00084   std::string text = "the";
00085   absl::string_view data(text);
00086 
00087   EXPECT_TRUE(absl::EqualsIgnoreCase(data, "The"));
00088   EXPECT_TRUE(absl::EqualsIgnoreCase(data, "THE"));
00089   EXPECT_TRUE(absl::EqualsIgnoreCase(data, "the"));
00090   EXPECT_FALSE(absl::EqualsIgnoreCase(data, "Quick"));
00091   EXPECT_FALSE(absl::EqualsIgnoreCase(data, "then"));
00092 }
00093 
00094 TEST(MatchTest, StartsWithIgnoreCase) {
00095   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "foo"));
00096   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "Fo"));
00097   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", ""));
00098   EXPECT_FALSE(absl::StartsWithIgnoreCase("foo", "fooo"));
00099   EXPECT_FALSE(absl::StartsWithIgnoreCase("", "fo"));
00100 }
00101 
00102 TEST(MatchTest, EndsWithIgnoreCase) {
00103   EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "foo"));
00104   EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "Oo"));
00105   EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", ""));
00106   EXPECT_FALSE(absl::EndsWithIgnoreCase("foo", "fooo"));
00107   EXPECT_FALSE(absl::EndsWithIgnoreCase("", "fo"));
00108 }
00109 
00110 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15