| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/id_map.h" | 5 #include "base/id_map.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> |
| 10 |
| 11 #include "base/memory/ptr_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 namespace { | 14 namespace { |
| 12 | 15 |
| 13 class TestObject { | 16 class TestObject { |
| 14 }; | 17 }; |
| 15 | 18 |
| 16 class DestructorCounter { | 19 class DestructorCounter { |
| 17 public: | 20 public: |
| 18 explicit DestructorCounter(int* counter) : counter_(counter) {} | 21 explicit DestructorCounter(int* counter) : counter_(counter) {} |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 254 } |
| 252 | 255 |
| 253 TEST(IDMapTest, OwningPointersDeletesThemOnRemove) { | 256 TEST(IDMapTest, OwningPointersDeletesThemOnRemove) { |
| 254 const int kCount = 3; | 257 const int kCount = 3; |
| 255 | 258 |
| 256 int external_del_count = 0; | 259 int external_del_count = 0; |
| 257 DestructorCounter* external_obj[kCount]; | 260 DestructorCounter* external_obj[kCount]; |
| 258 int map_external_ids[kCount]; | 261 int map_external_ids[kCount]; |
| 259 | 262 |
| 260 int owned_del_count = 0; | 263 int owned_del_count = 0; |
| 261 DestructorCounter* owned_obj[kCount]; | |
| 262 int map_owned_ids[kCount]; | 264 int map_owned_ids[kCount]; |
| 263 | 265 |
| 264 IDMap<DestructorCounter> map_external; | 266 IDMap<DestructorCounter> map_external; |
| 265 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; | 267 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; |
| 266 | 268 |
| 267 for (int i = 0; i < kCount; ++i) { | 269 for (int i = 0; i < kCount; ++i) { |
| 268 external_obj[i] = new DestructorCounter(&external_del_count); | 270 external_obj[i] = new DestructorCounter(&external_del_count); |
| 269 map_external_ids[i] = map_external.Add(external_obj[i]); | 271 map_external_ids[i] = map_external.Add(external_obj[i]); |
| 270 | 272 |
| 271 owned_obj[i] = new DestructorCounter(&owned_del_count); | 273 map_owned_ids[i] = |
| 272 map_owned_ids[i] = map_owned.Add(owned_obj[i]); | 274 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
| 273 } | 275 } |
| 274 | 276 |
| 275 for (int i = 0; i < kCount; ++i) { | 277 for (int i = 0; i < kCount; ++i) { |
| 276 EXPECT_EQ(external_del_count, 0); | 278 EXPECT_EQ(external_del_count, 0); |
| 277 EXPECT_EQ(owned_del_count, i); | 279 EXPECT_EQ(owned_del_count, i); |
| 278 | 280 |
| 279 map_external.Remove(map_external_ids[i]); | 281 map_external.Remove(map_external_ids[i]); |
| 280 map_owned.Remove(map_owned_ids[i]); | 282 map_owned.Remove(map_owned_ids[i]); |
| 281 } | 283 } |
| 282 | 284 |
| 283 for (int i = 0; i < kCount; ++i) { | 285 for (int i = 0; i < kCount; ++i) { |
| 284 delete external_obj[i]; | 286 delete external_obj[i]; |
| 285 } | 287 } |
| 286 | 288 |
| 287 EXPECT_EQ(external_del_count, kCount); | 289 EXPECT_EQ(external_del_count, kCount); |
| 288 EXPECT_EQ(owned_del_count, kCount); | 290 EXPECT_EQ(owned_del_count, kCount); |
| 289 } | 291 } |
| 290 | 292 |
| 291 TEST(IDMapTest, OwningPointersDeletesThemOnClear) { | 293 TEST(IDMapTest, OwningPointersDeletesThemOnClear) { |
| 292 const int kCount = 3; | 294 const int kCount = 3; |
| 293 | 295 |
| 294 int external_del_count = 0; | 296 int external_del_count = 0; |
| 295 DestructorCounter* external_obj[kCount]; | 297 DestructorCounter* external_obj[kCount]; |
| 296 | 298 |
| 297 int owned_del_count = 0; | 299 int owned_del_count = 0; |
| 298 DestructorCounter* owned_obj[kCount]; | |
| 299 | 300 |
| 300 IDMap<DestructorCounter> map_external; | 301 IDMap<DestructorCounter> map_external; |
| 301 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; | 302 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; |
| 302 | 303 |
| 303 for (int i = 0; i < kCount; ++i) { | 304 for (int i = 0; i < kCount; ++i) { |
| 304 external_obj[i] = new DestructorCounter(&external_del_count); | 305 external_obj[i] = new DestructorCounter(&external_del_count); |
| 305 map_external.Add(external_obj[i]); | 306 map_external.Add(external_obj[i]); |
| 306 | 307 |
| 307 owned_obj[i] = new DestructorCounter(&owned_del_count); | 308 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
| 308 map_owned.Add(owned_obj[i]); | |
| 309 } | 309 } |
| 310 | 310 |
| 311 EXPECT_EQ(external_del_count, 0); | 311 EXPECT_EQ(external_del_count, 0); |
| 312 EXPECT_EQ(owned_del_count, 0); | 312 EXPECT_EQ(owned_del_count, 0); |
| 313 | 313 |
| 314 map_external.Clear(); | 314 map_external.Clear(); |
| 315 map_owned.Clear(); | 315 map_owned.Clear(); |
| 316 | 316 |
| 317 EXPECT_EQ(external_del_count, 0); | 317 EXPECT_EQ(external_del_count, 0); |
| 318 EXPECT_EQ(owned_del_count, kCount); | 318 EXPECT_EQ(owned_del_count, kCount); |
| 319 | 319 |
| 320 for (int i = 0; i < kCount; ++i) { | 320 for (int i = 0; i < kCount; ++i) { |
| 321 delete external_obj[i]; | 321 delete external_obj[i]; |
| 322 } | 322 } |
| 323 | 323 |
| 324 EXPECT_EQ(external_del_count, kCount); | 324 EXPECT_EQ(external_del_count, kCount); |
| 325 EXPECT_EQ(owned_del_count, kCount); | 325 EXPECT_EQ(owned_del_count, kCount); |
| 326 } | 326 } |
| 327 | 327 |
| 328 TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) { | 328 TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) { |
| 329 const int kCount = 3; | 329 const int kCount = 3; |
| 330 | 330 |
| 331 int external_del_count = 0; | 331 int external_del_count = 0; |
| 332 DestructorCounter* external_obj[kCount]; | 332 DestructorCounter* external_obj[kCount]; |
| 333 | 333 |
| 334 int owned_del_count = 0; | 334 int owned_del_count = 0; |
| 335 DestructorCounter* owned_obj[kCount]; | |
| 336 | 335 |
| 337 { | 336 { |
| 338 IDMap<DestructorCounter> map_external; | 337 IDMap<DestructorCounter> map_external; |
| 339 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; | 338 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; |
| 340 | 339 |
| 341 for (int i = 0; i < kCount; ++i) { | 340 for (int i = 0; i < kCount; ++i) { |
| 342 external_obj[i] = new DestructorCounter(&external_del_count); | 341 external_obj[i] = new DestructorCounter(&external_del_count); |
| 343 map_external.Add(external_obj[i]); | 342 map_external.Add(external_obj[i]); |
| 344 | 343 |
| 345 owned_obj[i] = new DestructorCounter(&owned_del_count); | 344 map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
| 346 map_owned.Add(owned_obj[i]); | |
| 347 } | 345 } |
| 348 } | 346 } |
| 349 | 347 |
| 350 EXPECT_EQ(external_del_count, 0); | 348 EXPECT_EQ(external_del_count, 0); |
| 351 | 349 |
| 352 for (int i = 0; i < kCount; ++i) { | 350 for (int i = 0; i < kCount; ++i) { |
| 353 delete external_obj[i]; | 351 delete external_obj[i]; |
| 354 } | 352 } |
| 355 | 353 |
| 356 EXPECT_EQ(external_del_count, kCount); | 354 EXPECT_EQ(external_del_count, kCount); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 370 EXPECT_EQ(kId1, iter.GetCurrentKey()); | 368 EXPECT_EQ(kId1, iter.GetCurrentKey()); |
| 371 EXPECT_EQ(&obj1, iter.GetCurrentValue()); | 369 EXPECT_EQ(&obj1, iter.GetCurrentValue()); |
| 372 iter.Advance(); | 370 iter.Advance(); |
| 373 ASSERT_TRUE(iter.IsAtEnd()); | 371 ASSERT_TRUE(iter.IsAtEnd()); |
| 374 | 372 |
| 375 map.Remove(kId1); | 373 map.Remove(kId1); |
| 376 EXPECT_TRUE(map.IsEmpty()); | 374 EXPECT_TRUE(map.IsEmpty()); |
| 377 } | 375 } |
| 378 | 376 |
| 379 } // namespace | 377 } // namespace |
| OLD | NEW |