Debian Codegraph @

libstdc++-v3/testsuite/experimental/scopeguard/uniqueres.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-add-options no_pch }
3
4 #include <experimental/scope>
5
6 #ifndef __cpp_lib_experimental_scope
7 # error Feature-test macro is not defined.
8 #elif __cpp_lib_experimental_scope < 201902
9 # error Feature-test macro has bad value.
10 #endif
11
12 #include <testsuite_hooks.h>
13
14 using std::experimental::unique_resource;
15
16 void
17 test_default_cons()
18 {
19 struct val { int i; };
20
21 struct del
22 {
23 void operator()(val) const { VERIFY(false); }
24 int i;
25 };
26
27 static_assert( std::is_default_constructible_v<unique_resource<val, del>> );
28 static_assert( !std::is_default_constructible_v<unique_resource<val&, del>> );
29 // GCC extension:
30 static_assert( std::is_nothrow_default_constructible_v<unique_resource<val, del>> );
31 struct exval : val { exval() noexcept(false) { } };
32 static_assert( !std::is_nothrow_default_constructible_v<unique_resource<exval, del>> );
33
34 unique_resource<val, del> res;
35 VERIFY( res.get().i == 0 ); // value-initialized
36 VERIFY( res.get_deleter().i == 0 ); // value-initialized
37 }
38
39 void
40 test_cons()
41 {
42 struct val { int i; };
43
44 struct del
45 {
46 void operator()(val v) const { VERIFY(v.i == i); }
47 int i;
48 };
49
50 auto r1 = unique_resource<val, del>(val{}, del{});
51 VERIFY( r1.get().i == 0 );
52 VERIFY( r1.get_deleter().i == 0 );
53
54 auto r2 = unique_resource<val, del>(1, 2);
55 VERIFY( r2.get().i == 1 );
56 VERIFY( r2.get_deleter().i == 2 );
57 r2.release();
58
59 val v{3};
60 auto r3 = unique_resource<val&, del>(v, 3);
61
62 static_assert( !std::is_constructible_v<unique_resource<val&, del>, val, del> );
63 static_assert( !std::is_constructible_v<unique_resource<val&, del>, int, del> );
64 static_assert( !std::is_constructible_v<unique_resource<val&, del>, const val&, del> );
65
66 del d4{4};
67 auto r4 = unique_resource(std::ref(v), std::ref(d4));
68 --d4.i;
69
70 static_assert( std::is_same_v<decltype(r4),
71 unique_resource<std::reference_wrapper<val>,
72 std::reference_wrapper<del>>> );
73 static_assert( !std::is_constructible_v<decltype(r4), val, del> );
74
75 int counter = 0, dcounter = 99;
76 {
77 unique_resource r(std::ref(counter),
78 [&dcounter] (int& i) { ++dcounter; ++i; });
79 }
80 VERIFY( counter == 1 );
81 VERIFY( dcounter == 100 );
82
83 {
84 struct NothrowMove
85 {
86 NothrowMove() noexcept { }
87 NothrowMove(NothrowMove&&) noexcept(true) { }
88 NothrowMove(const NothrowMove&) { throw 1; }
89 };
90
91 unique_resource r(NothrowMove{},
92 [&dcounter] (NothrowMove&) { ++dcounter; });
93 }
94 VERIFY( dcounter == 101 );
95
96 {
97 struct ThrowOnCopy
98 {
99 ThrowOnCopy() noexcept { }
100 ThrowOnCopy(ThrowOnCopy&&) noexcept(false) { VERIFY(false); };
101 ThrowOnCopy(const ThrowOnCopy&) { throw 1; }
102 explicit ThrowOnCopy(val) noexcept(false) { VERIFY(false); }
103 explicit ThrowOnCopy(val&) noexcept(false) { }
104 };
105 auto d = [&dcounter] (auto&) { ++dcounter; };
106
107 unique_resource r(val(1), d); // uses ThrowOnCopy(val&)
108
109 try {
110 unique_resource r(ThrowOnCopy{}, d); // uses copy constructor
111 VERIFY( false );
112 } catch (int) {
113 VERIFY( dcounter == 102 );
114 }
115 }
116 VERIFY( dcounter == 103 );
117
118 {
119 struct CopyVal
120 {
121 explicit CopyVal(const val& v) : i(v.i) { }
122 int i;
123 };
124
125 struct Del
126 {
127 void operator()(const val&) { VERIFY(false); }
128 void operator()(const CopyVal& c) { ref = c.i; }
129 int& ref;
130 };
131
132 struct CopyDel
133 {
134 explicit CopyDel(Del&&) noexcept(false) { VERIFY(false); }
135 explicit CopyDel(const Del&) noexcept(false) { throw 1; }
136 void operator()(const val&) = delete;
137 void operator()(const CopyVal&) { VERIFY(false); }
138 };
139
140 try {
141 // CopyVal is constructed from val(11), then initializing CopyDel throws.
142 // The CopyVal member is passed to the Del argument to be freed.
143 unique_resource<CopyVal, CopyDel> r(val(11), Del{dcounter});
144 VERIFY( false );
145 } catch (int) {
146 VERIFY( dcounter == 11 );
147 }
148 }
149 }
150
151 void
152 test_move_cons()
153 {
154 {
155 struct Del
156 {
157 void operator()(int) const { VERIFY(false); }
158 };
159
160 unique_resource<int, Del> r0;
161 auto rr0 = std::move(r0);
162 VERIFY( r0.get() == 0 );
163 VERIFY( rr0.get() == 0 );
164
165 struct DelThrowingCopy
166 {
167 DelThrowingCopy() = default;
168 DelThrowingCopy(const DelThrowingCopy&) { throw 1; }
169 void operator()(int) const { VERIFY(false); }
170 };
171
172 unique_resource<int, DelThrowingCopy> r1;
173 try {
174 auto rr1 = std::move(r1); // Initializing deleter throws.
175 VERIFY( false );
176 } catch (int) {
177 }
178 }
179
180 {
181 struct Res
182 {
183 Res() = default;
184 Res(Res&& r) noexcept : moved(r.moved) { r.moved = true; }
185 Res(Res& r) : moved(r.moved) { }
186 bool moved = false;
187 };
188
189 unique_resource r(Res{}, [](const auto&) { });
190 auto rr = std::move(r);
191 VERIFY( r.get().moved == true );
192 VERIFY( rr.get().moved == false );
193 }
194
195 {
196 struct Res2
197 {
198 Res2() = default;
199 Res2(Res2&& r) noexcept(false) : moved(r.moved) { r.moved = false; }
200 Res2(Res2& r) : moved(r.moved) { }
201 bool moved = false;
202 };
203
204 unique_resource r2(Res2{}, [](const auto&) { });
205 auto rr2 = std::move(r2);
206 VERIFY( r2.get().moved == false );
207 VERIFY( rr2.get().moved == false );
208 }
209
210 {
211 struct ThrowingCopy
212 {
213 ThrowingCopy(int) { }
214 ThrowingCopy(const ThrowingCopy&) { throw 1; }
215 };
216
217 int dcounter = 0;
218 {
219 auto d = [&dcounter] (const auto&) { ++dcounter; };
220 unique_resource<ThrowingCopy, decltype(d)> r(1, d);
221 try {
222 auto rr = std::move(r); // Ownership of resource left with 'r'
223 VERIFY(false);
224 } catch (int) {
225 VERIFY( dcounter == 0 );
226 }
227 }
228 VERIFY( dcounter == 1 );
229 }
230 }
231
232 int called1 = 0;
233
234 void
235 test_assign()
236 {
237 struct ThrowingDel
238 {
239 ThrowingDel() = default;
240 ThrowingDel(int& called) : called(called) { }
241 ThrowingDel(const ThrowingDel&) = default;
242 ThrowingDel& operator=(const ThrowingDel&) { throw 1; }
243
244 void operator()(int i) const noexcept { ++called; }
245 int& called = called1;
246 };
247
248 int called2 = 0;
249 {
250 unique_resource<int, ThrowingDel> r1;
251 VERIFY( r1.get() == 0 );
252 unique_resource<int, ThrowingDel> r2(2, ThrowingDel{called2});
253 VERIFY( r2.get() == 2 );
254 try
255 {
256 r1 = std::move(r2);
257 VERIFY( false );
258 }
259 catch (int)
260 {
261 }
262 VERIFY( called1 == 0 ); // r1.reset() was called, but did nothing.
263 VERIFY( called2 == 0 ); // r2.reset() not called.
264 VERIFY( r1.get() == 0 );
265 VERIFY( r2.get() == 2 );
266 }
267 VERIFY( called1 == 0 ); // r1.reset() was called, but did nothing.
268 VERIFY( called2 == 1 ); // r2 destructor invoked its deleter.
269 }
270
271 void
272 test_modifiers()
273 {
274 int dcounter = 0;
275 auto d = [&dcounter] (int i) { dcounter += i; };
276 unique_resource<int, decltype(d)> r(1, d);
277 r.reset();
278 VERIFY( dcounter == 1 );
279 r.reset(2);
280 VERIFY( dcounter == 1 );
281 r.release();
282 VERIFY( dcounter == 1 );
283 r.release();
284 VERIFY( dcounter == 1 );
285 r.reset(3);
286 VERIFY( dcounter == 1 );
287 r.reset(4);
288 VERIFY( dcounter == 4 );
289 }
290
291 template<typename T> concept has_star = requires (T& t) { *t; };
292 template<typename T> concept has_arrow = requires (T& t) { t.operator->(); };
293
294 void
295 test_observers()
296 {
297 struct D { void operator()(int* p) const noexcept { delete p; } };
298 int* p = new int(3);
299 unique_resource<int*, D> r(p, D{});
300 VERIFY( r.get() == p );
301 VERIFY( *r == 3 );
302 VERIFY( r.operator->() == p );
303 (void) r.get_deleter();
304
305 using R1 = unique_resource<int, void(*)(int)>;
306 static_assert( ! has_star<R1> );
307 static_assert( ! has_arrow<R1> );
308 using R2 = unique_resource<const void*, void(*)(const void*)>;
309 static_assert( ! has_star<R2> );
310 static_assert( has_arrow<R2> );
311 }
312
313 void
314 test_make_checked()
315 {
316 struct Boolish {
317 explicit operator bool() const noexcept { return val; }
318 bool val;
319 };
320
321 using std::experimental::make_unique_resource_checked;
322
323 {
324 struct ThrowingCopy
325 {
326 ThrowingCopy(int i) : val(i) { }
327 ThrowingCopy(const ThrowingCopy&) { throw 1; }
328 Boolish operator==(int i) const noexcept { return {i == val}; }
329 int val;
330 };
331
332 int dcounter = 0;
333 auto d = [&dcounter] (const auto&) { ++dcounter; };
334
335 try
336 {
337 (void) make_unique_resource_checked(ThrowingCopy(1), 0, d);
338 VERIFY(false);
339 }
340 catch (int)
341 {
342 VERIFY(dcounter == 1);
343 }
344
345 dcounter = 0;
346 try
347 {
348 (void) make_unique_resource_checked(ThrowingCopy(1), 1, d);
349 VERIFY(false);
350 }
351 catch (int)
352 {
353 VERIFY(dcounter == 0);
354 }
355 }
356 }
357
358 int main()
359 {
360 test_default_cons();
361 test_cons();
362 test_move_cons();
363 test_assign();
364 test_modifiers();
365 test_observers();
366 test_make_checked();
367 }