Track supervised user creation with UMA
[chromium-blink-merge.git] / base / id_map_unittest.cc
blob76d7c3ed4e99eae72655c07c52fc72b6ea034c82
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(0, map.iteration_depth());
59 TEST(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) {
60 IDMap<TestObject> map;
62 TestObject obj1;
63 TestObject obj2;
64 TestObject obj3;
66 map.Add(&obj1);
67 map.Add(&obj2);
68 map.Add(&obj3);
71 IDMap<TestObject>::const_iterator iter(&map);
73 EXPECT_EQ(1, map.iteration_depth());
75 while (!iter.IsAtEnd()) {
76 map.Remove(iter.GetCurrentKey());
77 iter.Advance();
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;
95 const int kCount = 5;
96 TestObject obj[kCount];
97 int32 ids[kCount];
99 for (int i = 0; i < kCount; i++)
100 ids[i] = map.Add(&obj[i]);
102 int counter = 0;
103 for (IDMap<TestObject>::const_iterator iter(&map);
104 !iter.IsAtEnd(); iter.Advance()) {
105 EXPECT_EQ(1, map.iteration_depth());
107 switch (counter) {
108 case 0:
109 EXPECT_EQ(ids[0], iter.GetCurrentKey());
110 EXPECT_EQ(&obj[0], iter.GetCurrentValue());
111 map.Remove(ids[1]);
112 break;
113 case 1:
114 EXPECT_EQ(ids[2], iter.GetCurrentKey());
115 EXPECT_EQ(&obj[2], iter.GetCurrentValue());
116 map.Remove(ids[3]);
117 break;
118 case 2:
119 EXPECT_EQ(ids[4], iter.GetCurrentKey());
120 EXPECT_EQ(&obj[4], iter.GetCurrentValue());
121 map.Remove(ids[0]);
122 break;
123 default:
124 FAIL() << "should not have that many elements";
125 break;
128 counter++;
131 EXPECT_EQ(0, map.iteration_depth());
134 TEST(IDMapTest, CopyIterator) {
135 IDMap<TestObject> map;
137 TestObject obj1;
138 TestObject obj2;
139 TestObject obj3;
141 map.Add(&obj1);
142 map.Add(&obj2);
143 map.Add(&obj3);
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;
165 TestObject obj1;
166 TestObject obj2;
167 TestObject obj3;
169 map.Add(&obj1);
170 map.Add(&obj2);
171 map.Add(&obj3);
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];
197 int32 ids[kCount];
199 for (int i = 0; i < kCount; i++)
200 ids[i] = map.Add(&obj[i]);
202 int counter = 0;
203 for (IDMap<TestObject>::const_iterator iter(&map);
204 !iter.IsAtEnd(); iter.Advance()) {
205 switch (counter) {
206 case 0:
207 EXPECT_EQ(ids[0], iter.GetCurrentKey());
208 EXPECT_EQ(&obj[0], iter.GetCurrentValue());
209 break;
210 case 1:
211 EXPECT_EQ(ids[1], iter.GetCurrentKey());
212 EXPECT_EQ(&obj[1], iter.GetCurrentValue());
213 map.Clear();
214 EXPECT_TRUE(map.IsEmpty());
215 EXPECT_EQ(0U, map.size());
216 break;
217 default:
218 FAIL() << "should not have that many elements";
219 break;
221 counter++;
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();
290 map_owned.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);
335 } // namespace