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"
14 class DestructorCounter
{
16 explicit DestructorCounter(int* counter
) : counter_(counter
) {}
17 ~DestructorCounter() { ++(*counter_
); }
23 TEST(IDMapTest
, Basic
) {
24 IDMap
<TestObject
> map
;
25 EXPECT_TRUE(map
.IsEmpty());
26 EXPECT_EQ(0U, map
.size());
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
));
44 EXPECT_FALSE(map
.IsEmpty());
45 EXPECT_EQ(1U, map
.size());
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(0, map
.iteration_depth());
59 TEST(IDMapTest
, IteratorRemainsValidWhenRemovingCurrentElement
) {
60 IDMap
<TestObject
> map
;
71 IDMap
<TestObject
>::const_iterator
iter(&map
);
73 EXPECT_EQ(1, map
.iteration_depth());
75 while (!iter
.IsAtEnd()) {
76 map
.Remove(iter
.GetCurrentKey());
80 // Test that while an iterator is still in scope, we get the map emptiness
81 // right (http://crbug.com/35571).
82 EXPECT_TRUE(map
.IsEmpty());
83 EXPECT_EQ(0U, map
.size());
86 EXPECT_TRUE(map
.IsEmpty());
87 EXPECT_EQ(0U, map
.size());
89 EXPECT_EQ(0, map
.iteration_depth());
92 TEST(IDMapTest
, IteratorRemainsValidWhenRemovingOtherElements
) {
93 IDMap
<TestObject
> map
;
96 TestObject obj
[kCount
];
99 for (int i
= 0; i
< kCount
; i
++)
100 ids
[i
] = map
.Add(&obj
[i
]);
103 for (IDMap
<TestObject
>::const_iterator
iter(&map
);
104 !iter
.IsAtEnd(); iter
.Advance()) {
105 EXPECT_EQ(1, map
.iteration_depth());
109 EXPECT_EQ(ids
[0], iter
.GetCurrentKey());
110 EXPECT_EQ(&obj
[0], iter
.GetCurrentValue());
114 EXPECT_EQ(ids
[2], iter
.GetCurrentKey());
115 EXPECT_EQ(&obj
[2], iter
.GetCurrentValue());
119 EXPECT_EQ(ids
[4], iter
.GetCurrentKey());
120 EXPECT_EQ(&obj
[4], iter
.GetCurrentValue());
124 FAIL() << "should not have that many elements";
131 EXPECT_EQ(0, map
.iteration_depth());
134 TEST(IDMapTest
, CopyIterator
) {
135 IDMap
<TestObject
> map
;
145 EXPECT_EQ(0, map
.iteration_depth());
148 IDMap
<TestObject
>::const_iterator
iter1(&map
);
149 EXPECT_EQ(1, map
.iteration_depth());
151 // Make sure that copying the iterator correctly increments
152 // map's iteration depth.
153 IDMap
<TestObject
>::const_iterator
iter2(iter1
);
154 EXPECT_EQ(2, map
.iteration_depth());
157 // Make sure after destroying all iterators the map's iteration depth
158 // returns to initial state.
159 EXPECT_EQ(0, map
.iteration_depth());
162 TEST(IDMapTest
, AssignIterator
) {
163 IDMap
<TestObject
> map
;
173 EXPECT_EQ(0, map
.iteration_depth());
176 IDMap
<TestObject
>::const_iterator
iter1(&map
);
177 EXPECT_EQ(1, map
.iteration_depth());
179 IDMap
<TestObject
>::const_iterator
iter2(&map
);
180 EXPECT_EQ(2, map
.iteration_depth());
182 // Make sure that assigning the iterator correctly updates
183 // map's iteration depth (-1 for destruction, +1 for assignment).
184 EXPECT_EQ(2, map
.iteration_depth());
187 // Make sure after destroying all iterators the map's iteration depth
188 // returns to initial state.
189 EXPECT_EQ(0, map
.iteration_depth());
192 TEST(IDMapTest
, IteratorRemainsValidWhenClearing
) {
193 IDMap
<TestObject
> map
;
195 const int kCount
= 5;
196 TestObject obj
[kCount
];
199 for (int i
= 0; i
< kCount
; i
++)
200 ids
[i
] = map
.Add(&obj
[i
]);
203 for (IDMap
<TestObject
>::const_iterator
iter(&map
);
204 !iter
.IsAtEnd(); iter
.Advance()) {
207 EXPECT_EQ(ids
[0], iter
.GetCurrentKey());
208 EXPECT_EQ(&obj
[0], iter
.GetCurrentValue());
211 EXPECT_EQ(ids
[1], iter
.GetCurrentKey());
212 EXPECT_EQ(&obj
[1], iter
.GetCurrentValue());
214 EXPECT_TRUE(map
.IsEmpty());
215 EXPECT_EQ(0U, map
.size());
218 FAIL() << "should not have that many elements";
224 EXPECT_TRUE(map
.IsEmpty());
225 EXPECT_EQ(0U, map
.size());
228 TEST(IDMapTest
, OwningPointersDeletesThemOnRemove
) {
229 const int kCount
= 3;
231 int external_del_count
= 0;
232 DestructorCounter
* external_obj
[kCount
];
233 int map_external_ids
[kCount
];
235 int owned_del_count
= 0;
236 DestructorCounter
* owned_obj
[kCount
];
237 int map_owned_ids
[kCount
];
239 IDMap
<DestructorCounter
> map_external
;
240 IDMap
<DestructorCounter
, IDMapOwnPointer
> map_owned
;
242 for (int i
= 0; i
< kCount
; ++i
) {
243 external_obj
[i
] = new DestructorCounter(&external_del_count
);
244 map_external_ids
[i
] = map_external
.Add(external_obj
[i
]);
246 owned_obj
[i
] = new DestructorCounter(&owned_del_count
);
247 map_owned_ids
[i
] = map_owned
.Add(owned_obj
[i
]);
250 for (int i
= 0; i
< kCount
; ++i
) {
251 EXPECT_EQ(external_del_count
, 0);
252 EXPECT_EQ(owned_del_count
, i
);
254 map_external
.Remove(map_external_ids
[i
]);
255 map_owned
.Remove(map_owned_ids
[i
]);
258 for (int i
= 0; i
< kCount
; ++i
) {
259 delete external_obj
[i
];
262 EXPECT_EQ(external_del_count
, kCount
);
263 EXPECT_EQ(owned_del_count
, kCount
);
266 TEST(IDMapTest
, OwningPointersDeletesThemOnClear
) {
267 const int kCount
= 3;
269 int external_del_count
= 0;
270 DestructorCounter
* external_obj
[kCount
];
272 int owned_del_count
= 0;
273 DestructorCounter
* owned_obj
[kCount
];
275 IDMap
<DestructorCounter
> map_external
;
276 IDMap
<DestructorCounter
, IDMapOwnPointer
> map_owned
;
278 for (int i
= 0; i
< kCount
; ++i
) {
279 external_obj
[i
] = new DestructorCounter(&external_del_count
);
280 map_external
.Add(external_obj
[i
]);
282 owned_obj
[i
] = new DestructorCounter(&owned_del_count
);
283 map_owned
.Add(owned_obj
[i
]);
286 EXPECT_EQ(external_del_count
, 0);
287 EXPECT_EQ(owned_del_count
, 0);
289 map_external
.Clear();
292 EXPECT_EQ(external_del_count
, 0);
293 EXPECT_EQ(owned_del_count
, kCount
);
295 for (int i
= 0; i
< kCount
; ++i
) {
296 delete external_obj
[i
];
299 EXPECT_EQ(external_del_count
, kCount
);
300 EXPECT_EQ(owned_del_count
, kCount
);
303 TEST(IDMapTest
, OwningPointersDeletesThemOnDestruct
) {
304 const int kCount
= 3;
306 int external_del_count
= 0;
307 DestructorCounter
* external_obj
[kCount
];
309 int owned_del_count
= 0;
310 DestructorCounter
* owned_obj
[kCount
];
313 IDMap
<DestructorCounter
> map_external
;
314 IDMap
<DestructorCounter
, IDMapOwnPointer
> map_owned
;
316 for (int i
= 0; i
< kCount
; ++i
) {
317 external_obj
[i
] = new DestructorCounter(&external_del_count
);
318 map_external
.Add(external_obj
[i
]);
320 owned_obj
[i
] = new DestructorCounter(&owned_del_count
);
321 map_owned
.Add(owned_obj
[i
]);
325 EXPECT_EQ(external_del_count
, 0);
327 for (int i
= 0; i
< kCount
; ++i
) {
328 delete external_obj
[i
];
331 EXPECT_EQ(external_del_count
, kCount
);
332 EXPECT_EQ(owned_del_count
, kCount
);