]>
Commit | Line | Data |
---|---|---|
8868286e | 1 | // { dg-options "-std=gnu++11" } |
acc2a0cb | 2 | |
f1717362 | 3 | // Copyright (C) 2012-2016 Free Software Foundation, Inc. |
acc2a0cb | 4 | // |
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) | |
9 | // any later version. | |
10 | ||
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. | |
15 | ||
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/>. | |
19 | ||
20 | #include <memory> | |
21 | #include <scoped_allocator> | |
22 | #include <vector> | |
23 | #include <testsuite_hooks.h> | |
24 | #include <testsuite_allocator.h> | |
25 | ||
26 | // 20.12.4 Scoped allocator adaptor members [allocator.adaptor.members] | |
27 | // | |
28 | // Test piecewise construction of std::pair by scoped_allocator_adaptor | |
29 | ||
30 | using __gnu_test::uneq_allocator; | |
31 | using std::scoped_allocator_adaptor; | |
32 | ||
33 | // a DefaultConstructible and CopyConstructible type | |
34 | struct def | |
35 | { | |
36 | def() : id(999) { } | |
37 | ||
38 | int id; | |
39 | }; | |
40 | ||
41 | // a CopyConstructible and non-DefaultConstructible type | |
42 | struct copyable | |
43 | { | |
44 | copyable(int id) : id(id) { } | |
45 | ||
46 | // not constructed with an allocator so nothing to test | |
47 | bool verify() const { return true; } | |
48 | ||
49 | int id; | |
50 | }; | |
51 | ||
52 | // a MoveConstructible and non-DefaultConstructible type | |
53 | struct move_only | |
54 | { | |
55 | move_only(int id) : id(id) { } | |
56 | move_only(move_only&&) = default; | |
57 | ||
58 | // not constructed with an allocator so nothing to test | |
59 | bool verify() const { return true; } | |
60 | ||
61 | int id; | |
62 | }; | |
63 | ||
64 | // a type for which std::uses_allocator is true | |
65 | struct uses_alloc_post | |
66 | { | |
67 | typedef uneq_allocator<uses_alloc_post> allocator_type; | |
68 | ||
69 | uses_alloc_post(const allocator_type& alloc) | |
70 | : allocator_personality(alloc.get_personality()), id(999) | |
71 | { } | |
72 | ||
73 | uses_alloc_post(copyable arg, const allocator_type& alloc) | |
74 | : allocator_personality(alloc.get_personality()), id(arg.id) | |
75 | { } | |
76 | ||
77 | uses_alloc_post(move_only arg, const allocator_type& alloc) | |
78 | : allocator_personality(alloc.get_personality()), id(arg.id) | |
79 | { } | |
80 | ||
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) | |
84 | { } | |
85 | ||
86 | // verify we were constructed with right allocator | |
87 | bool verify() const { return allocator_personality == id; } | |
88 | ||
89 | int allocator_personality; | |
90 | int id; | |
91 | }; | |
92 | ||
93 | // a type for which std::uses_allocator is true | |
94 | struct uses_alloc_pre : uses_alloc_post | |
95 | { | |
96 | typedef uneq_allocator<uses_alloc_pre> allocator_type; | |
97 | ||
98 | uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc) | |
99 | : uses_alloc_post(alloc) | |
100 | { } | |
101 | ||
102 | uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc, | |
103 | copyable arg) | |
104 | : uses_alloc_post(arg, alloc) | |
105 | { } | |
106 | ||
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) | |
111 | { } | |
112 | ||
113 | uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc, | |
114 | move_only arg) | |
115 | : uses_alloc_post(std::move(arg), alloc) | |
116 | { } | |
117 | }; | |
118 | ||
119 | template<typename A, typename B> | |
120 | void | |
121 | test_def() | |
122 | { | |
123 | bool test __attribute((unused)) = false; | |
124 | ||
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; | |
128 | ||
129 | int inner_id = 2; | |
130 | alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2 | |
131 | ||
132 | // all pair members that can be constructed with an allocator | |
133 | // should be constructed with the inner allocator, with personality==2 | |
134 | ||
135 | auto p = a.allocate(1); | |
136 | ||
137 | // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>) | |
138 | std::tuple<> t; | |
139 | a.construct(p, std::piecewise_construct, t, t); | |
140 | VERIFY( p->first.id == 999 ); | |
141 | VERIFY( p->second.id == 999 ); | |
142 | a.destroy(p); | |
143 | ||
144 | // construct(pair<T1, T2>* __p) | |
145 | a.construct(p); | |
146 | VERIFY( p->first.id == 999 ); | |
147 | VERIFY( p->second.id == 999 ); | |
148 | auto pp = *p; | |
149 | a.destroy(p); | |
150 | ||
151 | // construct(pair<T1, T2>* p, const pair<U, V>& x) | |
152 | a.construct(p, pp); | |
153 | VERIFY( p->first.id == 999 ); | |
154 | VERIFY( p->second.id == 999 ); | |
155 | a.destroy(p); | |
156 | ||
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 ); | |
161 | a.destroy(p); | |
162 | ||
163 | a.deallocate(p, 1); | |
164 | } | |
165 | ||
166 | template<typename A, typename B> | |
167 | void | |
168 | test_copying() | |
169 | { | |
170 | bool test __attribute((unused)) = false; | |
171 | ||
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; | |
175 | ||
176 | int inner_id = 2; | |
177 | alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2 | |
178 | ||
179 | // all pair members that can be constructed with an allocator | |
180 | // should be constructed with the inner allocator, with personality==2 | |
181 | ||
182 | auto p = a.allocate(1); | |
183 | ||
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() ); | |
189 | a.destroy(p); | |
190 | ||
191 | // construct(pair<T1, T2>* __p) | |
192 | // cannot test this overload using non-DefaultConstructible types | |
193 | ||
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() ); | |
199 | auto pp = *p; | |
200 | a.destroy(p); | |
201 | ||
202 | // construct(pair<T1, T2>* p, const pair<U, V>& x) | |
203 | a.construct(p, pp); | |
204 | VERIFY( p->first.verify() ); | |
205 | VERIFY( p->second.verify() ); | |
206 | a.destroy(p); | |
207 | ||
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() ); | |
212 | a.destroy(p); | |
213 | ||
214 | a.deallocate(p, 1); | |
215 | } | |
216 | ||
217 | template<typename A, typename B> | |
218 | void | |
219 | test_moving() | |
220 | { | |
221 | bool test __attribute((unused)) = false; | |
222 | ||
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; | |
226 | ||
227 | int inner_id = 2; | |
228 | alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2 | |
229 | ||
230 | // all pair members that can be constructed with an allocator | |
231 | // should be constructed with the inner allocator, with personality==2 | |
232 | ||
233 | auto p = a.allocate(1); | |
234 | ||
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() ); | |
241 | a.destroy(p); | |
242 | ||
243 | // construct(pair<T1, T2>* __p) | |
244 | // cannot test this overload using non-DefaultConstructible types | |
245 | ||
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() ); | |
250 | a.destroy(p); | |
251 | ||
252 | // construct(pair<T1, T2>* p, const pair<U, V>& x) | |
253 | // cannot test this overload using move-only types | |
254 | ||
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() ); | |
259 | a.destroy(p); | |
260 | ||
261 | a.deallocate(p, 1); | |
262 | } | |
263 | ||
264 | void test01() | |
265 | { | |
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>(); | |
275 | } | |
276 | ||
277 | void test02() | |
278 | { | |
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>(); | |
288 | } | |
289 | ||
290 | void test03() | |
291 | { | |
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>(); | |
301 | } | |
302 | ||
303 | int main() | |
304 | { | |
305 | test01(); | |
306 | test02(); | |
307 | test03(); | |
308 | } |