1 | //===----------------------------------------------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // UNSUPPORTED: c++03 |
10 | |
11 | // <memory> |
12 | |
13 | // shared_ptr |
14 | |
15 | // template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r); |
16 | |
17 | #include <memory> |
18 | #include <type_traits> |
19 | #include <utility> |
20 | #include <cassert> |
21 | |
22 | #include "test_macros.h" |
23 | |
24 | struct B |
25 | { |
26 | static int count; |
27 | |
28 | B() {++count;} |
29 | B(const B&) {++count;} |
30 | virtual ~B() {--count;} |
31 | }; |
32 | |
33 | int B::count = 0; |
34 | |
35 | struct A |
36 | : public B |
37 | { |
38 | static int count; |
39 | |
40 | A() {++count;} |
41 | A(const A& other) : B(other) {++count;} |
42 | ~A() {--count;} |
43 | }; |
44 | |
45 | int A::count = 0; |
46 | |
47 | int main(int, char**) |
48 | { |
49 | { |
50 | std::shared_ptr<A> pA(new A); |
51 | A* ptrA = pA.get(); |
52 | { |
53 | std::shared_ptr<B> pB(new B); |
54 | pB = std::move(pA); |
55 | assert(B::count == 1); |
56 | assert(A::count == 1); |
57 | assert(pB.use_count() == 1); |
58 | assert(pA.use_count() == 0); |
59 | assert(pA.get() == 0); |
60 | assert(pB.get() == ptrA); |
61 | } |
62 | assert(pA.use_count() == 0); |
63 | assert(B::count == 0); |
64 | assert(A::count == 0); |
65 | } |
66 | assert(B::count == 0); |
67 | assert(A::count == 0); |
68 | { |
69 | std::shared_ptr<A> pA; |
70 | A* ptrA = pA.get(); |
71 | { |
72 | std::shared_ptr<B> pB(new B); |
73 | pB = std::move(pA); |
74 | assert(B::count == 0); |
75 | assert(A::count == 0); |
76 | assert(pB.use_count() == 0); |
77 | assert(pA.use_count() == 0); |
78 | assert(pA.get() == 0); |
79 | assert(pB.get() == ptrA); |
80 | } |
81 | assert(pA.use_count() == 0); |
82 | assert(B::count == 0); |
83 | assert(A::count == 0); |
84 | } |
85 | assert(B::count == 0); |
86 | assert(A::count == 0); |
87 | { |
88 | std::shared_ptr<A> pA(new A); |
89 | A* ptrA = pA.get(); |
90 | { |
91 | std::shared_ptr<B> pB; |
92 | pB = std::move(pA); |
93 | assert(B::count == 1); |
94 | assert(A::count == 1); |
95 | assert(pB.use_count() == 1); |
96 | assert(pA.use_count() == 0); |
97 | assert(pA.get() == 0); |
98 | assert(pB.get() == ptrA); |
99 | } |
100 | assert(pA.use_count() == 0); |
101 | assert(B::count == 0); |
102 | assert(A::count == 0); |
103 | } |
104 | assert(B::count == 0); |
105 | assert(A::count == 0); |
106 | { |
107 | std::shared_ptr<A> pA; |
108 | A* ptrA = pA.get(); |
109 | { |
110 | std::shared_ptr<B> pB; |
111 | pB = std::move(pA); |
112 | assert(B::count == 0); |
113 | assert(A::count == 0); |
114 | assert(pB.use_count() == 0); |
115 | assert(pA.use_count() == 0); |
116 | assert(pA.get() == 0); |
117 | assert(pB.get() == ptrA); |
118 | } |
119 | assert(pA.use_count() == 0); |
120 | assert(B::count == 0); |
121 | assert(A::count == 0); |
122 | } |
123 | assert(B::count == 0); |
124 | assert(A::count == 0); |
125 | |
126 | #if TEST_STD_VER > 14 |
127 | { |
128 | std::shared_ptr<A[]> p1(new A[8]); |
129 | A* ptr = p1.get(); |
130 | assert(A::count == 8); |
131 | { |
132 | std::shared_ptr<const A[]> p2; |
133 | p2 = std::move(p1); |
134 | assert(A::count == 8); |
135 | assert(p2.use_count() == 1); |
136 | assert(p1.use_count() == 0); |
137 | assert(p1.get() == nullptr); |
138 | assert(p2.get() == ptr); |
139 | } |
140 | assert(p1.use_count() == 0); |
141 | assert(A::count == 0); |
142 | } |
143 | assert(A::count == 0); |
144 | #endif |
145 | |
146 | return 0; |
147 | } |
148 |