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=(const shared_ptr<Y>& r);
14
15#include <memory>
16#include <type_traits>
17#include <cassert>
18
19#include "test_macros.h"
20
21struct B
22{
23 static int count;
24
25 B() {++count;}
26 B(const B&) {++count;}
27 virtual ~B() {--count;}
28};
29
30int B::count = 0;
31
32struct A
33 : public B
34{
35 static int count;
36
37 A() {++count;}
38 A(const A& other) : B(other) {++count;}
39 ~A() {--count;}
40};
41
42int A::count = 0;
43
44int main(int, char**)
45{
46 {
47 const std::shared_ptr<A> pA(new A);
48 A* ptrA = pA.get();
49 {
50 std::shared_ptr<B> pB(new B);
51 pB = pA;
52 assert(B::count == 1);
53 assert(A::count == 1);
54 assert(pB.use_count() == 2);
55 assert(pA.use_count() == 2);
56 assert(pA.get() == pB.get());
57 assert(pB.get() == ptrA);
58 }
59 assert(pA.use_count() == 1);
60 assert(B::count == 1);
61 assert(A::count == 1);
62 }
63 assert(B::count == 0);
64 assert(A::count == 0);
65 {
66 const std::shared_ptr<A> pA;
67 A* ptrA = pA.get();
68 {
69 std::shared_ptr<B> pB(new B);
70 pB = pA;
71 assert(B::count == 0);
72 assert(A::count == 0);
73 assert(pB.use_count() == 0);
74 assert(pA.use_count() == 0);
75 assert(pA.get() == pB.get());
76 assert(pB.get() == ptrA);
77 }
78 assert(pA.use_count() == 0);
79 assert(B::count == 0);
80 assert(A::count == 0);
81 }
82 assert(B::count == 0);
83 assert(A::count == 0);
84 {
85 const std::shared_ptr<A> pA(new A);
86 A* ptrA = pA.get();
87 {
88 std::shared_ptr<B> pB;
89 pB = pA;
90 assert(B::count == 1);
91 assert(A::count == 1);
92 assert(pB.use_count() == 2);
93 assert(pA.use_count() == 2);
94 assert(pA.get() == pB.get());
95 assert(pB.get() == ptrA);
96 }
97 assert(pA.use_count() == 1);
98 assert(B::count == 1);
99 assert(A::count == 1);
100 }
101 assert(B::count == 0);
102 assert(A::count == 0);
103 {
104 const std::shared_ptr<A> pA;
105 A* ptrA = pA.get();
106 {
107 std::shared_ptr<B> pB;
108 pB = pA;
109 assert(B::count == 0);
110 assert(A::count == 0);
111 assert(pB.use_count() == 0);
112 assert(pA.use_count() == 0);
113 assert(pA.get() == pB.get());
114 assert(pB.get() == ptrA);
115 }
116 assert(pA.use_count() == 0);
117 assert(B::count == 0);
118 assert(A::count == 0);
119 }
120 assert(B::count == 0);
121 assert(A::count == 0);
122
123#if TEST_STD_VER > 14
124 {
125 std::shared_ptr<A[]> p1(new A[8]);
126 A* ptr = p1.get();
127 assert(A::count == 8);
128 {
129 std::shared_ptr<const A[]> p2;
130 p2 = p1;
131 assert(A::count == 8);
132 assert(p2.use_count() == 2);
133 assert(p1.use_count() == 2);
134 assert(p1.get() == p2.get());
135 assert(p2.get() == ptr);
136 }
137 assert(p1.use_count() == 1);
138 assert(A::count == 8);
139 }
140 assert(A::count == 0);
141#endif
142
143 return 0;
144}
145

source code of libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp