OLD | NEW |
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 |
26 } // namespace | 39 } // namespace |
27 | 40 |
28 TEST(ScopedPtrTest, ScopedPtr) { | 41 TEST(ScopedPtrTest, ScopedPtr) { |
29 int constructed = 0; | 42 int constructed = 0; |
30 | 43 |
31 { | 44 { |
32 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); | 45 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); |
33 EXPECT_EQ(1, constructed); | 46 EXPECT_EQ(1, constructed); |
34 EXPECT_TRUE(scoper.get()); | 47 EXPECT_TRUE(scoper.get()); |
35 | 48 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 172 |
160 scoper2.swap(scoper1); | 173 scoper2.swap(scoper1); |
161 EXPECT_EQ(loggers, scoper2.get()); | 174 EXPECT_EQ(loggers, scoper2.get()); |
162 EXPECT_FALSE(scoper1.get()); | 175 EXPECT_FALSE(scoper1.get()); |
163 EXPECT_FALSE(scoper1 == scoper2.get()); | 176 EXPECT_FALSE(scoper1 == scoper2.get()); |
164 EXPECT_TRUE(scoper1 != scoper2.get()); | 177 EXPECT_TRUE(scoper1 != scoper2.get()); |
165 } | 178 } |
166 EXPECT_EQ(0, constructed); | 179 EXPECT_EQ(0, constructed); |
167 } | 180 } |
168 | 181 |
| 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 |
169 // TODO scoped_ptr_malloc | 261 // TODO scoped_ptr_malloc |
OLD | NEW |