• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
AA25     explicit A(int i) : state_(i) {++count;}
AA26     A(const A& a) : state_(a.state_) {++count;}
operator =A27     A& operator=(const A& a) {state_ = a.state_; return *this;}
~AA28     ~A() {--count;}
29 
operator ==(const A & x,const A & y)30     friend bool operator==(const A& x, const A& y)
31         {return x.state_ == y.state_;}
32 };
33 
34 int A::count = 0;
35 
main()36 int main()
37 {
38     {
39     A* p1 = new A(1);
40     std::unique_ptr<A, Deleter<A> > s1(p1, Deleter<A>(1));
41     A* p2 = new A(2);
42     std::unique_ptr<A, Deleter<A> > s2(p2, Deleter<A>(2));
43     assert(s1.get() == p1);
44     assert(*s1 == A(1));
45     assert(s1.get_deleter().state() == 1);
46     assert(s2.get() == p2);
47     assert(*s2 == A(2));
48     assert(s2.get_deleter().state() == 2);
49     s1.swap(s2);
50     assert(s1.get() == p2);
51     assert(*s1 == A(2));
52     assert(s1.get_deleter().state() == 2);
53     assert(s2.get() == p1);
54     assert(*s2 == A(1));
55     assert(s2.get_deleter().state() == 1);
56     assert(A::count == 2);
57     }
58     assert(A::count == 0);
59 }
60