match_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "absl/strings/match.h"
  15. #include "gtest/gtest.h"
  16. namespace {
  17. TEST(MatchTest, StartsWith) {
  18. const std::string s1("123\0abc", 7);
  19. const absl::string_view a("foobar");
  20. const absl::string_view b(s1);
  21. const absl::string_view e;
  22. EXPECT_TRUE(absl::StartsWith(a, a));
  23. EXPECT_TRUE(absl::StartsWith(a, "foo"));
  24. EXPECT_TRUE(absl::StartsWith(a, e));
  25. EXPECT_TRUE(absl::StartsWith(b, s1));
  26. EXPECT_TRUE(absl::StartsWith(b, b));
  27. EXPECT_TRUE(absl::StartsWith(b, e));
  28. EXPECT_TRUE(absl::StartsWith(e, ""));
  29. EXPECT_FALSE(absl::StartsWith(a, b));
  30. EXPECT_FALSE(absl::StartsWith(b, a));
  31. EXPECT_FALSE(absl::StartsWith(e, a));
  32. }
  33. TEST(MatchTest, EndsWith) {
  34. const std::string s1("123\0abc", 7);
  35. const absl::string_view a("foobar");
  36. const absl::string_view b(s1);
  37. const absl::string_view e;
  38. EXPECT_TRUE(absl::EndsWith(a, a));
  39. EXPECT_TRUE(absl::EndsWith(a, "bar"));
  40. EXPECT_TRUE(absl::EndsWith(a, e));
  41. EXPECT_TRUE(absl::EndsWith(b, s1));
  42. EXPECT_TRUE(absl::EndsWith(b, b));
  43. EXPECT_TRUE(absl::EndsWith(b, e));
  44. EXPECT_TRUE(absl::EndsWith(e, ""));
  45. EXPECT_FALSE(absl::EndsWith(a, b));
  46. EXPECT_FALSE(absl::EndsWith(b, a));
  47. EXPECT_FALSE(absl::EndsWith(e, a));
  48. }
  49. TEST(MatchTest, Contains) {
  50. absl::string_view a("abcdefg");
  51. absl::string_view b("abcd");
  52. absl::string_view c("efg");
  53. absl::string_view d("gh");
  54. EXPECT_TRUE(absl::StrContains(a, a));
  55. EXPECT_TRUE(absl::StrContains(a, b));
  56. EXPECT_TRUE(absl::StrContains(a, c));
  57. EXPECT_FALSE(absl::StrContains(a, d));
  58. EXPECT_TRUE(absl::StrContains("", ""));
  59. EXPECT_TRUE(absl::StrContains("abc", ""));
  60. EXPECT_FALSE(absl::StrContains("", "a"));
  61. }
  62. TEST(MatchTest, ContainsChar) {
  63. absl::string_view a("abcdefg");
  64. absl::string_view b("abcd");
  65. EXPECT_TRUE(absl::StrContains(a, 'a'));
  66. EXPECT_TRUE(absl::StrContains(a, 'b'));
  67. EXPECT_TRUE(absl::StrContains(a, 'e'));
  68. EXPECT_FALSE(absl::StrContains(a, 'h'));
  69. EXPECT_TRUE(absl::StrContains(b, 'a'));
  70. EXPECT_TRUE(absl::StrContains(b, 'b'));
  71. EXPECT_FALSE(absl::StrContains(b, 'e'));
  72. EXPECT_FALSE(absl::StrContains(b, 'h'));
  73. EXPECT_FALSE(absl::StrContains("", 'a'));
  74. EXPECT_FALSE(absl::StrContains("", 'a'));
  75. }
  76. TEST(MatchTest, ContainsNull) {
  77. const std::string s = "foo";
  78. const char* cs = "foo";
  79. const absl::string_view sv("foo");
  80. const absl::string_view sv2("foo\0bar", 4);
  81. EXPECT_EQ(s, "foo");
  82. EXPECT_EQ(sv, "foo");
  83. EXPECT_NE(sv2, "foo");
  84. EXPECT_TRUE(absl::EndsWith(s, sv));
  85. EXPECT_TRUE(absl::StartsWith(cs, sv));
  86. EXPECT_TRUE(absl::StrContains(cs, sv));
  87. EXPECT_FALSE(absl::StrContains(cs, sv2));
  88. }
  89. TEST(MatchTest, EqualsIgnoreCase) {
  90. std::string text = "the";
  91. absl::string_view data(text);
  92. EXPECT_TRUE(absl::EqualsIgnoreCase(data, "The"));
  93. EXPECT_TRUE(absl::EqualsIgnoreCase(data, "THE"));
  94. EXPECT_TRUE(absl::EqualsIgnoreCase(data, "the"));
  95. EXPECT_FALSE(absl::EqualsIgnoreCase(data, "Quick"));
  96. EXPECT_FALSE(absl::EqualsIgnoreCase(data, "then"));
  97. }
  98. TEST(MatchTest, StartsWithIgnoreCase) {
  99. EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "foo"));
  100. EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "Fo"));
  101. EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", ""));
  102. EXPECT_FALSE(absl::StartsWithIgnoreCase("foo", "fooo"));
  103. EXPECT_FALSE(absl::StartsWithIgnoreCase("", "fo"));
  104. }
  105. TEST(MatchTest, EndsWithIgnoreCase) {
  106. EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "foo"));
  107. EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "Oo"));
  108. EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", ""));
  109. EXPECT_FALSE(absl::EndsWithIgnoreCase("foo", "fooo"));
  110. EXPECT_FALSE(absl::EndsWithIgnoreCase("", "fo"));
  111. }
  112. } // namespace