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 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 | 7 |
8 namespace { | 8 namespace { |
9 | 9 |
10 class SelfAssign : public base::RefCounted<SelfAssign> { | 10 class SelfAssign : public base::RefCounted<SelfAssign> { |
11 friend class base::RefCounted<SelfAssign>; | 11 friend class base::RefCounted<SelfAssign>; |
12 | 12 |
13 ~SelfAssign() {} | 13 ~SelfAssign() {} |
14 }; | 14 }; |
15 | 15 |
16 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> { | 16 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> { |
17 public: | 17 public: |
18 CheckDerivedMemberAccess() { | 18 CheckDerivedMemberAccess() { |
19 // This shouldn't compile if we don't have access to the member variable. | 19 // This shouldn't compile if we don't have access to the member variable. |
20 SelfAssign** pptr = &ptr_; | 20 SelfAssign** pptr = &ptr_; |
21 EXPECT_EQ(*pptr, ptr_); | 21 EXPECT_EQ(*pptr, ptr_); |
22 } | 22 } |
23 }; | 23 }; |
24 | 24 |
25 class ScopedRefPtrToSelf : public base::RefCounted<ScopedRefPtrToSelf> { | 25 class ScopedRefPtrToSelf : public base::RefCounted<ScopedRefPtrToSelf> { |
26 public: | 26 public: |
27 ScopedRefPtrToSelf() | 27 ScopedRefPtrToSelf() |
28 : ALLOW_THIS_IN_INITIALIZER_LIST(self_ptr_(this)) { | 28 : ALLOW_THIS_IN_INITIALIZER_LIST(self_ptr_(this)) { |
29 } | 29 } |
30 ~ScopedRefPtrToSelf() { was_destroyed_ = true; } | |
31 | 30 |
32 static bool was_destroyed() { return was_destroyed_; } | 31 static bool was_destroyed() { return was_destroyed_; } |
33 | 32 |
34 void SelfDestruct() { self_ptr_ = NULL; } | 33 void SelfDestruct() { self_ptr_ = NULL; } |
35 | 34 |
36 private: | 35 private: |
37 friend class base::RefCounted<ScopedRefPtrToSelf>; | 36 friend class base::RefCounted<ScopedRefPtrToSelf>; |
| 37 ~ScopedRefPtrToSelf() { was_destroyed_ = true; } |
38 | 38 |
39 static bool was_destroyed_; | 39 static bool was_destroyed_; |
40 | 40 |
41 scoped_refptr<ScopedRefPtrToSelf> self_ptr_; | 41 scoped_refptr<ScopedRefPtrToSelf> self_ptr_; |
42 }; | 42 }; |
43 | 43 |
44 bool ScopedRefPtrToSelf::was_destroyed_ = false; | 44 bool ScopedRefPtrToSelf::was_destroyed_ = false; |
45 | 45 |
46 } // end namespace | 46 } // end namespace |
47 | 47 |
48 TEST(RefCountedUnitTest, TestSelfAssignment) { | 48 TEST(RefCountedUnitTest, TestSelfAssignment) { |
49 SelfAssign* p = new SelfAssign; | 49 SelfAssign* p = new SelfAssign; |
50 scoped_refptr<SelfAssign> var(p); | 50 scoped_refptr<SelfAssign> var(p); |
51 var = var; | 51 var = var; |
52 EXPECT_EQ(var.get(), p); | 52 EXPECT_EQ(var.get(), p); |
53 } | 53 } |
54 | 54 |
55 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { | 55 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { |
56 CheckDerivedMemberAccess check; | 56 CheckDerivedMemberAccess check; |
57 } | 57 } |
58 | 58 |
59 TEST(RefCountedUnitTest, ScopedRefPtrToSelf) { | 59 TEST(RefCountedUnitTest, ScopedRefPtrToSelf) { |
60 ScopedRefPtrToSelf* check = new ScopedRefPtrToSelf(); | 60 ScopedRefPtrToSelf* check = new ScopedRefPtrToSelf(); |
61 EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed()); | 61 EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed()); |
62 check->SelfDestruct(); | 62 check->SelfDestruct(); |
63 EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed()); | 63 EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed()); |
64 } | 64 } |
OLD | NEW |