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 // template<class Y> void reset(Y* p);
14 
15 #include <memory>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
20 struct B
21 {
22     static int count;
23 
BB24     B() {++count;}
BB25     B(const B&) {++count;}
~BB26     virtual ~B() {--count;}
27 };
28 
29 int B::count = 0;
30 
31 struct A
32     : public B
33 {
34     static int count;
35 
AA36     A() {++count;}
AA37     A(const A& other) : B(other) {++count;}
~AA38     ~A() {--count;}
39 };
40 
41 int A::count = 0;
42 
main(int,char **)43 int main(int, char**)
44 {
45     {
46         std::shared_ptr<B> p(new B);
47         A* ptr = new A;
48         p.reset(ptr);
49         assert(A::count == 1);
50         assert(B::count == 1);
51         assert(p.use_count() == 1);
52         assert(p.get() == ptr);
53     }
54     assert(A::count == 0);
55     {
56         std::shared_ptr<B> p;
57         A* ptr = new A;
58         p.reset(ptr);
59         assert(A::count == 1);
60         assert(B::count == 1);
61         assert(p.use_count() == 1);
62         assert(p.get() == ptr);
63     }
64     assert(A::count == 0);
65 
66 #if TEST_STD_VER > 14
67     {
68         std::shared_ptr<const A[]> p;
69         A* ptr = new A[8];
70         p.reset(ptr);
71         assert(A::count == 8);
72         assert(p.use_count() == 1);
73         assert(p.get() == ptr);
74     }
75     assert(A::count == 0);
76 #endif
77 
78   return 0;
79 }
80