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/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
| 9 #include "base/test/gtest_util.h" |
9 #include "base/test/opaque_ref_counted.h" | 10 #include "base/test/opaque_ref_counted.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 class SelfAssign : public base::RefCounted<SelfAssign> { | 15 class SelfAssign : public base::RefCounted<SelfAssign> { |
15 protected: | 16 protected: |
16 virtual ~SelfAssign() {} | 17 virtual ~SelfAssign() {} |
17 | 18 |
18 private: | 19 private: |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 }; | 116 }; |
116 | 117 |
117 scoped_refptr<Other> Overloaded(scoped_refptr<Other> other) { | 118 scoped_refptr<Other> Overloaded(scoped_refptr<Other> other) { |
118 return other; | 119 return other; |
119 } | 120 } |
120 | 121 |
121 scoped_refptr<SelfAssign> Overloaded(scoped_refptr<SelfAssign> self_assign) { | 122 scoped_refptr<SelfAssign> Overloaded(scoped_refptr<SelfAssign> self_assign) { |
122 return self_assign; | 123 return self_assign; |
123 } | 124 } |
124 | 125 |
| 126 class InitialRefCountIsOne : public base::RefCounted<InitialRefCountIsOne> { |
| 127 public: |
| 128 REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE(); |
| 129 |
| 130 InitialRefCountIsOne() {} |
| 131 |
| 132 private: |
| 133 friend class base::RefCounted<InitialRefCountIsOne>; |
| 134 ~InitialRefCountIsOne() {} |
| 135 }; |
125 | 136 |
126 } // end namespace | 137 } // end namespace |
127 | 138 |
128 TEST(RefCountedUnitTest, TestSelfAssignment) { | 139 TEST(RefCountedUnitTest, TestSelfAssignment) { |
129 SelfAssign* p = new SelfAssign; | 140 SelfAssign* p = new SelfAssign; |
130 scoped_refptr<SelfAssign> var(p); | 141 scoped_refptr<SelfAssign> var(p); |
131 var = var; | 142 var = var; |
132 EXPECT_EQ(var.get(), p); | 143 EXPECT_EQ(var.get(), p); |
133 } | 144 } |
134 | 145 |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 | 532 |
522 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { | 533 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { |
523 scoped_refptr<Derived> derived(new Derived); | 534 scoped_refptr<Derived> derived(new Derived); |
524 scoped_refptr<SelfAssign> expected(derived); | 535 scoped_refptr<SelfAssign> expected(derived); |
525 EXPECT_EQ(expected, Overloaded(std::move(derived))); | 536 EXPECT_EQ(expected, Overloaded(std::move(derived))); |
526 | 537 |
527 scoped_refptr<Other> other(new Other); | 538 scoped_refptr<Other> other(new Other); |
528 scoped_refptr<Other> other2(other); | 539 scoped_refptr<Other> other2(other); |
529 EXPECT_EQ(other2, Overloaded(std::move(other))); | 540 EXPECT_EQ(other2, Overloaded(std::move(other))); |
530 } | 541 } |
| 542 |
| 543 TEST(RefCountedUnitTest, TestInitialRefCountIsOne) { |
| 544 scoped_refptr<InitialRefCountIsOne> obj = |
| 545 base::MakeShared<InitialRefCountIsOne>(); |
| 546 EXPECT_TRUE(obj->HasOneRef()); |
| 547 obj = nullptr; |
| 548 |
| 549 scoped_refptr<InitialRefCountIsOne> obj2 = |
| 550 base::AdoptRef(new InitialRefCountIsOne); |
| 551 EXPECT_TRUE(obj2->HasOneRef()); |
| 552 obj2 = nullptr; |
| 553 |
| 554 scoped_refptr<Other> obj3 = base::MakeShared<Other>(); |
| 555 EXPECT_TRUE(obj3->HasOneRef()); |
| 556 obj3 = nullptr; |
| 557 } |
| 558 |
| 559 TEST(RefCountedDeathTest, TestAdoptRef) { |
| 560 EXPECT_DCHECK_DEATH(make_scoped_refptr(new InitialRefCountIsOne)); |
| 561 |
| 562 InitialRefCountIsOne* ptr = nullptr; |
| 563 EXPECT_DCHECK_DEATH(base::AdoptRef(ptr)); |
| 564 |
| 565 scoped_refptr<InitialRefCountIsOne> obj = |
| 566 base::MakeShared<InitialRefCountIsOne>(); |
| 567 EXPECT_DCHECK_DEATH(base::AdoptRef(obj.get())); |
| 568 } |
OLD | NEW |