Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / memory / util.smartptr / util.smartptr.weak / util.smartptr.weak.assign / weak_ptr.pass.cpp
blobcfb8a1df7fba9d3b79d6543d959d344c6a909108
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 // weak_ptr
13 // weak_ptr& operator=(const weak_ptr& r);
15 #include <memory>
16 #include <type_traits>
17 #include <cassert>
18 #include <utility>
20 #include "test_macros.h"
22 struct B
24 static int count;
26 B() {++count;}
27 B(const B&) {++count;}
28 virtual ~B() {--count;}
31 int B::count = 0;
33 struct A
34 : public B
36 static int count;
38 A() {++count;}
39 A(const A& other) : B(other) {++count;}
40 ~A() {--count;}
43 int A::count = 0;
45 int main(int, char**)
48 const std::shared_ptr<A> ps(new A);
49 const std::weak_ptr<A> pA(ps);
51 std::weak_ptr<A> pB;
52 pB = pA;
53 assert(B::count == 1);
54 assert(A::count == 1);
55 assert(pB.use_count() == 1);
56 assert(pA.use_count() == 1);
58 assert(pA.use_count() == 1);
59 assert(B::count == 1);
60 assert(A::count == 1);
62 assert(B::count == 0);
63 assert(A::count == 0);
66 const std::shared_ptr<A> ps(new A);
67 std::weak_ptr<A> pA(ps);
69 std::weak_ptr<A> pB;
70 pB = std::move(pA);
71 assert(B::count == 1);
72 assert(A::count == 1);
73 assert(pB.use_count() == 1);
75 assert(B::count == 1);
76 assert(A::count == 1);
78 assert(B::count == 0);
79 assert(A::count == 0);
81 return 0;