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 static constexpr base::subtle::StartRefCountFromOneTag kRefCountPreference = | |
129 base::subtle::kStartRefCountFromOneTag; | |
tzik
2017/03/30 13:16:28
We should probably replace this with a macro.
dcheng
2017/03/30 21:15:25
Sounds good to me.
(To give some background conte
| |
130 | |
131 InitialRefCountIsOne() {} | |
132 | |
133 private: | |
134 friend class base::RefCounted<InitialRefCountIsOne>; | |
135 ~InitialRefCountIsOne() {} | |
136 }; | |
125 | 137 |
126 } // end namespace | 138 } // end namespace |
127 | 139 |
128 TEST(RefCountedUnitTest, TestSelfAssignment) { | 140 TEST(RefCountedUnitTest, TestSelfAssignment) { |
129 SelfAssign* p = new SelfAssign; | 141 SelfAssign* p = new SelfAssign; |
130 scoped_refptr<SelfAssign> var(p); | 142 scoped_refptr<SelfAssign> var(p); |
131 var = var; | 143 var = var; |
132 EXPECT_EQ(var.get(), p); | 144 EXPECT_EQ(var.get(), p); |
133 } | 145 } |
134 | 146 |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
521 | 533 |
522 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { | 534 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { |
523 scoped_refptr<Derived> derived(new Derived); | 535 scoped_refptr<Derived> derived(new Derived); |
524 scoped_refptr<SelfAssign> expected(derived); | 536 scoped_refptr<SelfAssign> expected(derived); |
525 EXPECT_EQ(expected, Overloaded(std::move(derived))); | 537 EXPECT_EQ(expected, Overloaded(std::move(derived))); |
526 | 538 |
527 scoped_refptr<Other> other(new Other); | 539 scoped_refptr<Other> other(new Other); |
528 scoped_refptr<Other> other2(other); | 540 scoped_refptr<Other> other2(other); |
529 EXPECT_EQ(other2, Overloaded(std::move(other))); | 541 EXPECT_EQ(other2, Overloaded(std::move(other))); |
530 } | 542 } |
543 | |
544 TEST(RefCountedUnitTest, TestInitialRefCountIsOne) { | |
545 scoped_refptr<InitialRefCountIsOne> obj = | |
546 base::MakeShared<InitialRefCountIsOne>(); | |
547 obj = nullptr; | |
548 | |
549 scoped_refptr<InitialRefCountIsOne> obj2 = | |
550 base::AdoptRef(new InitialRefCountIsOne); | |
551 obj2 = nullptr; | |
552 } | |
553 | |
554 TEST(RefCountedDeathTest, TestAdoptRef) { | |
555 EXPECT_DCHECK_DEATH(make_scoped_refptr(new InitialRefCountIsOne)); | |
556 | |
557 InitialRefCountIsOne* ptr = nullptr; | |
558 EXPECT_DCHECK_DEATH(base::AdoptRef(ptr)); | |
559 | |
560 scoped_refptr<InitialRefCountIsOne> obj = | |
561 base::MakeShared<InitialRefCountIsOne>(); | |
562 EXPECT_DCHECK_DEATH(base::AdoptRef(obj.get())); | |
563 } | |
OLD | NEW |