OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_vector.h" | 5 #include "base/memory/scoped_vector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 } | 84 } |
85 | 85 |
86 LifeCycleState life_cycle_state() const { return life_cycle_state_; } | 86 LifeCycleState life_cycle_state() const { return life_cycle_state_; } |
87 | 87 |
88 // Factory method for creating a new LifeCycleObject tied to this | 88 // Factory method for creating a new LifeCycleObject tied to this |
89 // LifeCycleWatcher. | 89 // LifeCycleWatcher. |
90 LifeCycleObject* NewLifeCycleObject() { | 90 LifeCycleObject* NewLifeCycleObject() { |
91 return new LifeCycleObject(this); | 91 return new LifeCycleObject(this); |
92 } | 92 } |
93 | 93 |
94 // Returns true iff |object| is the same object that this watcher is tracking. | |
95 bool IsWatching(LifeCycleObject* object) const { | |
96 return object == constructed_life_cycle_object_.get(); | |
97 } | |
98 | |
94 private: | 99 private: |
95 LifeCycleState life_cycle_state_; | 100 LifeCycleState life_cycle_state_; |
96 scoped_ptr<LifeCycleObject> constructed_life_cycle_object_; | 101 scoped_ptr<LifeCycleObject> constructed_life_cycle_object_; |
97 | 102 |
98 DISALLOW_COPY_AND_ASSIGN(LifeCycleWatcher); | 103 DISALLOW_COPY_AND_ASSIGN(LifeCycleWatcher); |
99 }; | 104 }; |
100 | 105 |
101 TEST(ScopedVectorTest, LifeCycleWatcher) { | 106 TEST(ScopedVectorTest, LifeCycleWatcher) { |
102 LifeCycleWatcher watcher; | 107 LifeCycleWatcher watcher; |
103 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 108 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
104 LifeCycleObject* object = watcher.NewLifeCycleObject(); | 109 LifeCycleObject* object = watcher.NewLifeCycleObject(); |
105 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 110 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
106 delete object; | 111 delete object; |
107 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 112 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
108 } | 113 } |
109 | 114 |
110 TEST(ScopedVectorTest, Clear) { | 115 TEST(ScopedVectorTest, Clear) { |
111 LifeCycleWatcher watcher; | 116 LifeCycleWatcher watcher; |
112 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 117 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
113 ScopedVector<LifeCycleObject> scoped_vector; | 118 ScopedVector<LifeCycleObject> scoped_vector; |
114 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 119 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
115 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 120 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
121 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); | |
116 scoped_vector.clear(); | 122 scoped_vector.clear(); |
117 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 123 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
118 EXPECT_EQ(implicit_cast<size_t>(0), scoped_vector.size()); | 124 EXPECT_EQ(implicit_cast<size_t>(0), scoped_vector.size()); |
119 } | 125 } |
120 | 126 |
121 TEST(ScopedVectorTest, WeakClear) { | 127 TEST(ScopedVectorTest, WeakClear) { |
122 LifeCycleWatcher watcher; | 128 LifeCycleWatcher watcher; |
123 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 129 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
124 ScopedVector<LifeCycleObject> scoped_vector; | 130 ScopedVector<LifeCycleObject> scoped_vector; |
125 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 131 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
126 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 132 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
133 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); | |
127 scoped_vector.weak_clear(); | 134 scoped_vector.weak_clear(); |
128 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 135 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
129 EXPECT_EQ(implicit_cast<size_t>(0), scoped_vector.size()); | 136 EXPECT_EQ(implicit_cast<size_t>(0), scoped_vector.size()); |
130 } | 137 } |
131 | 138 |
132 TEST(ScopedVectorTest, ResizeShrink) { | 139 TEST(ScopedVectorTest, ResizeShrink) { |
133 LifeCycleWatcher first_watcher; | 140 LifeCycleWatcher first_watcher; |
134 EXPECT_EQ(LC_INITIAL, first_watcher.life_cycle_state()); | 141 EXPECT_EQ(LC_INITIAL, first_watcher.life_cycle_state()); |
135 LifeCycleWatcher second_watcher; | 142 LifeCycleWatcher second_watcher; |
136 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); | 143 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); |
137 ScopedVector<LifeCycleObject> scoped_vector; | 144 ScopedVector<LifeCycleObject> scoped_vector; |
138 | 145 |
139 scoped_vector.push_back(first_watcher.NewLifeCycleObject()); | 146 scoped_vector.push_back(first_watcher.NewLifeCycleObject()); |
140 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); | 147 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); |
141 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); | 148 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); |
149 EXPECT_TRUE(first_watcher.IsWatching(scoped_vector[0])); | |
150 EXPECT_FALSE(second_watcher.IsWatching(scoped_vector[0])); | |
142 | 151 |
143 scoped_vector.push_back(second_watcher.NewLifeCycleObject()); | 152 scoped_vector.push_back(second_watcher.NewLifeCycleObject()); |
144 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); | 153 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); |
145 EXPECT_EQ(LC_CONSTRUCTED, second_watcher.life_cycle_state()); | 154 EXPECT_EQ(LC_CONSTRUCTED, second_watcher.life_cycle_state()); |
155 EXPECT_FALSE(first_watcher.IsWatching(scoped_vector[1])); | |
156 EXPECT_TRUE(second_watcher.IsWatching(scoped_vector[1])); | |
146 | 157 |
147 // Test that shrinking a vector deletes elements in the dissapearing range. | 158 // Test that shrinking a vector deletes elements in the dissapearing range. |
willchan no longer on Chromium
2012/12/04 01:41:50
disappearing
gavinp
2012/12/08 17:39:59
Done.
| |
148 scoped_vector.resize(1); | 159 scoped_vector.resize(1); |
149 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); | 160 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); |
150 EXPECT_EQ(LC_DESTROYED, second_watcher.life_cycle_state()); | 161 EXPECT_EQ(LC_DESTROYED, second_watcher.life_cycle_state()); |
151 EXPECT_EQ(implicit_cast<size_t>(1), scoped_vector.size()); | 162 EXPECT_EQ(implicit_cast<size_t>(1), scoped_vector.size()); |
163 EXPECT_TRUE(first_watcher.IsWatching(scoped_vector[0])); | |
152 } | 164 } |
153 | 165 |
154 TEST(ScopedVectorTest, ResizeGrow) { | 166 TEST(ScopedVectorTest, ResizeGrow) { |
155 LifeCycleWatcher watcher; | 167 LifeCycleWatcher watcher; |
156 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 168 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
157 ScopedVector<LifeCycleObject> scoped_vector; | 169 ScopedVector<LifeCycleObject> scoped_vector; |
158 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 170 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
159 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 171 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
172 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); | |
160 | 173 |
161 scoped_vector.resize(5); | 174 scoped_vector.resize(5); |
162 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 175 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
176 EXPECT_TRUE(watcher.IsWatching(scoped_vector[0])); | |
177 EXPECT_FALSE(watcher.IsWatching(scoped_vector[1]) || | |
willchan no longer on Chromium
2012/12/04 01:41:50
Just do multiple EXPECT_FALSEs, since if one of th
gavinp
2012/12/08 17:39:59
Done.
| |
178 watcher.IsWatching(scoped_vector[2]) || | |
179 watcher.IsWatching(scoped_vector[3]) || | |
180 watcher.IsWatching(scoped_vector[4])); | |
163 EXPECT_EQ(implicit_cast<size_t>(5), scoped_vector.size()); | 181 EXPECT_EQ(implicit_cast<size_t>(5), scoped_vector.size()); |
willchan no longer on Chromium
2012/12/04 01:41:50
Move this ahead and do ASSERT_EQ, since if it's fa
gavinp
2012/12/08 17:39:59
Done. Good point, I'll remember to watch for this
| |
164 } | 182 } |
165 | 183 |
166 TEST(ScopedVectorTest, Scope) { | 184 TEST(ScopedVectorTest, Scope) { |
167 LifeCycleWatcher watcher; | 185 LifeCycleWatcher watcher; |
168 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 186 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
169 { | 187 { |
170 ScopedVector<LifeCycleObject> scoped_vector; | 188 ScopedVector<LifeCycleObject> scoped_vector; |
171 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 189 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
172 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 190 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
191 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); | |
173 } | 192 } |
174 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 193 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
175 } | 194 } |
176 | 195 |
177 TEST(ScopedVectorTest, MoveConstruct) { | 196 TEST(ScopedVectorTest, MoveConstruct) { |
178 LifeCycleWatcher watcher; | 197 LifeCycleWatcher watcher; |
179 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 198 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
180 { | 199 { |
181 ScopedVector<LifeCycleObject> scoped_vector; | 200 ScopedVector<LifeCycleObject> scoped_vector; |
182 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 201 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
183 EXPECT_FALSE(scoped_vector.empty()); | 202 EXPECT_FALSE(scoped_vector.empty()); |
203 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); | |
184 | 204 |
185 ScopedVector<LifeCycleObject> scoped_vector_copy(scoped_vector.Pass()); | 205 ScopedVector<LifeCycleObject> scoped_vector_copy(scoped_vector.Pass()); |
186 EXPECT_TRUE(scoped_vector.empty()); | 206 EXPECT_TRUE(scoped_vector.empty()); |
187 EXPECT_FALSE(scoped_vector_copy.empty()); | 207 EXPECT_FALSE(scoped_vector_copy.empty()); |
208 EXPECT_TRUE(watcher.IsWatching(scoped_vector_copy.back())); | |
188 | 209 |
189 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 210 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
190 } | 211 } |
191 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 212 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
192 } | 213 } |
193 | 214 |
194 TEST(ScopedVectorTest, MoveAssign) { | 215 TEST(ScopedVectorTest, MoveAssign) { |
195 LifeCycleWatcher watcher; | 216 LifeCycleWatcher watcher; |
196 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 217 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
197 { | 218 { |
198 ScopedVector<LifeCycleObject> scoped_vector; | 219 ScopedVector<LifeCycleObject> scoped_vector; |
199 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 220 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
200 ScopedVector<LifeCycleObject> scoped_vector_assign; | 221 ScopedVector<LifeCycleObject> scoped_vector_assign; |
201 EXPECT_FALSE(scoped_vector.empty()); | 222 EXPECT_FALSE(scoped_vector.empty()); |
223 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); | |
202 | 224 |
203 scoped_vector_assign = scoped_vector.Pass(); | 225 scoped_vector_assign = scoped_vector.Pass(); |
204 EXPECT_TRUE(scoped_vector.empty()); | 226 EXPECT_TRUE(scoped_vector.empty()); |
205 EXPECT_FALSE(scoped_vector_assign.empty()); | 227 EXPECT_FALSE(scoped_vector_assign.empty()); |
228 EXPECT_TRUE(watcher.IsWatching(scoped_vector_assign.back())); | |
206 | 229 |
207 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 230 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
208 } | 231 } |
209 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 232 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
210 } | 233 } |
211 | 234 |
212 class DeleteCounter { | 235 class DeleteCounter { |
213 public: | 236 public: |
214 explicit DeleteCounter(int* deletes) | 237 explicit DeleteCounter(int* deletes) |
215 : deletes_(deletes) { | 238 : deletes_(deletes) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it) | 290 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it) |
268 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); | 291 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); |
269 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it) | 292 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it) |
270 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state()); | 293 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state()); |
271 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers); | 294 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers); |
272 ++it) | 295 ++it) |
273 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); | 296 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); |
274 } | 297 } |
275 | 298 |
276 } // namespace | 299 } // namespace |
OLD | NEW |