Home | History | Annotate | Download | only in unique.ptr.runtime
      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;
     28     A() {++count;}
     29     A(const A&) {++count;}
     30     ~A() {--count;}
     31 };
     32 
     33 int A::count = 0;
     34 
     35 int main()
     36 {
     37     {
     38     std::unique_ptr<A[]> s1(new A[3]);
     39     A* p = s1.get();
     40     assert(A::count == 3);
     41     std::unique_ptr<A[]> s2(new A[2]);
     42     assert(A::count == 5);
     43     s2 = std::move(s1);
     44     assert(A::count == 3);
     45     assert(s2.get() == p);
     46     assert(s1.get() == 0);
     47     }
     48     assert(A::count == 0);
     49     {
     50     std::unique_ptr<A[], Deleter<A[]> > s1(new A[4], Deleter<A[]>(5));
     51     A* p = s1.get();
     52     assert(A::count == 4);
     53     std::unique_ptr<A[], Deleter<A[]> > s2(new A[5]);
     54     assert(A::count == 9);
     55     s2 = std::move(s1);
     56     assert(s2.get() == p);
     57     assert(s1.get() == 0);
     58     assert(A::count == 4);
     59     assert(s2.get_deleter().state() == 5);
     60     assert(s1.get_deleter().state() == 0);
     61     }
     62     assert(A::count == 0);
     63     {
     64     CDeleter<A[]> d1(5);
     65     std::unique_ptr<A[], CDeleter<A[]>&> s1(new A[6], d1);
     66     A* p = s1.get();
     67     assert(A::count == 6);
     68     CDeleter<A[]> d2(6);
     69     std::unique_ptr<A[], CDeleter<A[]>&> s2(new A[3], d2);
     70     assert(A::count == 9);
     71     s2 = std::move(s1);
     72     assert(A::count == 6);
     73     assert(s2.get() == p);
     74     assert(s1.get() == 0);
     75     assert(d1.state() == 5);
     76     assert(d2.state() == 5);
     77     }
     78     assert(A::count == 0);
     79 }
     80