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"
10 #include "testing/gtest/include/gtest/gtest.h"
17 IntTraits(std::vector<int>* freed) : freed_ints(freed) {}
19 static int InvalidValue() {
22 void Free(int value) {
23 freed_ints->push_back(value);
26 std::vector<int>* freed_ints;
29 typedef ScopedGeneric<int, IntTraits> ScopedInt;
33 TEST(ScopedGenericTest, ScopedGeneric) {
34 std::vector<int> values_freed;
35 IntTraits traits(&values_freed);
37 // Invalid case, delete should not be called.
39 ScopedInt a(IntTraits::InvalidValue(), traits);
41 EXPECT_TRUE(values_freed.empty());
43 // Simple deleting case.
44 static const int kFirst = 0;
46 ScopedInt a(kFirst, traits);
48 ASSERT_EQ(1u, values_freed.size());
49 ASSERT_EQ(kFirst, values_freed[0]);
52 // Release should return the right value and leave the object empty.
54 ScopedInt a(kFirst, traits);
55 EXPECT_EQ(kFirst, a.release());
57 ScopedInt b(IntTraits::InvalidValue(), traits);
58 EXPECT_EQ(IntTraits::InvalidValue(), b.release());
60 ASSERT_TRUE(values_freed.empty());
62 // Reset should free the old value, then the new one should go away when
63 // it goes out of scope.
64 static const int kSecond = 1;
66 ScopedInt b(kFirst, traits);
68 ASSERT_EQ(1u, values_freed.size());
69 ASSERT_EQ(kFirst, values_freed[0]);
71 ASSERT_EQ(2u, values_freed.size());
72 ASSERT_EQ(kSecond, values_freed[1]);
77 ScopedInt a(kFirst, traits);
78 ScopedInt b(kSecond, traits);
80 EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
81 EXPECT_EQ(kSecond, a.get());
82 EXPECT_EQ(kFirst, b.get());
84 // Values should be deleted in the opposite order.
85 ASSERT_EQ(2u, values_freed.size());
86 EXPECT_EQ(kFirst, values_freed[0]);
87 EXPECT_EQ(kSecond, values_freed[1]);
92 ScopedInt a(kFirst, traits);
93 ScopedInt b(std::move(a));
94 EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
95 ASSERT_EQ(IntTraits::InvalidValue(), a.get());
96 ASSERT_EQ(kFirst, b.get());
99 ASSERT_EQ(1u, values_freed.size());
100 ASSERT_EQ(kFirst, values_freed[0]);
101 values_freed.clear();
105 ScopedInt a(kFirst, traits);
106 ScopedInt b(kSecond, traits);
108 ASSERT_EQ(1u, values_freed.size());
109 EXPECT_EQ(kSecond, values_freed[0]);
110 ASSERT_EQ(IntTraits::InvalidValue(), a.get());
111 ASSERT_EQ(kFirst, b.get());
114 ASSERT_EQ(2u, values_freed.size());
115 EXPECT_EQ(kFirst, values_freed[1]);
116 values_freed.clear();
119 TEST(ScopedGenericTest, Operators) {
120 std::vector<int> values_freed;
121 IntTraits traits(&values_freed);
123 static const int kFirst = 0;
124 static const int kSecond = 1;
126 ScopedInt a(kFirst, traits);
127 EXPECT_TRUE(a == kFirst);
128 EXPECT_FALSE(a != kFirst);
129 EXPECT_FALSE(a == kSecond);
130 EXPECT_TRUE(a != kSecond);
132 EXPECT_TRUE(kFirst == a);
133 EXPECT_FALSE(kFirst != a);
134 EXPECT_FALSE(kSecond == a);
135 EXPECT_TRUE(kSecond != a);
140 ScopedInt a(kFirst, traits);
141 EXPECT_TRUE(a.is_valid());
143 EXPECT_FALSE(a.is_valid());
147 // Cheesy manual "no compile" test for manually validating changes.
149 TEST(ScopedGenericTest, NoCompile) {
150 // Assignment shouldn't work.
152 ScopedInt a(kFirst, traits);
156 // Comparison shouldn't work.
158 ScopedInt a(kFirst, traits);
159 ScopedInt b(kFirst, traits);
164 // Implicit conversion to bool shouldn't work.
166 ScopedInt a(kFirst, traits);