Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: base/containers/scoped_ptr_hash_map_unittest.cc

Issue 1128733002: Update from https://crrev.com/328418 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/containers/scoped_ptr_hash_map.h ('k') | base/critical_closure.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« no previous file with comments | « base/containers/scoped_ptr_hash_map.h ('k') | base/critical_closure.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698