1// { dg-options "-std=gnu++0x" }
2
3// Copyright (C) 2007, 2009 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 3, 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 COPYING3.  If not see
18// <http://www.gnu.org/licenses/>.
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
26struct A
27{
28  A() { ++ctor_count; }
29  virtual ~A() { ++dtor_count; }
30  static long ctor_count;
31  static long dtor_count;
32};
33long A::ctor_count = 0;
34long A::dtor_count = 0;
35
36struct B : A
37{
38  B() { ++ctor_count; }
39  virtual ~B() { ++dtor_count; }
40  static long ctor_count;
41  static long dtor_count;
42};
43long B::ctor_count = 0;
44long B::dtor_count = 0;
45
46struct D
47{
48  void operator()(B* p) const { delete p; ++delete_count; }
49  static long delete_count;
50};
51long D::delete_count = 0;
52
53struct 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
68int test01()
69{
70  reset_count_struct __attribute__((unused)) reset;
71  bool test __attribute__((unused)) = true;
72
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 );
81
82  return 0;
83}
84
85int
86test02()
87{
88  reset_count_struct __attribute__((unused)) reset;
89  bool test __attribute__((unused)) = true;
90
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 );
97
98  return 0;
99}
100
101int
102test03()
103{
104  reset_count_struct __attribute__((unused)) reset;
105  bool test __attribute__((unused)) = true;
106
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 );
115
116  return 0;
117}
118
119int
120test04()
121{
122  reset_count_struct __attribute__((unused)) reset;
123  bool test __attribute__((unused)) = true;
124
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 );
133
134  a = std::move(std::shared_ptr<A>());
135  VERIFY( D::delete_count == 1 );
136  VERIFY( B::dtor_count == 1 );
137
138  return 0;
139}
140
141int
142test05()
143{
144  reset_count_struct __attribute__((unused)) reset;
145  bool test __attribute__((unused)) = true;
146
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 );
151
152  return 0;
153}
154
155int
156main()
157{
158  test01();
159  test02();
160  test03();
161  test04();
162  test05();
163  return 0;
164}
165