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 <cassert>
     21 
     22 #include "../deleter.h"
     23 
     24 struct A
     25 {
     26     static int count;
     27     A() {++count;}
     28     A(const A&) {++count;}
     29     ~A() {--count;}
     30 };
     31 
     32 int A::count = 0;
     33 
     34 int main()
     35 {
     36     {
     37     std::unique_ptr<A[]> s1(new A[3]);
     38     A* p = s1.get();
     39     assert(A::count == 3);
     40     std::unique_ptr<A[]> s2(new A[2]);
     41     assert(A::count == 5);
     42     s2 = std::move(s1);
     43     assert(A::count == 3);
     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[4], Deleter<A[]>(5));
     50     A* p = s1.get();
     51     assert(A::count == 4);
     52     std::unique_ptr<A[], Deleter<A[]> > s2(new A[5]);
     53     assert(A::count == 9);
     54     s2 = std::move(s1);
     55     assert(s2.get() == p);
     56     assert(s1.get() == 0);
     57     assert(A::count == 4);
     58     assert(s2.get_deleter().state() == 5);
     59     assert(s1.get_deleter().state() == 0);
     60     }
     61     assert(A::count == 0);
     62     {
     63     CDeleter<A[]> d1(5);
     64     std::unique_ptr<A[], CDeleter<A[]>&> s1(new A[6], d1);
     65     A* p = s1.get();
     66     assert(A::count == 6);
     67     CDeleter<A[]> d2(6);
     68     std::unique_ptr<A[], CDeleter<A[]>&> s2(new A[3], d2);
     69     assert(A::count == 9);
     70     s2 = std::move(s1);
     71     assert(A::count == 6);
     72     assert(s2.get() == p);
     73     assert(s1.get() == 0);
     74     assert(d1.state() == 5);
     75     assert(d2.state() == 5);
     76     }
     77     assert(A::count == 0);
     78 }
     79