| OLD | NEW |
| 1 // Copyright (c) 2011 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/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 // Used to test depth subtyping. |
| 12 class ConDecLoggerParent { |
| 13 public: |
| 14 virtual ~ConDecLoggerParent() {} |
| 15 virtual void set_ptr(int* ptr) = 0; |
| 16 |
| 17 virtual int SomeMeth(int x) const = 0; |
| 18 }; |
| 19 |
| 20 class ConDecLogger : public ConDecLoggerParent { |
| 12 public: | 21 public: |
| 13 ConDecLogger() : ptr_(NULL) { } | 22 ConDecLogger() : ptr_(NULL) { } |
| 14 explicit ConDecLogger(int* ptr) { set_ptr(ptr); } | 23 explicit ConDecLogger(int* ptr) { set_ptr(ptr); } |
| 15 ~ConDecLogger() { --*ptr_; } | 24 virtual ~ConDecLogger() { --*ptr_; } |
| 16 | 25 |
| 17 void set_ptr(int* ptr) { ptr_ = ptr; ++*ptr_; } | 26 virtual void set_ptr(int* ptr) { ptr_ = ptr; ++*ptr_; } |
| 18 | 27 |
| 19 int SomeMeth(int x) { return x; } | 28 virtual int SomeMeth(int x) const { return x; } |
| 20 | 29 |
| 21 private: | 30 private: |
| 22 int* ptr_; | 31 int* ptr_; |
| 23 DISALLOW_COPY_AND_ASSIGN(ConDecLogger); | 32 DISALLOW_COPY_AND_ASSIGN(ConDecLogger); |
| 24 }; | 33 }; |
| 25 | 34 |
| 26 scoped_ptr<ConDecLogger> PassThru(scoped_ptr<ConDecLogger> logger) { | 35 scoped_ptr<ConDecLogger> PassThru(scoped_ptr<ConDecLogger> logger) { |
| 27 return logger.Pass(); | 36 return logger.Pass(); |
| 28 } | 37 } |
| 29 | 38 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 107 |
| 99 scoper2.swap(scoper1); | 108 scoper2.swap(scoper1); |
| 100 EXPECT_EQ(logger, scoper2.get()); | 109 EXPECT_EQ(logger, scoper2.get()); |
| 101 EXPECT_FALSE(scoper1.get()); | 110 EXPECT_FALSE(scoper1.get()); |
| 102 EXPECT_FALSE(scoper1 == scoper2.get()); | 111 EXPECT_FALSE(scoper1 == scoper2.get()); |
| 103 EXPECT_TRUE(scoper1 != scoper2.get()); | 112 EXPECT_TRUE(scoper1 != scoper2.get()); |
| 104 } | 113 } |
| 105 EXPECT_EQ(0, constructed); | 114 EXPECT_EQ(0, constructed); |
| 106 } | 115 } |
| 107 | 116 |
| 117 TEST(ScopedPtrTest, ScopedPtrDepthSubtyping) { |
| 118 int constructed = 0; |
| 119 |
| 120 // Test construction from a scoped_ptr to a derived class. |
| 121 { |
| 122 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); |
| 123 EXPECT_EQ(1, constructed); |
| 124 EXPECT_TRUE(scoper.get()); |
| 125 |
| 126 scoped_ptr<ConDecLoggerParent> scoper_parent(scoper.Pass()); |
| 127 EXPECT_EQ(1, constructed); |
| 128 EXPECT_TRUE(scoper_parent.get()); |
| 129 EXPECT_FALSE(scoper.get()); |
| 130 |
| 131 EXPECT_EQ(10, scoper_parent->SomeMeth(10)); |
| 132 EXPECT_EQ(10, scoper_parent.get()->SomeMeth(10)); |
| 133 EXPECT_EQ(10, (*scoper_parent).SomeMeth(10)); |
| 134 } |
| 135 EXPECT_EQ(0, constructed); |
| 136 |
| 137 // Test assignment from a scoped_ptr to a derived class. |
| 138 { |
| 139 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); |
| 140 EXPECT_EQ(1, constructed); |
| 141 EXPECT_TRUE(scoper.get()); |
| 142 |
| 143 scoped_ptr<ConDecLoggerParent> scoper_parent; |
| 144 scoper_parent = scoper.Pass(); |
| 145 EXPECT_EQ(1, constructed); |
| 146 EXPECT_TRUE(scoper_parent.get()); |
| 147 EXPECT_FALSE(scoper.get()); |
| 148 } |
| 149 EXPECT_EQ(0, constructed); |
| 150 |
| 151 // Test construction of a scoped_ptr with an additional const annotation. |
| 152 { |
| 153 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); |
| 154 EXPECT_EQ(1, constructed); |
| 155 EXPECT_TRUE(scoper.get()); |
| 156 |
| 157 scoped_ptr<const ConDecLogger> scoper_const(scoper.Pass()); |
| 158 EXPECT_EQ(1, constructed); |
| 159 EXPECT_TRUE(scoper_const.get()); |
| 160 EXPECT_FALSE(scoper.get()); |
| 161 |
| 162 EXPECT_EQ(10, scoper_const->SomeMeth(10)); |
| 163 EXPECT_EQ(10, scoper_const.get()->SomeMeth(10)); |
| 164 EXPECT_EQ(10, (*scoper_const).SomeMeth(10)); |
| 165 } |
| 166 EXPECT_EQ(0, constructed); |
| 167 |
| 168 // Test assignment to a scoped_ptr with an additional const annotation. |
| 169 { |
| 170 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); |
| 171 EXPECT_EQ(1, constructed); |
| 172 EXPECT_TRUE(scoper.get()); |
| 173 |
| 174 scoped_ptr<const ConDecLogger> scoper_const; |
| 175 scoper_const = scoper.Pass(); |
| 176 EXPECT_EQ(1, constructed); |
| 177 EXPECT_TRUE(scoper_const.get()); |
| 178 EXPECT_FALSE(scoper.get()); |
| 179 } |
| 180 EXPECT_EQ(0, constructed); |
| 181 } |
| 182 |
| 108 TEST(ScopedPtrTest, ScopedArray) { | 183 TEST(ScopedPtrTest, ScopedArray) { |
| 109 static const int kNumLoggers = 12; | 184 static const int kNumLoggers = 12; |
| 110 | 185 |
| 111 int constructed = 0; | 186 int constructed = 0; |
| 112 | 187 |
| 113 { | 188 { |
| 114 scoped_array<ConDecLogger> scoper(new ConDecLogger[kNumLoggers]); | 189 scoped_array<ConDecLogger> scoper(new ConDecLogger[kNumLoggers]); |
| 115 EXPECT_TRUE(scoper.get()); | 190 EXPECT_TRUE(scoper.get()); |
| 116 EXPECT_EQ(&scoper[0], scoper.get()); | 191 EXPECT_EQ(&scoper[0], scoper.get()); |
| 117 for (int i = 0; i < kNumLoggers; ++i) { | 192 for (int i = 0; i < kNumLoggers; ++i) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 | 327 |
| 253 // Call TestReturnOfType() so the compiler doesn't warn for an unused | 328 // Call TestReturnOfType() so the compiler doesn't warn for an unused |
| 254 // function. | 329 // function. |
| 255 { | 330 { |
| 256 TestReturnOfType(&constructed); | 331 TestReturnOfType(&constructed); |
| 257 } | 332 } |
| 258 EXPECT_EQ(0, constructed); | 333 EXPECT_EQ(0, constructed); |
| 259 } | 334 } |
| 260 | 335 |
| 261 // TODO scoped_ptr_malloc | 336 // TODO scoped_ptr_malloc |
| OLD | NEW |