aboutsummaryrefslogtreecommitdiff
path: root/test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.ctor/pointer.pass.cpp
blob: faa554d8b32094274df2179de012d8a402047188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <memory>

// unique_ptr

//=============================================================================
// TESTING std::unique_ptr::unique_ptr()
//
// Concerns:
//   1 The pointer constructor works for any default constructible deleter types.
//   2 The pointer constructor accepts pointers to derived types.
//   2 The stored type 'T' is allowed to be incomplete.
//
// Plan
//  1 Construct unique_ptr<T, D>'s with a pointer to 'T' and various deleter
//   types (C-1)
//  2 Construct unique_ptr<T, D>'s with a pointer to 'D' and various deleter
//    types where 'D' is derived from 'T'. (C-1,2)
//  3 Construct a unique_ptr<T, D> with a pointer to 'T' and various deleter
//    types where 'T' is an incomplete type (C-1,3)

// Test unique_ptr(pointer) ctor

#include <memory>
#include <cassert>

#include "../../deleter.h"

// unique_ptr(pointer) ctor should only require default Deleter ctor

struct A
{
    static int count;
    A() {++count;}
    A(const A&) {++count;}
    virtual ~A() {--count;}
};

int A::count = 0;


struct B
    : public A
{
    static int count;
    B() {++count;}
    B(const B&) {++count;}
    virtual ~B() {--count;}
};

int B::count = 0;


struct IncompleteT;

IncompleteT* getIncomplete();
void checkNumIncompleteTypeAlive(int i);

template <class Del = std::default_delete<IncompleteT> >
struct StoresIncomplete {
  std::unique_ptr<IncompleteT, Del> m_ptr;
  StoresIncomplete() {}
  explicit StoresIncomplete(IncompleteT* ptr) : m_ptr(ptr) {}
  ~StoresIncomplete();

  IncompleteT* get() const { return m_ptr.get(); }
  Del& get_deleter() { return m_ptr.get_deleter(); }
};

void test_pointer()
{
    {
        A* p = new A;
        assert(A::count == 1);
        std::unique_ptr<A> s(p);
        assert(s.get() == p);
    }
    assert(A::count == 0);
    {
        A* p = new A;
        assert(A::count == 1);
        std::unique_ptr<A, NCDeleter<A> > s(p);
        assert(s.get() == p);
        assert(s.get_deleter().state() == 0);
    }
    assert(A::count == 0);
}

void test_derived()
{
    {
        B* p = new B;
        assert(A::count == 1);
        assert(B::count == 1);
        std::unique_ptr<A> s(p);
        assert(s.get() == p);
    }
    assert(A::count == 0);
    assert(B::count == 0);
    {
        B* p = new B;
        assert(A::count == 1);
        assert(B::count == 1);
        std::unique_ptr<A, NCDeleter<A> > s(p);
        assert(s.get() == p);
        assert(s.get_deleter().state() == 0);
    }
    assert(A::count == 0);
    assert(B::count == 0);
}

void test_incomplete()
{
    {
        IncompleteT* p = getIncomplete();
        checkNumIncompleteTypeAlive(1);
        StoresIncomplete<> s(p);
        assert(s.get() == p);
    }
    checkNumIncompleteTypeAlive(0);
    {
        IncompleteT* p = getIncomplete();
        checkNumIncompleteTypeAlive(1);
        StoresIncomplete< NCDeleter<IncompleteT> > s(p);
        assert(s.get() == p);
        assert(s.get_deleter().state() == 0);
    }
    checkNumIncompleteTypeAlive(0);
}

struct IncompleteT {
    static int count;
    IncompleteT() { ++count; }
    ~IncompleteT() {--count; }
};

int IncompleteT::count = 0;

IncompleteT* getIncomplete() {
    return new IncompleteT;
}

void checkNumIncompleteTypeAlive(int i) {
    assert(IncompleteT::count == i);
}

template <class Del>
StoresIncomplete<Del>::~StoresIncomplete() { }

int main()
{
    test_pointer();
    test_derived();
    test_incomplete();
}