The URL elider shouldn't assume that file: URLs have something after the initial
[chromium-blink-merge.git] / base / id_map_unittest.cc
bloba9fb2b9decd198adac688f603b45b23026f3b238
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
3 // found in the LICENSE file.
5 #include "base/id_map.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace {
11 class TestObject {
14 class DestructorCounter {
15 public:
16 explicit DestructorCounter(int* counter) : counter_(counter) {}
17 ~DestructorCounter() { ++(*counter_); }
19 private:
20 int* counter_;
23 TEST(IDMapTest, Basic) {
24 IDMap<TestObject> map;
25 EXPECT_TRUE(map.IsEmpty());
26 EXPECT_EQ(0U, map.size());
28 TestObject obj1;
29 TestObject obj2;
31 int32 id1 = map.Add(&obj1);
32 EXPECT_FALSE(map.IsEmpty());
33 EXPECT_EQ(1U, map.size());
34 EXPECT_EQ(&obj1, map.Lookup(id1));
36 int32 id2 = map.Add(&obj2);
37 EXPECT_FALSE(map.IsEmpty());
38 EXPECT_EQ(2U, map.size());
40 EXPECT_EQ(&obj1, map.Lookup(id1));
41 EXPECT_EQ(&obj2, map.Lookup(id2));
43 map.Remove(id1);
44 EXPECT_FALSE(map.IsEmpty());
45 EXPECT_EQ(1U, map.size());
47 map.Remove(id2);
48 EXPECT_TRUE(map.IsEmpty());
49 EXPECT_EQ(0U, map.size());
51 map.AddWithID(&obj1, 1);
52 map.AddWithID(&obj2, 2);
53 EXPECT_EQ(&obj1, map.Lookup(1));
54 EXPECT_EQ(&obj2, map.Lookup(2));
56 EXPECT_EQ(&obj2, map.Replace(2, &obj1));
57 EXPECT_EQ(&obj1, map.Lookup(2));
59 EXPECT_EQ(0, map.iteration_depth());
62 TEST(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) {
63 IDMap<TestObject> map;
65 TestObject obj1;
66 TestObject obj2;
67 TestObject obj3;
69 map.Add(&obj1);
70 map.Add(&obj2);
71 map.Add(&obj3);
74 IDMap<TestObject>::const_iterator iter(&map);
76 EXPECT_EQ(1, map.iteration_depth());
78 while (!iter.IsAtEnd()) {
79 map.Remove(iter.GetCurrentKey());
80 iter.Advance();
83 // Test that while an iterator is still in scope, we get the map emptiness
84 // right (http://crbug.com/35571).
85 EXPECT_TRUE(map.IsEmpty());
86 EXPECT_EQ(0U, map.size());
89 EXPECT_TRUE(map.IsEmpty());
90 EXPECT_EQ(0U, map.size());
92 EXPECT_EQ(0, map.iteration_depth());
95 TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) {
96 IDMap<TestObject> map;
98 const int kCount = 5;
99 TestObject obj[kCount];
101 for (int i = 0; i < kCount; i++)
102 map.Add(&obj[i]);
104 // IDMap uses a hash_map, which has no predictable iteration order.
105 int32 ids_in_iteration_order[kCount];
106 const TestObject* objs_in_iteration_order[kCount];
107 int counter = 0;
108 for (IDMap<TestObject>::const_iterator iter(&map);
109 !iter.IsAtEnd(); iter.Advance()) {
110 ids_in_iteration_order[counter] = iter.GetCurrentKey();
111 objs_in_iteration_order[counter] = iter.GetCurrentValue();
112 counter++;
115 counter = 0;
116 for (IDMap<TestObject>::const_iterator iter(&map);
117 !iter.IsAtEnd(); iter.Advance()) {
118 EXPECT_EQ(1, map.iteration_depth());
120 switch (counter) {
121 case 0:
122 EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey());
123 EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue());
124 map.Remove(ids_in_iteration_order[1]);
125 break;
126 case 1:
127 EXPECT_EQ(ids_in_iteration_order[2], iter.GetCurrentKey());
128 EXPECT_EQ(objs_in_iteration_order[2], iter.GetCurrentValue());
129 map.Remove(ids_in_iteration_order[3]);
130 break;
131 case 2:
132 EXPECT_EQ(ids_in_iteration_order[4], iter.GetCurrentKey());
133 EXPECT_EQ(objs_in_iteration_order[4], iter.GetCurrentValue());
134 map.Remove(ids_in_iteration_order[0]);
135 break;
136 default:
137 FAIL() << "should not have that many elements";
138 break;
141 counter++;
144 EXPECT_EQ(0, map.iteration_depth());
147 TEST(IDMapTest, CopyIterator) {
148 IDMap<TestObject> map;
150 TestObject obj1;
151 TestObject obj2;
152 TestObject obj3;
154 map.Add(&obj1);
155 map.Add(&obj2);
156 map.Add(&obj3);
158 EXPECT_EQ(0, map.iteration_depth());
161 IDMap<TestObject>::const_iterator iter1(&map);
162 EXPECT_EQ(1, map.iteration_depth());
164 // Make sure that copying the iterator correctly increments
165 // map's iteration depth.
166 IDMap<TestObject>::const_iterator iter2(iter1);
167 EXPECT_EQ(2, map.iteration_depth());
170 // Make sure after destroying all iterators the map's iteration depth
171 // returns to initial state.
172 EXPECT_EQ(0, map.iteration_depth());
175 TEST(IDMapTest, AssignIterator) {
176 IDMap<TestObject> map;
178 TestObject obj1;
179 TestObject obj2;
180 TestObject obj3;
182 map.Add(&obj1);
183 map.Add(&obj2);
184 map.Add(&obj3);
186 EXPECT_EQ(0, map.iteration_depth());
189 IDMap<TestObject>::const_iterator iter1(&map);
190 EXPECT_EQ(1, map.iteration_depth());
192 IDMap<TestObject>::const_iterator iter2(&map);
193 EXPECT_EQ(2, map.iteration_depth());
195 // Make sure that assigning the iterator correctly updates
196 // map's iteration depth (-1 for destruction, +1 for assignment).
197 EXPECT_EQ(2, map.iteration_depth());
200 // Make sure after destroying all iterators the map's iteration depth
201 // returns to initial state.
202 EXPECT_EQ(0, map.iteration_depth());
205 TEST(IDMapTest, IteratorRemainsValidWhenClearing) {
206 IDMap<TestObject> map;
208 const int kCount = 5;
209 TestObject obj[kCount];
211 for (int i = 0; i < kCount; i++)
212 map.Add(&obj[i]);
214 // IDMap uses a hash_map, which has no predictable iteration order.
215 int32 ids_in_iteration_order[kCount];
216 const TestObject* objs_in_iteration_order[kCount];
217 int counter = 0;
218 for (IDMap<TestObject>::const_iterator iter(&map);
219 !iter.IsAtEnd(); iter.Advance()) {
220 ids_in_iteration_order[counter] = iter.GetCurrentKey();
221 objs_in_iteration_order[counter] = iter.GetCurrentValue();
222 counter++;
225 counter = 0;
226 for (IDMap<TestObject>::const_iterator iter(&map);
227 !iter.IsAtEnd(); iter.Advance()) {
228 switch (counter) {
229 case 0:
230 EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey());
231 EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue());
232 break;
233 case 1:
234 EXPECT_EQ(ids_in_iteration_order[1], iter.GetCurrentKey());
235 EXPECT_EQ(objs_in_iteration_order[1], iter.GetCurrentValue());
236 map.Clear();
237 EXPECT_TRUE(map.IsEmpty());
238 EXPECT_EQ(0U, map.size());
239 break;
240 default:
241 FAIL() << "should not have that many elements";
242 break;
244 counter++;
247 EXPECT_TRUE(map.IsEmpty());
248 EXPECT_EQ(0U, map.size());
251 TEST(IDMapTest, OwningPointersDeletesThemOnRemove) {
252 const int kCount = 3;
254 int external_del_count = 0;
255 DestructorCounter* external_obj[kCount];
256 int map_external_ids[kCount];
258 int owned_del_count = 0;
259 DestructorCounter* owned_obj[kCount];
260 int map_owned_ids[kCount];
262 IDMap<DestructorCounter> map_external;
263 IDMap<DestructorCounter, IDMapOwnPointer> map_owned;
265 for (int i = 0; i < kCount; ++i) {
266 external_obj[i] = new DestructorCounter(&external_del_count);
267 map_external_ids[i] = map_external.Add(external_obj[i]);
269 owned_obj[i] = new DestructorCounter(&owned_del_count);
270 map_owned_ids[i] = map_owned.Add(owned_obj[i]);
273 for (int i = 0; i < kCount; ++i) {
274 EXPECT_EQ(external_del_count, 0);
275 EXPECT_EQ(owned_del_count, i);
277 map_external.Remove(map_external_ids[i]);
278 map_owned.Remove(map_owned_ids[i]);
281 for (int i = 0; i < kCount; ++i) {
282 delete external_obj[i];
285 EXPECT_EQ(external_del_count, kCount);
286 EXPECT_EQ(owned_del_count, kCount);
289 TEST(IDMapTest, OwningPointersDeletesThemOnClear) {
290 const int kCount = 3;
292 int external_del_count = 0;
293 DestructorCounter* external_obj[kCount];
295 int owned_del_count = 0;
296 DestructorCounter* owned_obj[kCount];
298 IDMap<DestructorCounter> map_external;
299 IDMap<DestructorCounter, IDMapOwnPointer> map_owned;
301 for (int i = 0; i < kCount; ++i) {
302 external_obj[i] = new DestructorCounter(&external_del_count);
303 map_external.Add(external_obj[i]);
305 owned_obj[i] = new DestructorCounter(&owned_del_count);
306 map_owned.Add(owned_obj[i]);
309 EXPECT_EQ(external_del_count, 0);
310 EXPECT_EQ(owned_del_count, 0);
312 map_external.Clear();
313 map_owned.Clear();
315 EXPECT_EQ(external_del_count, 0);
316 EXPECT_EQ(owned_del_count, kCount);
318 for (int i = 0; i < kCount; ++i) {
319 delete external_obj[i];
322 EXPECT_EQ(external_del_count, kCount);
323 EXPECT_EQ(owned_del_count, kCount);
326 TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) {
327 const int kCount = 3;
329 int external_del_count = 0;
330 DestructorCounter* external_obj[kCount];
332 int owned_del_count = 0;
333 DestructorCounter* owned_obj[kCount];
336 IDMap<DestructorCounter> map_external;
337 IDMap<DestructorCounter, IDMapOwnPointer> map_owned;
339 for (int i = 0; i < kCount; ++i) {
340 external_obj[i] = new DestructorCounter(&external_del_count);
341 map_external.Add(external_obj[i]);
343 owned_obj[i] = new DestructorCounter(&owned_del_count);
344 map_owned.Add(owned_obj[i]);
348 EXPECT_EQ(external_del_count, 0);
350 for (int i = 0; i < kCount; ++i) {
351 delete external_obj[i];
354 EXPECT_EQ(external_del_count, kCount);
355 EXPECT_EQ(owned_del_count, kCount);
358 } // namespace