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 | // <memory> |
10 | |
11 | // shared_ptr |
12 | |
13 | // template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); |
14 | |
15 | // REQUIRES: c++03 || c++11 || c++14 |
16 | // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS |
17 | |
18 | #include <memory> |
19 | #include <type_traits> |
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::auto_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.get() == 0); |
59 | assert(pB.get() == ptrA); |
60 | } |
61 | assert(B::count == 0); |
62 | assert(A::count == 0); |
63 | } |
64 | assert(B::count == 0); |
65 | assert(A::count == 0); |
66 | { |
67 | std::auto_ptr<A> pA; |
68 | A* ptrA = pA.get(); |
69 | { |
70 | std::shared_ptr<B> pB(new B); |
71 | pB = std::move(pA); |
72 | assert(B::count == 0); |
73 | assert(A::count == 0); |
74 | assert(pB.use_count() == 1); |
75 | assert(pA.get() == 0); |
76 | assert(pB.get() == ptrA); |
77 | } |
78 | assert(B::count == 0); |
79 | assert(A::count == 0); |
80 | } |
81 | assert(B::count == 0); |
82 | assert(A::count == 0); |
83 | { |
84 | std::auto_ptr<A> pA(new A); |
85 | A* ptrA = pA.get(); |
86 | { |
87 | std::shared_ptr<B> pB; |
88 | pB = std::move(pA); |
89 | assert(B::count == 1); |
90 | assert(A::count == 1); |
91 | assert(pB.use_count() == 1); |
92 | assert(pA.get() == 0); |
93 | assert(pB.get() == ptrA); |
94 | } |
95 | assert(B::count == 0); |
96 | assert(A::count == 0); |
97 | } |
98 | assert(B::count == 0); |
99 | assert(A::count == 0); |
100 | { |
101 | std::auto_ptr<A> pA; |
102 | A* ptrA = pA.get(); |
103 | { |
104 | std::shared_ptr<B> pB; |
105 | pB = std::move(pA); |
106 | assert(B::count == 0); |
107 | assert(A::count == 0); |
108 | assert(pB.use_count() == 1); |
109 | assert(pA.get() == 0); |
110 | assert(pB.get() == ptrA); |
111 | } |
112 | assert(B::count == 0); |
113 | assert(A::count == 0); |
114 | } |
115 | assert(B::count == 0); |
116 | assert(A::count == 0); |
117 | |
118 | return 0; |
119 | } |
120 |