Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / memory / util.smartptr / util.smartptr.shared / util.smartptr.shared.mod / reset_pointer.pass.cpp
blobd1d19e4562355d8a2fa854a30191e28509bb9659
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 // template<class Y> void reset(Y* p);
15 #include <memory>
16 #include <cassert>
18 #include "reset_helper.h"
19 #include "test_macros.h"
21 struct B
23 static int count;
25 B() {++count;}
26 B(const B&) {++count;}
27 virtual ~B() {--count;}
30 int B::count = 0;
32 struct A
33 : public B
35 static int count;
37 A() {++count;}
38 A(const A& other) : B(other) {++count;}
39 ~A() {--count;}
42 int A::count = 0;
44 struct Derived : A {};
46 static_assert( HasReset<std::shared_ptr<int>, int*>::value, "");
47 static_assert( HasReset<std::shared_ptr<A>, Derived*>::value, "");
48 static_assert(!HasReset<std::shared_ptr<A>, int*>::value, "");
50 #if TEST_STD_VER >= 17
51 static_assert( HasReset<std::shared_ptr<int[]>, int*>::value, "");
52 static_assert(!HasReset<std::shared_ptr<int[]>, int(*)[]>::value, "");
53 static_assert( HasReset<std::shared_ptr<int[5]>, int*>::value, "");
54 static_assert(!HasReset<std::shared_ptr<int[5]>, int(*)[5]>::value, "");
55 #endif
57 int main(int, char**)
60 std::shared_ptr<B> p(new B);
61 A* ptr = new A;
62 p.reset(ptr);
63 assert(A::count == 1);
64 assert(B::count == 1);
65 assert(p.use_count() == 1);
66 assert(p.get() == ptr);
68 assert(A::count == 0);
70 std::shared_ptr<B> p;
71 A* ptr = new A;
72 p.reset(ptr);
73 assert(A::count == 1);
74 assert(B::count == 1);
75 assert(p.use_count() == 1);
76 assert(p.get() == ptr);
78 assert(A::count == 0);
80 #if TEST_STD_VER > 14
82 std::shared_ptr<const A[]> p;
83 A* ptr = new A[8];
84 p.reset(ptr);
85 assert(A::count == 8);
86 assert(p.use_count() == 1);
87 assert(p.get() == ptr);
89 assert(A::count == 0);
90 #endif
92 return 0;