| OLD | NEW |
| 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 <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 | 634 |
| 635 // Upcast with Pass() works. | 635 // Upcast with Pass() works. |
| 636 scoped_ptr<Super> super1 = sub1.Pass(); | 636 scoped_ptr<Super> super1 = sub1.Pass(); |
| 637 super1 = sub2.Pass(); | 637 super1 = sub2.Pass(); |
| 638 | 638 |
| 639 // Upcast with an rvalue works. | 639 // Upcast with an rvalue works. |
| 640 scoped_ptr<Super> super2 = SubClassReturn(); | 640 scoped_ptr<Super> super2 = SubClassReturn(); |
| 641 super2 = SubClassReturn(); | 641 super2 = SubClassReturn(); |
| 642 } | 642 } |
| 643 | 643 |
| 644 // Android death tests don't work properly with assert(). Yay. | |
| 645 #if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) | |
| 646 TEST(ScopedPtrTest, SelfResetAbortsWithDefaultDeleter) { | |
| 647 scoped_ptr<int> x(new int); | |
| 648 EXPECT_DEATH(x.reset(x.get()), ""); | |
| 649 } | |
| 650 | |
| 651 TEST(ScopedPtrTest, SelfResetAbortsWithDefaultArrayDeleter) { | |
| 652 scoped_ptr<int[]> y(new int[4]); | |
| 653 EXPECT_DEATH(y.reset(y.get()), ""); | |
| 654 } | |
| 655 | |
| 656 TEST(ScopedPtrTest, SelfResetAbortsWithDefaultFreeDeleter) { | |
| 657 scoped_ptr<int, base::FreeDeleter> z(static_cast<int*>(malloc(sizeof(int)))); | |
| 658 EXPECT_DEATH(z.reset(z.get()), ""); | |
| 659 } | |
| 660 | |
| 661 // A custom deleter that doesn't opt out should still crash. | |
| 662 TEST(ScopedPtrTest, SelfResetAbortsWithCustomDeleter) { | |
| 663 struct CustomDeleter { | |
| 664 inline void operator()(int* x) { delete x; } | |
| 665 }; | |
| 666 scoped_ptr<int, CustomDeleter> x(new int); | |
| 667 EXPECT_DEATH(x.reset(x.get()), ""); | |
| 668 } | |
| 669 #endif | |
| 670 | |
| 671 TEST(ScopedPtrTest, SelfResetWithCustomDeleterOptOut) { | |
| 672 // A custom deleter should be able to opt out of self-reset abort behavior. | |
| 673 struct NoOpDeleter { | |
| 674 #if !defined(NDEBUG) | |
| 675 typedef void AllowSelfReset; | |
| 676 #endif | |
| 677 inline void operator()(int*) {} | |
| 678 }; | |
| 679 scoped_ptr<int> owner(new int); | |
| 680 scoped_ptr<int, NoOpDeleter> x(owner.get()); | |
| 681 x.reset(x.get()); | |
| 682 } | |
| 683 | |
| 684 // Logging a scoped_ptr<T> to an ostream shouldn't convert it to a boolean | 644 // Logging a scoped_ptr<T> to an ostream shouldn't convert it to a boolean |
| 685 // value first. | 645 // value first. |
| 686 TEST(ScopedPtrTest, LoggingDoesntConvertToBoolean) { | 646 TEST(ScopedPtrTest, LoggingDoesntConvertToBoolean) { |
| 687 scoped_ptr<int> x(new int); | 647 scoped_ptr<int> x(new int); |
| 688 std::stringstream s1; | 648 std::stringstream s1; |
| 689 s1 << x; | 649 s1 << x; |
| 690 | 650 |
| 691 std::stringstream s2; | 651 std::stringstream s2; |
| 692 s2 << x.get(); | 652 s2 << x.get(); |
| 693 | 653 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 719 // Go again, but this time, break the cycle by invoking |a|'s destructor. This | 679 // Go again, but this time, break the cycle by invoking |a|'s destructor. This |
| 720 // tests that the implementation of ~scoped_ptr doesn't infinitely recurse | 680 // tests that the implementation of ~scoped_ptr doesn't infinitely recurse |
| 721 // into the destructors of |a| and |a->b|. Note, deleting |a| instead will | 681 // into the destructors of |a| and |a->b|. Note, deleting |a| instead will |
| 722 // cause |a| to be double-free'd because |a->b| owns |a| and deletes it via | 682 // cause |a| to be double-free'd because |a->b| owns |a| and deletes it via |
| 723 // its destructor. | 683 // its destructor. |
| 724 a = new StructA; | 684 a = new StructA; |
| 725 a->b.reset(new StructB); | 685 a->b.reset(new StructB); |
| 726 a->b->a.reset(a); | 686 a->b->a.reset(a); |
| 727 a->~StructA(); | 687 a->~StructA(); |
| 728 } | 688 } |
| OLD | NEW |