algorithm_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/algorithm/algorithm.h"
00016 
00017 #include <algorithm>
00018 #include <list>
00019 #include <vector>
00020 
00021 #include "gmock/gmock.h"
00022 #include "gtest/gtest.h"
00023 
00024 namespace {
00025 
00026 TEST(EqualTest, DefaultComparisonRandomAccess) {
00027   std::vector<int> v1{1, 2, 3};
00028   std::vector<int> v2 = v1;
00029   std::vector<int> v3 = {1, 2};
00030   std::vector<int> v4 = {1, 2, 4};
00031 
00032   EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
00033   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
00034   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
00035 }
00036 
00037 TEST(EqualTest, DefaultComparison) {
00038   std::list<int> lst1{1, 2, 3};
00039   std::list<int> lst2 = lst1;
00040   std::list<int> lst3{1, 2};
00041   std::list<int> lst4{1, 2, 4};
00042 
00043   EXPECT_TRUE(absl::equal(lst1.begin(), lst1.end(), lst2.begin(), lst2.end()));
00044   EXPECT_FALSE(absl::equal(lst1.begin(), lst1.end(), lst3.begin(), lst3.end()));
00045   EXPECT_FALSE(absl::equal(lst1.begin(), lst1.end(), lst4.begin(), lst4.end()));
00046 }
00047 
00048 TEST(EqualTest, EmptyRange) {
00049   std::vector<int> v1{1, 2, 3};
00050   std::vector<int> empty1;
00051   std::vector<int> empty2;
00052 
00053   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), empty1.begin(), empty1.end()));
00054   EXPECT_FALSE(absl::equal(empty1.begin(), empty1.end(), v1.begin(), v1.end()));
00055   EXPECT_TRUE(
00056       absl::equal(empty1.begin(), empty1.end(), empty2.begin(), empty2.end()));
00057 }
00058 
00059 TEST(EqualTest, MixedIterTypes) {
00060   std::vector<int> v1{1, 2, 3};
00061   std::list<int> lst1{v1.begin(), v1.end()};
00062   std::list<int> lst2{1, 2, 4};
00063   std::list<int> lst3{1, 2};
00064 
00065   EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), lst1.begin(), lst1.end()));
00066   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), lst2.begin(), lst2.end()));
00067   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), lst3.begin(), lst3.end()));
00068 }
00069 
00070 TEST(EqualTest, MixedValueTypes) {
00071   std::vector<int> v1{1, 2, 3};
00072   std::vector<char> v2{1, 2, 3};
00073   std::vector<char> v3{1, 2};
00074   std::vector<char> v4{1, 2, 4};
00075 
00076   EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
00077   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
00078   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
00079 }
00080 
00081 TEST(EqualTest, WeirdIterators) {
00082   std::vector<bool> v1{true, false};
00083   std::vector<bool> v2 = v1;
00084   std::vector<bool> v3{true};
00085   std::vector<bool> v4{true, true, true};
00086 
00087   EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
00088   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
00089   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
00090 }
00091 
00092 TEST(EqualTest, CustomComparison) {
00093   int n[] = {1, 2, 3, 4};
00094   std::vector<int*> v1{&n[0], &n[1], &n[2]};
00095   std::vector<int*> v2 = v1;
00096   std::vector<int*> v3{&n[0], &n[1], &n[3]};
00097   std::vector<int*> v4{&n[0], &n[1]};
00098 
00099   auto eq = [](int* a, int* b) { return *a == *b; };
00100 
00101   EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(), eq));
00102   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(), eq));
00103   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end(), eq));
00104 }
00105 
00106 TEST(EqualTest, MoveOnlyPredicate) {
00107   std::vector<int> v1{1, 2, 3};
00108   std::vector<int> v2{4, 5, 6};
00109 
00110   // move-only equality predicate
00111   struct Eq {
00112     Eq() = default;
00113     Eq(Eq &&) = default;
00114     Eq(const Eq &) = delete;
00115     Eq &operator=(const Eq &) = delete;
00116     bool operator()(const int a, const int b) const { return a == b; }
00117   };
00118 
00119   EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v1.begin(), v1.end(), Eq()));
00120   EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(), Eq()));
00121 }
00122 
00123 struct CountingTrivialPred {
00124   int* count;
00125   bool operator()(int, int) const {
00126     ++*count;
00127     return true;
00128   }
00129 };
00130 
00131 TEST(EqualTest, RandomAccessComplexity) {
00132   std::vector<int> v1{1, 1, 3};
00133   std::vector<int> v2 = v1;
00134   std::vector<int> v3{1, 2};
00135 
00136   do {
00137     int count = 0;
00138     absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(),
00139                 CountingTrivialPred{&count});
00140     EXPECT_LE(count, 3);
00141   } while (std::next_permutation(v2.begin(), v2.end()));
00142 
00143   int count = 0;
00144   absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(),
00145               CountingTrivialPred{&count});
00146   EXPECT_EQ(count, 0);
00147 }
00148 
00149 class LinearSearchTest : public testing::Test {
00150  protected:
00151   LinearSearchTest() : container_{1, 2, 3} {}
00152 
00153   static bool Is3(int n) { return n == 3; }
00154   static bool Is4(int n) { return n == 4; }
00155 
00156   std::vector<int> container_;
00157 };
00158 
00159 TEST_F(LinearSearchTest, linear_search) {
00160   EXPECT_TRUE(absl::linear_search(container_.begin(), container_.end(), 3));
00161   EXPECT_FALSE(absl::linear_search(container_.begin(), container_.end(), 4));
00162 }
00163 
00164 TEST_F(LinearSearchTest, linear_searchConst) {
00165   const std::vector<int> *const const_container = &container_;
00166   EXPECT_TRUE(
00167       absl::linear_search(const_container->begin(), const_container->end(), 3));
00168   EXPECT_FALSE(
00169       absl::linear_search(const_container->begin(), const_container->end(), 4));
00170 }
00171 
00172 TEST(RotateTest, Rotate) {
00173   std::vector<int> v{0, 1, 2, 3, 4};
00174   EXPECT_EQ(*absl::rotate(v.begin(), v.begin() + 2, v.end()), 0);
00175   EXPECT_THAT(v, testing::ElementsAreArray({2, 3, 4, 0, 1}));
00176 
00177   std::list<int> l{0, 1, 2, 3, 4};
00178   EXPECT_EQ(*absl::rotate(l.begin(), std::next(l.begin(), 3), l.end()), 0);
00179   EXPECT_THAT(l, testing::ElementsAreArray({3, 4, 0, 1, 2}));
00180 }
00181 
00182 }  // namespace


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