Chromium Code Reviews| 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 base::TestOpaqueRefCounted(initial); |
| 165 base::TestOpaqueRefCounted(q); | 167 base::TestOpaqueRefCounted(assigned); |
| 168 | |
| 169 scoped_refptr<base::OpaqueRefCounted> copied(initial); | |
| 170 base::TestOpaqueRefCounted(initial); | |
| 171 base::TestOpaqueRefCounted(copied); | |
| 172 | |
| 173 scoped_refptr<base::OpaqueRefCounted> moved(std::move(initial)); | |
|
danakj
2016/10/27 22:10:54
Isn't this just testing different ways to make a s
gab
2016/10/28 14:02:26
Right I guess the multiple calls to TestOpaqueRefC
danakj
2016/10/29 00:49:09
Yes if u wanna test them do it as a separate test
gab
2016/10/31 15:01:14
Removed extraneous calls.
| |
| 174 EXPECT_EQ(nullptr, initial.get()); | |
| 175 base::TestOpaqueRefCounted(moved); | |
| 176 | |
| 177 scoped_refptr<base::OpaqueRefCounted> move_assigned; | |
| 178 move_assigned = std::move(moved); | |
| 179 EXPECT_EQ(nullptr, moved.get()); | |
| 180 base::TestOpaqueRefCounted(move_assigned); | |
| 181 } | |
| 182 | |
| 183 TEST(RefCountedUnitTest, ScopedRefPtrToOpaqueThreadSafe) { | |
| 184 scoped_refptr<base::OpaqueRefCountedThreadSafe> initial = | |
| 185 base::MakeOpaqueRefCountedThreadSafe(); | |
| 186 base::TestOpaqueRefCountedThreadSafe(initial); | |
| 187 | |
| 188 scoped_refptr<base::OpaqueRefCountedThreadSafe> assigned; | |
| 189 assigned = initial; | |
| 190 base::TestOpaqueRefCountedThreadSafe(initial); | |
| 191 base::TestOpaqueRefCountedThreadSafe(assigned); | |
| 192 | |
| 193 scoped_refptr<base::OpaqueRefCountedThreadSafe> copied(initial); | |
| 194 base::TestOpaqueRefCountedThreadSafe(initial); | |
| 195 base::TestOpaqueRefCountedThreadSafe(copied); | |
| 196 | |
| 197 scoped_refptr<base::OpaqueRefCountedThreadSafe> moved(std::move(initial)); | |
| 198 EXPECT_EQ(nullptr, initial.get()); | |
| 199 base::TestOpaqueRefCountedThreadSafe(moved); | |
| 200 | |
| 201 scoped_refptr<base::OpaqueRefCountedThreadSafe> move_assigned; | |
| 202 move_assigned = std::move(moved); | |
| 203 EXPECT_EQ(nullptr, moved.get()); | |
| 204 base::TestOpaqueRefCountedThreadSafe(move_assigned); | |
| 166 } | 205 } |
| 167 | 206 |
| 168 TEST(RefCountedUnitTest, BooleanTesting) { | 207 TEST(RefCountedUnitTest, BooleanTesting) { |
| 169 scoped_refptr<SelfAssign> ptr_to_an_instance = new SelfAssign; | 208 scoped_refptr<SelfAssign> ptr_to_an_instance = new SelfAssign; |
| 170 EXPECT_TRUE(ptr_to_an_instance); | 209 EXPECT_TRUE(ptr_to_an_instance); |
| 171 EXPECT_FALSE(!ptr_to_an_instance); | 210 EXPECT_FALSE(!ptr_to_an_instance); |
| 172 | 211 |
| 173 if (ptr_to_an_instance) { | 212 if (ptr_to_an_instance) { |
| 174 } else { | 213 } else { |
| 175 ADD_FAILURE() << "Pointer to an instance should result in true."; | 214 ADD_FAILURE() << "Pointer to an instance should result in true."; |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 | 537 |
| 499 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { | 538 TEST(RefCountedUnitTest, TestOverloadResolutionMove) { |
| 500 scoped_refptr<Derived> derived(new Derived); | 539 scoped_refptr<Derived> derived(new Derived); |
| 501 scoped_refptr<SelfAssign> expected(derived); | 540 scoped_refptr<SelfAssign> expected(derived); |
| 502 EXPECT_EQ(expected, Overloaded(std::move(derived))); | 541 EXPECT_EQ(expected, Overloaded(std::move(derived))); |
| 503 | 542 |
| 504 scoped_refptr<Other> other(new Other); | 543 scoped_refptr<Other> other(new Other); |
| 505 scoped_refptr<Other> other2(other); | 544 scoped_refptr<Other> other2(other); |
| 506 EXPECT_EQ(other2, Overloaded(std::move(other))); | 545 EXPECT_EQ(other2, Overloaded(std::move(other))); |
| 507 } | 546 } |
| OLD | NEW |