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

Side by Side Diff: base/memory/scoped_ptr_unittest.cc

Issue 8931008: Revert of "Redo r113722 - Add Pass(), which implements move semantics, to scoped_ptr, scoped_ar..." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « base/memory/scoped_ptr.h ('k') | chrome/common/extensions/extension_manifests_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 namespace { 9 namespace {
10 10
11 class ConDecLogger { 11 class ConDecLogger {
12 public: 12 public:
13 ConDecLogger() : ptr_(NULL) { } 13 ConDecLogger() : ptr_(NULL) { }
14 explicit ConDecLogger(int* ptr) { set_ptr(ptr); } 14 explicit ConDecLogger(int* ptr) { set_ptr(ptr); }
15 ~ConDecLogger() { --*ptr_; } 15 ~ConDecLogger() { --*ptr_; }
16 16
17 void set_ptr(int* ptr) { ptr_ = ptr; ++*ptr_; } 17 void set_ptr(int* ptr) { ptr_ = ptr; ++*ptr_; }
18 18
19 int SomeMeth(int x) { return x; } 19 int SomeMeth(int x) { return x; }
20 20
21 private: 21 private:
22 int* ptr_; 22 int* ptr_;
23 DISALLOW_COPY_AND_ASSIGN(ConDecLogger); 23 DISALLOW_COPY_AND_ASSIGN(ConDecLogger);
24 }; 24 };
25 25
26 scoped_ptr<ConDecLogger> PassThru(scoped_ptr<ConDecLogger> logger) {
27 return logger.Pass();
28 }
29
30 void GrabAndDrop(scoped_ptr<ConDecLogger> logger) {
31 }
32
33 // Do not delete this function! It's existence is to test that you can
34 // return a temporarily constructed version of the scoper.
35 scoped_ptr<ConDecLogger> TestReturnOfType(int* constructed) {
36 return scoped_ptr<ConDecLogger>(new ConDecLogger(constructed));
37 }
38
39 } // namespace 26 } // namespace
40 27
41 TEST(ScopedPtrTest, ScopedPtr) { 28 TEST(ScopedPtrTest, ScopedPtr) {
42 int constructed = 0; 29 int constructed = 0;
43 30
44 { 31 {
45 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); 32 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
46 EXPECT_EQ(1, constructed); 33 EXPECT_EQ(1, constructed);
47 EXPECT_TRUE(scoper.get()); 34 EXPECT_TRUE(scoper.get());
48 35
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 159
173 scoper2.swap(scoper1); 160 scoper2.swap(scoper1);
174 EXPECT_EQ(loggers, scoper2.get()); 161 EXPECT_EQ(loggers, scoper2.get());
175 EXPECT_FALSE(scoper1.get()); 162 EXPECT_FALSE(scoper1.get());
176 EXPECT_FALSE(scoper1 == scoper2.get()); 163 EXPECT_FALSE(scoper1 == scoper2.get());
177 EXPECT_TRUE(scoper1 != scoper2.get()); 164 EXPECT_TRUE(scoper1 != scoper2.get());
178 } 165 }
179 EXPECT_EQ(0, constructed); 166 EXPECT_EQ(0, constructed);
180 } 167 }
181 168
182 TEST(ScopedPtrTest, PassBehavior) {
183 int constructed = 0;
184 {
185 ConDecLogger* logger = new ConDecLogger(&constructed);
186 scoped_ptr<ConDecLogger> scoper(logger);
187 EXPECT_EQ(1, constructed);
188
189 // Test Pass() with constructor;
190 scoped_ptr<ConDecLogger> scoper2(scoper.Pass());
191 EXPECT_EQ(1, constructed);
192
193 // Test Pass() with assignment;
194 scoped_ptr<ConDecLogger> scoper3;
195 scoper3 = scoper2.Pass();
196 EXPECT_EQ(1, constructed);
197 EXPECT_FALSE(scoper.get());
198 EXPECT_FALSE(scoper2.get());
199 EXPECT_TRUE(scoper3.get());
200 }
201
202 // Test uncaught Pass() does not leak.
203 {
204 ConDecLogger* logger = new ConDecLogger(&constructed);
205 scoped_ptr<ConDecLogger> scoper(logger);
206 EXPECT_EQ(1, constructed);
207
208 // Should auto-destruct logger by end of scope.
209 scoper.Pass();
210 EXPECT_FALSE(scoper.get());
211 }
212 EXPECT_EQ(0, constructed);
213
214 // Test that passing to function which does nothing does not leak.
215 {
216 ConDecLogger* logger = new ConDecLogger(&constructed);
217 scoped_ptr<ConDecLogger> scoper(logger);
218 EXPECT_EQ(1, constructed);
219
220 // Should auto-destruct logger by end of scope.
221 GrabAndDrop(scoper.Pass());
222 EXPECT_FALSE(scoper.get());
223 }
224 EXPECT_EQ(0, constructed);
225 }
226
227 TEST(ScopedPtrTest, ReturnTypeBehavior) {
228 int constructed = 0;
229
230 // Test that we can return a scoped_ptr.
231 {
232 ConDecLogger* logger = new ConDecLogger(&constructed);
233 scoped_ptr<ConDecLogger> scoper(logger);
234 EXPECT_EQ(1, constructed);
235
236 PassThru(scoper.Pass());
237 EXPECT_FALSE(scoper.get());
238 }
239 EXPECT_EQ(0, constructed);
240
241 // Test uncaught return type not leak.
242 {
243 ConDecLogger* logger = new ConDecLogger(&constructed);
244 scoped_ptr<ConDecLogger> scoper(logger);
245 EXPECT_EQ(1, constructed);
246
247 // Should auto-destruct logger by end of scope.
248 PassThru(scoper.Pass());
249 EXPECT_FALSE(scoper.get());
250 }
251 EXPECT_EQ(0, constructed);
252
253 // Call TestReturnOfType() so the compiler doesn't warn for an unused
254 // function.
255 {
256 TestReturnOfType(&constructed);
257 }
258 EXPECT_EQ(0, constructed);
259 }
260
261 // TODO scoped_ptr_malloc 169 // TODO scoped_ptr_malloc
OLDNEW
« no previous file with comments | « base/memory/scoped_ptr.h ('k') | chrome/common/extensions/extension_manifests_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698