Debian Codegraph @

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr.pass.cpp

raw
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 // shared_ptr(const shared_ptr& r);
14
15 #include <memory>
16 #include <cassert>
17
18 #include "test_macros.h"
19
20 struct A
21 {
22 static int count;
23
24 A() {++count;}
25 A(const A&) {++count;}
26 ~A() {--count;}
27 };
28
29 int A::count = 0;
30
31 int main(int, char**)
32 {
33 {
34 std::shared_ptr<A> pA(new A);
35 assert(pA.use_count() == 1);
36 assert(A::count == 1);
37 {
38 std::shared_ptr<A> pA2(pA);
39 assert(A::count == 1);
40 assert(pA.use_count() == 2);
41 assert(pA2.use_count() == 2);
42 assert(pA2.get() == pA.get());
43 }
44 assert(pA.use_count() == 1);
45 assert(A::count == 1);
46 }
47 assert(A::count == 0);
48 {
49 std::shared_ptr<A> pA;
50 assert(pA.use_count() == 0);
51 assert(A::count == 0);
52 {
53 std::shared_ptr<A> pA2(pA);
54 assert(A::count == 0);
55 assert(pA.use_count() == 0);
56 assert(pA2.use_count() == 0);
57 assert(pA2.get() == pA.get());
58 }
59 assert(pA.use_count() == 0);
60 assert(A::count == 0);
61 }
62 assert(A::count == 0);
63
64 {
65 std::shared_ptr<A const> pA(new A);
66 std::shared_ptr<A const> pA2(pA);
67 assert(pA.get() == pA2.get());
68 }
69
70 return 0;
71 }