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

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