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

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: backtothefuture 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 EXPECT_FALSE(scoper.get());
jamesr 2014/09/26 21:58:26 does this test fail now?
danakj 2014/09/26 22:00:09 Oh, this can stay now, thanks. I'm going to add a
410 } 409 }
411 EXPECT_EQ(0, constructed); 410 EXPECT_EQ(0, constructed);
412 411
413 // Test that passing to function which does nothing does not leak. 412 // Test that passing to function which does nothing does not leak.
414 { 413 {
415 ConDecLogger* logger = new ConDecLogger(&constructed); 414 ConDecLogger* logger = new ConDecLogger(&constructed);
416 scoped_ptr<ConDecLogger> scoper(logger); 415 scoped_ptr<ConDecLogger> scoper(logger);
417 EXPECT_EQ(1, constructed); 416 EXPECT_EQ(1, constructed);
418 417
419 // Should auto-destruct logger by end of scope. 418 // Should auto-destruct logger by end of scope.
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 { 594 {
596 OverloadedNewAndDelete::ResetCounters(); 595 OverloadedNewAndDelete::ResetCounters();
597 scoped_ptr<OverloadedNewAndDelete> scoper(new OverloadedNewAndDelete()); 596 scoped_ptr<OverloadedNewAndDelete> scoper(new OverloadedNewAndDelete());
598 EXPECT_TRUE(scoper.get()); 597 EXPECT_TRUE(scoper.get());
599 598
600 scoped_ptr<OverloadedNewAndDelete> scoper2(scoper.Pass()); 599 scoped_ptr<OverloadedNewAndDelete> scoper2(scoper.Pass());
601 } 600 }
602 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count()); 601 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count());
603 EXPECT_EQ(1, OverloadedNewAndDelete::new_count()); 602 EXPECT_EQ(1, OverloadedNewAndDelete::new_count());
604 } 603 }
604
605 scoped_ptr<int> NullIntReturn() {
606 return nullptr;
607 }
608
609 TEST(ScopedPtrTest, Nullptr) {
610 scoped_ptr<int> scoper1(nullptr);
611 scoped_ptr<int> scoper2(new int);
612 scoper2 = nullptr;
613 scoped_ptr<int> scoper3(NullIntReturn());
614 scoped_ptr<int> scoper4 = NullIntReturn();
615 EXPECT_EQ(nullptr, scoper1.get());
616 EXPECT_EQ(nullptr, scoper2.get());
617 EXPECT_EQ(nullptr, scoper3.get());
618 EXPECT_EQ(nullptr, scoper4.get());
619 }
620
621 scoped_ptr<int[]> NullIntArrayReturn() {
622 return nullptr;
623 }
624
625 TEST(ScopedPtrTest, NullptrArray) {
626 scoped_ptr<int[]> scoper1(nullptr);
627 scoped_ptr<int[]> scoper2(new int);
628 scoper2 = nullptr;
629 scoped_ptr<int[]> scoper3(NullIntArrayReturn());
630 scoped_ptr<int[]> scoper4 = NullIntArrayReturn();
631 EXPECT_EQ(nullptr, scoper1.get());
632 EXPECT_EQ(nullptr, scoper2.get());
633 EXPECT_EQ(nullptr, scoper3.get());
634 EXPECT_EQ(nullptr, scoper4.get());
635 }
636
637 class Super {};
638 class Sub : public Super {};
639
640 scoped_ptr<Sub> SubClassReturn() {
641 return make_scoped_ptr(new Sub);
642 }
643
644 TEST(ScopedPtrTest, Conversion) {
645 scoped_ptr<Sub> sub1(new Sub);
646 scoped_ptr<Sub> sub2(new Sub);
647
648 // Upcast with Pass() works.
649 scoped_ptr<Super> super1 = sub1.Pass();
650 super1 = sub2.Pass();
651
652 // Upcast with an rvalue works.
653 scoped_ptr<Super> super2 = SubClassReturn();
654 super2 = SubClassReturn();
655 }
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