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.
5 #include "base/containers/scoped_ptr_hash_map.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 struct DeleteCounter
{
16 ~DeleteCounter() { g_delete_count
++; }
18 static void ResetCounter() { g_delete_count
= 0; }
19 static int delete_count() { return g_delete_count
; }
22 static int g_delete_count
;
25 int DeleteCounter::g_delete_count
= 0;
27 struct CountingDeleter
{
29 inline void operator()(DeleteCounter
* ptr
) const {
30 g_deleter_call_count
++;
34 static int count() { return g_deleter_call_count
; }
35 static void ResetCounter() { g_deleter_call_count
= 0; }
38 static int g_deleter_call_count
;
41 int CountingDeleter::g_deleter_call_count
= 0;
43 TEST(ScopedPtrHashMapTest
, CustomDeleter
) {
47 DeleteCounter::ResetCounter();
48 CountingDeleter::ResetCounter();
50 ScopedPtrHashMap
<int, scoped_ptr
<DeleteCounter
, CountingDeleter
>> map
;
51 map
.set(key
, scoped_ptr
<DeleteCounter
, CountingDeleter
>(new DeleteCounter
));
53 EXPECT_EQ(1, DeleteCounter::delete_count());
54 EXPECT_EQ(1, CountingDeleter::count());
56 // Test set and erase.
57 DeleteCounter::ResetCounter();
58 CountingDeleter::ResetCounter();
60 ScopedPtrHashMap
<int, scoped_ptr
<DeleteCounter
, CountingDeleter
>> map
;
62 key
, scoped_ptr
<DeleteCounter
, CountingDeleter
>(new DeleteCounter
)));
63 EXPECT_EQ(1, DeleteCounter::delete_count());
64 EXPECT_EQ(1, CountingDeleter::count());
66 EXPECT_EQ(1, DeleteCounter::delete_count());
67 EXPECT_EQ(1, CountingDeleter::count());
69 // Test set more than once.
70 DeleteCounter::ResetCounter();
71 CountingDeleter::ResetCounter();
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());
80 EXPECT_EQ(3, DeleteCounter::delete_count());
81 EXPECT_EQ(3, CountingDeleter::count());