1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/id_map.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class IDMapTest : public testing::Test {
};
class TestObject {
};
class DestructorCounter {
public:
explicit DestructorCounter(int* counter) : counter_(counter) {}
~DestructorCounter() { ++(*counter_); }
private:
int* counter_;
};
TEST_F(IDMapTest, Basic) {
IDMap<TestObject> map;
EXPECT_TRUE(map.IsEmpty());
EXPECT_EQ(0U, map.size());
TestObject obj1;
TestObject obj2;
int32 id1 = map.Add(&obj1);
EXPECT_FALSE(map.IsEmpty());
EXPECT_EQ(1U, map.size());
EXPECT_EQ(&obj1, map.Lookup(id1));
int32 id2 = map.Add(&obj2);
EXPECT_FALSE(map.IsEmpty());
EXPECT_EQ(2U, map.size());
EXPECT_EQ(&obj1, map.Lookup(id1));
EXPECT_EQ(&obj2, map.Lookup(id2));
map.Remove(id1);
EXPECT_FALSE(map.IsEmpty());
EXPECT_EQ(1U, map.size());
map.Remove(id2);
EXPECT_TRUE(map.IsEmpty());
EXPECT_EQ(0U, map.size());
map.AddWithID(&obj1, 1);
map.AddWithID(&obj2, 2);
EXPECT_EQ(&obj1, map.Lookup(1));
EXPECT_EQ(&obj2, map.Lookup(2));
}
TEST_F(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) {
IDMap<TestObject> map;
TestObject obj1;
TestObject obj2;
TestObject obj3;
map.Add(&obj1);
map.Add(&obj2);
map.Add(&obj3);
{
IDMap<TestObject>::const_iterator iter(&map);
while (!iter.IsAtEnd()) {
map.Remove(iter.GetCurrentKey());
iter.Advance();
}
// Test that while an iterator is still in scope, we get the map emptiness
// right (http://crbug.com/35571).
EXPECT_TRUE(map.IsEmpty());
EXPECT_EQ(0U, map.size());
}
EXPECT_TRUE(map.IsEmpty());
EXPECT_EQ(0U, map.size());
}
TEST_F(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) {
IDMap<TestObject> map;
const int kCount = 5;
TestObject obj[kCount];
int32 ids[kCount];
for (int i = 0; i < kCount; i++)
ids[i] = map.Add(&obj[i]);
int counter = 0;
for (IDMap<TestObject>::const_iterator iter(&map);
!iter.IsAtEnd(); iter.Advance()) {
switch (counter) {
case 0:
EXPECT_EQ(ids[0], iter.GetCurrentKey());
EXPECT_EQ(&obj[0], iter.GetCurrentValue());
map.Remove(ids[1]);
break;
case 1:
EXPECT_EQ(ids[2], iter.GetCurrentKey());
EXPECT_EQ(&obj[2], iter.GetCurrentValue());
map.Remove(ids[3]);
break;
case 2:
EXPECT_EQ(ids[4], iter.GetCurrentKey());
EXPECT_EQ(&obj[4], iter.GetCurrentValue());
map.Remove(ids[0]);
break;
default:
FAIL() << "should not have that many elements";
break;
}
counter++;
}
}
TEST_F(IDMapTest, OwningPointersDeletesThemOnRemove) {
const int kCount = 3;
int external_del_count = 0;
DestructorCounter* external_obj[kCount];
int map_external_ids[kCount];
int owned_del_count = 0;
DestructorCounter* owned_obj[kCount];
int map_owned_ids[kCount];
IDMap<DestructorCounter> map_external;
IDMap<DestructorCounter, IDMapOwnPointer> map_owned;
for (int i = 0; i < kCount; ++i) {
external_obj[i] = new DestructorCounter(&external_del_count);
map_external_ids[i] = map_external.Add(external_obj[i]);
owned_obj[i] = new DestructorCounter(&owned_del_count);
map_owned_ids[i] = map_owned.Add(owned_obj[i]);
}
for (int i = 0; i < kCount; ++i) {
EXPECT_EQ(external_del_count, 0);
EXPECT_EQ(owned_del_count, i);
map_external.Remove(map_external_ids[i]);
map_owned.Remove(map_owned_ids[i]);
}
for (int i = 0; i < kCount; ++i) {
delete external_obj[i];
}
EXPECT_EQ(external_del_count, kCount);
EXPECT_EQ(owned_del_count, kCount);
}
TEST_F(IDMapTest, OwningPointersDeletesThemOnDestruct) {
const int kCount = 3;
int external_del_count = 0;
DestructorCounter* external_obj[kCount];
int map_external_ids[kCount];
int owned_del_count = 0;
DestructorCounter* owned_obj[kCount];
int map_owned_ids[kCount];
{
IDMap<DestructorCounter> map_external;
IDMap<DestructorCounter, IDMapOwnPointer> map_owned;
for (int i = 0; i < kCount; ++i) {
external_obj[i] = new DestructorCounter(&external_del_count);
map_external_ids[i] = map_external.Add(external_obj[i]);
owned_obj[i] = new DestructorCounter(&owned_del_count);
map_owned_ids[i] = map_owned.Add(owned_obj[i]);
}
}
EXPECT_EQ(external_del_count, 0);
for (int i = 0; i < kCount; ++i) {
delete external_obj[i];
}
EXPECT_EQ(external_del_count, kCount);
EXPECT_EQ(owned_del_count, kCount);
}
} // namespace
|