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 // unique_ptr 13 14 // Test unique_ptr converting move assignment 15 16 #include <memory> 17 #include <utility> 18 #include <cassert> 19 20 #include "../../deleter.h" 21 22 struct A 23 { 24 static int count; 25 A() {++count;} 26 A(const A&) {++count;} 27 virtual ~A() {--count;} 28 }; 29 30 int A::count = 0; 31 32 struct B 33 : public A 34 { 35 static int count; 36 B() {++count;} 37 B(const B&) {++count;} 38 virtual ~B() {--count;} 39 }; 40 41 int B::count = 0; 42 43 44 template <class APtr, class BPtr> 45 void testAssign(APtr& aptr, BPtr& bptr) { 46 A* p = bptr.get(); 47 assert(A::count == 2); 48 aptr = std::move(bptr); 49 assert(aptr.get() == p); 50 assert(bptr.get() == 0); 51 assert(A::count == 1); 52 assert(B::count == 1); 53 } 54 55 template <class LHS, class RHS> 56 void checkDeleter(LHS& lhs, RHS& rhs, int LHSState, int RHSState) { 57 assert(lhs.get_deleter().state() == LHSState); 58 assert(rhs.get_deleter().state() == RHSState); 59 } 60 61 int main() 62 { 63 { 64 std::unique_ptr<B> bptr(new B); 65 std::unique_ptr<A> aptr(new A); 66 testAssign(aptr, bptr); 67 } 68 assert(A::count == 0); 69 assert(B::count == 0); 70 { 71 Deleter<B> del(42); 72 std::unique_ptr<B, Deleter<B> > bptr(new B, std::move(del)); 73 std::unique_ptr<A, Deleter<A> > aptr(new A); 74 testAssign(aptr, bptr); 75 checkDeleter(aptr, bptr, 42, 0); 76 } 77 assert(A::count == 0); 78 assert(B::count == 0); 79 { 80 CDeleter<A> adel(6); 81 CDeleter<B> bdel(42); 82 std::unique_ptr<B, CDeleter<B>&> bptr(new B, bdel); 83 std::unique_ptr<A, CDeleter<A>&> aptr(new A, adel); 84 testAssign(aptr, bptr); 85 checkDeleter(aptr, bptr, 42, 42); 86 } 87 assert(A::count == 0); 88 assert(B::count == 0); 89 } 90