]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/shared_ptr/cons/array.cc
libstdc++: Disable hosted-only tests [PR103626]
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / array.cc
CommitLineData
b2343559 1// { dg-do run { target c++11 } }
7cc9022f 2// { dg-require-effective-target hosted }
b2343559 3
7adcbafe 4// Copyright (C) 2016-2022 Free Software Foundation, Inc.
b2343559
JW
5//
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)
10// any later version.
11
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.
16
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/>.
20
21#include <memory>
22#include <testsuite_hooks.h>
23
7b936140 24// C++17 20.11.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
b2343559
JW
25
26template<typename To, typename From>
27constexpr bool check()
28{
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;
33}
34
35static_assert( check<int, int[]>() );
36static_assert( check<int, int[2]>() );
37static_assert( check<int[2], void>() );
38static_assert( check<int[2], int>() );
39static_assert( check<int[2], int[]>() );
40static_assert( check<int[], void>() );
41static_assert( check<int[], int>() );
42
43int count = 0;
44
45struct A {
46 A() { ++count; }
47 ~A() { --count; }
48};
49
50struct B : A { };
51
52static_assert( check<A, B[2]>() );
53static_assert( check<A, B[]>() );
54static_assert( check<A[2], B>() );
55static_assert( check<A[2], B[2]>() );
56static_assert( check<A[2], B[]>() );
57static_assert( check<A[], B>() );
58static_assert( check<A[], B[2]>() );
59static_assert( check<A[], B[]>() );
60
61void
62test01()
63{
64 std::shared_ptr<A[2]> p;
65 VERIFY( p.get() == nullptr );
66 VERIFY( p.use_count() == 0 );
67 p.reset();
68 VERIFY( count == 0 );
69}
70
71void
72test02()
73{
74 std::shared_ptr<A[]> p;
75 VERIFY( p.get() == nullptr );
76 VERIFY( p.use_count() == 0 );
77 p.reset();
78 VERIFY( count == 0 );
79}
80
81void
82test03()
83{
84 std::shared_ptr<A[2]> p(nullptr);
85 VERIFY( p.get() == nullptr );
86 VERIFY( p.use_count() == 0 );
87 p.reset();
88 VERIFY( count == 0 );
89}
90
91void
92test04()
93{
94 std::shared_ptr<A[]> p(nullptr);
95 VERIFY( p.get() == nullptr );
96 VERIFY( p.use_count() == 0 );
97 p.reset();
98 VERIFY( count == 0 );
99}
100
101// Construction from pointer
102
103void
104test05()
105{
106 A * const a = nullptr;
107 std::shared_ptr<A[2]> p(a);
108 VERIFY( p.get() == nullptr );
109 VERIFY( p.use_count() == 1 );
110 p.reset();
111 VERIFY( count == 0 );
112}
113
114void
115test06()
116{
117 A * const a = nullptr;
118 std::shared_ptr<A[]> p(a);
119 VERIFY( p.get() == nullptr );
120 VERIFY( p.use_count() == 1 );
121 p.reset();
122 VERIFY( count == 0 );
123}
124
125void
126test07()
127{
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 );
132 p.reset();
133 VERIFY( count == 0 );
134}
135
136void
137test08()
138{
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 );
143 p.reset();
144 VERIFY( count == 0 );
145}
146
45cae5b6 147// Converting constructor
b2343559
JW
148
149void
150test09()
151{
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 );
158 p.reset();
159 VERIFY( count != 0 );
160 p2.reset();
161 VERIFY( count == 0 );
162}
163
164void
165test10()
166{
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 );
173 p.reset();
174 VERIFY( count != 0 );
175 p2.reset();
176 VERIFY( count == 0 );
177}
178
179void
180test11()
181{
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 );
188 p.reset();
189 VERIFY( count != 0 );
190 p2.reset();
191 VERIFY( count == 0 );
192}
193
194// Copy construction
195
196void
197test12()
198{
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 );
205 p.reset();
206 VERIFY( count != 0 );
207 p2.reset();
208 VERIFY( count == 0 );
209}
210
211void
212test13()
213{
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 );
220 p.reset();
221 VERIFY( count != 0 );
222 p2.reset();
223 VERIFY( count == 0 );
224}
225
226// Move construction
227
228void
229test14()
230{
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 );
238 p2.reset();
239 VERIFY( count == 0 );
240}
241
242void
243test15()
244{
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 );
252 p2.reset();
253 VERIFY( count == 0 );
254}
255
256int
257main()
258{
259 test01();
260 test02();
261 test03();
262 test04();
263 test05();
264 test06();
265 test07();
266 test08();
267 test09();
268 test10();
269 test11();
270 test12();
271 test13();
272 test14();
273 test15();
274}