hunspell: Cleanup to fix the header include guards under google/ directory.
[chromium-blink-merge.git] / cc / base / scoped_ptr_vector_unittest.cc
blob7b9a86bd24183a25c29a0cf65f3bf35ffddebcaf
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <set>
7 #include "cc/base/scoped_ptr_vector.h"
8 #include "testing/gmock/include/gmock/gmock.h"
10 namespace cc {
11 namespace {
13 class Data {
14 public:
15 static scoped_ptr<Data> Create(int i) { return make_scoped_ptr(new Data(i)); }
16 int data() const { return data_; }
17 private:
18 explicit Data(int i) : data_(i) {}
19 int data_;
22 class IsOddPredicate {
23 public:
24 bool operator()(const Data* data) { return (data->data() % 2) == 1; }
27 TEST(ScopedPtrVectorTest, PushBack) {
28 ScopedPtrVector<Data> v;
30 // Insert 5 things into the vector.
31 v.push_back(Data::Create(1));
32 v.push_back(Data::Create(2));
33 v.push_back(Data::Create(3));
34 v.push_back(Data::Create(4));
35 v.push_back(Data::Create(5));
37 EXPECT_EQ(5u, v.size());
38 EXPECT_EQ(1, v[0]->data());
39 EXPECT_EQ(2, v[1]->data());
40 EXPECT_EQ(3, v[2]->data());
41 EXPECT_EQ(4, v[3]->data());
42 EXPECT_EQ(5, v[4]->data());
45 TEST(ScopedPtrVectorTest, InsertAndTake) {
46 // Insert 3 things into each vector.
47 ScopedPtrVector<Data> v;
48 v.push_back(Data::Create(1));
49 v.push_back(Data::Create(2));
50 v.push_back(Data::Create(6));
52 ScopedPtrVector<Data> v2;
53 v2.push_back(Data::Create(3));
54 v2.push_back(Data::Create(4));
55 v2.push_back(Data::Create(5));
57 ScopedPtrVector<Data>::iterator it = v.begin();
58 ++it;
59 ++it;
60 EXPECT_EQ(6, (*it)->data());
62 v.insert_and_take(it, &v2);
64 EXPECT_EQ(6u, v.size());
65 EXPECT_EQ(1, v[0]->data());
66 EXPECT_EQ(2, v[1]->data());
67 EXPECT_EQ(3, v[2]->data());
68 EXPECT_EQ(4, v[3]->data());
69 EXPECT_EQ(5, v[4]->data());
70 EXPECT_EQ(6, v[5]->data());
72 EXPECT_EQ(3u, v2.size());
73 EXPECT_EQ(nullptr, v2[0]);
74 EXPECT_EQ(nullptr, v2[1]);
75 EXPECT_EQ(nullptr, v2[2]);
78 TEST(ScopedPtrVectorTest, Partition) {
79 ScopedPtrVector<Data> v;
80 v.push_back(Data::Create(1));
81 v.push_back(Data::Create(2));
82 v.push_back(Data::Create(3));
83 v.push_back(Data::Create(4));
84 v.push_back(Data::Create(5));
86 ScopedPtrVector<Data>::iterator it = v.partition(IsOddPredicate());
87 std::set<int> odd_numbers;
88 for (ScopedPtrVector<Data>::iterator second_it = v.begin();
89 second_it != it;
90 ++second_it) {
91 EXPECT_EQ(1, (*second_it)->data() % 2);
92 odd_numbers.insert((*second_it)->data());
94 EXPECT_EQ(3u, odd_numbers.size());
96 std::set<int> even_numbers;
97 for (; it != v.end(); ++it) {
98 EXPECT_EQ(0, (*it)->data() % 2);
99 even_numbers.insert((*it)->data());
101 EXPECT_EQ(2u, even_numbers.size());
104 class DataWithDestruction {
105 public:
106 static scoped_ptr<DataWithDestruction> Create(int i, int* destroy_count) {
107 return make_scoped_ptr(new DataWithDestruction(i, destroy_count));
109 int data() const { return data_; }
110 ~DataWithDestruction() { ++(*destroy_count_); }
112 private:
113 explicit DataWithDestruction(int i, int* destroy_count)
114 : data_(i), destroy_count_(destroy_count) {}
115 int data_;
116 int* destroy_count_;
119 TEST(ScopedPtrVectorTest, RemoveIf) {
120 ScopedPtrVector<DataWithDestruction> v;
121 int destroyed[6] = {0};
122 v.push_back(DataWithDestruction::Create(1, &destroyed[0]));
123 v.push_back(DataWithDestruction::Create(2, &destroyed[1]));
124 v.push_back(DataWithDestruction::Create(3, &destroyed[2]));
125 v.push_back(DataWithDestruction::Create(3, &destroyed[3]));
126 v.push_back(DataWithDestruction::Create(4, &destroyed[4]));
127 v.push_back(DataWithDestruction::Create(5, &destroyed[5]));
129 int expect_destroyed[6] = {0};
131 // Removing more than one thing that matches.
132 auto is_three = [](DataWithDestruction* d) { return d->data() == 3; };
133 v.erase(v.remove_if(is_three), v.end());
134 EXPECT_EQ(4u, v.size());
135 expect_destroyed[2]++;
136 expect_destroyed[3]++;
137 for (size_t i = 0; i < arraysize(destroyed); ++i)
138 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i;
140 int expect_data[4] = {1, 2, 4, 5};
141 for (size_t i = 0; i < arraysize(expect_data); ++i)
142 EXPECT_EQ(expect_data[i], v[i]->data()) << i;
145 // Removing from the back of the vector.
146 auto is_five = [](DataWithDestruction* d) { return d->data() == 5; };
147 v.erase(v.remove_if(is_five), v.end());
148 EXPECT_EQ(3u, v.size());
149 expect_destroyed[5]++;
150 for (size_t i = 0; i < arraysize(destroyed); ++i)
151 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i;
153 int expect_data[3] = {1, 2, 4};
154 for (size_t i = 0; i < arraysize(expect_data); ++i)
155 EXPECT_EQ(expect_data[i], v[i]->data()) << i;
158 // Removing from the front of the vector.
159 auto is_one = [](DataWithDestruction* d) { return d->data() == 1; };
160 v.erase(v.remove_if(is_one), v.end());
161 EXPECT_EQ(2u, v.size());
162 expect_destroyed[0]++;
163 for (size_t i = 0; i < arraysize(destroyed); ++i)
164 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i;
166 int expect_data[2] = {2, 4};
167 for (size_t i = 0; i < arraysize(expect_data); ++i)
168 EXPECT_EQ(expect_data[i], v[i]->data()) << i;
171 // Removing things that aren't in the vector does nothing.
172 v.erase(v.remove_if(is_one), v.end());
173 EXPECT_EQ(2u, v.size());
174 for (size_t i = 0; i < arraysize(destroyed); ++i)
175 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i;
177 int expect_data[2] = {2, 4};
178 for (size_t i = 0; i < arraysize(expect_data); ++i)
179 EXPECT_EQ(expect_data[i], v[i]->data()) << i;
183 } // namespace
184 } // namespace cc