Home | History | Annotate | Download | only in unique.ptr.runtime.modifiers
      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 swap
     15 
     16 #include <memory>
     17 #include <cassert>
     18 
     19 #include "../../deleter.h"
     20 
     21 struct A
     22 {
     23     int state_;
     24     static int count;
     25     A() : state_(0) {++count;}
     26     explicit A(int i) : state_(i) {++count;}
     27     A(const A& a) : state_(a.state_) {++count;}
     28     A& operator=(const A& a) {state_ = a.state_; return *this;}
     29     ~A() {--count;}
     30 
     31     friend bool operator==(const A& x, const A& y)
     32         {return x.state_ == y.state_;}
     33 };
     34 
     35 int A::count = 0;
     36 
     37 int main()
     38 {
     39     {
     40     A* p1 = new A[3];
     41     std::unique_ptr<A[], Deleter<A[]> > s1(p1, Deleter<A[]>(1));
     42     A* p2 = new A[3];
     43     std::unique_ptr<A[], Deleter<A[]> > s2(p2, Deleter<A[]>(2));
     44     assert(s1.get() == p1);
     45     assert(s1.get_deleter().state() == 1);
     46     assert(s2.get() == p2);
     47     assert(s2.get_deleter().state() == 2);
     48     s1.swap(s2);
     49     assert(s1.get() == p2);
     50     assert(s1.get_deleter().state() == 2);
     51     assert(s2.get() == p1);
     52     assert(s2.get_deleter().state() == 1);
     53     assert(A::count == 6);
     54     }
     55     assert(A::count == 0);
     56 }
     57