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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 EXPECT_FALSE(p < nullptr); | 825 EXPECT_FALSE(p < nullptr); |
866 EXPECT_TRUE(nullptr < p); | 826 EXPECT_TRUE(nullptr < p); |
867 EXPECT_FALSE(pnull < nullptr); | 827 EXPECT_FALSE(pnull < nullptr); |
868 EXPECT_FALSE(nullptr < pnull); | 828 EXPECT_FALSE(nullptr < pnull); |
869 | 829 |
870 EXPECT_FALSE(p <= nullptr); | 830 EXPECT_FALSE(p <= nullptr); |
871 EXPECT_TRUE(nullptr <= p); | 831 EXPECT_TRUE(nullptr <= p); |
872 EXPECT_TRUE(pnull <= nullptr); | 832 EXPECT_TRUE(pnull <= nullptr); |
873 EXPECT_TRUE(nullptr <= pnull); | 833 EXPECT_TRUE(nullptr <= pnull); |
874 } | 834 } |
OLD | NEW |