]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/scoped_allocator/2.cc
1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2012-2019 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/>.
21 #include <scoped_allocator>
23 #include <testsuite_hooks.h>
24 #include <testsuite_allocator.h>
26 // 20.12.4 Scoped allocator adaptor members [allocator.adaptor.members]
28 // Test piecewise construction of std::pair by scoped_allocator_adaptor
30 using __gnu_test::uneq_allocator
;
31 using std::scoped_allocator_adaptor
;
33 // a DefaultConstructible and CopyConstructible type
41 // a CopyConstructible and non-DefaultConstructible type
44 copyable(int id
) : id(id
) { }
46 // not constructed with an allocator so nothing to test
47 bool verify() const { return true; }
52 // a MoveConstructible and non-DefaultConstructible type
55 move_only(int id
) : id(id
) { }
56 move_only(move_only
&&) = default;
58 // not constructed with an allocator so nothing to test
59 bool verify() const { return true; }
64 // a type for which std::uses_allocator is true
65 struct uses_alloc_post
67 typedef uneq_allocator
<uses_alloc_post
> allocator_type
;
69 uses_alloc_post(const allocator_type
& alloc
)
70 : allocator_personality(alloc
.get_personality()), id(999)
73 uses_alloc_post(copyable arg
, const allocator_type
& alloc
)
74 : allocator_personality(alloc
.get_personality()), id(arg
.id
)
77 uses_alloc_post(move_only arg
, const allocator_type
& alloc
)
78 : allocator_personality(alloc
.get_personality()), id(arg
.id
)
81 // allocator-extended copy ctor
82 uses_alloc_post(const uses_alloc_post
& other
, const allocator_type
& alloc
)
83 : allocator_personality(alloc
.get_personality()), id(other
.id
)
86 // verify we were constructed with right allocator
87 bool verify() const { return allocator_personality
== id
; }
89 int allocator_personality
;
93 // a type for which std::uses_allocator is true
94 struct uses_alloc_pre
: uses_alloc_post
96 typedef uneq_allocator
<uses_alloc_pre
> allocator_type
;
98 uses_alloc_pre(std::allocator_arg_t
, const allocator_type
& alloc
)
99 : uses_alloc_post(alloc
)
102 uses_alloc_pre(std::allocator_arg_t
, const allocator_type
& alloc
,
104 : uses_alloc_post(arg
, alloc
)
107 // allocator-extended copy ctor
108 uses_alloc_pre(std::allocator_arg_t
, const allocator_type
& alloc
,
109 const uses_alloc_pre
& other
)
110 : uses_alloc_post(other
, alloc
)
113 uses_alloc_pre(std::allocator_arg_t
, const allocator_type
& alloc
,
115 : uses_alloc_post(std::move(arg
), alloc
)
119 template<typename A
, typename B
>
123 typedef std::pair
<A
, B
> test_type
;
124 typedef uneq_allocator
<test_type
> alloc_type
;
125 typedef scoped_allocator_adaptor
<alloc_type
, alloc_type
> alloc_adaptor
;
128 alloc_adaptor
a(-1, alloc_type(inner_id
)); // outer=-1, inner=2
130 // all pair members that can be constructed with an allocator
131 // should be constructed with the inner allocator, with personality==2
133 auto p
= a
.allocate(1);
135 // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
137 a
.construct(p
, std::piecewise_construct
, t
, t
);
138 VERIFY( p
->first
.id
== 999 );
139 VERIFY( p
->second
.id
== 999 );
142 // construct(pair<T1, T2>* __p)
144 VERIFY( p
->first
.id
== 999 );
145 VERIFY( p
->second
.id
== 999 );
149 // construct(pair<T1, T2>* p, const pair<U, V>& x)
151 VERIFY( p
->first
.id
== 999 );
152 VERIFY( p
->second
.id
== 999 );
155 // construct(pair<T1, T2>* p, pair<U, V>&& x)
156 a
.construct(p
, std::move(pp
));
157 VERIFY( p
->first
.id
== 999 );
158 VERIFY( p
->second
.id
== 999 );
164 template<typename A
, typename B
>
168 typedef std::pair
<A
, B
> test_type
;
169 typedef uneq_allocator
<test_type
> alloc_type
;
170 typedef scoped_allocator_adaptor
<alloc_type
, alloc_type
> alloc_adaptor
;
173 alloc_adaptor
a(-1, alloc_type(inner_id
)); // outer=-1, inner=2
175 // all pair members that can be constructed with an allocator
176 // should be constructed with the inner allocator, with personality==2
178 auto p
= a
.allocate(1);
180 // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
181 auto t
= std::make_tuple(copyable(inner_id
));
182 a
.construct(p
, std::piecewise_construct
, t
, t
);
183 VERIFY( p
->first
.verify() );
184 VERIFY( p
->second
.verify() );
187 // construct(pair<T1, T2>* __p)
188 // cannot test this overload using non-DefaultConstructible types
190 // construct(pair<T1, T2>* p, U&& x, V&& y)
191 copyable
c(inner_id
);
192 a
.construct(p
, c
, c
);
193 VERIFY( p
->first
.verify() );
194 VERIFY( p
->second
.verify() );
198 // construct(pair<T1, T2>* p, const pair<U, V>& x)
200 VERIFY( p
->first
.verify() );
201 VERIFY( p
->second
.verify() );
204 // construct(pair<T1, T2>* p, pair<U, V>&& x)
205 a
.construct(p
, std::move(pp
));
206 VERIFY( p
->first
.verify() );
207 VERIFY( p
->second
.verify() );
213 template<typename A
, typename B
>
217 typedef std::pair
<A
, B
> test_type
;
218 typedef uneq_allocator
<test_type
> alloc_type
;
219 typedef scoped_allocator_adaptor
<alloc_type
, alloc_type
> alloc_adaptor
;
222 alloc_adaptor
a(-1, alloc_type(inner_id
)); // outer=-1, inner=2
224 // all pair members that can be constructed with an allocator
225 // should be constructed with the inner allocator, with personality==2
227 auto p
= a
.allocate(1);
229 // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
230 a
.construct(p
, std::piecewise_construct
,
231 std::make_tuple(move_only(inner_id
)),
232 std::make_tuple(move_only(inner_id
)));
233 VERIFY( p
->first
.verify() );
234 VERIFY( p
->second
.verify() );
237 // construct(pair<T1, T2>* __p)
238 // cannot test this overload using non-DefaultConstructible types
240 // construct(pair<T1, T2>* p, U&& x, V&& y)
241 a
.construct(p
, move_only(inner_id
), move_only(inner_id
));
242 VERIFY( p
->first
.verify() );
243 VERIFY( p
->second
.verify() );
246 // construct(pair<T1, T2>* p, const pair<U, V>& x)
247 // cannot test this overload using move-only types
249 // construct(pair<T1, T2>* p, pair<U, V>&& x)
250 a
.construct(p
, std::make_pair(move_only(inner_id
), move_only(inner_id
)));
251 VERIFY( p
->first
.verify() );
252 VERIFY( p
->second
.verify() );
260 test_def
<def
, def
>();
261 test_def
<def
, uses_alloc_pre
>();
262 test_def
<def
, uses_alloc_post
>();
263 test_def
<uses_alloc_pre
, def
>();
264 test_def
<uses_alloc_pre
, uses_alloc_pre
>();
265 test_def
<uses_alloc_pre
, uses_alloc_post
>();
266 test_def
<uses_alloc_post
, def
>();
267 test_def
<uses_alloc_post
, uses_alloc_pre
>();
268 test_def
<uses_alloc_post
, uses_alloc_post
>();
273 test_copying
<copyable
, copyable
>();
274 test_copying
<copyable
, uses_alloc_pre
>();
275 test_copying
<copyable
, uses_alloc_post
>();
276 test_copying
<uses_alloc_pre
, copyable
>();
277 test_copying
<uses_alloc_pre
, uses_alloc_pre
>();
278 test_copying
<uses_alloc_pre
, uses_alloc_post
>();
279 test_copying
<uses_alloc_post
, copyable
>();
280 test_copying
<uses_alloc_post
, uses_alloc_pre
>();
281 test_copying
<uses_alloc_post
, uses_alloc_post
>();
286 test_moving
<move_only
, move_only
>();
287 test_moving
<move_only
, uses_alloc_pre
>();
288 test_moving
<move_only
, uses_alloc_post
>();
289 test_moving
<uses_alloc_pre
, move_only
>();
290 test_moving
<uses_alloc_pre
, uses_alloc_pre
>();
291 test_moving
<uses_alloc_pre
, uses_alloc_post
>();
292 test_moving
<uses_alloc_post
, move_only
>();
293 test_moving
<uses_alloc_post
, uses_alloc_pre
>();
294 test_moving
<uses_alloc_post
, uses_alloc_post
>();