OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/id_map.h" | 5 #include "base/id_map.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 class TestObject { | 16 class TestObject { |
17 }; | 17 }; |
18 | 18 |
19 class DestructorCounter { | 19 class DestructorCounter { |
20 public: | 20 public: |
21 explicit DestructorCounter(int* counter) : counter_(counter) {} | 21 explicit DestructorCounter(int* counter) : counter_(counter) {} |
22 ~DestructorCounter() { ++(*counter_); } | 22 ~DestructorCounter() { ++(*counter_); } |
23 | 23 |
24 private: | 24 private: |
25 int* counter_; | 25 int* counter_; |
26 }; | 26 }; |
27 | 27 |
28 TEST(IDMapTest, Basic) { | 28 TEST(IDMapTest, Basic) { |
29 IDMap<TestObject> map; | 29 IDMap<TestObject*> map; |
30 EXPECT_TRUE(map.IsEmpty()); | 30 EXPECT_TRUE(map.IsEmpty()); |
31 EXPECT_EQ(0U, map.size()); | 31 EXPECT_EQ(0U, map.size()); |
32 | 32 |
33 TestObject obj1; | 33 TestObject obj1; |
34 TestObject obj2; | 34 TestObject obj2; |
35 | 35 |
36 int32_t id1 = map.Add(&obj1); | 36 int32_t id1 = map.Add(&obj1); |
37 EXPECT_FALSE(map.IsEmpty()); | 37 EXPECT_FALSE(map.IsEmpty()); |
38 EXPECT_EQ(1U, map.size()); | 38 EXPECT_EQ(1U, map.size()); |
39 EXPECT_EQ(&obj1, map.Lookup(id1)); | 39 EXPECT_EQ(&obj1, map.Lookup(id1)); |
(...skipping 18 matching lines...) Expand all Loading... |
58 EXPECT_EQ(&obj1, map.Lookup(1)); | 58 EXPECT_EQ(&obj1, map.Lookup(1)); |
59 EXPECT_EQ(&obj2, map.Lookup(2)); | 59 EXPECT_EQ(&obj2, map.Lookup(2)); |
60 | 60 |
61 EXPECT_EQ(&obj2, map.Replace(2, &obj1)); | 61 EXPECT_EQ(&obj2, map.Replace(2, &obj1)); |
62 EXPECT_EQ(&obj1, map.Lookup(2)); | 62 EXPECT_EQ(&obj1, map.Lookup(2)); |
63 | 63 |
64 EXPECT_EQ(0, map.iteration_depth()); | 64 EXPECT_EQ(0, map.iteration_depth()); |
65 } | 65 } |
66 | 66 |
67 TEST(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) { | 67 TEST(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) { |
68 IDMap<TestObject> map; | 68 IDMap<TestObject*> map; |
69 | 69 |
70 TestObject obj1; | 70 TestObject obj1; |
71 TestObject obj2; | 71 TestObject obj2; |
72 TestObject obj3; | 72 TestObject obj3; |
73 | 73 |
74 map.Add(&obj1); | 74 map.Add(&obj1); |
75 map.Add(&obj2); | 75 map.Add(&obj2); |
76 map.Add(&obj3); | 76 map.Add(&obj3); |
77 | 77 |
78 { | 78 { |
79 IDMap<TestObject>::const_iterator iter(&map); | 79 IDMap<TestObject*>::const_iterator iter(&map); |
80 | 80 |
81 EXPECT_EQ(1, map.iteration_depth()); | 81 EXPECT_EQ(1, map.iteration_depth()); |
82 | 82 |
83 while (!iter.IsAtEnd()) { | 83 while (!iter.IsAtEnd()) { |
84 map.Remove(iter.GetCurrentKey()); | 84 map.Remove(iter.GetCurrentKey()); |
85 iter.Advance(); | 85 iter.Advance(); |
86 } | 86 } |
87 | 87 |
88 // Test that while an iterator is still in scope, we get the map emptiness | 88 // Test that while an iterator is still in scope, we get the map emptiness |
89 // right (http://crbug.com/35571). | 89 // right (http://crbug.com/35571). |
90 EXPECT_TRUE(map.IsEmpty()); | 90 EXPECT_TRUE(map.IsEmpty()); |
91 EXPECT_EQ(0U, map.size()); | 91 EXPECT_EQ(0U, map.size()); |
92 } | 92 } |
93 | 93 |
94 EXPECT_TRUE(map.IsEmpty()); | 94 EXPECT_TRUE(map.IsEmpty()); |
95 EXPECT_EQ(0U, map.size()); | 95 EXPECT_EQ(0U, map.size()); |
96 | 96 |
97 EXPECT_EQ(0, map.iteration_depth()); | 97 EXPECT_EQ(0, map.iteration_depth()); |
98 } | 98 } |
99 | 99 |
100 TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) { | 100 TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) { |
101 IDMap<TestObject> map; | 101 IDMap<TestObject*> map; |
102 | 102 |
103 const int kCount = 5; | 103 const int kCount = 5; |
104 TestObject obj[kCount]; | 104 TestObject obj[kCount]; |
105 | 105 |
106 for (int i = 0; i < kCount; i++) | 106 for (int i = 0; i < kCount; i++) |
107 map.Add(&obj[i]); | 107 map.Add(&obj[i]); |
108 | 108 |
109 // IDMap uses a hash_map, which has no predictable iteration order. | 109 // IDMap uses a hash_map, which has no predictable iteration order. |
110 int32_t ids_in_iteration_order[kCount]; | 110 int32_t ids_in_iteration_order[kCount]; |
111 const TestObject* objs_in_iteration_order[kCount]; | 111 const TestObject* objs_in_iteration_order[kCount]; |
112 int counter = 0; | 112 int counter = 0; |
113 for (IDMap<TestObject>::const_iterator iter(&map); | 113 for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
114 !iter.IsAtEnd(); iter.Advance()) { | 114 iter.Advance()) { |
115 ids_in_iteration_order[counter] = iter.GetCurrentKey(); | 115 ids_in_iteration_order[counter] = iter.GetCurrentKey(); |
116 objs_in_iteration_order[counter] = iter.GetCurrentValue(); | 116 objs_in_iteration_order[counter] = iter.GetCurrentValue(); |
117 counter++; | 117 counter++; |
118 } | 118 } |
119 | 119 |
120 counter = 0; | 120 counter = 0; |
121 for (IDMap<TestObject>::const_iterator iter(&map); | 121 for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
122 !iter.IsAtEnd(); iter.Advance()) { | 122 iter.Advance()) { |
123 EXPECT_EQ(1, map.iteration_depth()); | 123 EXPECT_EQ(1, map.iteration_depth()); |
124 | 124 |
125 switch (counter) { | 125 switch (counter) { |
126 case 0: | 126 case 0: |
127 EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey()); | 127 EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey()); |
128 EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue()); | 128 EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue()); |
129 map.Remove(ids_in_iteration_order[1]); | 129 map.Remove(ids_in_iteration_order[1]); |
130 break; | 130 break; |
131 case 1: | 131 case 1: |
132 EXPECT_EQ(ids_in_iteration_order[2], iter.GetCurrentKey()); | 132 EXPECT_EQ(ids_in_iteration_order[2], iter.GetCurrentKey()); |
(...skipping 10 matching lines...) Expand all Loading... |
143 break; | 143 break; |
144 } | 144 } |
145 | 145 |
146 counter++; | 146 counter++; |
147 } | 147 } |
148 | 148 |
149 EXPECT_EQ(0, map.iteration_depth()); | 149 EXPECT_EQ(0, map.iteration_depth()); |
150 } | 150 } |
151 | 151 |
152 TEST(IDMapTest, CopyIterator) { | 152 TEST(IDMapTest, CopyIterator) { |
153 IDMap<TestObject> map; | 153 IDMap<TestObject*> map; |
154 | 154 |
155 TestObject obj1; | 155 TestObject obj1; |
156 TestObject obj2; | 156 TestObject obj2; |
157 TestObject obj3; | 157 TestObject obj3; |
158 | 158 |
159 map.Add(&obj1); | 159 map.Add(&obj1); |
160 map.Add(&obj2); | 160 map.Add(&obj2); |
161 map.Add(&obj3); | 161 map.Add(&obj3); |
162 | 162 |
163 EXPECT_EQ(0, map.iteration_depth()); | 163 EXPECT_EQ(0, map.iteration_depth()); |
164 | 164 |
165 { | 165 { |
166 IDMap<TestObject>::const_iterator iter1(&map); | 166 IDMap<TestObject*>::const_iterator iter1(&map); |
167 EXPECT_EQ(1, map.iteration_depth()); | 167 EXPECT_EQ(1, map.iteration_depth()); |
168 | 168 |
169 // Make sure that copying the iterator correctly increments | 169 // Make sure that copying the iterator correctly increments |
170 // map's iteration depth. | 170 // map's iteration depth. |
171 IDMap<TestObject>::const_iterator iter2(iter1); | 171 IDMap<TestObject*>::const_iterator iter2(iter1); |
172 EXPECT_EQ(2, map.iteration_depth()); | 172 EXPECT_EQ(2, map.iteration_depth()); |
173 } | 173 } |
174 | 174 |
175 // Make sure after destroying all iterators the map's iteration depth | 175 // Make sure after destroying all iterators the map's iteration depth |
176 // returns to initial state. | 176 // returns to initial state. |
177 EXPECT_EQ(0, map.iteration_depth()); | 177 EXPECT_EQ(0, map.iteration_depth()); |
178 } | 178 } |
179 | 179 |
180 TEST(IDMapTest, AssignIterator) { | 180 TEST(IDMapTest, AssignIterator) { |
181 IDMap<TestObject> map; | 181 IDMap<TestObject*> map; |
182 | 182 |
183 TestObject obj1; | 183 TestObject obj1; |
184 TestObject obj2; | 184 TestObject obj2; |
185 TestObject obj3; | 185 TestObject obj3; |
186 | 186 |
187 map.Add(&obj1); | 187 map.Add(&obj1); |
188 map.Add(&obj2); | 188 map.Add(&obj2); |
189 map.Add(&obj3); | 189 map.Add(&obj3); |
190 | 190 |
191 EXPECT_EQ(0, map.iteration_depth()); | 191 EXPECT_EQ(0, map.iteration_depth()); |
192 | 192 |
193 { | 193 { |
194 IDMap<TestObject>::const_iterator iter1(&map); | 194 IDMap<TestObject*>::const_iterator iter1(&map); |
195 EXPECT_EQ(1, map.iteration_depth()); | 195 EXPECT_EQ(1, map.iteration_depth()); |
196 | 196 |
197 IDMap<TestObject>::const_iterator iter2(&map); | 197 IDMap<TestObject*>::const_iterator iter2(&map); |
198 EXPECT_EQ(2, map.iteration_depth()); | 198 EXPECT_EQ(2, map.iteration_depth()); |
199 | 199 |
200 // Make sure that assigning the iterator correctly updates | 200 // Make sure that assigning the iterator correctly updates |
201 // map's iteration depth (-1 for destruction, +1 for assignment). | 201 // map's iteration depth (-1 for destruction, +1 for assignment). |
202 EXPECT_EQ(2, map.iteration_depth()); | 202 EXPECT_EQ(2, map.iteration_depth()); |
203 } | 203 } |
204 | 204 |
205 // Make sure after destroying all iterators the map's iteration depth | 205 // Make sure after destroying all iterators the map's iteration depth |
206 // returns to initial state. | 206 // returns to initial state. |
207 EXPECT_EQ(0, map.iteration_depth()); | 207 EXPECT_EQ(0, map.iteration_depth()); |
208 } | 208 } |
209 | 209 |
210 TEST(IDMapTest, IteratorRemainsValidWhenClearing) { | 210 TEST(IDMapTest, IteratorRemainsValidWhenClearing) { |
211 IDMap<TestObject> map; | 211 IDMap<TestObject*> map; |
212 | 212 |
213 const int kCount = 5; | 213 const int kCount = 5; |
214 TestObject obj[kCount]; | 214 TestObject obj[kCount]; |
215 | 215 |
216 for (int i = 0; i < kCount; i++) | 216 for (int i = 0; i < kCount; i++) |
217 map.Add(&obj[i]); | 217 map.Add(&obj[i]); |
218 | 218 |
219 // IDMap uses a hash_map, which has no predictable iteration order. | 219 // IDMap uses a hash_map, which has no predictable iteration order. |
220 int32_t ids_in_iteration_order[kCount]; | 220 int32_t ids_in_iteration_order[kCount]; |
221 const TestObject* objs_in_iteration_order[kCount]; | 221 const TestObject* objs_in_iteration_order[kCount]; |
222 int counter = 0; | 222 int counter = 0; |
223 for (IDMap<TestObject>::const_iterator iter(&map); | 223 for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
224 !iter.IsAtEnd(); iter.Advance()) { | 224 iter.Advance()) { |
225 ids_in_iteration_order[counter] = iter.GetCurrentKey(); | 225 ids_in_iteration_order[counter] = iter.GetCurrentKey(); |
226 objs_in_iteration_order[counter] = iter.GetCurrentValue(); | 226 objs_in_iteration_order[counter] = iter.GetCurrentValue(); |
227 counter++; | 227 counter++; |
228 } | 228 } |
229 | 229 |
230 counter = 0; | 230 counter = 0; |
231 for (IDMap<TestObject>::const_iterator iter(&map); | 231 for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
232 !iter.IsAtEnd(); iter.Advance()) { | 232 iter.Advance()) { |
233 switch (counter) { | 233 switch (counter) { |
234 case 0: | 234 case 0: |
235 EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey()); | 235 EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey()); |
236 EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue()); | 236 EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue()); |
237 break; | 237 break; |
238 case 1: | 238 case 1: |
239 EXPECT_EQ(ids_in_iteration_order[1], iter.GetCurrentKey()); | 239 EXPECT_EQ(ids_in_iteration_order[1], iter.GetCurrentKey()); |
240 EXPECT_EQ(objs_in_iteration_order[1], iter.GetCurrentValue()); | 240 EXPECT_EQ(objs_in_iteration_order[1], iter.GetCurrentValue()); |
241 map.Clear(); | 241 map.Clear(); |
242 EXPECT_TRUE(map.IsEmpty()); | 242 EXPECT_TRUE(map.IsEmpty()); |
(...skipping 13 matching lines...) Expand all Loading... |
256 TEST(IDMapTest, OwningPointersDeletesThemOnRemove) { | 256 TEST(IDMapTest, OwningPointersDeletesThemOnRemove) { |
257 const int kCount = 3; | 257 const int kCount = 3; |
258 | 258 |
259 int external_del_count = 0; | 259 int external_del_count = 0; |
260 DestructorCounter* external_obj[kCount]; | 260 DestructorCounter* external_obj[kCount]; |
261 int map_external_ids[kCount]; | 261 int map_external_ids[kCount]; |
262 | 262 |
263 int owned_del_count = 0; | 263 int owned_del_count = 0; |
264 int map_owned_ids[kCount]; | 264 int map_owned_ids[kCount]; |
265 | 265 |
266 IDMap<DestructorCounter> map_external; | 266 IDMap<DestructorCounter*> map_external; |
267 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; | 267 IDMap<std::unique_ptr<DestructorCounter>> map_owned; |
268 | 268 |
269 for (int i = 0; i < kCount; ++i) { | 269 for (int i = 0; i < kCount; ++i) { |
270 external_obj[i] = new DestructorCounter(&external_del_count); | 270 external_obj[i] = new DestructorCounter(&external_del_count); |
271 map_external_ids[i] = map_external.Add(external_obj[i]); | 271 map_external_ids[i] = map_external.Add(external_obj[i]); |
272 | 272 |
273 map_owned_ids[i] = | 273 map_owned_ids[i] = |
274 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); | 274 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
275 } | 275 } |
276 | 276 |
277 for (int i = 0; i < kCount; ++i) { | 277 for (int i = 0; i < kCount; ++i) { |
(...skipping 13 matching lines...) Expand all Loading... |
291 } | 291 } |
292 | 292 |
293 TEST(IDMapTest, OwningPointersDeletesThemOnClear) { | 293 TEST(IDMapTest, OwningPointersDeletesThemOnClear) { |
294 const int kCount = 3; | 294 const int kCount = 3; |
295 | 295 |
296 int external_del_count = 0; | 296 int external_del_count = 0; |
297 DestructorCounter* external_obj[kCount]; | 297 DestructorCounter* external_obj[kCount]; |
298 | 298 |
299 int owned_del_count = 0; | 299 int owned_del_count = 0; |
300 | 300 |
301 IDMap<DestructorCounter> map_external; | 301 IDMap<DestructorCounter*> map_external; |
302 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; | 302 IDMap<std::unique_ptr<DestructorCounter>> map_owned; |
303 | 303 |
304 for (int i = 0; i < kCount; ++i) { | 304 for (int i = 0; i < kCount; ++i) { |
305 external_obj[i] = new DestructorCounter(&external_del_count); | 305 external_obj[i] = new DestructorCounter(&external_del_count); |
306 map_external.Add(external_obj[i]); | 306 map_external.Add(external_obj[i]); |
307 | 307 |
308 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); | 308 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
309 } | 309 } |
310 | 310 |
311 EXPECT_EQ(external_del_count, 0); | 311 EXPECT_EQ(external_del_count, 0); |
312 EXPECT_EQ(owned_del_count, 0); | 312 EXPECT_EQ(owned_del_count, 0); |
(...skipping 14 matching lines...) Expand all Loading... |
327 | 327 |
328 TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) { | 328 TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) { |
329 const int kCount = 3; | 329 const int kCount = 3; |
330 | 330 |
331 int external_del_count = 0; | 331 int external_del_count = 0; |
332 DestructorCounter* external_obj[kCount]; | 332 DestructorCounter* external_obj[kCount]; |
333 | 333 |
334 int owned_del_count = 0; | 334 int owned_del_count = 0; |
335 | 335 |
336 { | 336 { |
337 IDMap<DestructorCounter> map_external; | 337 IDMap<DestructorCounter*> map_external; |
338 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; | 338 IDMap<std::unique_ptr<DestructorCounter>> map_owned; |
339 | 339 |
340 for (int i = 0; i < kCount; ++i) { | 340 for (int i = 0; i < kCount; ++i) { |
341 external_obj[i] = new DestructorCounter(&external_del_count); | 341 external_obj[i] = new DestructorCounter(&external_del_count); |
342 map_external.Add(external_obj[i]); | 342 map_external.Add(external_obj[i]); |
343 | 343 |
344 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); | 344 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
345 } | 345 } |
346 } | 346 } |
347 | 347 |
348 EXPECT_EQ(external_del_count, 0); | 348 EXPECT_EQ(external_del_count, 0); |
349 | 349 |
350 for (int i = 0; i < kCount; ++i) { | 350 for (int i = 0; i < kCount; ++i) { |
351 delete external_obj[i]; | 351 delete external_obj[i]; |
352 } | 352 } |
353 | 353 |
354 EXPECT_EQ(external_del_count, kCount); | 354 EXPECT_EQ(external_del_count, kCount); |
355 EXPECT_EQ(owned_del_count, kCount); | 355 EXPECT_EQ(owned_del_count, kCount); |
356 } | 356 } |
357 | 357 |
358 TEST(IDMapTest, Int64KeyType) { | 358 TEST(IDMapTest, Int64KeyType) { |
359 IDMap<TestObject, IDMapExternalPointer, int64_t> map; | 359 IDMap<TestObject*, int64_t> map; |
360 TestObject obj1; | 360 TestObject obj1; |
361 const int64_t kId1 = 999999999999999999; | 361 const int64_t kId1 = 999999999999999999; |
362 | 362 |
363 map.AddWithID(&obj1, kId1); | 363 map.AddWithID(&obj1, kId1); |
364 EXPECT_EQ(&obj1, map.Lookup(kId1)); | 364 EXPECT_EQ(&obj1, map.Lookup(kId1)); |
365 | 365 |
366 IDMap<TestObject, IDMapExternalPointer, int64_t>::const_iterator iter(&map); | 366 IDMap<TestObject*, int64_t>::const_iterator iter(&map); |
367 ASSERT_FALSE(iter.IsAtEnd()); | 367 ASSERT_FALSE(iter.IsAtEnd()); |
368 EXPECT_EQ(kId1, iter.GetCurrentKey()); | 368 EXPECT_EQ(kId1, iter.GetCurrentKey()); |
369 EXPECT_EQ(&obj1, iter.GetCurrentValue()); | 369 EXPECT_EQ(&obj1, iter.GetCurrentValue()); |
370 iter.Advance(); | 370 iter.Advance(); |
371 ASSERT_TRUE(iter.IsAtEnd()); | 371 ASSERT_TRUE(iter.IsAtEnd()); |
372 | 372 |
373 map.Remove(kId1); | 373 map.Remove(kId1); |
374 EXPECT_TRUE(map.IsEmpty()); | 374 EXPECT_TRUE(map.IsEmpty()); |
375 } | 375 } |
376 | 376 |
377 } // namespace | 377 } // namespace |
OLD | NEW |