| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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. | |
| 4 | |
| 5 #include "base/containers/scoped_ptr_hash_map.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace base { | |
| 11 namespace { | |
| 12 | |
| 13 struct DeleteCounter { | |
| 14 public: | |
| 15 DeleteCounter() {} | |
| 16 ~DeleteCounter() { g_delete_count++; } | |
| 17 | |
| 18 static void ResetCounter() { g_delete_count = 0; } | |
| 19 static int delete_count() { return g_delete_count; } | |
| 20 | |
| 21 private: | |
| 22 static int g_delete_count; | |
| 23 }; | |
| 24 | |
| 25 int DeleteCounter::g_delete_count = 0; | |
| 26 | |
| 27 struct CountingDeleter { | |
| 28 public: | |
| 29 inline void operator()(DeleteCounter* ptr) const { | |
| 30 g_deleter_call_count++; | |
| 31 delete ptr; | |
| 32 } | |
| 33 | |
| 34 static int count() { return g_deleter_call_count; } | |
| 35 static void ResetCounter() { g_deleter_call_count = 0; } | |
| 36 | |
| 37 private: | |
| 38 static int g_deleter_call_count; | |
| 39 }; | |
| 40 | |
| 41 int CountingDeleter::g_deleter_call_count = 0; | |
| 42 | |
| 43 TEST(ScopedPtrHashMapTest, CustomDeleter) { | |
| 44 int key = 123; | |
| 45 | |
| 46 // Test dtor. | |
| 47 DeleteCounter::ResetCounter(); | |
| 48 CountingDeleter::ResetCounter(); | |
| 49 { | |
| 50 ScopedPtrHashMap<int, scoped_ptr<DeleteCounter, CountingDeleter>> map; | |
| 51 map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter)); | |
| 52 } | |
| 53 EXPECT_EQ(1, DeleteCounter::delete_count()); | |
| 54 EXPECT_EQ(1, CountingDeleter::count()); | |
| 55 | |
| 56 // Test set and erase. | |
| 57 DeleteCounter::ResetCounter(); | |
| 58 CountingDeleter::ResetCounter(); | |
| 59 { | |
| 60 ScopedPtrHashMap<int, scoped_ptr<DeleteCounter, CountingDeleter>> map; | |
| 61 map.erase(map.set( | |
| 62 key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter))); | |
| 63 EXPECT_EQ(1, DeleteCounter::delete_count()); | |
| 64 EXPECT_EQ(1, CountingDeleter::count()); | |
| 65 } | |
| 66 EXPECT_EQ(1, DeleteCounter::delete_count()); | |
| 67 EXPECT_EQ(1, CountingDeleter::count()); | |
| 68 | |
| 69 // Test set more than once. | |
| 70 DeleteCounter::ResetCounter(); | |
| 71 CountingDeleter::ResetCounter(); | |
| 72 { | |
| 73 ScopedPtrHashMap<int, scoped_ptr<DeleteCounter, CountingDeleter>> map; | |
| 74 map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter)); | |
| 75 map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter)); | |
| 76 map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter)); | |
| 77 EXPECT_EQ(2, DeleteCounter::delete_count()); | |
| 78 EXPECT_EQ(2, CountingDeleter::count()); | |
| 79 } | |
| 80 EXPECT_EQ(3, DeleteCounter::delete_count()); | |
| 81 EXPECT_EQ(3, CountingDeleter::count()); | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 } // namespace base | |
| OLD | NEW |