]>
Commit | Line | Data |
---|---|---|
52066eae | 1 | // { dg-do run { target c++11 } } |
aaf0ca6f | 2 | |
8d9254fc | 3 | // Copyright (C) 2005-2020 Free Software Foundation, Inc. |
aaf0ca6f JW |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
aaf0ca6f JW |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
aaf0ca6f JW |
19 | |
20 | // 20.6.6.2 Template class shared_ptr [util.smartptr.shared] | |
21 | ||
22 | #include <memory> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
25 | struct A | |
26 | { | |
27 | A() { ++ctor_count; } | |
28 | ~A() { ++dtor_count; } | |
29 | static long ctor_count; | |
30 | static long dtor_count; | |
31 | }; | |
32 | long A::ctor_count = 0; | |
33 | long A::dtor_count = 0; | |
34 | ||
35 | struct B : A | |
36 | { | |
37 | B() { ++ctor_count; } | |
38 | ~B() { ++dtor_count; } | |
39 | static long ctor_count; | |
40 | static long dtor_count; | |
41 | }; | |
42 | long B::ctor_count = 0; | |
43 | long B::dtor_count = 0; | |
44 | ||
45 | struct D | |
46 | { | |
47 | void operator()(const B* p) { delete p; ++delete_count; } | |
48 | static long delete_count; | |
49 | }; | |
50 | long D::delete_count = 0; | |
51 | ||
52 | struct reset_count_struct | |
53 | { | |
54 | ~reset_count_struct() | |
55 | { | |
56 | A::ctor_count = 0; | |
57 | A::dtor_count = 0; | |
58 | B::ctor_count = 0; | |
59 | B::dtor_count = 0; | |
60 | D::delete_count = 0; | |
61 | } | |
62 | }; | |
63 | ||
64 | ||
65 | // 20.6.6.2.2 shared_ptr destructor [util.smartptr.shared.dest] | |
66 | ||
67 | // empty shared_ptr | |
68 | int | |
69 | test01() | |
70 | { | |
71 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
72 | |
73 | { | |
74 | std::shared_ptr<A> a; | |
75 | } | |
76 | VERIFY( A::ctor_count == 0 ); | |
77 | VERIFY( A::dtor_count == 0 ); | |
78 | VERIFY( B::ctor_count == 0 ); | |
79 | VERIFY( B::dtor_count == 0 ); | |
80 | VERIFY( D::delete_count == 0 ); | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
85 | // shared ownership | |
86 | int | |
87 | test02() | |
88 | { | |
89 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
90 | |
91 | std::shared_ptr<A> a; | |
92 | { | |
93 | a = std::shared_ptr<A>(new B, D()); | |
94 | } | |
95 | VERIFY( A::ctor_count == 1 ); | |
96 | VERIFY( A::dtor_count == 0 ); | |
97 | VERIFY( B::ctor_count == 1 ); | |
98 | VERIFY( B::dtor_count == 0 ); | |
99 | VERIFY( D::delete_count == 0 ); | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | // exclusive ownership | |
105 | int | |
106 | test03() | |
107 | { | |
108 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
109 | |
110 | { | |
111 | std::shared_ptr<A> a1(new B); | |
112 | std::shared_ptr<A> a2(new B, D()); | |
113 | } | |
114 | VERIFY( A::ctor_count == 2 ); | |
115 | VERIFY( A::dtor_count == 2 ); | |
116 | VERIFY( B::ctor_count == 2 ); | |
117 | VERIFY( B::dtor_count == 2 ); | |
118 | VERIFY( D::delete_count == 1 ); | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
123 | ||
124 | int | |
125 | main() | |
126 | { | |
127 | test01(); | |
128 | test02(); | |
129 | test03(); | |
130 | return 0; | |
131 | } |