Fix network quality estimator tests
[chromium-blink-merge.git] / base / scoped_generic_unittest.cc
blobb28e154372e6f07ffdec17eec07f25bbf9ad8d8c
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 constructor.
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());
97 ASSERT_EQ(1u, values_freed.size());
98 ASSERT_EQ(kFirst, values_freed[0]);
99 values_freed.clear();
101 // Pass assign.
103 ScopedInt a(kFirst, traits);
104 ScopedInt b(kSecond, traits);
105 b = a.Pass();
106 ASSERT_EQ(1u, values_freed.size());
107 EXPECT_EQ(kSecond, values_freed[0]);
108 ASSERT_EQ(IntTraits::InvalidValue(), a.get());
109 ASSERT_EQ(kFirst, b.get());
112 ASSERT_EQ(2u, values_freed.size());
113 EXPECT_EQ(kFirst, values_freed[1]);
114 values_freed.clear();
117 TEST(ScopedGenericTest, Operators) {
118 std::vector<int> values_freed;
119 IntTraits traits(&values_freed);
121 static const int kFirst = 0;
122 static const int kSecond = 1;
124 ScopedInt a(kFirst, traits);
125 EXPECT_TRUE(a == kFirst);
126 EXPECT_FALSE(a != kFirst);
127 EXPECT_FALSE(a == kSecond);
128 EXPECT_TRUE(a != kSecond);
130 EXPECT_TRUE(kFirst == a);
131 EXPECT_FALSE(kFirst != a);
132 EXPECT_FALSE(kSecond == a);
133 EXPECT_TRUE(kSecond != a);
136 // is_valid().
138 ScopedInt a(kFirst, traits);
139 EXPECT_TRUE(a.is_valid());
140 a.reset();
141 EXPECT_FALSE(a.is_valid());
145 // Cheesy manual "no compile" test for manually validating changes.
146 #if 0
147 TEST(ScopedGenericTest, NoCompile) {
148 // Assignment shouldn't work.
150 ScopedInt a(kFirst, traits);
151 ScopedInt b(a);
154 // Comparison shouldn't work.
156 ScopedInt a(kFirst, traits);
157 ScopedInt b(kFirst, traits);
158 if (a == b) {
162 // Implicit conversion to bool shouldn't work.
164 ScopedInt a(kFirst, traits);
165 bool result = a;
168 #endif
170 } // namespace base