Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / scoped_generic_unittest.cc
blobf0dca22e693179b8d8c6d8c838ef9b07c61ea69a
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 <vector>
7 #include "base/scoped_generic.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace base {
12 namespace {
14 struct IntTraits {
15 IntTraits(std::vector<int>* freed) : freed_ints(freed) {}
17 static int InvalidValue() {
18 return -1;
20 void Free(int value) {
21 freed_ints->push_back(value);
24 std::vector<int>* freed_ints;
27 typedef ScopedGeneric<int, IntTraits> ScopedInt;
29 } // namespace
31 TEST(ScopedGenericTest, ScopedGeneric) {
32 std::vector<int> values_freed;
33 IntTraits traits(&values_freed);
35 // Invalid case, delete should not be called.
37 ScopedInt a(IntTraits::InvalidValue(), traits);
39 EXPECT_TRUE(values_freed.empty());
41 // Simple deleting case.
42 static const int kFirst = 0;
44 ScopedInt a(kFirst, traits);
46 ASSERT_EQ(1u, values_freed.size());
47 ASSERT_EQ(kFirst, values_freed[0]);
48 values_freed.clear();
50 // Release should return the right value and leave the object empty.
52 ScopedInt a(kFirst, traits);
53 EXPECT_EQ(kFirst, a.release());
55 ScopedInt b(IntTraits::InvalidValue(), traits);
56 EXPECT_EQ(IntTraits::InvalidValue(), b.release());
58 ASSERT_TRUE(values_freed.empty());
60 // Reset should free the old value, then the new one should go away when
61 // it goes out of scope.
62 static const int kSecond = 1;
64 ScopedInt b(kFirst, traits);
65 b.reset(kSecond);
66 ASSERT_EQ(1u, values_freed.size());
67 ASSERT_EQ(kFirst, values_freed[0]);
69 ASSERT_EQ(2u, values_freed.size());
70 ASSERT_EQ(kSecond, values_freed[1]);
71 values_freed.clear();
73 // Swap.
75 ScopedInt a(kFirst, traits);
76 ScopedInt b(kSecond, traits);
77 a.swap(b);
78 EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
79 EXPECT_EQ(kSecond, a.get());
80 EXPECT_EQ(kFirst, b.get());
82 // Values should be deleted in the opposite order.
83 ASSERT_EQ(2u, values_freed.size());
84 EXPECT_EQ(kFirst, values_freed[0]);
85 EXPECT_EQ(kSecond, values_freed[1]);
86 values_freed.clear();
88 // Pass.
90 ScopedInt a(kFirst, traits);
91 ScopedInt b(a.Pass());
92 EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
93 ASSERT_EQ(IntTraits::InvalidValue(), a.get());
94 ASSERT_EQ(kFirst, b.get());
96 ASSERT_EQ(1u, values_freed.size());
97 ASSERT_EQ(kFirst, values_freed[0]);
100 TEST(ScopedGenericTest, Operators) {
101 std::vector<int> values_freed;
102 IntTraits traits(&values_freed);
104 static const int kFirst = 0;
105 static const int kSecond = 1;
107 ScopedInt a(kFirst, traits);
108 EXPECT_TRUE(a == kFirst);
109 EXPECT_FALSE(a != kFirst);
110 EXPECT_FALSE(a == kSecond);
111 EXPECT_TRUE(a != kSecond);
113 EXPECT_TRUE(kFirst == a);
114 EXPECT_FALSE(kFirst != a);
115 EXPECT_FALSE(kSecond == a);
116 EXPECT_TRUE(kSecond != a);
119 // is_valid().
121 ScopedInt a(kFirst, traits);
122 EXPECT_TRUE(a.is_valid());
123 a.reset();
124 EXPECT_FALSE(a.is_valid());
128 // Cheesy manual "no compile" test for manually validating changes.
129 #if 0
130 TEST(ScopedGenericTest, NoCompile) {
131 // Assignment shouldn't work.
133 ScopedInt a(kFirst, traits);
134 ScopedInt b(a);
137 // Comparison shouldn't work.
139 ScopedInt a(kFirst, traits);
140 ScopedInt b(kFirst, traits);
141 if (a == b) {
145 // Implicit conversion to bool shouldn't work.
147 ScopedInt a(kFirst, traits);
148 bool result = a;
151 #endif
153 } // namespace base