]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/copy_ctor.cc
1 // { dg-do run { target c++14 } }
3 // Copyright (C) 2015-2016 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 // 8.2.1 Class template shared_ptr [memory.smartptr.shared]
22 #include <experimental/memory>
23 #include <testsuite_hooks.h>
28 virtual ~A() { ++dtor_count
; }
29 static long ctor_count
;
30 static long dtor_count
;
32 long A::ctor_count
= 0;
33 long A::dtor_count
= 0;
38 virtual ~B() { ++dtor_count
; }
39 static long ctor_count
;
40 static long dtor_count
;
42 long B::ctor_count
= 0;
43 long B::dtor_count
= 0;
45 void deleter(A
* p
) { delete [] p
; }
47 struct reset_count_struct
58 // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const]
65 reset_count_struct
__attribute__((unused
)) reset
;
66 bool test
__attribute__((unused
)) = true;
68 std::experimental::shared_ptr
<A
[5]> a1
;
69 std::experimental::shared_ptr
<A
[5]> a2(a1
);
70 VERIFY( a2
.use_count() == 0 );
71 VERIFY( A::ctor_count
== 0 );
72 VERIFY( A::dtor_count
== 0 );
73 VERIFY( B::ctor_count
== 0 );
74 VERIFY( B::dtor_count
== 0 );
82 reset_count_struct
__attribute__((unused
)) reset
;
83 bool test
__attribute__((unused
)) = true;
85 std::experimental::shared_ptr
<A
[5]> a1(new A
[5]);
86 std::experimental::shared_ptr
<A
[5]> a2(a1
);
87 VERIFY( a2
.use_count() == 2 );
88 VERIFY( A::ctor_count
== 5 );
89 VERIFY( A::dtor_count
== 0 );
90 VERIFY( B::ctor_count
== 0 );
91 VERIFY( B::dtor_count
== 0 );
99 reset_count_struct
__attribute__((unused
)) reset
;
100 bool test
__attribute__((unused
)) = true;
102 std::experimental::shared_ptr
<A
[5]> a1(new A
[5], &deleter
);
103 std::experimental::shared_ptr
<A
[5]> a2(a1
);
104 VERIFY( a2
.use_count() == 2 );
105 VERIFY( A::ctor_count
== 5 );
106 VERIFY( A::dtor_count
== 0 );
107 VERIFY( B::ctor_count
== 0 );
108 VERIFY( B::dtor_count
== 0 );
116 reset_count_struct
__attribute__((unused
)) reset
;
117 bool test
__attribute__((unused
)) = true;
119 std::experimental::shared_ptr
<A
[5]> a1(std::experimental::shared_ptr
<A
[5]>
121 VERIFY( a1
.use_count() == 1 );
122 VERIFY( A::ctor_count
== 5 );
123 VERIFY( A::dtor_count
== 0 );
124 VERIFY( B::ctor_count
== 0 );
125 VERIFY( B::dtor_count
== 0 );
133 reset_count_struct
__attribute__((unused
)) reset
;
134 bool test
__attribute__((unused
)) = true;
136 std::experimental::shared_ptr
<A
[5]> a1(new A
[5]);
137 std::experimental::shared_ptr
<A
[]> a2(a1
);
139 VERIFY( a2
.use_count() == 2 );
140 VERIFY( a2
.get() == a1
.get() );
141 VERIFY( A::ctor_count
== 5 );
142 VERIFY( A::dtor_count
== 0 );
143 VERIFY( B::ctor_count
== 0 );
144 VERIFY( B::dtor_count
== 0 );
152 reset_count_struct
__attribute__((unused
)) reset
;
153 bool test
__attribute__((unused
)) = true;
155 std::experimental::shared_ptr
<B
> a1(new B
);
156 std::experimental::shared_ptr
<A
> a2(a1
);
158 VERIFY( a2
.use_count() == 2 );
159 VERIFY( a2
.get() == a1
.get() );
160 VERIFY( A::ctor_count
== 1 );
161 VERIFY( A::dtor_count
== 0 );
162 VERIFY( B::ctor_count
== 1 );
163 VERIFY( B::dtor_count
== 0 );