Refactored not to expose raw pointers on ProxyList class.
[chromium-blink-merge.git] / base / containers / scoped_ptr_hash_map_unittest.cc
blob88fe41f9a0dfe4aa2cc020d80ac26720f6482c43
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"
10 namespace base {
11 namespace {
13 struct DeleteCounter {
14 public:
15 DeleteCounter() {}
16 ~DeleteCounter() { g_delete_count++; }
18 static void ResetCounter() { g_delete_count = 0; }
19 static int delete_count() { return g_delete_count; }
21 private:
22 static int g_delete_count;
25 int DeleteCounter::g_delete_count = 0;
27 struct CountingDeleter {
28 public:
29 inline void operator()(DeleteCounter* ptr) const {
30 g_deleter_call_count++;
31 delete ptr;
34 static int count() { return g_deleter_call_count; }
35 static void ResetCounter() { g_deleter_call_count = 0; }
37 private:
38 static int g_deleter_call_count;
41 int CountingDeleter::g_deleter_call_count = 0;
43 TEST(ScopedPtrHashMapTest, CustomDeleter) {
44 int key = 123;
46 // Test dtor.
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;
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());
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());
84 } // namespace
85 } // namespace base