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

Side by Side Diff: base/memory/scoped_ptr_unittest.cc

Issue 599313003: Add nullptr support to scoped_ptr. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nullptr: use-new-array-to-create-array Created 6 years, 2 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/memory/scoped_ptr.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 } 399 }
400 400
401 // Test uncaught Pass() does not leak. 401 // Test uncaught Pass() does not leak.
402 { 402 {
403 ConDecLogger* logger = new ConDecLogger(&constructed); 403 ConDecLogger* logger = new ConDecLogger(&constructed);
404 scoped_ptr<ConDecLogger> scoper(logger); 404 scoped_ptr<ConDecLogger> scoper(logger);
405 EXPECT_EQ(1, constructed); 405 EXPECT_EQ(1, constructed);
406 406
407 // Should auto-destruct logger by end of scope. 407 // Should auto-destruct logger by end of scope.
408 scoper.Pass(); 408 scoper.Pass();
409 // This differs from unique_ptr, as Pass() has side effects but std::move()
410 // does not.
409 EXPECT_FALSE(scoper.get()); 411 EXPECT_FALSE(scoper.get());
410 } 412 }
411 EXPECT_EQ(0, constructed); 413 EXPECT_EQ(0, constructed);
412 414
413 // Test that passing to function which does nothing does not leak. 415 // Test that passing to function which does nothing does not leak.
414 { 416 {
415 ConDecLogger* logger = new ConDecLogger(&constructed); 417 ConDecLogger* logger = new ConDecLogger(&constructed);
416 scoped_ptr<ConDecLogger> scoper(logger); 418 scoped_ptr<ConDecLogger> scoper(logger);
417 EXPECT_EQ(1, constructed); 419 EXPECT_EQ(1, constructed);
418 420
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 { 597 {
596 OverloadedNewAndDelete::ResetCounters(); 598 OverloadedNewAndDelete::ResetCounters();
597 scoped_ptr<OverloadedNewAndDelete> scoper(new OverloadedNewAndDelete()); 599 scoped_ptr<OverloadedNewAndDelete> scoper(new OverloadedNewAndDelete());
598 EXPECT_TRUE(scoper.get()); 600 EXPECT_TRUE(scoper.get());
599 601
600 scoped_ptr<OverloadedNewAndDelete> scoper2(scoper.Pass()); 602 scoped_ptr<OverloadedNewAndDelete> scoper2(scoper.Pass());
601 } 603 }
602 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count()); 604 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count());
603 EXPECT_EQ(1, OverloadedNewAndDelete::new_count()); 605 EXPECT_EQ(1, OverloadedNewAndDelete::new_count());
604 } 606 }
607
608 scoped_ptr<int> NullIntReturn() {
609 return nullptr;
610 }
611
612 TEST(ScopedPtrTest, Nullptr) {
613 scoped_ptr<int> scoper1(nullptr);
614 scoped_ptr<int> scoper2(new int);
615 scoper2 = nullptr;
616 scoped_ptr<int> scoper3(NullIntReturn());
617 scoped_ptr<int> scoper4 = NullIntReturn();
618 EXPECT_EQ(nullptr, scoper1.get());
619 EXPECT_EQ(nullptr, scoper2.get());
620 EXPECT_EQ(nullptr, scoper3.get());
621 EXPECT_EQ(nullptr, scoper4.get());
622 }
623
624 scoped_ptr<int[]> NullIntArrayReturn() {
625 return nullptr;
626 }
627
628 TEST(ScopedPtrTest, NullptrArray) {
629 scoped_ptr<int[]> scoper1(nullptr);
630 scoped_ptr<int[]> scoper2(new int[3]);
jamesr 2014/09/27 20:30:36 oh, that would explain it! we do seem to have pre
631 scoper2 = nullptr;
632 scoped_ptr<int[]> scoper3(NullIntArrayReturn());
633 scoped_ptr<int[]> scoper4 = NullIntArrayReturn();
634 EXPECT_EQ(nullptr, scoper1.get());
635 EXPECT_EQ(nullptr, scoper2.get());
636 EXPECT_EQ(nullptr, scoper3.get());
637 EXPECT_EQ(nullptr, scoper4.get());
638 }
639
640 class Super {};
641 class Sub : public Super {};
642
643 scoped_ptr<Sub> SubClassReturn() {
644 return make_scoped_ptr(new Sub);
645 }
646
647 TEST(ScopedPtrTest, Conversion) {
648 scoped_ptr<Sub> sub1(new Sub);
649 scoped_ptr<Sub> sub2(new Sub);
650
651 // Upcast with Pass() works.
652 scoped_ptr<Super> super1 = sub1.Pass();
653 super1 = sub2.Pass();
654
655 // Upcast with an rvalue works.
656 scoped_ptr<Super> super2 = SubClassReturn();
657 super2 = SubClassReturn();
658 }
OLDNEW
« no previous file with comments | « base/memory/scoped_ptr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698