~ubuntu-branches/ubuntu/trusty/libc++/trusty

« back to all changes in this revision

Viewing changes to libcxx/test/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.modifiers/swap.pass.cpp

  • Committer: Package Import Robot
  • Author(s): Andrej Belym
  • Date: 2012-06-02 19:25:47 UTC
  • Revision ID: package-import@ubuntu.com-20120602192547-b6ta950a8ckc9um2
Tags: upstream-1.0~svn160132
ImportĀ upstreamĀ versionĀ 1.0~svn160132

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    explicit A(int i) : state_(i) {++count;}
 
26
    A(const A& a) : state_(a.state_) {++count;}
 
27
    A& operator=(const A& a) {state_ = a.state_; return *this;}
 
28
    ~A() {--count;}
 
29
 
 
30
    friend bool operator==(const A& x, const A& y)
 
31
        {return x.state_ == y.state_;}
 
32
};
 
33
 
 
34
int A::count = 0;
 
35
 
 
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
}