]>
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-2024 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
;
67 std::experimental::shared_ptr
<A
[5]> a1
;
68 std::experimental::shared_ptr
<A
[5]> a2(a1
);
69 VERIFY( a2
.use_count() == 0 );
70 VERIFY( A::ctor_count
== 0 );
71 VERIFY( A::dtor_count
== 0 );
72 VERIFY( B::ctor_count
== 0 );
73 VERIFY( B::dtor_count
== 0 );
81 reset_count_struct
__attribute__((unused
)) reset
;
83 std::experimental::shared_ptr
<A
[5]> a1(new A
[5]);
84 std::experimental::shared_ptr
<A
[5]> a2(a1
);
85 VERIFY( a2
.use_count() == 2 );
86 VERIFY( A::ctor_count
== 5 );
87 VERIFY( A::dtor_count
== 0 );
88 VERIFY( B::ctor_count
== 0 );
89 VERIFY( B::dtor_count
== 0 );
97 reset_count_struct
__attribute__((unused
)) reset
;
99 std::experimental::shared_ptr
<A
[5]> a1(new A
[5], &deleter
);
100 std::experimental::shared_ptr
<A
[5]> a2(a1
);
101 VERIFY( a2
.use_count() == 2 );
102 VERIFY( A::ctor_count
== 5 );
103 VERIFY( A::dtor_count
== 0 );
104 VERIFY( B::ctor_count
== 0 );
105 VERIFY( B::dtor_count
== 0 );
113 reset_count_struct
__attribute__((unused
)) reset
;
115 std::experimental::shared_ptr
<A
[5]> a1(std::experimental::shared_ptr
<A
[5]>
117 VERIFY( a1
.use_count() == 1 );
118 VERIFY( A::ctor_count
== 5 );
119 VERIFY( A::dtor_count
== 0 );
120 VERIFY( B::ctor_count
== 0 );
121 VERIFY( B::dtor_count
== 0 );
129 reset_count_struct
__attribute__((unused
)) reset
;
131 std::experimental::shared_ptr
<A
[5]> a1(new A
[5]);
132 std::experimental::shared_ptr
<A
[]> a2(a1
);
134 VERIFY( a2
.use_count() == 2 );
135 VERIFY( a2
.get() == a1
.get() );
136 VERIFY( A::ctor_count
== 5 );
137 VERIFY( A::dtor_count
== 0 );
138 VERIFY( B::ctor_count
== 0 );
139 VERIFY( B::dtor_count
== 0 );
147 reset_count_struct
__attribute__((unused
)) reset
;
149 std::experimental::shared_ptr
<B
> a1(new B
);
150 std::experimental::shared_ptr
<A
> a2(a1
);
152 VERIFY( a2
.use_count() == 2 );
153 VERIFY( a2
.get() == a1
.get() );
154 VERIFY( A::ctor_count
== 1 );
155 VERIFY( A::dtor_count
== 0 );
156 VERIFY( B::ctor_count
== 1 );
157 VERIFY( B::dtor_count
== 0 );