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

Unified Diff: base/containers/scoped_ptr_hash_map_unittest.cc

Issue 1099383002: Change ScopedPtrHashMap's 2nd template parameter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: custom deleter Created 5 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/containers/scoped_ptr_hash_map.h ('k') | cc/output/direct_renderer.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/containers/scoped_ptr_hash_map_unittest.cc
diff --git a/base/containers/scoped_ptr_hash_map_unittest.cc b/base/containers/scoped_ptr_hash_map_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9833e6ff1847640e18d1ceb1699e1dcf8baf203b
--- /dev/null
+++ b/base/containers/scoped_ptr_hash_map_unittest.cc
@@ -0,0 +1,83 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/containers/scoped_ptr_hash_map.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
danakj 2015/04/27 17:52:00 sorry, but wrap this in namespace base { outside,
kcwu 2015/04/27 17:56:20 Done.
+
+struct DeleteCounter {
+ public:
+ DeleteCounter() {}
+ ~DeleteCounter() { g_delete_count++; }
+
+ static void ResetCounter() { g_delete_count = 0; }
+ static int delete_count() { return g_delete_count; }
+
+ private:
+ static int g_delete_count;
+};
+
+int DeleteCounter::g_delete_count = 0;
+
+struct CountingDeleter {
+ public:
+ inline void operator()(DeleteCounter* ptr) const {
+ g_deleter_call_count++;
+ delete ptr;
+ }
+
+ static int count() { return g_deleter_call_count; }
+ static void ResetCounter() { g_deleter_call_count = 0; }
+
+ private:
+ static int g_deleter_call_count;
+};
+
+int CountingDeleter::g_deleter_call_count = 0;
+
+TEST(ScopedPtrHashMapTest, CustomDeleter) {
+ int key = 123;
+
+ // Test dtor.
+ DeleteCounter::ResetCounter();
+ CountingDeleter::ResetCounter();
+ {
+ base::ScopedPtrHashMap<int, scoped_ptr<DeleteCounter, CountingDeleter>> map;
+ map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter));
+ }
+ EXPECT_EQ(1, DeleteCounter::delete_count());
+ EXPECT_EQ(1, CountingDeleter::count());
+
+ // Test set and erase.
+ DeleteCounter::ResetCounter();
+ CountingDeleter::ResetCounter();
+ {
+ base::ScopedPtrHashMap<int, scoped_ptr<DeleteCounter, CountingDeleter>> map;
+ map.erase(map.set(
+ key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter)));
+ EXPECT_EQ(1, DeleteCounter::delete_count());
+ EXPECT_EQ(1, CountingDeleter::count());
+ }
+ EXPECT_EQ(1, DeleteCounter::delete_count());
+ EXPECT_EQ(1, CountingDeleter::count());
+
+ // Test set more than once.
+ DeleteCounter::ResetCounter();
+ CountingDeleter::ResetCounter();
+ {
+ base::ScopedPtrHashMap<int, scoped_ptr<DeleteCounter, CountingDeleter>> map;
+ map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter));
+ map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter));
+ map.set(key, scoped_ptr<DeleteCounter, CountingDeleter>(new DeleteCounter));
+ EXPECT_EQ(2, DeleteCounter::delete_count());
+ EXPECT_EQ(2, CountingDeleter::count());
+ }
+ EXPECT_EQ(3, DeleteCounter::delete_count());
+ EXPECT_EQ(3, CountingDeleter::count());
+}
+
+} // namespace
« no previous file with comments | « base/containers/scoped_ptr_hash_map.h ('k') | cc/output/direct_renderer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698