]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/array.cc
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
4 // Copyright (C) 2016-2022 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 #include <testsuite_hooks.h>
24 // C++17 20.11.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
26 template<typename To
, typename From
>
27 constexpr bool check()
29 using std::shared_ptr
;
30 using std::is_constructible
;
31 return !is_constructible
<shared_ptr
<To
>, shared_ptr
<From
>>::value
32 && !is_constructible
<shared_ptr
<To
>, shared_ptr
<From
>&>::value
;
35 static_assert( check
<int, int[]>() );
36 static_assert( check
<int, int[2]>() );
37 static_assert( check
<int[2], void>() );
38 static_assert( check
<int[2], int>() );
39 static_assert( check
<int[2], int[]>() );
40 static_assert( check
<int[], void>() );
41 static_assert( check
<int[], int>() );
52 static_assert( check
<A
, B
[2]>() );
53 static_assert( check
<A
, B
[]>() );
54 static_assert( check
<A
[2], B
>() );
55 static_assert( check
<A
[2], B
[2]>() );
56 static_assert( check
<A
[2], B
[]>() );
57 static_assert( check
<A
[], B
>() );
58 static_assert( check
<A
[], B
[2]>() );
59 static_assert( check
<A
[], B
[]>() );
64 std::shared_ptr
<A
[2]> p
;
65 VERIFY( p
.get() == nullptr );
66 VERIFY( p
.use_count() == 0 );
74 std::shared_ptr
<A
[]> p
;
75 VERIFY( p
.get() == nullptr );
76 VERIFY( p
.use_count() == 0 );
84 std::shared_ptr
<A
[2]> p(nullptr);
85 VERIFY( p
.get() == nullptr );
86 VERIFY( p
.use_count() == 0 );
94 std::shared_ptr
<A
[]> p(nullptr);
95 VERIFY( p
.get() == nullptr );
96 VERIFY( p
.use_count() == 0 );
101 // Construction from pointer
106 A
* const a
= nullptr;
107 std::shared_ptr
<A
[2]> p(a
);
108 VERIFY( p
.get() == nullptr );
109 VERIFY( p
.use_count() == 1 );
111 VERIFY( count
== 0 );
117 A
* const a
= nullptr;
118 std::shared_ptr
<A
[]> p(a
);
119 VERIFY( p
.get() == nullptr );
120 VERIFY( p
.use_count() == 1 );
122 VERIFY( count
== 0 );
128 A
* const a
= new A
[2];
129 std::shared_ptr
<A
[2]> p(a
);
130 VERIFY( p
.get() == a
);
131 VERIFY( p
.use_count() == 1 );
133 VERIFY( count
== 0 );
139 A
* const a
= new A
[2];
140 std::shared_ptr
<A
[]> p(a
);
141 VERIFY( p
.get() == a
);
142 VERIFY( p
.use_count() == 1 );
144 VERIFY( count
== 0 );
147 // Converting constructor
152 A
* const a
= new A
[2];
153 std::shared_ptr
<A
[2]> p(a
);
154 std::shared_ptr
<const A
[2]> p2(p
);
155 VERIFY( p2
.get() == a
);
156 VERIFY( p
.use_count() == 2 );
157 VERIFY( p2
.use_count() == 2 );
159 VERIFY( count
!= 0 );
161 VERIFY( count
== 0 );
167 A
* const a
= new A
[2];
168 std::shared_ptr
<A
[]> p(a
);
169 std::shared_ptr
<const A
[]> p2(p
);
170 VERIFY( p2
.get() == a
);
171 VERIFY( p
.use_count() == 2 );
172 VERIFY( p2
.use_count() == 2 );
174 VERIFY( count
!= 0 );
176 VERIFY( count
== 0 );
182 A
* const a
= new A
[2];
183 std::shared_ptr
<A
[2]> p(a
);
184 std::shared_ptr
<const A
[]> p2(p
);
185 VERIFY( p2
.get() == a
);
186 VERIFY( p
.use_count() == 2 );
187 VERIFY( p2
.use_count() == 2 );
189 VERIFY( count
!= 0 );
191 VERIFY( count
== 0 );
199 A
* const a
= new A
[2];
200 std::shared_ptr
<A
[2]> p(a
);
201 std::shared_ptr
<A
[2]> p2(p
);
202 VERIFY( p2
.get() == a
);
203 VERIFY( p
.use_count() == 2 );
204 VERIFY( p2
.use_count() == 2 );
206 VERIFY( count
!= 0 );
208 VERIFY( count
== 0 );
214 A
* const a
= new A
[2];
215 std::shared_ptr
<A
[2]> p(a
);
216 std::shared_ptr
<A
[]> p2(p
);
217 VERIFY( p2
.get() == a
);
218 VERIFY( p
.use_count() == 2 );
219 VERIFY( p2
.use_count() == 2 );
221 VERIFY( count
!= 0 );
223 VERIFY( count
== 0 );
231 A
* const a
= new A
[2];
232 std::shared_ptr
<A
[2]> p(a
);
233 std::shared_ptr
<A
[2]> p2(std::move(p
));
234 VERIFY( p
.get() == nullptr );
235 VERIFY( p2
.get() == a
);
236 VERIFY( p
.use_count() == 0 );
237 VERIFY( p2
.use_count() == 1 );
239 VERIFY( count
== 0 );
245 A
* const a
= new A
[2];
246 std::shared_ptr
<A
[2]> p(a
);
247 std::shared_ptr
<A
[]> p2(std::move(p
));
248 VERIFY( p
.get() == nullptr );
249 VERIFY( p2
.get() == a
);
250 VERIFY( p
.use_count() == 0 );
251 VERIFY( p2
.use_count() == 1 );
253 VERIFY( count
== 0 );