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

Unified Diff: base/memory/scoped_ptr_unittest.cc

Issue 9021032: Redo r113722 - Add Pass(), which implements move semantics, to scoped_ptr, scoped_array.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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/memory/scoped_ptr.h ('k') | chrome/common/extensions/extension_manifests_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/scoped_ptr_unittest.cc
diff --git a/base/memory/scoped_ptr_unittest.cc b/base/memory/scoped_ptr_unittest.cc
index 75190519402788798c5ba091883583182292e466..9a37cbfdcb61af099865af7f0283692933e77a52 100644
--- a/base/memory/scoped_ptr_unittest.cc
+++ b/base/memory/scoped_ptr_unittest.cc
@@ -23,6 +23,19 @@ class ConDecLogger {
DISALLOW_COPY_AND_ASSIGN(ConDecLogger);
};
+scoped_ptr<ConDecLogger> PassThru(scoped_ptr<ConDecLogger> logger) {
+ return logger.Pass();
+}
+
+void GrabAndDrop(scoped_ptr<ConDecLogger> logger) {
+}
+
+// Do not delete this function! It's existence is to test that you can
+// return a temporarily constructed version of the scoper.
+scoped_ptr<ConDecLogger> TestReturnOfType(int* constructed) {
+ return scoped_ptr<ConDecLogger>(new ConDecLogger(constructed));
+}
+
} // namespace
TEST(ScopedPtrTest, ScopedPtr) {
@@ -166,4 +179,83 @@ TEST(ScopedPtrTest, ScopedArray) {
EXPECT_EQ(0, constructed);
}
+TEST(ScopedPtrTest, PassBehavior) {
+ int constructed = 0;
+ {
+ ConDecLogger* logger = new ConDecLogger(&constructed);
+ scoped_ptr<ConDecLogger> scoper(logger);
+ EXPECT_EQ(1, constructed);
+
+ // Test Pass() with constructor;
+ scoped_ptr<ConDecLogger> scoper2(scoper.Pass());
+ EXPECT_EQ(1, constructed);
+
+ // Test Pass() with assignment;
+ scoped_ptr<ConDecLogger> scoper3;
+ scoper3 = scoper2.Pass();
+ EXPECT_EQ(1, constructed);
+ EXPECT_FALSE(scoper.get());
+ EXPECT_FALSE(scoper2.get());
+ EXPECT_TRUE(scoper3.get());
+ }
+
+ // Test uncaught Pass() does not leak.
+ {
+ ConDecLogger* logger = new ConDecLogger(&constructed);
+ scoped_ptr<ConDecLogger> scoper(logger);
+ EXPECT_EQ(1, constructed);
+
+ // Should auto-destruct logger by end of scope.
+ scoper.Pass();
+ EXPECT_FALSE(scoper.get());
+ }
+ EXPECT_EQ(0, constructed);
+
+ // Test that passing to function which does nothing does not leak.
+ {
+ ConDecLogger* logger = new ConDecLogger(&constructed);
+ scoped_ptr<ConDecLogger> scoper(logger);
+ EXPECT_EQ(1, constructed);
+
+ // Should auto-destruct logger by end of scope.
+ GrabAndDrop(scoper.Pass());
+ EXPECT_FALSE(scoper.get());
+ }
+ EXPECT_EQ(0, constructed);
+}
+
+TEST(ScopedPtrTest, ReturnTypeBehavior) {
+ int constructed = 0;
+
+ // Test that we can return a scoped_ptr.
+ {
+ ConDecLogger* logger = new ConDecLogger(&constructed);
+ scoped_ptr<ConDecLogger> scoper(logger);
+ EXPECT_EQ(1, constructed);
+
+ PassThru(scoper.Pass());
+ EXPECT_FALSE(scoper.get());
+ }
+ EXPECT_EQ(0, constructed);
+
+ // Test uncaught return type not leak.
+ {
+ ConDecLogger* logger = new ConDecLogger(&constructed);
+ scoped_ptr<ConDecLogger> scoper(logger);
+ EXPECT_EQ(1, constructed);
+
+ // Should auto-destruct logger by end of scope.
+ PassThru(scoper.Pass());
+ EXPECT_FALSE(scoper.get());
+ }
+ EXPECT_EQ(0, constructed);
+
+ // Call TestReturnOfType() so the compiler doesn't warn for an unused
+ // function.
+ {
+ TestReturnOfType(&constructed);
+ }
+ EXPECT_EQ(0, constructed);
+}
+
// TODO scoped_ptr_malloc
« no previous file with comments | « base/memory/scoped_ptr.h ('k') | chrome/common/extensions/extension_manifests_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698