Roll src/third_party/WebKit 640e652:eec14d5 (svn 200948:200949)
[chromium-blink-merge.git] / base / memory / scoped_ptr_unittest.cc
blob766f4444001e828cd077ca39b85c7dd326779c65
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
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
7 #include <sstream>
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace {
16 // Used to test depth subtyping.
17 class ConDecLoggerParent {
18 public:
19 virtual ~ConDecLoggerParent() {}
21 virtual void SetPtr(int* ptr) = 0;
23 virtual int SomeMeth(int x) const = 0;
26 class ConDecLogger : public ConDecLoggerParent {
27 public:
28 ConDecLogger() : ptr_(NULL) { }
29 explicit ConDecLogger(int* ptr) { SetPtr(ptr); }
30 ~ConDecLogger() override { --*ptr_; }
32 void SetPtr(int* ptr) override {
33 ptr_ = ptr;
34 ++*ptr_;
37 int SomeMeth(int x) const override { return x; }
39 private:
40 int* ptr_;
42 DISALLOW_COPY_AND_ASSIGN(ConDecLogger);
45 struct CountingDeleter {
46 explicit CountingDeleter(int* count) : count_(count) {}
47 inline void operator()(double* ptr) const {
48 (*count_)++;
50 int* count_;
53 // Used to test assignment of convertible deleters.
54 struct CountingDeleterChild : public CountingDeleter {
55 explicit CountingDeleterChild(int* count) : CountingDeleter(count) {}
58 class OverloadedNewAndDelete {
59 public:
60 void* operator new(size_t size) {
61 g_new_count++;
62 return malloc(size);
65 void operator delete(void* ptr) {
66 g_delete_count++;
67 free(ptr);
70 static void ResetCounters() {
71 g_new_count = 0;
72 g_delete_count = 0;
75 static int new_count() { return g_new_count; }
76 static int delete_count() { return g_delete_count; }
78 private:
79 static int g_new_count;
80 static int g_delete_count;
83 int OverloadedNewAndDelete::g_new_count = 0;
84 int OverloadedNewAndDelete::g_delete_count = 0;
86 scoped_ptr<ConDecLogger> PassThru(scoped_ptr<ConDecLogger> logger) {
87 return logger.Pass();
90 void GrabAndDrop(scoped_ptr<ConDecLogger> logger) {
93 // Do not delete this function! It's existence is to test that you can
94 // return a temporarily constructed version of the scoper.
95 scoped_ptr<ConDecLogger> TestReturnOfType(int* constructed) {
96 return scoped_ptr<ConDecLogger>(new ConDecLogger(constructed));
99 } // namespace
101 TEST(ScopedPtrTest, ScopedPtr) {
102 int constructed = 0;
104 // Ensure size of scoped_ptr<> doesn't increase unexpectedly.
105 COMPILE_ASSERT(sizeof(int*) >= sizeof(scoped_ptr<int>),
106 scoped_ptr_larger_than_raw_ptr);
109 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
110 EXPECT_EQ(1, constructed);
111 EXPECT_TRUE(scoper.get());
113 EXPECT_EQ(10, scoper->SomeMeth(10));
114 EXPECT_EQ(10, scoper.get()->SomeMeth(10));
115 EXPECT_EQ(10, (*scoper).SomeMeth(10));
117 EXPECT_EQ(0, constructed);
119 // Test reset() and release()
121 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
122 EXPECT_EQ(1, constructed);
123 EXPECT_TRUE(scoper.get());
125 scoper.reset(new ConDecLogger(&constructed));
126 EXPECT_EQ(1, constructed);
127 EXPECT_TRUE(scoper.get());
129 scoper.reset();
130 EXPECT_EQ(0, constructed);
131 EXPECT_FALSE(scoper.get());
133 scoper.reset(new ConDecLogger(&constructed));
134 EXPECT_EQ(1, constructed);
135 EXPECT_TRUE(scoper.get());
137 ConDecLogger* take = scoper.release();
138 EXPECT_EQ(1, constructed);
139 EXPECT_FALSE(scoper.get());
140 delete take;
141 EXPECT_EQ(0, constructed);
143 scoper.reset(new ConDecLogger(&constructed));
144 EXPECT_EQ(1, constructed);
145 EXPECT_TRUE(scoper.get());
147 EXPECT_EQ(0, constructed);
149 // Test swap(), == and !=
151 scoped_ptr<ConDecLogger> scoper1;
152 scoped_ptr<ConDecLogger> scoper2;
153 EXPECT_TRUE(scoper1 == scoper2.get());
154 EXPECT_FALSE(scoper1 != scoper2.get());
156 ConDecLogger* logger = new ConDecLogger(&constructed);
157 scoper1.reset(logger);
158 EXPECT_EQ(logger, scoper1.get());
159 EXPECT_FALSE(scoper2.get());
160 EXPECT_FALSE(scoper1 == scoper2.get());
161 EXPECT_TRUE(scoper1 != scoper2.get());
163 scoper2.swap(scoper1);
164 EXPECT_EQ(logger, scoper2.get());
165 EXPECT_FALSE(scoper1.get());
166 EXPECT_FALSE(scoper1 == scoper2.get());
167 EXPECT_TRUE(scoper1 != scoper2.get());
169 EXPECT_EQ(0, constructed);
172 TEST(ScopedPtrTest, ScopedPtrDepthSubtyping) {
173 int constructed = 0;
175 // Test construction from a scoped_ptr to a derived class.
177 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
178 EXPECT_EQ(1, constructed);
179 EXPECT_TRUE(scoper.get());
181 scoped_ptr<ConDecLoggerParent> scoper_parent(scoper.Pass());
182 EXPECT_EQ(1, constructed);
183 EXPECT_TRUE(scoper_parent.get());
184 EXPECT_FALSE(scoper.get());
186 EXPECT_EQ(10, scoper_parent->SomeMeth(10));
187 EXPECT_EQ(10, scoper_parent.get()->SomeMeth(10));
188 EXPECT_EQ(10, (*scoper_parent).SomeMeth(10));
190 EXPECT_EQ(0, constructed);
192 // Test assignment from a scoped_ptr to a derived class.
194 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
195 EXPECT_EQ(1, constructed);
196 EXPECT_TRUE(scoper.get());
198 scoped_ptr<ConDecLoggerParent> scoper_parent;
199 scoper_parent = scoper.Pass();
200 EXPECT_EQ(1, constructed);
201 EXPECT_TRUE(scoper_parent.get());
202 EXPECT_FALSE(scoper.get());
204 EXPECT_EQ(0, constructed);
206 // Test construction of a scoped_ptr with an additional const annotation.
208 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
209 EXPECT_EQ(1, constructed);
210 EXPECT_TRUE(scoper.get());
212 scoped_ptr<const ConDecLogger> scoper_const(scoper.Pass());
213 EXPECT_EQ(1, constructed);
214 EXPECT_TRUE(scoper_const.get());
215 EXPECT_FALSE(scoper.get());
217 EXPECT_EQ(10, scoper_const->SomeMeth(10));
218 EXPECT_EQ(10, scoper_const.get()->SomeMeth(10));
219 EXPECT_EQ(10, (*scoper_const).SomeMeth(10));
221 EXPECT_EQ(0, constructed);
223 // Test assignment to a scoped_ptr with an additional const annotation.
225 scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
226 EXPECT_EQ(1, constructed);
227 EXPECT_TRUE(scoper.get());
229 scoped_ptr<const ConDecLogger> scoper_const;
230 scoper_const = scoper.Pass();
231 EXPECT_EQ(1, constructed);
232 EXPECT_TRUE(scoper_const.get());
233 EXPECT_FALSE(scoper.get());
235 EXPECT_EQ(0, constructed);
237 // Test assignment to a scoped_ptr deleter of parent type.
239 // Custom deleters never touch these value.
240 double dummy_value, dummy_value2;
241 int deletes = 0;
242 int alternate_deletes = 0;
243 scoped_ptr<double, CountingDeleter> scoper(&dummy_value,
244 CountingDeleter(&deletes));
245 scoped_ptr<double, CountingDeleterChild> scoper_child(
246 &dummy_value2, CountingDeleterChild(&alternate_deletes));
248 EXPECT_TRUE(scoper);
249 EXPECT_TRUE(scoper_child);
250 EXPECT_EQ(0, deletes);
251 EXPECT_EQ(0, alternate_deletes);
253 // Test this compiles and correctly overwrites the deleter state.
254 scoper = scoper_child.Pass();
255 EXPECT_TRUE(scoper);
256 EXPECT_FALSE(scoper_child);
257 EXPECT_EQ(1, deletes);
258 EXPECT_EQ(0, alternate_deletes);
260 scoper.reset();
261 EXPECT_FALSE(scoper);
262 EXPECT_FALSE(scoper_child);
263 EXPECT_EQ(1, deletes);
264 EXPECT_EQ(1, alternate_deletes);
266 scoper_child.reset(&dummy_value);
267 EXPECT_TRUE(scoper_child);
268 EXPECT_EQ(1, deletes);
269 EXPECT_EQ(1, alternate_deletes);
270 scoped_ptr<double, CountingDeleter> scoper_construct(scoper_child.Pass());
271 EXPECT_TRUE(scoper_construct);
272 EXPECT_FALSE(scoper_child);
273 EXPECT_EQ(1, deletes);
274 EXPECT_EQ(1, alternate_deletes);
276 scoper_construct.reset();
277 EXPECT_EQ(1, deletes);
278 EXPECT_EQ(2, alternate_deletes);
282 TEST(ScopedPtrTest, ScopedPtrWithArray) {
283 static const int kNumLoggers = 12;
285 int constructed = 0;
288 scoped_ptr<ConDecLogger[]> scoper(new ConDecLogger[kNumLoggers]);
289 EXPECT_TRUE(scoper);
290 EXPECT_EQ(&scoper[0], scoper.get());
291 for (int i = 0; i < kNumLoggers; ++i) {
292 scoper[i].SetPtr(&constructed);
294 EXPECT_EQ(12, constructed);
296 EXPECT_EQ(10, scoper.get()->SomeMeth(10));
297 EXPECT_EQ(10, scoper[2].SomeMeth(10));
299 EXPECT_EQ(0, constructed);
301 // Test reset() and release()
303 scoped_ptr<ConDecLogger[]> scoper;
304 EXPECT_FALSE(scoper.get());
305 EXPECT_FALSE(scoper.release());
306 EXPECT_FALSE(scoper.get());
307 scoper.reset();
308 EXPECT_FALSE(scoper.get());
310 scoper.reset(new ConDecLogger[kNumLoggers]);
311 for (int i = 0; i < kNumLoggers; ++i) {
312 scoper[i].SetPtr(&constructed);
314 EXPECT_EQ(12, constructed);
315 scoper.reset();
316 EXPECT_EQ(0, constructed);
318 scoper.reset(new ConDecLogger[kNumLoggers]);
319 for (int i = 0; i < kNumLoggers; ++i) {
320 scoper[i].SetPtr(&constructed);
322 EXPECT_EQ(12, constructed);
323 ConDecLogger* ptr = scoper.release();
324 EXPECT_EQ(12, constructed);
325 delete[] ptr;
326 EXPECT_EQ(0, constructed);
328 EXPECT_EQ(0, constructed);
330 // Test swap(), ==, !=, and type-safe Boolean.
332 scoped_ptr<ConDecLogger[]> scoper1;
333 scoped_ptr<ConDecLogger[]> scoper2;
334 EXPECT_TRUE(scoper1 == scoper2.get());
335 EXPECT_FALSE(scoper1 != scoper2.get());
337 ConDecLogger* loggers = new ConDecLogger[kNumLoggers];
338 for (int i = 0; i < kNumLoggers; ++i) {
339 loggers[i].SetPtr(&constructed);
341 scoper1.reset(loggers);
342 EXPECT_TRUE(scoper1);
343 EXPECT_EQ(loggers, scoper1.get());
344 EXPECT_FALSE(scoper2);
345 EXPECT_FALSE(scoper2.get());
346 EXPECT_FALSE(scoper1 == scoper2.get());
347 EXPECT_TRUE(scoper1 != scoper2.get());
349 scoper2.swap(scoper1);
350 EXPECT_EQ(loggers, scoper2.get());
351 EXPECT_FALSE(scoper1.get());
352 EXPECT_FALSE(scoper1 == scoper2.get());
353 EXPECT_TRUE(scoper1 != scoper2.get());
355 EXPECT_EQ(0, constructed);
358 ConDecLogger* loggers = new ConDecLogger[kNumLoggers];
359 scoped_ptr<ConDecLogger[]> scoper(loggers);
360 EXPECT_TRUE(scoper);
361 for (int i = 0; i < kNumLoggers; ++i) {
362 scoper[i].SetPtr(&constructed);
364 EXPECT_EQ(kNumLoggers, constructed);
366 // Test Pass() with constructor;
367 scoped_ptr<ConDecLogger[]> scoper2(scoper.Pass());
368 EXPECT_EQ(kNumLoggers, constructed);
370 // Test Pass() with assignment;
371 scoped_ptr<ConDecLogger[]> scoper3;
372 scoper3 = scoper2.Pass();
373 EXPECT_EQ(kNumLoggers, constructed);
374 EXPECT_FALSE(scoper);
375 EXPECT_FALSE(scoper2);
376 EXPECT_TRUE(scoper3);
378 EXPECT_EQ(0, constructed);
381 TEST(ScopedPtrTest, PassBehavior) {
382 int constructed = 0;
384 ConDecLogger* logger = new ConDecLogger(&constructed);
385 scoped_ptr<ConDecLogger> scoper(logger);
386 EXPECT_EQ(1, constructed);
388 // Test Pass() with constructor;
389 scoped_ptr<ConDecLogger> scoper2(scoper.Pass());
390 EXPECT_EQ(1, constructed);
392 // Test Pass() with assignment;
393 scoped_ptr<ConDecLogger> scoper3;
394 scoper3 = scoper2.Pass();
395 EXPECT_EQ(1, constructed);
396 EXPECT_FALSE(scoper.get());
397 EXPECT_FALSE(scoper2.get());
398 EXPECT_TRUE(scoper3.get());
401 // Test uncaught Pass() does not have side effects.
403 ConDecLogger* logger = new ConDecLogger(&constructed);
404 scoped_ptr<ConDecLogger> scoper(logger);
405 EXPECT_EQ(1, constructed);
407 // Should auto-destruct logger by end of scope.
408 scoped_ptr<ConDecLogger>&& rvalue = scoper.Pass();
409 // The Pass() function mimics std::move(), which does not have side-effects.
410 EXPECT_TRUE(scoper.get());
411 EXPECT_TRUE(rvalue);
413 EXPECT_EQ(0, constructed);
415 // Test that passing to function which does nothing does not leak.
417 ConDecLogger* logger = new ConDecLogger(&constructed);
418 scoped_ptr<ConDecLogger> scoper(logger);
419 EXPECT_EQ(1, constructed);
421 // Should auto-destruct logger by end of scope.
422 GrabAndDrop(scoper.Pass());
423 EXPECT_FALSE(scoper.get());
425 EXPECT_EQ(0, constructed);
428 TEST(ScopedPtrTest, ReturnTypeBehavior) {
429 int constructed = 0;
431 // Test that we can return a scoped_ptr.
433 ConDecLogger* logger = new ConDecLogger(&constructed);
434 scoped_ptr<ConDecLogger> scoper(logger);
435 EXPECT_EQ(1, constructed);
437 PassThru(scoper.Pass());
438 EXPECT_FALSE(scoper.get());
440 EXPECT_EQ(0, constructed);
442 // Test uncaught return type not leak.
444 ConDecLogger* logger = new ConDecLogger(&constructed);
445 scoped_ptr<ConDecLogger> scoper(logger);
446 EXPECT_EQ(1, constructed);
448 // Should auto-destruct logger by end of scope.
449 PassThru(scoper.Pass());
450 EXPECT_FALSE(scoper.get());
452 EXPECT_EQ(0, constructed);
454 // Call TestReturnOfType() so the compiler doesn't warn for an unused
455 // function.
457 TestReturnOfType(&constructed);
459 EXPECT_EQ(0, constructed);
462 TEST(ScopedPtrTest, CustomDeleter) {
463 double dummy_value; // Custom deleter never touches this value.
464 int deletes = 0;
465 int alternate_deletes = 0;
467 // Normal delete support.
469 deletes = 0;
470 scoped_ptr<double, CountingDeleter> scoper(&dummy_value,
471 CountingDeleter(&deletes));
472 EXPECT_EQ(0, deletes);
473 EXPECT_TRUE(scoper.get());
475 EXPECT_EQ(1, deletes);
477 // Test reset() and release().
478 deletes = 0;
480 scoped_ptr<double, CountingDeleter> scoper(NULL,
481 CountingDeleter(&deletes));
482 EXPECT_FALSE(scoper.get());
483 EXPECT_FALSE(scoper.release());
484 EXPECT_FALSE(scoper.get());
485 scoper.reset();
486 EXPECT_FALSE(scoper.get());
487 EXPECT_EQ(0, deletes);
489 scoper.reset(&dummy_value);
490 scoper.reset();
491 EXPECT_EQ(1, deletes);
493 scoper.reset(&dummy_value);
494 EXPECT_EQ(&dummy_value, scoper.release());
496 EXPECT_EQ(1, deletes);
498 // Test get_deleter().
499 deletes = 0;
500 alternate_deletes = 0;
502 scoped_ptr<double, CountingDeleter> scoper(&dummy_value,
503 CountingDeleter(&deletes));
504 // Call deleter manually.
505 EXPECT_EQ(0, deletes);
506 scoper.get_deleter()(&dummy_value);
507 EXPECT_EQ(1, deletes);
509 // Deleter is still there after reset.
510 scoper.reset();
511 EXPECT_EQ(2, deletes);
512 scoper.get_deleter()(&dummy_value);
513 EXPECT_EQ(3, deletes);
515 // Deleter can be assigned into (matches C++11 unique_ptr<> spec).
516 scoper.get_deleter() = CountingDeleter(&alternate_deletes);
517 scoper.reset(&dummy_value);
518 EXPECT_EQ(0, alternate_deletes);
521 EXPECT_EQ(3, deletes);
522 EXPECT_EQ(1, alternate_deletes);
524 // Test operator= deleter support.
525 deletes = 0;
526 alternate_deletes = 0;
528 double dummy_value2;
529 scoped_ptr<double, CountingDeleter> scoper(&dummy_value,
530 CountingDeleter(&deletes));
531 scoped_ptr<double, CountingDeleter> scoper2(
532 &dummy_value2,
533 CountingDeleter(&alternate_deletes));
534 EXPECT_EQ(0, deletes);
535 EXPECT_EQ(0, alternate_deletes);
537 // Pass the second deleter through a constructor and an operator=. Then
538 // reinitialize the empty scopers to ensure that each one is deleting
539 // properly.
540 scoped_ptr<double, CountingDeleter> scoper3(scoper2.Pass());
541 scoper = scoper3.Pass();
542 EXPECT_EQ(1, deletes);
544 scoper2.reset(&dummy_value2);
545 scoper3.reset(&dummy_value2);
546 EXPECT_EQ(0, alternate_deletes);
549 EXPECT_EQ(1, deletes);
550 EXPECT_EQ(3, alternate_deletes);
552 // Test swap(), ==, !=, and type-safe Boolean.
554 scoped_ptr<double, CountingDeleter> scoper1(NULL,
555 CountingDeleter(&deletes));
556 scoped_ptr<double, CountingDeleter> scoper2(NULL,
557 CountingDeleter(&deletes));
558 EXPECT_TRUE(scoper1 == scoper2.get());
559 EXPECT_FALSE(scoper1 != scoper2.get());
561 scoper1.reset(&dummy_value);
562 EXPECT_TRUE(scoper1);
563 EXPECT_EQ(&dummy_value, scoper1.get());
564 EXPECT_FALSE(scoper2);
565 EXPECT_FALSE(scoper2.get());
566 EXPECT_FALSE(scoper1 == scoper2.get());
567 EXPECT_TRUE(scoper1 != scoper2.get());
569 scoper2.swap(scoper1);
570 EXPECT_EQ(&dummy_value, scoper2.get());
571 EXPECT_FALSE(scoper1.get());
572 EXPECT_FALSE(scoper1 == scoper2.get());
573 EXPECT_TRUE(scoper1 != scoper2.get());
577 // Sanity check test for overloaded new and delete operators. Does not do full
578 // coverage of reset/release/Pass() operations as that is redundant with the
579 // above.
580 TEST(ScopedPtrTest, OverloadedNewAndDelete) {
582 OverloadedNewAndDelete::ResetCounters();
583 scoped_ptr<OverloadedNewAndDelete> scoper(new OverloadedNewAndDelete());
584 EXPECT_TRUE(scoper.get());
586 scoped_ptr<OverloadedNewAndDelete> scoper2(scoper.Pass());
588 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count());
589 EXPECT_EQ(1, OverloadedNewAndDelete::new_count());
592 scoped_ptr<int> NullIntReturn() {
593 return nullptr;
596 TEST(ScopedPtrTest, Nullptr) {
597 scoped_ptr<int> scoper1(nullptr);
598 scoped_ptr<int> scoper2(new int);
599 scoper2 = nullptr;
600 scoped_ptr<int> scoper3(NullIntReturn());
601 scoped_ptr<int> scoper4 = NullIntReturn();
602 EXPECT_EQ(nullptr, scoper1.get());
603 EXPECT_EQ(nullptr, scoper2.get());
604 EXPECT_EQ(nullptr, scoper3.get());
605 EXPECT_EQ(nullptr, scoper4.get());
608 scoped_ptr<int[]> NullIntArrayReturn() {
609 return nullptr;
612 TEST(ScopedPtrTest, NullptrArray) {
613 scoped_ptr<int[]> scoper1(nullptr);
614 scoped_ptr<int[]> scoper2(new int[3]);
615 scoper2 = nullptr;
616 scoped_ptr<int[]> scoper3(NullIntArrayReturn());
617 scoped_ptr<int[]> scoper4 = NullIntArrayReturn();
618 EXPECT_EQ(nullptr, scoper1.get());
619 EXPECT_EQ(nullptr, scoper2.get());
620 EXPECT_EQ(nullptr, scoper3.get());
621 EXPECT_EQ(nullptr, scoper4.get());
624 class Super {};
625 class Sub : public Super {};
627 scoped_ptr<Sub> SubClassReturn() {
628 return make_scoped_ptr(new Sub);
631 TEST(ScopedPtrTest, Conversion) {
632 scoped_ptr<Sub> sub1(new Sub);
633 scoped_ptr<Sub> sub2(new Sub);
635 // Upcast with Pass() works.
636 scoped_ptr<Super> super1 = sub1.Pass();
637 super1 = sub2.Pass();
639 // Upcast with an rvalue works.
640 scoped_ptr<Super> super2 = SubClassReturn();
641 super2 = SubClassReturn();
644 // Android death tests don't work properly with assert(). Yay.
645 #if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
646 TEST(ScopedPtrTest, SelfResetAbortsWithDefaultDeleter) {
647 scoped_ptr<int> x(new int);
648 EXPECT_DEATH(x.reset(x.get()), "");
651 TEST(ScopedPtrTest, SelfResetAbortsWithDefaultArrayDeleter) {
652 scoped_ptr<int[]> y(new int[4]);
653 EXPECT_DEATH(y.reset(y.get()), "");
656 TEST(ScopedPtrTest, SelfResetAbortsWithDefaultFreeDeleter) {
657 scoped_ptr<int, base::FreeDeleter> z(static_cast<int*>(malloc(sizeof(int))));
658 EXPECT_DEATH(z.reset(z.get()), "");
661 // A custom deleter that doesn't opt out should still crash.
662 TEST(ScopedPtrTest, SelfResetAbortsWithCustomDeleter) {
663 struct CustomDeleter {
664 inline void operator()(int* x) { delete x; }
666 scoped_ptr<int, CustomDeleter> x(new int);
667 EXPECT_DEATH(x.reset(x.get()), "");
669 #endif
671 TEST(ScopedPtrTest, SelfResetWithCustomDeleterOptOut) {
672 // A custom deleter should be able to opt out of self-reset abort behavior.
673 struct NoOpDeleter {
674 #if !defined(NDEBUG)
675 typedef void AllowSelfReset;
676 #endif
677 inline void operator()(int*) {}
679 scoped_ptr<int> owner(new int);
680 scoped_ptr<int, NoOpDeleter> x(owner.get());
681 x.reset(x.get());
684 // Logging a scoped_ptr<T> to an ostream shouldn't convert it to a boolean
685 // value first.
686 TEST(ScopedPtrTest, LoggingDoesntConvertToBoolean) {
687 scoped_ptr<int> x(new int);
688 std::stringstream s1;
689 s1 << x;
691 std::stringstream s2;
692 s2 << x.get();
694 EXPECT_EQ(s2.str(), s1.str());