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 <memory> |
7 #include <sstream> | 8 #include <sstream> |
| 9 #include <utility> |
8 | 10 |
9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
10 #include "base/bind.h" | 12 #include "base/bind.h" |
11 #include "base/callback.h" | 13 #include "base/callback.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 // Used to test depth subtyping. | 18 // Used to test depth subtyping. |
17 class ConDecLoggerParent { | 19 class ConDecLoggerParent { |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 // Go again, but this time, break the cycle by invoking |a|'s destructor. This | 721 // 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 | 722 // 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 | 723 // 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 | 724 // cause |a| to be double-free'd because |a->b| owns |a| and deletes it via |
723 // its destructor. | 725 // its destructor. |
724 a = new StructA; | 726 a = new StructA; |
725 a->b.reset(new StructB); | 727 a->b.reset(new StructB); |
726 a->b->a.reset(a); | 728 a->b->a.reset(a); |
727 a->~StructA(); | 729 a->~StructA(); |
728 } | 730 } |
| 731 |
| 732 TEST(ScopedPtrTest, UniquePtrToScopedPtr) { |
| 733 { |
| 734 std::unique_ptr<int> u(new int(8)); |
| 735 scoped_ptr<int> s(std::move(u)); |
| 736 EXPECT_EQ(8, *s); |
| 737 } |
| 738 |
| 739 { |
| 740 std::unique_ptr<int> u(new int(8)); |
| 741 scoped_ptr<int> s; |
| 742 s = std::move(u); |
| 743 EXPECT_EQ(8, *s); |
| 744 } |
| 745 } |
| 746 |
| 747 TEST(ScopedPtrTest, UniquePtrToScopedPtrWithCustomDeleter) { |
| 748 double dummy, dummy2; |
| 749 int deletes = 0; |
| 750 |
| 751 { |
| 752 std::unique_ptr<double, CountingDeleterChild> u( |
| 753 &dummy, CountingDeleterChild(&deletes)); |
| 754 scoped_ptr<double, CountingDeleter> s(std::move(u)); |
| 755 EXPECT_EQ(&dummy, s.get()); |
| 756 } |
| 757 EXPECT_EQ(1, deletes); |
| 758 |
| 759 int deletes2 = 0; |
| 760 { |
| 761 std::unique_ptr<double, CountingDeleterChild> u( |
| 762 &dummy, CountingDeleterChild(&deletes)); |
| 763 scoped_ptr<double, CountingDeleter> s(&dummy2, CountingDeleter(&deletes2)); |
| 764 s = std::move(u); |
| 765 EXPECT_EQ(&dummy, s.get()); |
| 766 } |
| 767 EXPECT_EQ(2, deletes); |
| 768 EXPECT_EQ(1, deletes2); |
| 769 } |
| 770 |
| 771 TEST(ScopedPtrTest, ScopedPtrToUniquePtr) { |
| 772 { |
| 773 scoped_ptr<int> s(new int(9)); |
| 774 std::unique_ptr<int> u(std::move(s)); |
| 775 EXPECT_EQ(9, *u); |
| 776 } |
| 777 |
| 778 { |
| 779 scoped_ptr<int> s(new int(9)); |
| 780 std::unique_ptr<int> u; |
| 781 u = std::move(s); |
| 782 EXPECT_EQ(9, *u); |
| 783 } |
| 784 } |
| 785 |
| 786 TEST(ScopedPtrTest, ScopedPtrToUniquePtrWithCustomDeleter) { |
| 787 double dummy, dummy2; |
| 788 int deletes = 0; |
| 789 |
| 790 { |
| 791 scoped_ptr<double, CountingDeleterChild> s(&dummy, |
| 792 CountingDeleterChild(&deletes)); |
| 793 std::unique_ptr<double, CountingDeleter> u(std::move(s)); |
| 794 EXPECT_EQ(&dummy, u.get()); |
| 795 } |
| 796 EXPECT_EQ(1, deletes); |
| 797 |
| 798 int deletes2 = 0; |
| 799 { |
| 800 scoped_ptr<double, CountingDeleterChild> s(&dummy, |
| 801 CountingDeleterChild(&deletes)); |
| 802 std::unique_ptr<double, CountingDeleter> u(&dummy2, |
| 803 CountingDeleter(&deletes2)); |
| 804 u = std::move(s); |
| 805 EXPECT_EQ(&dummy, u.get()); |
| 806 } |
| 807 EXPECT_EQ(2, deletes); |
| 808 EXPECT_EQ(1, deletes2); |
| 809 } |
OLD | NEW |