|
8 | 8 |
|
9 | 9 | namespace fml { |
10 | 10 |
|
| 11 | +// Test state used in CFTest.SupportsCustomRetainRelease. |
| 12 | +struct CFRefTestState { |
| 13 | + bool retain_called; |
| 14 | + bool release_called; |
| 15 | +}; |
| 16 | + |
11 | 17 | // Template specialization used in CFTest.SupportsCustomRetainRelease. |
12 | 18 | template <> |
13 | | -struct CFRefTraits<int64_t> { |
14 | | - static bool retain_called; |
15 | | - static bool release_called; |
16 | | - |
17 | | - static constexpr int64_t kNullValue = 0; |
18 | | - static void Retain(int64_t instance) { retain_called = true; } |
19 | | - static void Release(int64_t instance) { release_called = true; } |
| 19 | +struct CFRefTraits<CFRefTestState*> { |
| 20 | + static constexpr CFRefTestState* kNullValue = nullptr; |
| 21 | + static void Retain(CFRefTestState* instance) { instance->retain_called = true; } |
| 22 | + static void Release(CFRefTestState* instance) { instance->release_called = true; } |
20 | 23 | }; |
21 | | -bool CFRefTraits<int64_t>::retain_called = false; |
22 | | -bool CFRefTraits<int64_t>::release_called = false; |
23 | 24 |
|
24 | 25 | namespace testing { |
25 | 26 |
|
|
98 | 99 | } |
99 | 100 |
|
100 | 101 | TEST(CFTest, SupportsCustomRetainRelease) { |
101 | | - CFRef<int64_t> ref(1); |
102 | | - ASSERT_EQ(ref.Get(), 1); |
103 | | - ASSERT_FALSE(CFRefTraits<int64_t>::retain_called); |
104 | | - ASSERT_FALSE(CFRefTraits<int64_t>::release_called); |
| 102 | + CFRefTestState instance{}; |
| 103 | + CFRef<CFRefTestState*> ref(&instance); |
| 104 | + ASSERT_EQ(ref.Get(), &instance); |
| 105 | + ASSERT_FALSE(instance.retain_called); |
| 106 | + ASSERT_FALSE(instance.release_called); |
105 | 107 | ref.Reset(); |
106 | | - ASSERT_EQ(ref.Get(), 0); |
107 | | - ASSERT_TRUE(CFRefTraits<int64_t>::release_called); |
108 | | - ref.Retain(2); |
109 | | - ASSERT_EQ(ref.Get(), 2); |
110 | | - ASSERT_TRUE(CFRefTraits<int64_t>::retain_called); |
111 | | - CFRefTraits<int64_t>::retain_called = false; |
112 | | - CFRefTraits<int64_t>::release_called = false; |
| 108 | + ASSERT_EQ(ref.Get(), nullptr); |
| 109 | + ASSERT_FALSE(instance.retain_called); |
| 110 | + ASSERT_TRUE(instance.release_called); |
| 111 | + |
| 112 | + CFRefTestState other_instance{}; |
| 113 | + ref.Retain(&other_instance); |
| 114 | + ASSERT_EQ(ref.Get(), &other_instance); |
| 115 | + ASSERT_TRUE(other_instance.retain_called); |
| 116 | + ASSERT_FALSE(other_instance.release_called); |
113 | 117 | } |
114 | 118 |
|
115 | 119 | } // namespace testing |
|
0 commit comments