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> |
| 8 |
7 #include "base/test/opaque_ref_counted.h" | 9 #include "base/test/opaque_ref_counted.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 | 11 |
10 namespace { | 12 namespace { |
11 | 13 |
12 class SelfAssign : public base::RefCounted<SelfAssign> { | 14 class SelfAssign : public base::RefCounted<SelfAssign> { |
13 protected: | 15 protected: |
14 virtual ~SelfAssign() {} | 16 virtual ~SelfAssign() {} |
15 | 17 |
16 private: | 18 private: |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 ScopedRefPtrToSelf* check = new ScopedRefPtrToSelf(); | 151 ScopedRefPtrToSelf* check = new ScopedRefPtrToSelf(); |
150 EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed()); | 152 EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed()); |
151 // Releasing |check->self_ptr_| will delete |check|. | 153 // Releasing |check->self_ptr_| will delete |check|. |
152 // The move assignment operator must assign |check->self_ptr_| first then | 154 // The move assignment operator must assign |check->self_ptr_| first then |
153 // release |check->self_ptr_|. | 155 // release |check->self_ptr_|. |
154 check->self_ptr_ = scoped_refptr<ScopedRefPtrToSelf>(); | 156 check->self_ptr_ = scoped_refptr<ScopedRefPtrToSelf>(); |
155 EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed()); | 157 EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed()); |
156 } | 158 } |
157 | 159 |
158 TEST(RefCountedUnitTest, ScopedRefPtrToOpaque) { | 160 TEST(RefCountedUnitTest, ScopedRefPtrToOpaque) { |
159 scoped_refptr<base::OpaqueRefCounted> p = base::MakeOpaqueRefCounted(); | 161 scoped_refptr<base::OpaqueRefCounted> initial = base::MakeOpaqueRefCounted(); |
160 base::TestOpaqueRefCounted(p); | 162 base::TestOpaqueRefCounted(initial); |
161 | 163 |
162 scoped_refptr<base::OpaqueRefCounted> q; | 164 scoped_refptr<base::OpaqueRefCounted> assigned; |
163 q = p; | 165 assigned = initial; |
164 base::TestOpaqueRefCounted(p); | 166 |
165 base::TestOpaqueRefCounted(q); | 167 scoped_refptr<base::OpaqueRefCounted> copied(initial); |
| 168 |
| 169 scoped_refptr<base::OpaqueRefCounted> moved(std::move(initial)); |
| 170 |
| 171 scoped_refptr<base::OpaqueRefCounted> move_assigned; |
| 172 move_assigned = std::move(moved); |
| 173 } |
| 174 |
| 175 TEST(RefCountedUnitTest, ScopedRefPtrToOpaqueThreadSafe) { |
| 176 scoped_refptr<base::OpaqueRefCountedThreadSafe> initial = |
| 177 base::MakeOpaqueRefCountedThreadSafe(); |
| 178 base::TestOpaqueRefCountedThreadSafe(initial); |
| 179 |
| 180 scoped_refptr<base::OpaqueRefCountedThreadSafe> assigned; |
| 181 assigned = initial; |
| 182 |
| 183 scoped_refptr<base::OpaqueRefCountedThreadSafe> copied(initial); |
| 184 |
| 185 scoped_refptr<base::OpaqueRefCountedThreadSafe> moved(std::move(initial)); |
| 186 |
| 187 scoped_refptr<base::OpaqueRefCountedThreadSafe> move_assigned; |
| 188 move_assigned = std::move(moved); |
166 } | 189 } |
167 | 190 |
168 TEST(RefCountedUnitTest, BooleanTesting) { | 191 TEST(RefCountedUnitTest, BooleanTesting) { |
169 scoped_refptr<SelfAssign> ptr_to_an_instance = new SelfAssign; | 192 scoped_refptr<SelfAssign> ptr_to_an_instance = new SelfAssign; |
170 EXPECT_TRUE(ptr_to_an_instance); | 193 EXPECT_TRUE(ptr_to_an_instance); |
171 EXPECT_FALSE(!ptr_to_an_instance); | 194 EXPECT_FALSE(!ptr_to_an_instance); |
172 | 195 |
173 if (ptr_to_an_instance) { | 196 if (ptr_to_an_instance) { |
174 } else { | 197 } else { |
175 ADD_FAILURE() << "Pointer to an instance should result in true."; | 198 ADD_FAILURE() << "Pointer to an instance should result in true."; |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 | 521 |
499 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { | 522 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { |
500 scoped_refptr<Derived> derived(new Derived); | 523 scoped_refptr<Derived> derived(new Derived); |
501 scoped_refptr<SelfAssign> expected(derived); | 524 scoped_refptr<SelfAssign> expected(derived); |
502 EXPECT_EQ(expected, Overloaded(std::move(derived))); | 525 EXPECT_EQ(expected, Overloaded(std::move(derived))); |
503 | 526 |
504 scoped_refptr<Other> other(new Other); | 527 scoped_refptr<Other> other(new Other); |
505 scoped_refptr<Other> other2(other); | 528 scoped_refptr<Other> other2(other); |
506 EXPECT_EQ(other2, Overloaded(std::move(other))); | 529 EXPECT_EQ(other2, Overloaded(std::move(other))); |
507 } | 530 } |
OLD | NEW |