]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/scoped_allocator/2.cc
1 // { dg-options "-std=gnu++11" }
3 // Copyright (C) 2012-2014 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 bool test
__attribute((unused
)) = false;
125 typedef std::pair
<A
, B
> test_type
;
126 typedef uneq_allocator
<test_type
> alloc_type
;
127 typedef scoped_allocator_adaptor
<alloc_type
, alloc_type
> alloc_adaptor
;
130 alloc_adaptor
a(-1, alloc_type(inner_id
)); // outer=-1, inner=2
132 // all pair members that can be constructed with an allocator
133 // should be constructed with the inner allocator, with personality==2
135 auto p
= a
.allocate(1);
137 // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
139 a
.construct(p
, std::piecewise_construct
, t
, t
);
140 VERIFY( p
->first
.id
== 999 );
141 VERIFY( p
->second
.id
== 999 );
144 // construct(pair<T1, T2>* __p)
146 VERIFY( p
->first
.id
== 999 );
147 VERIFY( p
->second
.id
== 999 );
151 // construct(pair<T1, T2>* p, const pair<U, V>& x)
153 VERIFY( p
->first
.id
== 999 );
154 VERIFY( p
->second
.id
== 999 );
157 // construct(pair<T1, T2>* p, pair<U, V>&& x)
158 a
.construct(p
, std::move(pp
));
159 VERIFY( p
->first
.id
== 999 );
160 VERIFY( p
->second
.id
== 999 );
166 template<typename A
, typename B
>
170 bool test
__attribute((unused
)) = false;
172 typedef std::pair
<A
, B
> test_type
;
173 typedef uneq_allocator
<test_type
> alloc_type
;
174 typedef scoped_allocator_adaptor
<alloc_type
, alloc_type
> alloc_adaptor
;
177 alloc_adaptor
a(-1, alloc_type(inner_id
)); // outer=-1, inner=2
179 // all pair members that can be constructed with an allocator
180 // should be constructed with the inner allocator, with personality==2
182 auto p
= a
.allocate(1);
184 // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
185 auto t
= std::make_tuple(copyable(inner_id
));
186 a
.construct(p
, std::piecewise_construct
, t
, t
);
187 VERIFY( p
->first
.verify() );
188 VERIFY( p
->second
.verify() );
191 // construct(pair<T1, T2>* __p)
192 // cannot test this overload using non-DefaultConstructible types
194 // construct(pair<T1, T2>* p, U&& x, V&& y)
195 copyable
c(inner_id
);
196 a
.construct(p
, c
, c
);
197 VERIFY( p
->first
.verify() );
198 VERIFY( p
->second
.verify() );
202 // construct(pair<T1, T2>* p, const pair<U, V>& x)
204 VERIFY( p
->first
.verify() );
205 VERIFY( p
->second
.verify() );
208 // construct(pair<T1, T2>* p, pair<U, V>&& x)
209 a
.construct(p
, std::move(pp
));
210 VERIFY( p
->first
.verify() );
211 VERIFY( p
->second
.verify() );
217 template<typename A
, typename B
>
221 bool test
__attribute((unused
)) = false;
223 typedef std::pair
<A
, B
> test_type
;
224 typedef uneq_allocator
<test_type
> alloc_type
;
225 typedef scoped_allocator_adaptor
<alloc_type
, alloc_type
> alloc_adaptor
;
228 alloc_adaptor
a(-1, alloc_type(inner_id
)); // outer=-1, inner=2
230 // all pair members that can be constructed with an allocator
231 // should be constructed with the inner allocator, with personality==2
233 auto p
= a
.allocate(1);
235 // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
236 a
.construct(p
, std::piecewise_construct
,
237 std::make_tuple(move_only(inner_id
)),
238 std::make_tuple(move_only(inner_id
)));
239 VERIFY( p
->first
.verify() );
240 VERIFY( p
->second
.verify() );
243 // construct(pair<T1, T2>* __p)
244 // cannot test this overload using non-DefaultConstructible types
246 // construct(pair<T1, T2>* p, U&& x, V&& y)
247 a
.construct(p
, move_only(inner_id
), move_only(inner_id
));
248 VERIFY( p
->first
.verify() );
249 VERIFY( p
->second
.verify() );
252 // construct(pair<T1, T2>* p, const pair<U, V>& x)
253 // cannot test this overload using move-only types
255 // construct(pair<T1, T2>* p, pair<U, V>&& x)
256 a
.construct(p
, std::make_pair(move_only(inner_id
), move_only(inner_id
)));
257 VERIFY( p
->first
.verify() );
258 VERIFY( p
->second
.verify() );
266 test_def
<def
, def
>();
267 test_def
<def
, uses_alloc_pre
>();
268 test_def
<def
, uses_alloc_post
>();
269 test_def
<uses_alloc_pre
, def
>();
270 test_def
<uses_alloc_pre
, uses_alloc_pre
>();
271 test_def
<uses_alloc_pre
, uses_alloc_post
>();
272 test_def
<uses_alloc_post
, def
>();
273 test_def
<uses_alloc_post
, uses_alloc_pre
>();
274 test_def
<uses_alloc_post
, uses_alloc_post
>();
279 test_copying
<copyable
, copyable
>();
280 test_copying
<copyable
, uses_alloc_pre
>();
281 test_copying
<copyable
, uses_alloc_post
>();
282 test_copying
<uses_alloc_pre
, copyable
>();
283 test_copying
<uses_alloc_pre
, uses_alloc_pre
>();
284 test_copying
<uses_alloc_pre
, uses_alloc_post
>();
285 test_copying
<uses_alloc_post
, copyable
>();
286 test_copying
<uses_alloc_post
, uses_alloc_pre
>();
287 test_copying
<uses_alloc_post
, uses_alloc_post
>();
292 test_moving
<move_only
, move_only
>();
293 test_moving
<move_only
, uses_alloc_pre
>();
294 test_moving
<move_only
, uses_alloc_post
>();
295 test_moving
<uses_alloc_pre
, move_only
>();
296 test_moving
<uses_alloc_pre
, uses_alloc_pre
>();
297 test_moving
<uses_alloc_pre
, uses_alloc_post
>();
298 test_moving
<uses_alloc_post
, move_only
>();
299 test_moving
<uses_alloc_post
, uses_alloc_pre
>();
300 test_moving
<uses_alloc_post
, uses_alloc_post
>();