Revert "[libc] Breakup freelist_malloc into separate files" (#119749)
[llvm-project.git] / libcxx / test / std / utilities / memory / util.smartptr / util.smartptr.shared / util.smartptr.shared.mod / swap.pass.cpp
blob5070dbc1112b4d72d16bb6cc329b85b054a30ec7
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 //===----------------------------------------------------------------------===//
9 // <memory>
11 // shared_ptr
13 // void swap(shared_ptr& r);
15 #include <memory>
16 #include <cassert>
18 #include "test_macros.h"
20 struct A
22 static int count;
24 A() {++count;}
25 A(const A&) {++count;}
26 ~A() {--count;}
29 int A::count = 0;
31 int main(int, char**)
34 A* ptr1 = new A;
35 A* ptr2 = new A;
36 std::shared_ptr<A> p1(ptr1);
38 std::shared_ptr<A> p2(ptr2);
39 p1.swap(p2);
40 assert(p1.use_count() == 1);
41 assert(p1.get() == ptr2);
42 assert(p2.use_count() == 1);
43 assert(p2.get() == ptr1);
44 assert(A::count == 2);
46 assert(p1.use_count() == 1);
47 assert(p1.get() == ptr2);
48 assert(A::count == 1);
50 assert(A::count == 0);
52 A* ptr1 = new A;
53 A* ptr2 = 0;
54 std::shared_ptr<A> p1(ptr1);
56 std::shared_ptr<A> p2;
57 p1.swap(p2);
58 assert(p1.use_count() == 0);
59 assert(p1.get() == ptr2);
60 assert(p2.use_count() == 1);
61 assert(p2.get() == ptr1);
62 assert(A::count == 1);
64 assert(p1.use_count() == 0);
65 assert(p1.get() == ptr2);
66 assert(A::count == 0);
68 assert(A::count == 0);
70 A* ptr1 = 0;
71 A* ptr2 = new A;
72 std::shared_ptr<A> p1;
74 std::shared_ptr<A> p2(ptr2);
75 p1.swap(p2);
76 assert(p1.use_count() == 1);
77 assert(p1.get() == ptr2);
78 assert(p2.use_count() == 0);
79 assert(p2.get() == ptr1);
80 assert(A::count == 1);
82 assert(p1.use_count() == 1);
83 assert(p1.get() == ptr2);
84 assert(A::count == 1);
86 assert(A::count == 0);
88 A* ptr1 = 0;
89 A* ptr2 = 0;
90 std::shared_ptr<A> p1;
92 std::shared_ptr<A> p2;
93 p1.swap(p2);
94 assert(p1.use_count() == 0);
95 assert(p1.get() == ptr2);
96 assert(p2.use_count() == 0);
97 assert(p2.get() == ptr1);
98 assert(A::count == 0);
100 assert(p1.use_count() == 0);
101 assert(p1.get() == ptr2);
102 assert(A::count == 0);
104 assert(A::count == 0);
106 return 0;