]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/array.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / array.cc
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
3
4 // Copyright (C) 2016-2024 Free Software Foundation, Inc.
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
24 // C++17 20.11.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
25
26 template<typename To, typename From>
27 constexpr 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
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>() );
42
43 int count = 0;
44
45 struct A {
46 A() { ++count; }
47 ~A() { --count; }
48 };
49
50 struct B : A { };
51
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[]>() );
60
61 void
62 test01()
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
71 void
72 test02()
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
81 void
82 test03()
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
91 void
92 test04()
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
103 void
104 test05()
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
114 void
115 test06()
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
125 void
126 test07()
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
136 void
137 test08()
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
147 // Converting constructor
148
149 void
150 test09()
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
164 void
165 test10()
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
179 void
180 test11()
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
196 void
197 test12()
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
211 void
212 test13()
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
228 void
229 test14()
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
242 void
243 test15()
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
256 int
257 main()
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 }