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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
// Copyright (c) 2011 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 <stdint.h>
#include <memory>
#include "base/memory/ptr_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class TestObject {
};
class DestructorCounter {
public:
explicit DestructorCounter(int* counter) : counter_(counter) {}
~DestructorCounter() { ++(*counter_); }
private:
int* counter_;
};
TEST(IDMapTest, Basic) {
IDMap<TestObject*> map;
EXPECT_TRUE(map.IsEmpty());
EXPECT_EQ(0U, map.size());
TestObject obj1;
TestObject obj2;
int32_t id1 = map.Add(&obj1);
EXPECT_FALSE(map.IsEmpty());
EXPECT_EQ(1U, map.size());
EXPECT_EQ(&obj1, map.Lookup(id1));
int32_t 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));
EXPECT_EQ(&obj2, map.Replace(2, &obj1));
EXPECT_EQ(&obj1, map.Lookup(2));
EXPECT_EQ(0, map.iteration_depth());
}
TEST(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);
EXPECT_EQ(1, map.iteration_depth());
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());
EXPECT_EQ(0, map.iteration_depth());
}
TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) {
IDMap<TestObject*> map;
const int kCount = 5;
TestObject obj[kCount];
for (int i = 0; i < kCount; i++)
map.Add(&obj[i]);
// IDMap uses a hash_map, which has no predictable iteration order.
int32_t ids_in_iteration_order[kCount];
const TestObject* objs_in_iteration_order[kCount];
int counter = 0;
for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd();
iter.Advance()) {
ids_in_iteration_order[counter] = iter.GetCurrentKey();
objs_in_iteration_order[counter] = iter.GetCurrentValue();
counter++;
}
counter = 0;
for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd();
iter.Advance()) {
EXPECT_EQ(1, map.iteration_depth());
switch (counter) {
case 0:
EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey());
EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue());
map.Remove(ids_in_iteration_order[1]);
break;
case 1:
EXPECT_EQ(ids_in_iteration_order[2], iter.GetCurrentKey());
EXPECT_EQ(objs_in_iteration_order[2], iter.GetCurrentValue());
map.Remove(ids_in_iteration_order[3]);
break;
case 2:
EXPECT_EQ(ids_in_iteration_order[4], iter.GetCurrentKey());
EXPECT_EQ(objs_in_iteration_order[4], iter.GetCurrentValue());
map.Remove(ids_in_iteration_order[0]);
break;
default:
FAIL() << "should not have that many elements";
break;
}
counter++;
}
EXPECT_EQ(0, map.iteration_depth());
}
TEST(IDMapTest, CopyIterator) {
IDMap<TestObject*> map;
TestObject obj1;
TestObject obj2;
TestObject obj3;
map.Add(&obj1);
map.Add(&obj2);
map.Add(&obj3);
EXPECT_EQ(0, map.iteration_depth());
{
IDMap<TestObject*>::const_iterator iter1(&map);
EXPECT_EQ(1, map.iteration_depth());
// Make sure that copying the iterator correctly increments
// map's iteration depth.
IDMap<TestObject*>::const_iterator iter2(iter1);
EXPECT_EQ(2, map.iteration_depth());
}
// Make sure after destroying all iterators the map's iteration depth
// returns to initial state.
EXPECT_EQ(0, map.iteration_depth());
}
TEST(IDMapTest, AssignIterator) {
IDMap<TestObject*> map;
TestObject obj1;
TestObject obj2;
TestObject obj3;
map.Add(&obj1);
map.Add(&obj2);
map.Add(&obj3);
EXPECT_EQ(0, map.iteration_depth());
{
IDMap<TestObject*>::const_iterator iter1(&map);
EXPECT_EQ(1, map.iteration_depth());
IDMap<TestObject*>::const_iterator iter2(&map);
EXPECT_EQ(2, map.iteration_depth());
// Make sure that assigning the iterator correctly updates
// map's iteration depth (-1 for destruction, +1 for assignment).
EXPECT_EQ(2, map.iteration_depth());
}
// Make sure after destroying all iterators the map's iteration depth
// returns to initial state.
EXPECT_EQ(0, map.iteration_depth());
}
TEST(IDMapTest, IteratorRemainsValidWhenClearing) {
IDMap<TestObject*> map;
const int kCount = 5;
TestObject obj[kCount];
for (int i = 0; i < kCount; i++)
map.Add(&obj[i]);
// IDMap uses a hash_map, which has no predictable iteration order.
int32_t ids_in_iteration_order[kCount];
const TestObject* objs_in_iteration_order[kCount];
int counter = 0;
for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd();
iter.Advance()) {
ids_in_iteration_order[counter] = iter.GetCurrentKey();
objs_in_iteration_order[counter] = iter.GetCurrentValue();
counter++;
}
counter = 0;
for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd();
iter.Advance()) {
switch (counter) {
case 0:
EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey());
EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue());
break;
case 1:
EXPECT_EQ(ids_in_iteration_order[1], iter.GetCurrentKey());
EXPECT_EQ(objs_in_iteration_order[1], iter.GetCurrentValue());
map.Clear();
EXPECT_TRUE(map.IsEmpty());
EXPECT_EQ(0U, map.size());
break;
default:
FAIL() << "should not have that many elements";
break;
}
counter++;
}
EXPECT_TRUE(map.IsEmpty());
EXPECT_EQ(0U, map.size());
}
TEST(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;
int map_owned_ids[kCount];
IDMap<DestructorCounter*> map_external;
IDMap<std::unique_ptr<DestructorCounter>> 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]);
map_owned_ids[i] =
map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count));
}
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(IDMapTest, OwningPointersDeletesThemOnClear) {
const int kCount = 3;
int external_del_count = 0;
DestructorCounter* external_obj[kCount];
int owned_del_count = 0;
IDMap<DestructorCounter*> map_external;
IDMap<std::unique_ptr<DestructorCounter>> map_owned;
for (int i = 0; i < kCount; ++i) {
external_obj[i] = new DestructorCounter(&external_del_count);
map_external.Add(external_obj[i]);
map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count));
}
EXPECT_EQ(external_del_count, 0);
EXPECT_EQ(owned_del_count, 0);
map_external.Clear();
map_owned.Clear();
EXPECT_EQ(external_del_count, 0);
EXPECT_EQ(owned_del_count, kCount);
for (int i = 0; i < kCount; ++i) {
delete external_obj[i];
}
EXPECT_EQ(external_del_count, kCount);
EXPECT_EQ(owned_del_count, kCount);
}
TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) {
const int kCount = 3;
int external_del_count = 0;
DestructorCounter* external_obj[kCount];
int owned_del_count = 0;
{
IDMap<DestructorCounter*> map_external;
IDMap<std::unique_ptr<DestructorCounter>> map_owned;
for (int i = 0; i < kCount; ++i) {
external_obj[i] = new DestructorCounter(&external_del_count);
map_external.Add(external_obj[i]);
map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count));
}
}
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);
}
TEST(IDMapTest, Int64KeyType) {
IDMap<TestObject*, int64_t> map;
TestObject obj1;
const int64_t kId1 = 999999999999999999;
map.AddWithID(&obj1, kId1);
EXPECT_EQ(&obj1, map.Lookup(kId1));
IDMap<TestObject*, int64_t>::const_iterator iter(&map);
ASSERT_FALSE(iter.IsAtEnd());
EXPECT_EQ(kId1, iter.GetCurrentKey());
EXPECT_EQ(&obj1, iter.GetCurrentValue());
iter.Advance();
ASSERT_TRUE(iter.IsAtEnd());
map.Remove(kId1);
EXPECT_TRUE(map.IsEmpty());
}
} // namespace
|