algorithm_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/algorithm/algorithm.h"
  15. #include <algorithm>
  16. #include <list>
  17. #include <vector>
  18. #include "gmock/gmock.h"
  19. #include "gtest/gtest.h"
  20. namespace {
  21. TEST(EqualTest, DefaultComparisonRandomAccess) {
  22. std::vector<int> v1{1, 2, 3};
  23. std::vector<int> v2 = v1;
  24. std::vector<int> v3 = {1, 2};
  25. std::vector<int> v4 = {1, 2, 4};
  26. EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
  27. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
  28. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
  29. }
  30. TEST(EqualTest, DefaultComparison) {
  31. std::list<int> lst1{1, 2, 3};
  32. std::list<int> lst2 = lst1;
  33. std::list<int> lst3{1, 2};
  34. std::list<int> lst4{1, 2, 4};
  35. EXPECT_TRUE(absl::equal(lst1.begin(), lst1.end(), lst2.begin(), lst2.end()));
  36. EXPECT_FALSE(absl::equal(lst1.begin(), lst1.end(), lst3.begin(), lst3.end()));
  37. EXPECT_FALSE(absl::equal(lst1.begin(), lst1.end(), lst4.begin(), lst4.end()));
  38. }
  39. TEST(EqualTest, EmptyRange) {
  40. std::vector<int> v1{1, 2, 3};
  41. std::vector<int> empty1;
  42. std::vector<int> empty2;
  43. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), empty1.begin(), empty1.end()));
  44. EXPECT_FALSE(absl::equal(empty1.begin(), empty1.end(), v1.begin(), v1.end()));
  45. EXPECT_TRUE(
  46. absl::equal(empty1.begin(), empty1.end(), empty2.begin(), empty2.end()));
  47. }
  48. TEST(EqualTest, MixedIterTypes) {
  49. std::vector<int> v1{1, 2, 3};
  50. std::list<int> lst1{v1.begin(), v1.end()};
  51. std::list<int> lst2{1, 2, 4};
  52. std::list<int> lst3{1, 2};
  53. EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), lst1.begin(), lst1.end()));
  54. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), lst2.begin(), lst2.end()));
  55. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), lst3.begin(), lst3.end()));
  56. }
  57. TEST(EqualTest, MixedValueTypes) {
  58. std::vector<int> v1{1, 2, 3};
  59. std::vector<char> v2{1, 2, 3};
  60. std::vector<char> v3{1, 2};
  61. std::vector<char> v4{1, 2, 4};
  62. EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
  63. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
  64. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
  65. }
  66. TEST(EqualTest, WeirdIterators) {
  67. std::vector<bool> v1{true, false};
  68. std::vector<bool> v2 = v1;
  69. std::vector<bool> v3{true};
  70. std::vector<bool> v4{true, true, true};
  71. EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
  72. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
  73. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
  74. }
  75. TEST(EqualTest, CustomComparison) {
  76. int n[] = {1, 2, 3, 4};
  77. std::vector<int*> v1{&n[0], &n[1], &n[2]};
  78. std::vector<int*> v2 = v1;
  79. std::vector<int*> v3{&n[0], &n[1], &n[3]};
  80. std::vector<int*> v4{&n[0], &n[1]};
  81. auto eq = [](int* a, int* b) { return *a == *b; };
  82. EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(), eq));
  83. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(), eq));
  84. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end(), eq));
  85. }
  86. TEST(EqualTest, MoveOnlyPredicate) {
  87. std::vector<int> v1{1, 2, 3};
  88. std::vector<int> v2{4, 5, 6};
  89. // move-only equality predicate
  90. struct Eq {
  91. Eq() = default;
  92. Eq(Eq &&) = default;
  93. Eq(const Eq &) = delete;
  94. Eq &operator=(const Eq &) = delete;
  95. bool operator()(const int a, const int b) const { return a == b; }
  96. };
  97. EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v1.begin(), v1.end(), Eq()));
  98. EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(), Eq()));
  99. }
  100. struct CountingTrivialPred {
  101. int* count;
  102. bool operator()(int, int) const {
  103. ++*count;
  104. return true;
  105. }
  106. };
  107. TEST(EqualTest, RandomAccessComplexity) {
  108. std::vector<int> v1{1, 1, 3};
  109. std::vector<int> v2 = v1;
  110. std::vector<int> v3{1, 2};
  111. do {
  112. int count = 0;
  113. absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(),
  114. CountingTrivialPred{&count});
  115. EXPECT_LE(count, 3);
  116. } while (std::next_permutation(v2.begin(), v2.end()));
  117. int count = 0;
  118. absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(),
  119. CountingTrivialPred{&count});
  120. EXPECT_EQ(count, 0);
  121. }
  122. class LinearSearchTest : public testing::Test {
  123. protected:
  124. LinearSearchTest() : container_{1, 2, 3} {}
  125. static bool Is3(int n) { return n == 3; }
  126. static bool Is4(int n) { return n == 4; }
  127. std::vector<int> container_;
  128. };
  129. TEST_F(LinearSearchTest, linear_search) {
  130. EXPECT_TRUE(absl::linear_search(container_.begin(), container_.end(), 3));
  131. EXPECT_FALSE(absl::linear_search(container_.begin(), container_.end(), 4));
  132. }
  133. TEST_F(LinearSearchTest, linear_searchConst) {
  134. const std::vector<int> *const const_container = &container_;
  135. EXPECT_TRUE(
  136. absl::linear_search(const_container->begin(), const_container->end(), 3));
  137. EXPECT_FALSE(
  138. absl::linear_search(const_container->begin(), const_container->end(), 4));
  139. }
  140. TEST(RotateTest, Rotate) {
  141. std::vector<int> v{0, 1, 2, 3, 4};
  142. EXPECT_EQ(*absl::rotate(v.begin(), v.begin() + 2, v.end()), 0);
  143. EXPECT_THAT(v, testing::ElementsAreArray({2, 3, 4, 0, 1}));
  144. std::list<int> l{0, 1, 2, 3, 4};
  145. EXPECT_EQ(*absl::rotate(l.begin(), std::next(l.begin(), 3), l.end()), 0);
  146. EXPECT_THAT(l, testing::ElementsAreArray({3, 4, 0, 1, 2}));
  147. }
  148. } // namespace