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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 friend class base::RefCounted<ScopedRefPtrToSelf>; | 46 friend class base::RefCounted<ScopedRefPtrToSelf>; |
47 ~ScopedRefPtrToSelf() { was_destroyed_ = true; } | 47 ~ScopedRefPtrToSelf() { was_destroyed_ = true; } |
48 | 48 |
49 static bool was_destroyed_; | 49 static bool was_destroyed_; |
50 | 50 |
51 scoped_refptr<ScopedRefPtrToSelf> self_ptr_; | 51 scoped_refptr<ScopedRefPtrToSelf> self_ptr_; |
52 }; | 52 }; |
53 | 53 |
54 bool ScopedRefPtrToSelf::was_destroyed_ = false; | 54 bool ScopedRefPtrToSelf::was_destroyed_ = false; |
55 | 55 |
| 56 class ScopedRefPtrCount : public base::RefCounted<ScopedRefPtrCount> { |
| 57 public: |
| 58 ScopedRefPtrCount() { ++constructor_count_; } |
| 59 |
| 60 static int constructor_count() { return constructor_count_; } |
| 61 |
| 62 static int destructor_count() { return destructor_count_; } |
| 63 |
| 64 static void reset_count() { |
| 65 constructor_count_ = 0; |
| 66 destructor_count_ = 0; |
| 67 } |
| 68 |
| 69 private: |
| 70 friend class base::RefCounted<ScopedRefPtrCount>; |
| 71 ~ScopedRefPtrCount() { ++destructor_count_; } |
| 72 |
| 73 static int constructor_count_; |
| 74 static int destructor_count_; |
| 75 }; |
| 76 |
| 77 int ScopedRefPtrCount::constructor_count_ = 0; |
| 78 int ScopedRefPtrCount::destructor_count_ = 0; |
| 79 |
56 } // end namespace | 80 } // end namespace |
57 | 81 |
58 TEST(RefCountedUnitTest, TestSelfAssignment) { | 82 TEST(RefCountedUnitTest, TestSelfAssignment) { |
59 SelfAssign* p = new SelfAssign; | 83 SelfAssign* p = new SelfAssign; |
60 scoped_refptr<SelfAssign> var(p); | 84 scoped_refptr<SelfAssign> var(p); |
61 var = var; | 85 var = var; |
62 EXPECT_EQ(var.get(), p); | 86 EXPECT_EQ(var.get(), p); |
63 } | 87 } |
64 | 88 |
65 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { | 89 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 scoped_refptr<SelfAssign> p2; | 130 scoped_refptr<SelfAssign> p2; |
107 | 131 |
108 EXPECT_NE(p1, p2); | 132 EXPECT_NE(p1, p2); |
109 EXPECT_NE(p2, p1); | 133 EXPECT_NE(p2, p1); |
110 | 134 |
111 p2 = p1; | 135 p2 = p1; |
112 | 136 |
113 EXPECT_EQ(p1, p2); | 137 EXPECT_EQ(p1, p2); |
114 EXPECT_EQ(p2, p1); | 138 EXPECT_EQ(p2, p1); |
115 } | 139 } |
| 140 |
| 141 TEST(RefCountedUnitTest, MoveAssignment1) { |
| 142 ScopedRefPtrCount::reset_count(); |
| 143 |
| 144 { |
| 145 scoped_refptr<ScopedRefPtrCount> p1(new ScopedRefPtrCount()); |
| 146 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 147 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 148 |
| 149 scoped_refptr<ScopedRefPtrCount> p2(new ScopedRefPtrCount()); |
| 150 EXPECT_EQ(2, ScopedRefPtrCount::constructor_count()); |
| 151 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 152 |
| 153 p1 = p2.Pass(); |
| 154 EXPECT_EQ(2, ScopedRefPtrCount::constructor_count()); |
| 155 EXPECT_EQ(1, ScopedRefPtrCount::destructor_count()); |
| 156 EXPECT_EQ(nullptr, p2.get()); |
| 157 |
| 158 // p1 and p2 go out of scope. |
| 159 } |
| 160 EXPECT_EQ(2, ScopedRefPtrCount::constructor_count()); |
| 161 EXPECT_EQ(2, ScopedRefPtrCount::destructor_count()); |
| 162 } |
| 163 |
| 164 TEST(RefCountedUnitTest, MoveAssignment2) { |
| 165 ScopedRefPtrCount::reset_count(); |
| 166 |
| 167 { |
| 168 scoped_refptr<ScopedRefPtrCount> p1(new ScopedRefPtrCount); |
| 169 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 170 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 171 |
| 172 { |
| 173 scoped_refptr<ScopedRefPtrCount> p2; |
| 174 p2 = p1.Pass(); |
| 175 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 176 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 177 EXPECT_EQ(nullptr, p1.get()); |
| 178 |
| 179 // p2 goes out of scope. |
| 180 } |
| 181 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 182 EXPECT_EQ(1, ScopedRefPtrCount::destructor_count()); |
| 183 |
| 184 // p1 goes out of scope. |
| 185 } |
| 186 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 187 EXPECT_EQ(1, ScopedRefPtrCount::destructor_count()); |
| 188 } |
| 189 |
| 190 TEST(RefCountedUnitTest, MoveAssignment3) { |
| 191 ScopedRefPtrCount::reset_count(); |
| 192 |
| 193 { |
| 194 scoped_refptr<ScopedRefPtrCount> p1; |
| 195 EXPECT_EQ(0, ScopedRefPtrCount::constructor_count()); |
| 196 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 197 |
| 198 { |
| 199 scoped_refptr<ScopedRefPtrCount> p2(new ScopedRefPtrCount()); |
| 200 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 201 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 202 |
| 203 p1 = p2.Pass(); |
| 204 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 205 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 206 EXPECT_EQ(nullptr, p2.get()); |
| 207 |
| 208 // p2 goes out of scope. |
| 209 } |
| 210 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 211 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 212 |
| 213 // p1 goes out of scope. |
| 214 } |
| 215 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 216 EXPECT_EQ(1, ScopedRefPtrCount::destructor_count()); |
| 217 } |
| 218 |
| 219 TEST(RefCountedUnitTest, MoveConstructor) { |
| 220 ScopedRefPtrCount::reset_count(); |
| 221 |
| 222 { |
| 223 scoped_refptr<ScopedRefPtrCount> p1(new ScopedRefPtrCount); |
| 224 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 225 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 226 |
| 227 { |
| 228 scoped_refptr<ScopedRefPtrCount> p2(p1.Pass()); |
| 229 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 230 EXPECT_EQ(0, ScopedRefPtrCount::destructor_count()); |
| 231 EXPECT_EQ(nullptr, p1.get()); |
| 232 |
| 233 // p2 goes out of scope. |
| 234 } |
| 235 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 236 EXPECT_EQ(1, ScopedRefPtrCount::destructor_count()); |
| 237 |
| 238 // p1 goes out of scope. |
| 239 } |
| 240 EXPECT_EQ(1, ScopedRefPtrCount::constructor_count()); |
| 241 EXPECT_EQ(1, ScopedRefPtrCount::destructor_count()); |
| 242 } |
OLD | NEW |