Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: base/scoped_generic_unittest.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/scoped_generic.h ('k') | base/scoped_native_library.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include <vector>
6
7 #include "base/scoped_generic.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace base {
11
12 namespace {
13
14 struct IntTraits {
15 IntTraits(std::vector<int>* freed) : freed_ints(freed) {}
16
17 static int InvalidValue() {
18 return -1;
19 }
20 void Free(int value) {
21 freed_ints->push_back(value);
22 }
23
24 std::vector<int>* freed_ints;
25 };
26
27 typedef ScopedGeneric<int, IntTraits> ScopedInt;
28
29 } // namespace
30
31 TEST(ScopedGenericTest, ScopedGeneric) {
32 std::vector<int> values_freed;
33 IntTraits traits(&values_freed);
34
35 // Invalid case, delete should not be called.
36 {
37 ScopedInt a(IntTraits::InvalidValue(), traits);
38 }
39 EXPECT_TRUE(values_freed.empty());
40
41 // Simple deleting case.
42 static const int kFirst = 0;
43 {
44 ScopedInt a(kFirst, traits);
45 }
46 ASSERT_EQ(1u, values_freed.size());
47 ASSERT_EQ(kFirst, values_freed[0]);
48 values_freed.clear();
49
50 // Release should return the right value and leave the object empty.
51 {
52 ScopedInt a(kFirst, traits);
53 EXPECT_EQ(kFirst, a.release());
54
55 ScopedInt b(IntTraits::InvalidValue(), traits);
56 EXPECT_EQ(IntTraits::InvalidValue(), b.release());
57 }
58 ASSERT_TRUE(values_freed.empty());
59
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;
63 {
64 ScopedInt b(kFirst, traits);
65 b.reset(kSecond);
66 ASSERT_EQ(1u, values_freed.size());
67 ASSERT_EQ(kFirst, values_freed[0]);
68 }
69 ASSERT_EQ(2u, values_freed.size());
70 ASSERT_EQ(kSecond, values_freed[1]);
71 values_freed.clear();
72
73 // Swap.
74 {
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());
81 }
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();
87
88 // Pass constructor.
89 {
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());
95 }
96
97 ASSERT_EQ(1u, values_freed.size());
98 ASSERT_EQ(kFirst, values_freed[0]);
99 values_freed.clear();
100
101 // Pass assign.
102 {
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());
110 }
111
112 ASSERT_EQ(2u, values_freed.size());
113 EXPECT_EQ(kFirst, values_freed[1]);
114 values_freed.clear();
115 }
116
117 TEST(ScopedGenericTest, Operators) {
118 std::vector<int> values_freed;
119 IntTraits traits(&values_freed);
120
121 static const int kFirst = 0;
122 static const int kSecond = 1;
123 {
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);
129
130 EXPECT_TRUE(kFirst == a);
131 EXPECT_FALSE(kFirst != a);
132 EXPECT_FALSE(kSecond == a);
133 EXPECT_TRUE(kSecond != a);
134 }
135
136 // is_valid().
137 {
138 ScopedInt a(kFirst, traits);
139 EXPECT_TRUE(a.is_valid());
140 a.reset();
141 EXPECT_FALSE(a.is_valid());
142 }
143 }
144
145 // Cheesy manual "no compile" test for manually validating changes.
146 #if 0
147 TEST(ScopedGenericTest, NoCompile) {
148 // Assignment shouldn't work.
149 /*{
150 ScopedInt a(kFirst, traits);
151 ScopedInt b(a);
152 }*/
153
154 // Comparison shouldn't work.
155 /*{
156 ScopedInt a(kFirst, traits);
157 ScopedInt b(kFirst, traits);
158 if (a == b) {
159 }
160 }*/
161
162 // Implicit conversion to bool shouldn't work.
163 /*{
164 ScopedInt a(kFirst, traits);
165 bool result = a;
166 }*/
167 }
168 #endif
169
170 } // namespace base
OLDNEW
« no previous file with comments | « base/scoped_generic.h ('k') | base/scoped_native_library.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698