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 { |
11 | 11 |
12 class SelfAssign : public base::RefCounted<SelfAssign> { | 12 class SelfAssign : public base::RefCounted<SelfAssign> { |
13 protected: | |
14 virtual ~SelfAssign() {} | |
15 | |
16 private: | |
13 friend class base::RefCounted<SelfAssign>; | 17 friend class base::RefCounted<SelfAssign>; |
18 }; | |
14 | 19 |
15 ~SelfAssign() {} | 20 class Derived : public SelfAssign { |
21 protected: | |
22 ~Derived() override {} | |
23 | |
24 private: | |
25 friend class base::RefCounted<Derived>; | |
16 }; | 26 }; |
17 | 27 |
18 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> { | 28 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> { |
19 public: | 29 public: |
20 CheckDerivedMemberAccess() { | 30 CheckDerivedMemberAccess() { |
21 // This shouldn't compile if we don't have access to the member variable. | 31 // This shouldn't compile if we don't have access to the member variable. |
22 SelfAssign** pptr = &ptr_; | 32 SelfAssign** pptr = &ptr_; |
23 EXPECT_EQ(*pptr, ptr_); | 33 EXPECT_EQ(*pptr, ptr_); |
24 } | 34 } |
25 }; | 35 }; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 | 75 |
66 TEST(RefCountedUnitTest, ScopedRefPtrToOpaque) { | 76 TEST(RefCountedUnitTest, ScopedRefPtrToOpaque) { |
67 scoped_refptr<base::OpaqueRefCounted> p = base::MakeOpaqueRefCounted(); | 77 scoped_refptr<base::OpaqueRefCounted> p = base::MakeOpaqueRefCounted(); |
68 base::TestOpaqueRefCounted(p); | 78 base::TestOpaqueRefCounted(p); |
69 | 79 |
70 scoped_refptr<base::OpaqueRefCounted> q; | 80 scoped_refptr<base::OpaqueRefCounted> q; |
71 q = p; | 81 q = p; |
72 base::TestOpaqueRefCounted(p); | 82 base::TestOpaqueRefCounted(p); |
73 base::TestOpaqueRefCounted(q); | 83 base::TestOpaqueRefCounted(q); |
74 } | 84 } |
85 | |
86 TEST(RefCountedUnitTest, BooleanTesting) { | |
87 scoped_refptr<SelfAssign> p; | |
88 EXPECT_FALSE(p); | |
89 p = new SelfAssign; | |
90 EXPECT_TRUE(p); | |
91 } | |
92 | |
93 TEST(RefCountedUnitTest, Equality) { | |
94 scoped_refptr<SelfAssign> p1(new SelfAssign); | |
95 scoped_refptr<SelfAssign> p2(new SelfAssign); | |
96 | |
97 EXPECT_EQ(p1, p1); | |
98 EXPECT_EQ(p2, p2); | |
99 | |
100 EXPECT_NE(p1, p2); | |
dcheng
2014/11/25 08:44:36
This is primarily to ensure that we're not inadver
| |
101 EXPECT_NE(p2, p1); | |
102 } | |
103 | |
104 TEST(RefCountedUnitTest, ConvertibleEquality) { | |
dcheng
2014/11/25 08:44:36
shared_ptr allows comparisons between convertible
| |
105 scoped_refptr<Derived> p1(new Derived); | |
106 scoped_refptr<SelfAssign> p2; | |
107 | |
108 EXPECT_NE(p1, p2); | |
109 EXPECT_NE(p2, p1); | |
110 | |
111 p2 = p1; | |
112 | |
113 EXPECT_EQ(p1, p2); | |
114 EXPECT_EQ(p2, p1); | |
115 } | |
OLD | NEW |