]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/move.cc
1 // { dg-options "-std=gnu++0x" }
3 // Copyright (C) 2007-2013 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
;
71 bool test
__attribute__((unused
)) = true;
73 std::shared_ptr
<A
> a1
;
74 std::shared_ptr
<A
> a2(std::move(a1
));
75 VERIFY( a1
.use_count() == 0 );
76 VERIFY( a2
.use_count() == 0 );
77 VERIFY( A::ctor_count
== 0 );
78 VERIFY( A::dtor_count
== 0 );
79 VERIFY( B::ctor_count
== 0 );
80 VERIFY( B::dtor_count
== 0 );
88 reset_count_struct
__attribute__((unused
)) reset
;
89 bool test
__attribute__((unused
)) = true;
91 std::shared_ptr
<A
> a1(new A
);
92 std::shared_ptr
<A
> a2(std::move(a1
));
93 VERIFY( a1
.use_count() == 0 );
94 VERIFY( a2
.use_count() == 1 );
95 VERIFY( A::ctor_count
== 1 );
96 VERIFY( A::dtor_count
== 0 );
104 reset_count_struct
__attribute__((unused
)) reset
;
105 bool test
__attribute__((unused
)) = true;
107 std::shared_ptr
<B
> b(new B
);
108 std::shared_ptr
<A
> a(std::move(b
));
109 VERIFY( b
.use_count() == 0 );
110 VERIFY( a
.use_count() == 1 );
111 VERIFY( A::ctor_count
== 1 );
112 VERIFY( A::dtor_count
== 0 );
113 VERIFY( B::ctor_count
== 1 );
114 VERIFY( B::dtor_count
== 0 );
122 reset_count_struct
__attribute__((unused
)) reset
;
123 bool test
__attribute__((unused
)) = true;
125 std::shared_ptr
<B
> b(new B
, D());
126 std::shared_ptr
<A
> a(std::move(b
));
127 VERIFY( b
.use_count() == 0 );
128 VERIFY( a
.use_count() == 1 );
129 VERIFY( A::ctor_count
== 1 );
130 VERIFY( A::dtor_count
== 0 );
131 VERIFY( B::ctor_count
== 1 );
132 VERIFY( B::dtor_count
== 0 );
134 a
= std::move(std::shared_ptr
<A
>());
135 VERIFY( D::delete_count
== 1 );
136 VERIFY( B::dtor_count
== 1 );
144 reset_count_struct
__attribute__((unused
)) reset
;
145 bool test
__attribute__((unused
)) = true;
147 std::shared_ptr
<A
> a(std::move(std::shared_ptr
<A
>(new A
)));
148 VERIFY( a
.use_count() == 1 );
149 VERIFY( A::ctor_count
== 1 );
150 VERIFY( A::dtor_count
== 0 );