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// shared_ptr& operator=(shared_ptr&& r);
16
17#include <memory>
18#include <type_traits>
19#include <cassert>
20#include <utility>
21
22#include "test_macros.h"
23
24struct B
25{
26 static int count;
27
28 B() {++count;}
29 B(const B&) {++count;}
30 virtual ~B() {--count;}
31};
32
33int B::count = 0;
34
35struct 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
45int A::count = 0;
46
47int main(int, char**)
48{
49 {
50 std::shared_ptr<A> pA(new A);
51 A* ptrA = pA.get();
52 {
53 std::shared_ptr<A> pB(new A);
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<A> pB(new A);
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<A> 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<A> 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 return 0;
127}
128

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