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 "base/test/opaque_ref_counted.h" | 7 #include "base/test/opaque_ref_counted.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 private: | 98 private: |
99 friend class base::RefCounted<ScopedRefPtrCountDerived>; | 99 friend class base::RefCounted<ScopedRefPtrCountDerived>; |
100 | 100 |
101 static int constructor_count_; | 101 static int constructor_count_; |
102 static int destructor_count_; | 102 static int destructor_count_; |
103 }; | 103 }; |
104 | 104 |
105 int ScopedRefPtrCountDerived::constructor_count_ = 0; | 105 int ScopedRefPtrCountDerived::constructor_count_ = 0; |
106 int ScopedRefPtrCountDerived::destructor_count_ = 0; | 106 int ScopedRefPtrCountDerived::destructor_count_ = 0; |
107 | 107 |
| 108 class Other : public base::RefCounted<Other> { |
| 109 private: |
| 110 friend class base::RefCounted<Other>; |
| 111 |
| 112 ~Other() {} |
| 113 }; |
| 114 |
| 115 scoped_refptr<Other> Overloaded(scoped_refptr<Other> other) { |
| 116 return other; |
| 117 } |
| 118 |
| 119 scoped_refptr<SelfAssign> Overloaded(scoped_refptr<SelfAssign> self_assign) { |
| 120 return self_assign; |
| 121 } |
| 122 |
| 123 |
108 } // end namespace | 124 } // end namespace |
109 | 125 |
110 TEST(RefCountedUnitTest, TestSelfAssignment) { | 126 TEST(RefCountedUnitTest, TestSelfAssignment) { |
111 SelfAssign* p = new SelfAssign; | 127 SelfAssign* p = new SelfAssign; |
112 scoped_refptr<SelfAssign> var(p); | 128 scoped_refptr<SelfAssign> var(p); |
113 var = var; | 129 var = var; |
114 EXPECT_EQ(var.get(), p); | 130 EXPECT_EQ(var.get(), p); |
115 } | 131 } |
116 | 132 |
117 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { | 133 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 EXPECT_EQ(1, ScopedRefPtrCountDerived::destructor_count()); | 470 EXPECT_EQ(1, ScopedRefPtrCountDerived::destructor_count()); |
455 | 471 |
456 // p1 goes out of scope. | 472 // p1 goes out of scope. |
457 } | 473 } |
458 EXPECT_EQ(1, ScopedRefPtrCountBase::constructor_count()); | 474 EXPECT_EQ(1, ScopedRefPtrCountBase::constructor_count()); |
459 EXPECT_EQ(1, ScopedRefPtrCountBase::destructor_count()); | 475 EXPECT_EQ(1, ScopedRefPtrCountBase::destructor_count()); |
460 EXPECT_EQ(1, ScopedRefPtrCountDerived::constructor_count()); | 476 EXPECT_EQ(1, ScopedRefPtrCountDerived::constructor_count()); |
461 EXPECT_EQ(1, ScopedRefPtrCountDerived::destructor_count()); | 477 EXPECT_EQ(1, ScopedRefPtrCountDerived::destructor_count()); |
462 } | 478 } |
463 | 479 |
| 480 TEST(RefCountedUnitTest, TestOverloadResolutionCopy) { |
| 481 scoped_refptr<Derived> derived(new Derived); |
| 482 scoped_refptr<SelfAssign> expected(derived); |
| 483 EXPECT_EQ(expected, Overloaded(derived)); |
| 484 |
| 485 scoped_refptr<Other> other(new Other); |
| 486 EXPECT_EQ(other, Overloaded(other)); |
| 487 } |
| 488 |
| 489 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { |
| 490 scoped_refptr<Derived> derived(new Derived); |
| 491 scoped_refptr<SelfAssign> expected(derived); |
| 492 EXPECT_EQ(expected, Overloaded(std::move(derived))); |
| 493 |
| 494 scoped_refptr<Other> other(new Other); |
| 495 scoped_refptr<Other> other2(other); |
| 496 EXPECT_EQ(other2, Overloaded(std::move(other))); |
| 497 } |
OLD | NEW |