1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/scoped_generic.h"
8 #include <unordered_map>
9 #include <unordered_set>
13 #include "base/stl_util.h"
14 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h"
22 IntTraits(std::vector<int>* freed) : freed_ints(freed) {}
24 static int InvalidValue() {
27 void Free(int value) {
28 freed_ints->push_back(value);
31 std::vector<int>* freed_ints;
34 using ScopedInt = ScopedGeneric<int, IntTraits>;
38 TEST(ScopedGenericTest, ScopedGeneric) {
39 std::vector<int> values_freed;
40 IntTraits traits(&values_freed);
42 // Invalid case, delete should not be called.
44 ScopedInt a(IntTraits::InvalidValue(), traits);
46 EXPECT_TRUE(values_freed.empty());
48 // Simple deleting case.
49 static const int kFirst = 0;
51 ScopedInt a(kFirst, traits);
53 ASSERT_EQ(1u, values_freed.size());
54 ASSERT_EQ(kFirst, values_freed[0]);
57 // Release should return the right value and leave the object empty.
59 ScopedInt a(kFirst, traits);
60 EXPECT_EQ(kFirst, a.release());
62 ScopedInt b(IntTraits::InvalidValue(), traits);
63 EXPECT_EQ(IntTraits::InvalidValue(), b.release());
65 ASSERT_TRUE(values_freed.empty());
67 // Reset should free the old value, then the new one should go away when
68 // it goes out of scope.
69 static const int kSecond = 1;
71 ScopedInt b(kFirst, traits);
73 ASSERT_EQ(1u, values_freed.size());
74 ASSERT_EQ(kFirst, values_freed[0]);
76 ASSERT_EQ(2u, values_freed.size());
77 ASSERT_EQ(kSecond, values_freed[1]);
82 ScopedInt a(kFirst, traits);
83 ScopedInt b(kSecond, traits);
85 EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
86 EXPECT_EQ(kSecond, a.get());
87 EXPECT_EQ(kFirst, b.get());
89 // Values should be deleted in the opposite order.
90 ASSERT_EQ(2u, values_freed.size());
91 EXPECT_EQ(kFirst, values_freed[0]);
92 EXPECT_EQ(kSecond, values_freed[1]);
97 ScopedInt a(kFirst, traits);
98 ScopedInt b(std::move(a));
99 EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
100 ASSERT_EQ(IntTraits::InvalidValue(), a.get());
101 ASSERT_EQ(kFirst, b.get());
104 ASSERT_EQ(1u, values_freed.size());
105 ASSERT_EQ(kFirst, values_freed[0]);
106 values_freed.clear();
110 ScopedInt a(kFirst, traits);
111 ScopedInt b(kSecond, traits);
113 ASSERT_EQ(1u, values_freed.size());
114 EXPECT_EQ(kSecond, values_freed[0]);
115 ASSERT_EQ(IntTraits::InvalidValue(), a.get());
116 ASSERT_EQ(kFirst, b.get());
119 ASSERT_EQ(2u, values_freed.size());
120 EXPECT_EQ(kFirst, values_freed[1]);
121 values_freed.clear();
124 TEST(ScopedGenericTest, Operators) {
125 std::vector<int> values_freed;
126 IntTraits traits(&values_freed);
128 static const int kFirst = 0;
129 static const int kSecond = 1;
131 ScopedInt a(kFirst, traits);
132 EXPECT_TRUE(a == kFirst);
133 EXPECT_FALSE(a != kFirst);
134 EXPECT_FALSE(a == kSecond);
135 EXPECT_TRUE(a != kSecond);
137 EXPECT_TRUE(kFirst == a);
138 EXPECT_FALSE(kFirst != a);
139 EXPECT_FALSE(kSecond == a);
140 EXPECT_TRUE(kSecond != a);
145 ScopedInt a(kFirst, traits);
146 EXPECT_TRUE(a.is_valid());
148 EXPECT_FALSE(a.is_valid());
152 TEST(ScopedGenericTest, Receive) {
153 std::vector<int> values_freed;
154 IntTraits traits(&values_freed);
155 auto a = std::make_unique<ScopedInt>(123, traits);
157 EXPECT_EQ(123, a->get());
160 ScopedInt::Receiver r(*a);
161 EXPECT_EQ(123, a->get());
163 EXPECT_EQ(123, a->get());
166 EXPECT_EQ(456, a->get());
169 ScopedInt::Receiver r(*a);
170 EXPECT_DEATH_IF_SUPPORTED(a.reset(), "");
171 EXPECT_DEATH_IF_SUPPORTED(ScopedInt::Receiver(*a).get(), "");
177 struct TrackedIntTraits : public ScopedGenericOwnershipTracking {
179 std::unordered_map<int, const ScopedGeneric<int, TrackedIntTraits>*>;
180 TrackedIntTraits(std::unordered_set<int>* freed, OwnerMap* owners)
181 : freed(freed), owners(owners) {}
183 static int InvalidValue() { return -1; }
185 void Free(int value) {
186 auto it = owners->find(value);
187 ASSERT_EQ(owners->end(), it);
189 ASSERT_EQ(0U, freed->count(value));
190 freed->insert(value);
193 void Acquire(const ScopedGeneric<int, TrackedIntTraits>& owner, int value) {
194 auto it = owners->find(value);
195 ASSERT_EQ(owners->end(), it);
196 (*owners)[value] = &owner;
199 void Release(const ScopedGeneric<int, TrackedIntTraits>& owner, int value) {
200 auto it = owners->find(value);
201 ASSERT_NE(owners->end(), it);
205 std::unordered_set<int>* freed;
209 using ScopedTrackedInt = ScopedGeneric<int, TrackedIntTraits>;
213 TEST(ScopedGenericTest, OwnershipTracking) {
214 TrackedIntTraits::OwnerMap owners;
215 std::unordered_set<int> freed;
216 TrackedIntTraits traits(&freed, &owners);
218 #define ASSERT_OWNED(value, owner) \
219 ASSERT_TRUE(base::ContainsKey(owners, value)); \
220 ASSERT_EQ(&owner, owners[value]); \
221 ASSERT_FALSE(base::ContainsKey(freed, value))
223 #define ASSERT_UNOWNED(value) \
224 ASSERT_FALSE(base::ContainsKey(owners, value)); \
225 ASSERT_FALSE(base::ContainsKey(freed, value))
227 #define ASSERT_FREED(value) \
228 ASSERT_FALSE(base::ContainsKey(owners, value)); \
229 ASSERT_TRUE(base::ContainsKey(freed, value))
234 ScopedTrackedInt a(0, traits);
245 ScopedTrackedInt a(0, traits);
261 ScopedTrackedInt a(0, traits);
263 int released = a.release();
264 ASSERT_EQ(0, released);
275 ScopedTrackedInt a(0, traits);
278 ScopedTrackedInt b(std::move(a));
290 ScopedTrackedInt a(0, traits);
291 ScopedTrackedInt b(1, traits);
307 ScopedTrackedInt a(0, traits);
308 ScopedTrackedInt b(1, traits);
323 #undef ASSERT_UNOWNED
327 // Cheesy manual "no compile" test for manually validating changes.
329 TEST(ScopedGenericTest, NoCompile) {
330 // Assignment shouldn't work.
332 ScopedInt a(kFirst, traits);
336 // Comparison shouldn't work.
338 ScopedInt a(kFirst, traits);
339 ScopedInt b(kFirst, traits);
344 // Implicit conversion to bool shouldn't work.
346 ScopedInt a(kFirst, traits);