1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <memory> 11 12 // shared_ptr 13 14 // template<class Y, class D, class A> void reset(Y* p, D d, A a); 15 16 #include <memory> 17 #include <cassert> 18 #include "../test_deleter.h" 19 #include "test_allocator.h" 20 21 struct B 22 { 23 static int count; 24 25 B() {++count;} 26 B(const B&) {++count;} 27 virtual ~B() {--count;} 28 }; 29 30 int B::count = 0; 31 32 struct A 33 : public B 34 { 35 static int count; 36 37 A() {++count;} 38 A(const A&) {++count;} 39 ~A() {--count;} 40 }; 41 42 int A::count = 0; 43 44 int main() 45 { 46 { 47 std::shared_ptr<B> p(new B); 48 A* ptr = new A; 49 p.reset(ptr, test_deleter<A>(3), test_allocator<A>(4)); 50 assert(A::count == 1); 51 assert(B::count == 1); 52 assert(p.use_count() == 1); 53 assert(p.get() == ptr); 54 test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); 55 assert(test_deleter<A>::count == 1); 56 assert(test_deleter<A>::dealloc_count == 0); 57 assert(d); 58 assert(d->state() == 3); 59 assert(test_allocator<A>::count == 1); 60 assert(test_allocator<A>::alloc_count == 1); 61 } 62 assert(A::count == 0); 63 assert(test_deleter<A>::count == 0); 64 assert(test_deleter<A>::dealloc_count == 1); 65 assert(test_allocator<A>::count == 0); 66 assert(test_allocator<A>::alloc_count == 0); 67 { 68 std::shared_ptr<B> p; 69 A* ptr = new A; 70 p.reset(ptr, test_deleter<A>(3), test_allocator<A>(4)); 71 assert(A::count == 1); 72 assert(B::count == 1); 73 assert(p.use_count() == 1); 74 assert(p.get() == ptr); 75 test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); 76 assert(test_deleter<A>::count == 1); 77 assert(test_deleter<A>::dealloc_count == 1); 78 assert(d); 79 assert(d->state() == 3); 80 assert(test_allocator<A>::count == 1); 81 assert(test_allocator<A>::alloc_count == 1); 82 } 83 assert(A::count == 0); 84 assert(test_deleter<A>::count == 0); 85 assert(test_deleter<A>::dealloc_count == 2); 86 assert(test_allocator<A>::count == 0); 87 assert(test_allocator<A>::alloc_count == 0); 88 } 89