Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / base / memory / scoped_vector_unittest.cc
bloba91776afe92a97a66b02b300dca9112966232af1
1 // Copyright (c) 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 "base/memory/scoped_vector.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace {
14 // The LifeCycleObject notifies its Observer upon construction & destruction.
15 class LifeCycleObject {
16 public:
17 class Observer {
18 public:
19 virtual void OnLifeCycleConstruct(LifeCycleObject* o) = 0;
20 virtual void OnLifeCycleDestroy(LifeCycleObject* o) = 0;
22 protected:
23 virtual ~Observer() {}
26 ~LifeCycleObject() {
27 observer_->OnLifeCycleDestroy(this);
30 private:
31 friend class LifeCycleWatcher;
33 explicit LifeCycleObject(Observer* observer)
34 : observer_(observer) {
35 observer_->OnLifeCycleConstruct(this);
38 Observer* observer_;
40 DISALLOW_COPY_AND_ASSIGN(LifeCycleObject);
43 // The life cycle states we care about for the purposes of testing ScopedVector
44 // against objects.
45 enum LifeCycleState {
46 LC_INITIAL,
47 LC_CONSTRUCTED,
48 LC_DESTROYED,
51 // Because we wish to watch the life cycle of an object being constructed and
52 // destroyed, and further wish to test expectations against the state of that
53 // object, we cannot save state in that object itself. Instead, we use this
54 // pairing of the watcher, which observes the object and notifies of
55 // construction & destruction. Since we also may be testing assumptions about
56 // things not getting freed, this class also acts like a scoping object and
57 // deletes the |constructed_life_cycle_object_|, if any when the
58 // LifeCycleWatcher is destroyed. To keep this simple, the only expected state
59 // changes are:
60 // INITIAL -> CONSTRUCTED -> DESTROYED.
61 // Anything more complicated than that should start another test.
62 class LifeCycleWatcher : public LifeCycleObject::Observer {
63 public:
64 LifeCycleWatcher()
65 : life_cycle_state_(LC_INITIAL),
66 constructed_life_cycle_object_(NULL) {}
67 virtual ~LifeCycleWatcher() {}
69 // Assert INITIAL -> CONSTRUCTED and no LifeCycleObject associated with this
70 // LifeCycleWatcher.
71 virtual void OnLifeCycleConstruct(LifeCycleObject* object) OVERRIDE {
72 ASSERT_EQ(LC_INITIAL, life_cycle_state_);
73 ASSERT_EQ(NULL, constructed_life_cycle_object_.get());
74 life_cycle_state_ = LC_CONSTRUCTED;
75 constructed_life_cycle_object_.reset(object);
78 // Assert CONSTRUCTED -> DESTROYED and the |object| being destroyed is the
79 // same one we saw constructed.
80 virtual void OnLifeCycleDestroy(LifeCycleObject* object) OVERRIDE {
81 ASSERT_EQ(LC_CONSTRUCTED, life_cycle_state_);
82 LifeCycleObject* constructed_life_cycle_object =
83 constructed_life_cycle_object_.release();
84 ASSERT_EQ(constructed_life_cycle_object, object);
85 life_cycle_state_ = LC_DESTROYED;
88 LifeCycleState life_cycle_state() const { return life_cycle_state_; }
90 // Factory method for creating a new LifeCycleObject tied to this
91 // LifeCycleWatcher.
92 LifeCycleObject* NewLifeCycleObject() {
93 return new LifeCycleObject(this);
96 // Returns true iff |object| is the same object that this watcher is tracking.
97 bool IsWatching(LifeCycleObject* object) const {
98 return object == constructed_life_cycle_object_.get();
101 private:
102 LifeCycleState life_cycle_state_;
103 scoped_ptr<LifeCycleObject> constructed_life_cycle_object_;
105 DISALLOW_COPY_AND_ASSIGN(LifeCycleWatcher);
108 TEST(ScopedVectorTest, LifeCycleWatcher) {
109 LifeCycleWatcher watcher;
110 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
111 LifeCycleObject* object = watcher.NewLifeCycleObject();
112 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
113 delete object;
114 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
117 TEST(ScopedVectorTest, Clear) {
118 LifeCycleWatcher watcher;
119 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
120 ScopedVector<LifeCycleObject> scoped_vector;
121 scoped_vector.push_back(watcher.NewLifeCycleObject());
122 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
123 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
124 scoped_vector.clear();
125 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
126 EXPECT_TRUE(scoped_vector.empty());
129 TEST(ScopedVectorTest, WeakClear) {
130 LifeCycleWatcher watcher;
131 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
132 ScopedVector<LifeCycleObject> scoped_vector;
133 scoped_vector.push_back(watcher.NewLifeCycleObject());
134 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
135 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
136 scoped_vector.weak_clear();
137 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
138 EXPECT_TRUE(scoped_vector.empty());
141 TEST(ScopedVectorTest, ResizeShrink) {
142 LifeCycleWatcher first_watcher;
143 EXPECT_EQ(LC_INITIAL, first_watcher.life_cycle_state());
144 LifeCycleWatcher second_watcher;
145 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state());
146 ScopedVector<LifeCycleObject> scoped_vector;
148 scoped_vector.push_back(first_watcher.NewLifeCycleObject());
149 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state());
150 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state());
151 EXPECT_TRUE(first_watcher.IsWatching(scoped_vector[0]));
152 EXPECT_FALSE(second_watcher.IsWatching(scoped_vector[0]));
154 scoped_vector.push_back(second_watcher.NewLifeCycleObject());
155 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state());
156 EXPECT_EQ(LC_CONSTRUCTED, second_watcher.life_cycle_state());
157 EXPECT_FALSE(first_watcher.IsWatching(scoped_vector[1]));
158 EXPECT_TRUE(second_watcher.IsWatching(scoped_vector[1]));
160 // Test that shrinking a vector deletes elements in the disappearing range.
161 scoped_vector.resize(1);
162 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state());
163 EXPECT_EQ(LC_DESTROYED, second_watcher.life_cycle_state());
164 EXPECT_EQ(1u, scoped_vector.size());
165 EXPECT_TRUE(first_watcher.IsWatching(scoped_vector[0]));
168 TEST(ScopedVectorTest, ResizeGrow) {
169 LifeCycleWatcher watcher;
170 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
171 ScopedVector<LifeCycleObject> scoped_vector;
172 scoped_vector.push_back(watcher.NewLifeCycleObject());
173 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
174 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
176 scoped_vector.resize(5);
177 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
178 ASSERT_EQ(5u, scoped_vector.size());
179 EXPECT_TRUE(watcher.IsWatching(scoped_vector[0]));
180 EXPECT_FALSE(watcher.IsWatching(scoped_vector[1]));
181 EXPECT_FALSE(watcher.IsWatching(scoped_vector[2]));
182 EXPECT_FALSE(watcher.IsWatching(scoped_vector[3]));
183 EXPECT_FALSE(watcher.IsWatching(scoped_vector[4]));
186 TEST(ScopedVectorTest, Scope) {
187 LifeCycleWatcher watcher;
188 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
190 ScopedVector<LifeCycleObject> scoped_vector;
191 scoped_vector.push_back(watcher.NewLifeCycleObject());
192 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
193 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
195 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
198 TEST(ScopedVectorTest, MoveConstruct) {
199 LifeCycleWatcher watcher;
200 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
202 ScopedVector<LifeCycleObject> scoped_vector;
203 scoped_vector.push_back(watcher.NewLifeCycleObject());
204 EXPECT_FALSE(scoped_vector.empty());
205 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
207 ScopedVector<LifeCycleObject> scoped_vector_copy(scoped_vector.Pass());
208 EXPECT_TRUE(scoped_vector.empty());
209 EXPECT_FALSE(scoped_vector_copy.empty());
210 EXPECT_TRUE(watcher.IsWatching(scoped_vector_copy.back()));
212 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
214 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
217 TEST(ScopedVectorTest, MoveAssign) {
218 LifeCycleWatcher watcher;
219 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
221 ScopedVector<LifeCycleObject> scoped_vector;
222 scoped_vector.push_back(watcher.NewLifeCycleObject());
223 ScopedVector<LifeCycleObject> scoped_vector_assign;
224 EXPECT_FALSE(scoped_vector.empty());
225 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
227 scoped_vector_assign = scoped_vector.Pass();
228 EXPECT_TRUE(scoped_vector.empty());
229 EXPECT_FALSE(scoped_vector_assign.empty());
230 EXPECT_TRUE(watcher.IsWatching(scoped_vector_assign.back()));
232 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
234 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
237 class DeleteCounter {
238 public:
239 explicit DeleteCounter(int* deletes)
240 : deletes_(deletes) {
243 ~DeleteCounter() {
244 (*deletes_)++;
247 void VoidMethod0() {}
249 private:
250 int* const deletes_;
252 DISALLOW_COPY_AND_ASSIGN(DeleteCounter);
255 template <typename T>
256 ScopedVector<T> PassThru(ScopedVector<T> scoper) {
257 return scoper.Pass();
260 TEST(ScopedVectorTest, Passed) {
261 int deletes = 0;
262 ScopedVector<DeleteCounter> deleter_vector;
263 deleter_vector.push_back(new DeleteCounter(&deletes));
264 EXPECT_EQ(0, deletes);
265 base::Callback<ScopedVector<DeleteCounter>(void)> callback =
266 base::Bind(&PassThru<DeleteCounter>, base::Passed(&deleter_vector));
267 EXPECT_EQ(0, deletes);
268 ScopedVector<DeleteCounter> result = callback.Run();
269 EXPECT_EQ(0, deletes);
270 result.clear();
271 EXPECT_EQ(1, deletes);
274 TEST(ScopedVectorTest, InsertRange) {
275 LifeCycleWatcher watchers[5];
277 std::vector<LifeCycleObject*> vec;
278 for(LifeCycleWatcher* it = watchers; it != watchers + arraysize(watchers);
279 ++it) {
280 EXPECT_EQ(LC_INITIAL, it->life_cycle_state());
281 vec.push_back(it->NewLifeCycleObject());
282 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state());
284 // Start scope for ScopedVector.
286 ScopedVector<LifeCycleObject> scoped_vector;
287 scoped_vector.insert(scoped_vector.end(), vec.begin() + 1, vec.begin() + 3);
288 for(LifeCycleWatcher* it = watchers; it != watchers + arraysize(watchers);
289 ++it)
290 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state());
292 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it)
293 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state());
294 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it)
295 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state());
296 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers);
297 ++it)
298 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state());
301 } // namespace