]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/shared_ptr/cons/move.cc
boost_shared_ptr.h: Add support for allocators, aliasing, make_shared and rvalue...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / move.cc
CommitLineData
aaf0ca6f
JW
1// { dg-options "-std=gnu++0x" }
2
3// Copyright (C) 2007 Free Software Foundation
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
8// Free Software Foundation; either version 2, or (at your option)
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
17// with this library; see the file COPYING. If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// TR1 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
22
23#include <memory>
24#include <utility>
25#include <testsuite_hooks.h>
26
27struct A
28{
29 A() { ++ctor_count; }
30 virtual ~A() { ++dtor_count; }
31 static long ctor_count;
32 static long dtor_count;
33};
34long A::ctor_count = 0;
35long A::dtor_count = 0;
36
37struct B : A
38{
39 B() { ++ctor_count; }
40 virtual ~B() { ++dtor_count; }
41 static long ctor_count;
42 static long dtor_count;
43};
44long B::ctor_count = 0;
45long B::dtor_count = 0;
46
47struct D
48{
49 void operator()(B* p) const { delete p; ++delete_count; }
50 static long delete_count;
51};
52long D::delete_count = 0;
53
54struct reset_count_struct
55{
56 ~reset_count_struct()
57 {
58 A::ctor_count = 0;
59 A::dtor_count = 0;
60 B::ctor_count = 0;
61 B::dtor_count = 0;
62 D::delete_count = 0;
63 }
64};
65
66// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
67
68// Rvalue construction
69int test01()
70{
71 reset_count_struct __attribute__((unused)) reset;
72 bool test __attribute__((unused)) = true;
73
74 std::shared_ptr<A> a1;
75 std::shared_ptr<A> a2(std::move(a1));
76 VERIFY( a1.use_count() == 0 );
77 VERIFY( a2.use_count() == 0 );
78 VERIFY( A::ctor_count == 0 );
79 VERIFY( A::dtor_count == 0 );
80 VERIFY( B::ctor_count == 0 );
81 VERIFY( B::dtor_count == 0 );
82
83 return 0;
84}
85
86int
87test02()
88{
89 reset_count_struct __attribute__((unused)) reset;
90 bool test __attribute__((unused)) = true;
91
92 std::shared_ptr<A> a1(new A);
93 std::shared_ptr<A> a2(std::move(a1));
94 VERIFY( a1.use_count() == 0 );
95 VERIFY( a2.use_count() == 1 );
96 VERIFY( A::ctor_count == 1 );
97 VERIFY( A::dtor_count == 0 );
98
99 return 0;
100}
101
102int
103test03()
104{
105 reset_count_struct __attribute__((unused)) reset;
106 bool test __attribute__((unused)) = true;
107
108 std::shared_ptr<B> b(new B);
109 std::shared_ptr<A> a(std::move(b));
110 VERIFY( b.use_count() == 0 );
111 VERIFY( a.use_count() == 1 );
112 VERIFY( A::ctor_count == 1 );
113 VERIFY( A::dtor_count == 0 );
114 VERIFY( B::ctor_count == 1 );
115 VERIFY( B::dtor_count == 0 );
116
117 return 0;
118}
119
120int
121test04()
122{
123 reset_count_struct __attribute__((unused)) reset;
124 bool test __attribute__((unused)) = true;
125
126 std::shared_ptr<B> b(new B, D());
127 std::shared_ptr<A> a(std::move(b));
128 VERIFY( b.use_count() == 0 );
129 VERIFY( a.use_count() == 1 );
130 VERIFY( A::ctor_count == 1 );
131 VERIFY( A::dtor_count == 0 );
132 VERIFY( B::ctor_count == 1 );
133 VERIFY( B::dtor_count == 0 );
134
135 a = std::move(std::shared_ptr<A>());
136 VERIFY( D::delete_count == 1 );
137 VERIFY( B::dtor_count == 1 );
138
139 return 0;
140}
141
142int
143test05()
144{
145 reset_count_struct __attribute__((unused)) reset;
146 bool test __attribute__((unused)) = true;
147
148 std::shared_ptr<A> a(std::move(std::shared_ptr<A>(new A)));
149 VERIFY( a.use_count() == 1 );
150 VERIFY( A::ctor_count == 1 );
151 VERIFY( A::dtor_count == 0 );
152
153 return 0;
154}
155
156int
157main()
158{
159 test01();
160 test02();
161 test03();
162 test04();
163 test05();
164 return 0;
165}