OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 84 |
85 void GrabAndDrop(scoped_ptr<ConDecLogger> logger) { | 85 void GrabAndDrop(scoped_ptr<ConDecLogger> logger) { |
86 } | 86 } |
87 | 87 |
88 // Do not delete this function! It's existence is to test that you can | 88 // Do not delete this function! It's existence is to test that you can |
89 // return a temporarily constructed version of the scoper. | 89 // return a temporarily constructed version of the scoper. |
90 scoped_ptr<ConDecLogger> TestReturnOfType(int* constructed) { | 90 scoped_ptr<ConDecLogger> TestReturnOfType(int* constructed) { |
91 return scoped_ptr<ConDecLogger>(new ConDecLogger(constructed)); | 91 return scoped_ptr<ConDecLogger>(new ConDecLogger(constructed)); |
92 } | 92 } |
93 | 93 |
94 scoped_ptr<ConDecLoggerParent> UpcastUsingPassAs( | |
95 scoped_ptr<ConDecLogger> object) { | |
96 return object.PassAs<ConDecLoggerParent>(); | |
97 } | |
98 | |
99 } // namespace | 94 } // namespace |
100 | 95 |
101 TEST(ScopedPtrTest, ScopedPtr) { | 96 TEST(ScopedPtrTest, ScopedPtr) { |
102 int constructed = 0; | 97 int constructed = 0; |
103 | 98 |
104 // Ensure size of scoped_ptr<> doesn't increase unexpectedly. | 99 // Ensure size of scoped_ptr<> doesn't increase unexpectedly. |
105 COMPILE_ASSERT(sizeof(int*) >= sizeof(scoped_ptr<int>), | 100 COMPILE_ASSERT(sizeof(int*) >= sizeof(scoped_ptr<int>), |
106 scoped_ptr_larger_than_raw_ptr); | 101 scoped_ptr_larger_than_raw_ptr); |
107 | 102 |
108 { | 103 { |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 EXPECT_EQ(0, constructed); | 447 EXPECT_EQ(0, constructed); |
453 | 448 |
454 // Call TestReturnOfType() so the compiler doesn't warn for an unused | 449 // Call TestReturnOfType() so the compiler doesn't warn for an unused |
455 // function. | 450 // function. |
456 { | 451 { |
457 TestReturnOfType(&constructed); | 452 TestReturnOfType(&constructed); |
458 } | 453 } |
459 EXPECT_EQ(0, constructed); | 454 EXPECT_EQ(0, constructed); |
460 } | 455 } |
461 | 456 |
462 TEST(ScopedPtrTest, PassAs) { | |
463 int constructed = 0; | |
464 { | |
465 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); | |
466 EXPECT_EQ(1, constructed); | |
467 EXPECT_TRUE(scoper.get()); | |
468 | |
469 scoped_ptr<ConDecLoggerParent> scoper_parent; | |
470 scoper_parent = UpcastUsingPassAs(scoper.Pass()); | |
471 EXPECT_EQ(1, constructed); | |
472 EXPECT_TRUE(scoper_parent.get()); | |
473 EXPECT_FALSE(scoper.get()); | |
474 } | |
475 EXPECT_EQ(0, constructed); | |
476 } | |
477 | |
478 TEST(ScopedPtrTest, CustomDeleter) { | 457 TEST(ScopedPtrTest, CustomDeleter) { |
479 double dummy_value; // Custom deleter never touches this value. | 458 double dummy_value; // Custom deleter never touches this value. |
480 int deletes = 0; | 459 int deletes = 0; |
481 int alternate_deletes = 0; | 460 int alternate_deletes = 0; |
482 | 461 |
483 // Normal delete support. | 462 // Normal delete support. |
484 { | 463 { |
485 deletes = 0; | 464 deletes = 0; |
486 scoped_ptr<double, CountingDeleter> scoper(&dummy_value, | 465 scoped_ptr<double, CountingDeleter> scoper(&dummy_value, |
487 CountingDeleter(&deletes)); | 466 CountingDeleter(&deletes)); |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 TEST(ScopedPtrTest, SelfResetWithCustomDeleterOptOut) { | 666 TEST(ScopedPtrTest, SelfResetWithCustomDeleterOptOut) { |
688 // A custom deleter should be able to opt out of self-reset abort behavior. | 667 // A custom deleter should be able to opt out of self-reset abort behavior. |
689 struct NoOpDeleter { | 668 struct NoOpDeleter { |
690 typedef void AllowSelfReset; | 669 typedef void AllowSelfReset; |
691 inline void operator()(int*) {} | 670 inline void operator()(int*) {} |
692 }; | 671 }; |
693 scoped_ptr<int> owner(new int); | 672 scoped_ptr<int> owner(new int); |
694 scoped_ptr<int, NoOpDeleter> x(owner.get()); | 673 scoped_ptr<int, NoOpDeleter> x(owner.get()); |
695 x.reset(x.get()); | 674 x.reset(x.get()); |
696 } | 675 } |
OLD | NEW |