]>
Commit | Line | Data |
---|---|---|
52066eae | 1 | // { dg-do run { target c++11 } } |
aaf0ca6f | 2 | |
8d9254fc | 3 | // Copyright (C) 2007-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 | // TR1 20.6.6.2 Template class shared_ptr [util.smartptr.shared] | |
21 | ||
22 | #include <memory> | |
23 | #include <utility> | |
24 | #include <testsuite_hooks.h> | |
25 | ||
26 | struct A | |
27 | { | |
28 | A() { ++ctor_count; } | |
29 | virtual ~A() { ++dtor_count; } | |
30 | static long ctor_count; | |
31 | static long dtor_count; | |
32 | }; | |
33 | long A::ctor_count = 0; | |
34 | long A::dtor_count = 0; | |
35 | ||
36 | struct B : A | |
37 | { | |
38 | B() { ++ctor_count; } | |
39 | virtual ~B() { ++dtor_count; } | |
40 | static long ctor_count; | |
41 | static long dtor_count; | |
42 | }; | |
43 | long B::ctor_count = 0; | |
44 | long B::dtor_count = 0; | |
45 | ||
46 | struct D | |
47 | { | |
48 | void operator()(B* p) const { delete p; ++delete_count; } | |
49 | static long delete_count; | |
50 | }; | |
51 | long D::delete_count = 0; | |
52 | ||
53 | struct reset_count_struct | |
54 | { | |
55 | ~reset_count_struct() | |
56 | { | |
57 | A::ctor_count = 0; | |
58 | A::dtor_count = 0; | |
59 | B::ctor_count = 0; | |
60 | B::dtor_count = 0; | |
61 | D::delete_count = 0; | |
62 | } | |
63 | }; | |
64 | ||
65 | // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const] | |
66 | ||
67 | // Rvalue construction | |
68 | int test01() | |
69 | { | |
70 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
71 | |
72 | std::shared_ptr<A> a1; | |
73 | std::shared_ptr<A> a2(std::move(a1)); | |
74 | VERIFY( a1.use_count() == 0 ); | |
75 | VERIFY( a2.use_count() == 0 ); | |
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 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | int | |
85 | test02() | |
86 | { | |
87 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
88 | |
89 | std::shared_ptr<A> a1(new A); | |
90 | std::shared_ptr<A> a2(std::move(a1)); | |
91 | VERIFY( a1.use_count() == 0 ); | |
92 | VERIFY( a2.use_count() == 1 ); | |
93 | VERIFY( A::ctor_count == 1 ); | |
94 | VERIFY( A::dtor_count == 0 ); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | int | |
100 | test03() | |
101 | { | |
102 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
103 | |
104 | std::shared_ptr<B> b(new B); | |
105 | std::shared_ptr<A> a(std::move(b)); | |
106 | VERIFY( b.use_count() == 0 ); | |
107 | VERIFY( a.use_count() == 1 ); | |
108 | VERIFY( A::ctor_count == 1 ); | |
109 | VERIFY( A::dtor_count == 0 ); | |
110 | VERIFY( B::ctor_count == 1 ); | |
111 | VERIFY( B::dtor_count == 0 ); | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | int | |
117 | test04() | |
118 | { | |
119 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
120 | |
121 | std::shared_ptr<B> b(new B, D()); | |
122 | std::shared_ptr<A> a(std::move(b)); | |
123 | VERIFY( b.use_count() == 0 ); | |
124 | VERIFY( a.use_count() == 1 ); | |
125 | VERIFY( A::ctor_count == 1 ); | |
126 | VERIFY( A::dtor_count == 0 ); | |
127 | VERIFY( B::ctor_count == 1 ); | |
128 | VERIFY( B::dtor_count == 0 ); | |
129 | ||
130 | a = std::move(std::shared_ptr<A>()); | |
131 | VERIFY( D::delete_count == 1 ); | |
132 | VERIFY( B::dtor_count == 1 ); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | int | |
138 | test05() | |
139 | { | |
140 | reset_count_struct __attribute__((unused)) reset; | |
aaf0ca6f JW |
141 | |
142 | std::shared_ptr<A> a(std::move(std::shared_ptr<A>(new A))); | |
143 | VERIFY( a.use_count() == 1 ); | |
144 | VERIFY( A::ctor_count == 1 ); | |
145 | VERIFY( A::dtor_count == 0 ); | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | int | |
151 | main() | |
152 | { | |
153 | test01(); | |
154 | test02(); | |
155 | test03(); | |
156 | test04(); | |
157 | test05(); | |
158 | return 0; | |
159 | } |