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 "base/basictypes.h"
9 #include "base/callback.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 // Used to test depth subtyping.
15 class ConDecLoggerParent
{
17 virtual ~ConDecLoggerParent() {}
19 virtual void SetPtr(int* ptr
) = 0;
21 virtual int SomeMeth(int x
) const = 0;
24 class ConDecLogger
: public ConDecLoggerParent
{
26 ConDecLogger() : ptr_(NULL
) { }
27 explicit ConDecLogger(int* ptr
) { SetPtr(ptr
); }
28 virtual ~ConDecLogger() { --*ptr_
; }
30 virtual void SetPtr(int* ptr
) override
{ ptr_
= ptr
; ++*ptr_
; }
32 virtual int SomeMeth(int x
) const override
{ return x
; }
37 DISALLOW_COPY_AND_ASSIGN(ConDecLogger
);
40 struct CountingDeleter
{
41 explicit CountingDeleter(int* count
) : count_(count
) {}
42 inline void operator()(double* ptr
) const {
48 // Used to test assignment of convertible deleters.
49 struct CountingDeleterChild
: public CountingDeleter
{
50 explicit CountingDeleterChild(int* count
) : CountingDeleter(count
) {}
53 class OverloadedNewAndDelete
{
55 void* operator new(size_t size
) {
60 void operator delete(void* ptr
) {
65 static void ResetCounters() {
70 static int new_count() { return g_new_count
; }
71 static int delete_count() { return g_delete_count
; }
74 static int g_new_count
;
75 static int g_delete_count
;
78 int OverloadedNewAndDelete::g_new_count
= 0;
79 int OverloadedNewAndDelete::g_delete_count
= 0;
81 scoped_ptr
<ConDecLogger
> PassThru(scoped_ptr
<ConDecLogger
> logger
) {
85 void GrabAndDrop(scoped_ptr
<ConDecLogger
> logger
) {
88 // Do not delete this function! It's existence is to test that you can
89 // return a temporarily constructed version of the scoper.
90 scoped_ptr
<ConDecLogger
> TestReturnOfType(int* constructed
) {
91 return scoped_ptr
<ConDecLogger
>(new ConDecLogger(constructed
));
94 scoped_ptr
<ConDecLoggerParent
> UpcastUsingPassAs(
95 scoped_ptr
<ConDecLogger
> object
) {
96 return object
.PassAs
<ConDecLoggerParent
>();
101 TEST(ScopedPtrTest
, ScopedPtr
) {
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());
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());
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
) {
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
;
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
));
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();
256 EXPECT_FALSE(scoper_child
);
257 EXPECT_EQ(1, deletes
);
258 EXPECT_EQ(0, alternate_deletes
);
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;
288 scoped_ptr
<ConDecLogger
[]> scoper(new ConDecLogger
[kNumLoggers
]);
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());
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
);
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
);
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
);
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
) {
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());
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
) {
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
457 TestReturnOfType(&constructed
);
459 EXPECT_EQ(0, constructed
);
462 TEST(ScopedPtrTest
, PassAs
) {
465 scoped_ptr
<ConDecLogger
> scoper(new ConDecLogger(&constructed
));
466 EXPECT_EQ(1, constructed
);
467 EXPECT_TRUE(scoper
.get());
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());
475 EXPECT_EQ(0, constructed
);
478 TEST(ScopedPtrTest
, CustomDeleter
) {
479 double dummy_value
; // Custom deleter never touches this value.
481 int alternate_deletes
= 0;
483 // Normal delete support.
486 scoped_ptr
<double, CountingDeleter
> scoper(&dummy_value
,
487 CountingDeleter(&deletes
));
488 EXPECT_EQ(0, deletes
);
489 EXPECT_TRUE(scoper
.get());
491 EXPECT_EQ(1, deletes
);
493 // Test reset() and release().
496 scoped_ptr
<double, CountingDeleter
> scoper(NULL
,
497 CountingDeleter(&deletes
));
498 EXPECT_FALSE(scoper
.get());
499 EXPECT_FALSE(scoper
.release());
500 EXPECT_FALSE(scoper
.get());
502 EXPECT_FALSE(scoper
.get());
503 EXPECT_EQ(0, deletes
);
505 scoper
.reset(&dummy_value
);
507 EXPECT_EQ(1, deletes
);
509 scoper
.reset(&dummy_value
);
510 EXPECT_EQ(&dummy_value
, scoper
.release());
512 EXPECT_EQ(1, deletes
);
514 // Test get_deleter().
516 alternate_deletes
= 0;
518 scoped_ptr
<double, CountingDeleter
> scoper(&dummy_value
,
519 CountingDeleter(&deletes
));
520 // Call deleter manually.
521 EXPECT_EQ(0, deletes
);
522 scoper
.get_deleter()(&dummy_value
);
523 EXPECT_EQ(1, deletes
);
525 // Deleter is still there after reset.
527 EXPECT_EQ(2, deletes
);
528 scoper
.get_deleter()(&dummy_value
);
529 EXPECT_EQ(3, deletes
);
531 // Deleter can be assigned into (matches C++11 unique_ptr<> spec).
532 scoper
.get_deleter() = CountingDeleter(&alternate_deletes
);
533 scoper
.reset(&dummy_value
);
534 EXPECT_EQ(0, alternate_deletes
);
537 EXPECT_EQ(3, deletes
);
538 EXPECT_EQ(1, alternate_deletes
);
540 // Test operator= deleter support.
542 alternate_deletes
= 0;
545 scoped_ptr
<double, CountingDeleter
> scoper(&dummy_value
,
546 CountingDeleter(&deletes
));
547 scoped_ptr
<double, CountingDeleter
> scoper2(
549 CountingDeleter(&alternate_deletes
));
550 EXPECT_EQ(0, deletes
);
551 EXPECT_EQ(0, alternate_deletes
);
553 // Pass the second deleter through a constructor and an operator=. Then
554 // reinitialize the empty scopers to ensure that each one is deleting
556 scoped_ptr
<double, CountingDeleter
> scoper3(scoper2
.Pass());
557 scoper
= scoper3
.Pass();
558 EXPECT_EQ(1, deletes
);
560 scoper2
.reset(&dummy_value2
);
561 scoper3
.reset(&dummy_value2
);
562 EXPECT_EQ(0, alternate_deletes
);
565 EXPECT_EQ(1, deletes
);
566 EXPECT_EQ(3, alternate_deletes
);
568 // Test swap(), ==, !=, and type-safe Boolean.
570 scoped_ptr
<double, CountingDeleter
> scoper1(NULL
,
571 CountingDeleter(&deletes
));
572 scoped_ptr
<double, CountingDeleter
> scoper2(NULL
,
573 CountingDeleter(&deletes
));
574 EXPECT_TRUE(scoper1
== scoper2
.get());
575 EXPECT_FALSE(scoper1
!= scoper2
.get());
577 scoper1
.reset(&dummy_value
);
578 EXPECT_TRUE(scoper1
);
579 EXPECT_EQ(&dummy_value
, scoper1
.get());
580 EXPECT_FALSE(scoper2
);
581 EXPECT_FALSE(scoper2
.get());
582 EXPECT_FALSE(scoper1
== scoper2
.get());
583 EXPECT_TRUE(scoper1
!= scoper2
.get());
585 scoper2
.swap(scoper1
);
586 EXPECT_EQ(&dummy_value
, scoper2
.get());
587 EXPECT_FALSE(scoper1
.get());
588 EXPECT_FALSE(scoper1
== scoper2
.get());
589 EXPECT_TRUE(scoper1
!= scoper2
.get());
593 // Sanity check test for overloaded new and delete operators. Does not do full
594 // coverage of reset/release/Pass() operations as that is redundant with the
596 TEST(ScopedPtrTest
, OverloadedNewAndDelete
) {
598 OverloadedNewAndDelete::ResetCounters();
599 scoped_ptr
<OverloadedNewAndDelete
> scoper(new OverloadedNewAndDelete());
600 EXPECT_TRUE(scoper
.get());
602 scoped_ptr
<OverloadedNewAndDelete
> scoper2(scoper
.Pass());
604 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count());
605 EXPECT_EQ(1, OverloadedNewAndDelete::new_count());
608 scoped_ptr
<int> NullIntReturn() {
612 TEST(ScopedPtrTest
, Nullptr
) {
613 scoped_ptr
<int> scoper1(nullptr);
614 scoped_ptr
<int> scoper2(new int);
616 scoped_ptr
<int> scoper3(NullIntReturn());
617 scoped_ptr
<int> scoper4
= NullIntReturn();
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 scoped_ptr
<int[]> NullIntArrayReturn() {
628 TEST(ScopedPtrTest
, NullptrArray
) {
629 scoped_ptr
<int[]> scoper1(nullptr);
630 scoped_ptr
<int[]> scoper2(new int[3]);
632 scoped_ptr
<int[]> scoper3(NullIntArrayReturn());
633 scoped_ptr
<int[]> scoper4
= NullIntArrayReturn();
634 EXPECT_EQ(nullptr, scoper1
.get());
635 EXPECT_EQ(nullptr, scoper2
.get());
636 EXPECT_EQ(nullptr, scoper3
.get());
637 EXPECT_EQ(nullptr, scoper4
.get());
641 class Sub
: public Super
{};
643 scoped_ptr
<Sub
> SubClassReturn() {
644 return make_scoped_ptr(new Sub
);
647 TEST(ScopedPtrTest
, Conversion
) {
648 scoped_ptr
<Sub
> sub1(new Sub
);
649 scoped_ptr
<Sub
> sub2(new Sub
);
651 // Upcast with Pass() works.
652 scoped_ptr
<Super
> super1
= sub1
.Pass();
653 super1
= sub2
.Pass();
655 // Upcast with an rvalue works.
656 scoped_ptr
<Super
> super2
= SubClassReturn();
657 super2
= SubClassReturn();
660 // Android death tests don't work properly with assert(). Yay.
661 #if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
662 TEST(ScopedPtrTest
, SelfResetAbortsWithDefaultDeleter
) {
663 scoped_ptr
<int> x(new int);
664 EXPECT_DEATH(x
.reset(x
.get()), "");
667 TEST(ScopedPtrTest
, SelfResetAbortsWithDefaultArrayDeleter
) {
668 scoped_ptr
<int[]> y(new int[4]);
669 EXPECT_DEATH(y
.reset(y
.get()), "");
672 TEST(ScopedPtrTest
, SelfResetAbortsWithDefaultFreeDeleter
) {
673 scoped_ptr
<int, base::FreeDeleter
> z(static_cast<int*>(malloc(sizeof(int))));
674 EXPECT_DEATH(z
.reset(z
.get()), "");
677 // A custom deleter that doesn't opt out should still crash.
678 TEST(ScopedPtrTest
, SelfResetAbortsWithCustomDeleter
) {
679 struct CustomDeleter
{
680 inline void operator()(int* x
) { delete x
; }
682 scoped_ptr
<int, CustomDeleter
> x(new int);
683 EXPECT_DEATH(x
.reset(x
.get()), "");
687 TEST(ScopedPtrTest
, SelfResetWithCustomDeleterOptOut
) {
688 // A custom deleter should be able to opt out of self-reset abort behavior.
690 typedef void AllowSelfReset
;
691 inline void operator()(int*) {}
693 scoped_ptr
<int> owner(new int);
694 scoped_ptr
<int, NoOpDeleter
> x(owner
.get());