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 move assignment 15 16 // test move assignment. Should only require a MoveConstructible deleter, or if 17 // deleter is a reference, not even that. 18 19 #include <memory> 20 #include <utility> 21 #include <cassert> 22 23 #include "../../deleter.h" 24 25 struct A 26 { 27 static int count; AA28 A() {++count;} AA29 A(const A&) {++count;} ~AA30 ~A() {--count;} 31 }; 32 33 int A::count = 0; 34 main()35int main() 36 { 37 { 38 std::unique_ptr<A> s1(new A); 39 A* p = s1.get(); 40 std::unique_ptr<A> s2(new A); 41 assert(A::count == 2); 42 s2 = std::move(s1); 43 assert(A::count == 1); 44 assert(s2.get() == p); 45 assert(s1.get() == 0); 46 } 47 assert(A::count == 0); 48 { 49 std::unique_ptr<A, Deleter<A> > s1(new A, Deleter<A>(5)); 50 A* p = s1.get(); 51 std::unique_ptr<A, Deleter<A> > s2(new A); 52 assert(A::count == 2); 53 s2 = std::move(s1); 54 assert(s2.get() == p); 55 assert(s1.get() == 0); 56 assert(A::count == 1); 57 assert(s2.get_deleter().state() == 5); 58 assert(s1.get_deleter().state() == 0); 59 } 60 assert(A::count == 0); 61 { 62 CDeleter<A> d1(5); 63 std::unique_ptr<A, CDeleter<A>&> s1(new A, d1); 64 A* p = s1.get(); 65 CDeleter<A> d2(6); 66 std::unique_ptr<A, CDeleter<A>&> s2(new A, d2); 67 s2 = std::move(s1); 68 assert(s2.get() == p); 69 assert(s1.get() == 0); 70 assert(A::count == 1); 71 assert(d1.state() == 5); 72 assert(d2.state() == 5); 73 } 74 assert(A::count == 0); 75 } 76