]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/move.cc
1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2007-2019 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // TR1 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
24 #include <testsuite_hooks.h>
29 virtual ~A() { ++dtor_count
; }
30 static long ctor_count
;
31 static long dtor_count
;
33 long A::ctor_count
= 0;
34 long A::dtor_count
= 0;
39 virtual ~B() { ++dtor_count
; }
40 static long ctor_count
;
41 static long dtor_count
;
43 long B::ctor_count
= 0;
44 long B::dtor_count
= 0;
48 void operator()(B
* p
) const { delete p
; ++delete_count
; }
49 static long delete_count
;
51 long D::delete_count
= 0;
53 struct reset_count_struct
65 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
67 // Rvalue construction
70 reset_count_struct
__attribute__((unused
)) reset
;
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 );
87 reset_count_struct
__attribute__((unused
)) reset
;
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 );
102 reset_count_struct
__attribute__((unused
)) reset
;
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 );
119 reset_count_struct
__attribute__((unused
)) reset
;
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 );
130 a
= std::move(std::shared_ptr
<A
>());
131 VERIFY( D::delete_count
== 1 );
132 VERIFY( B::dtor_count
== 1 );
140 reset_count_struct
__attribute__((unused
)) reset
;
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 );