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