21 #include "gmock/gmock.h" 22 #include "gtest/gtest.h" 26 TEST(EqualTest, DefaultComparisonRandomAccess) {
27 std::vector<int> v1{1, 2, 3};
28 std::vector<int> v2 = v1;
29 std::vector<int> v3 = {1, 2};
30 std::vector<int> v4 = {1, 2, 4};
32 EXPECT_TRUE(
absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
33 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
34 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
37 TEST(EqualTest, DefaultComparison) {
38 std::list<int> lst1{1, 2, 3};
39 std::list<int> lst2 = lst1;
40 std::list<int> lst3{1, 2};
41 std::list<int> lst4{1, 2, 4};
43 EXPECT_TRUE(
absl::equal(lst1.begin(), lst1.end(), lst2.begin(), lst2.end()));
44 EXPECT_FALSE(
absl::equal(lst1.begin(), lst1.end(), lst3.begin(), lst3.end()));
45 EXPECT_FALSE(
absl::equal(lst1.begin(), lst1.end(), lst4.begin(), lst4.end()));
48 TEST(EqualTest, EmptyRange) {
49 std::vector<int> v1{1, 2, 3};
50 std::vector<int> empty1;
51 std::vector<int> empty2;
53 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), empty1.begin(), empty1.end()));
54 EXPECT_FALSE(
absl::equal(empty1.begin(), empty1.end(), v1.begin(), v1.end()));
56 absl::equal(empty1.begin(), empty1.end(), empty2.begin(), empty2.end()));
59 TEST(EqualTest, MixedIterTypes) {
60 std::vector<int> v1{1, 2, 3};
61 std::list<int> lst1{v1.begin(), v1.end()};
62 std::list<int> lst2{1, 2, 4};
63 std::list<int> lst3{1, 2};
65 EXPECT_TRUE(
absl::equal(v1.begin(), v1.end(), lst1.begin(), lst1.end()));
66 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), lst2.begin(), lst2.end()));
67 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), lst3.begin(), lst3.end()));
70 TEST(EqualTest, MixedValueTypes) {
71 std::vector<int> v1{1, 2, 3};
72 std::vector<char> v2{1, 2, 3};
73 std::vector<char> v3{1, 2};
74 std::vector<char> v4{1, 2, 4};
76 EXPECT_TRUE(
absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
77 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
78 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
81 TEST(EqualTest, WeirdIterators) {
82 std::vector<bool> v1{
true,
false};
83 std::vector<bool> v2 = v1;
84 std::vector<bool> v3{
true};
85 std::vector<bool> v4{
true,
true,
true};
87 EXPECT_TRUE(
absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
88 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
89 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
92 TEST(EqualTest, CustomComparison) {
93 int n[] = {1, 2, 3, 4};
94 std::vector<int*> v1{&n[0], &n[1], &n[2]};
95 std::vector<int*> v2 = v1;
96 std::vector<int*> v3{&n[0], &n[1], &n[3]};
97 std::vector<int*> v4{&n[0], &n[1]};
99 auto eq = [](
int*
a,
int*
b) {
return *a == *
b; };
101 EXPECT_TRUE(
absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(),
eq));
102 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(),
eq));
103 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end(),
eq));
106 TEST(EqualTest, MoveOnlyPredicate) {
107 std::vector<int> v1{1, 2, 3};
108 std::vector<int> v2{4, 5, 6};
114 Eq(
const Eq &) =
delete;
115 Eq &operator=(
const Eq &) =
delete;
116 bool operator()(
const int a,
const int b)
const {
return a ==
b; }
119 EXPECT_TRUE(
absl::equal(v1.begin(), v1.end(), v1.begin(), v1.end(), Eq()));
120 EXPECT_FALSE(
absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(), Eq()));
123 struct CountingTrivialPred {
125 bool operator()(
int,
int)
const {
131 TEST(EqualTest, RandomAccessComplexity) {
132 std::vector<int> v1{1, 1, 3};
133 std::vector<int> v2 = v1;
134 std::vector<int> v3{1, 2};
138 absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(),
139 CountingTrivialPred{&count});
141 }
while (std::next_permutation(v2.begin(), v2.end()));
144 absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(),
145 CountingTrivialPred{&count});
149 class LinearSearchTest :
public testing::Test {
151 LinearSearchTest() : container_{1, 2, 3} {}
153 static bool Is3(
int n) {
return n == 3; }
154 static bool Is4(
int n) {
return n == 4; }
156 std::vector<int> container_;
164 TEST_F(LinearSearchTest, linear_searchConst) {
165 const std::vector<int> *
const const_container = &container_;
173 std::vector<int>
v{0, 1, 2, 3, 4};
175 EXPECT_THAT(
v, testing::ElementsAreArray({2, 3, 4, 0, 1}));
177 std::list<int> l{0, 1, 2, 3, 4};
179 EXPECT_THAT(l, testing::ElementsAreArray({3, 4, 0, 1, 2}));
AllocList * next[kMaxLevel]
TEST_F(GraphCyclesTest, NoCycle)
static uint64_t Rotate(uint64_t val, int shift)
bool linear_search(InputIterator first, InputIterator last, const EqualityComparable &value)
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Pred &&pred)
ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last)
TEST(Symbolize, Unimplemented)
hash_default_eq< typename T::first_type > eq