1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef SET_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
10#define SET_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
11
12// <set>
13// <unordered_set>
14
15// class set
16// class unordered_set
17
18// insert(...);
19// emplace(...);
20// emplace_hint(...);
21
22
23#include <cassert>
24#include <iterator>
25
26#include "test_macros.h"
27#include "count_new.h"
28#include "container_test_types.h"
29
30
31template <class Container>
32void testSetInsert()
33{
34 typedef typename Container::value_type ValueTp;
35 ConstructController* cc = getConstructController();
36 cc->reset();
37 {
38 // Testing C::insert(const value_type&)"
39 Container c;
40 const ValueTp v(42);
41 cc->expect<const ValueTp&>();
42 assert(c.insert(v).second);
43 assert(!cc->unchecked());
44 {
45 DisableAllocationGuard g;
46 const ValueTp v2(42);
47 assert(c.insert(v2).second == false);
48 }
49 }
50 {
51 // Testing C::insert(value_type&)"
52 Container c;
53 ValueTp v(42);
54 cc->expect<const ValueTp&>();
55 assert(c.insert(v).second);
56 assert(!cc->unchecked());
57 {
58 DisableAllocationGuard g;
59 ValueTp v2(42);
60 assert(c.insert(v2).second == false);
61 }
62 }
63 {
64 // Testing C::insert(value_type&&)"
65 Container c;
66 ValueTp v(42);
67 cc->expect<ValueTp&&>();
68 assert(c.insert(std::move(v)).second);
69 assert(!cc->unchecked());
70 {
71 DisableAllocationGuard g;
72 ValueTp v2(42);
73 assert(c.insert(std::move(v2)).second == false);
74 }
75 }
76 {
77 // Testing C::insert(const value_type&&)"
78 Container c;
79 const ValueTp v(42);
80 cc->expect<const ValueTp&>();
81 assert(c.insert(std::move(v)).second);
82 assert(!cc->unchecked());
83 {
84 DisableAllocationGuard g;
85 const ValueTp v2(42);
86 assert(c.insert(std::move(v2)).second == false);
87 }
88 }
89 {
90 // Testing C::insert(std::initializer_list<ValueTp>)"
91 Container c;
92 std::initializer_list<ValueTp> il = { ValueTp(1), ValueTp(2) };
93 cc->expect<ValueTp const&>(2);
94 c.insert(il);
95 assert(!cc->unchecked());
96 {
97 DisableAllocationGuard g;
98 c.insert(il);
99 }
100 }
101 {
102 // Testing C::insert(Iter, Iter) for *Iter = value_type const&"
103 Container c;
104 const ValueTp ValueList[] = { ValueTp(1), ValueTp(2), ValueTp(3) };
105 cc->expect<ValueTp const&>(3);
106 c.insert(std::begin(ValueList), std::end(ValueList));
107 assert(!cc->unchecked());
108 {
109 DisableAllocationGuard g;
110 c.insert(std::begin(ValueList), std::end(ValueList));
111 }
112 }
113 {
114 // Testing C::insert(Iter, Iter) for *Iter = value_type&&"
115 Container c;
116 ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
117 cc->expect<ValueTp&&>(3);
118 c.insert(std::move_iterator<ValueTp*>(std::begin(ValueList)),
119 std::move_iterator<ValueTp*>(std::end(ValueList)));
120 assert(!cc->unchecked());
121 {
122 DisableAllocationGuard g;
123 ValueTp ValueList2[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
124 c.insert(std::move_iterator<ValueTp*>(std::begin(ValueList2)),
125 std::move_iterator<ValueTp*>(std::end(ValueList2)));
126 }
127 }
128 {
129 // Testing C::insert(Iter, Iter) for *Iter = value_type&"
130 Container c;
131 ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
132 cc->expect<ValueTp const&>(3);
133 c.insert(std::begin(ValueList), std::end(ValueList));
134 assert(!cc->unchecked());
135 {
136 DisableAllocationGuard g;
137 c.insert(std::begin(ValueList), std::end(ValueList));
138 }
139 }
140}
141
142
143template <class Container>
144void testSetEmplace()
145{
146 typedef typename Container::value_type ValueTp;
147 ConstructController* cc = getConstructController();
148 cc->reset();
149 {
150 // Testing C::emplace(const value_type&)"
151 Container c;
152 const ValueTp v(42);
153 cc->expect<const ValueTp&>();
154 assert(c.emplace(v).second);
155 assert(!cc->unchecked());
156 {
157 DisableAllocationGuard g;
158 const ValueTp v2(42);
159 assert(c.emplace(v2).second == false);
160 }
161 }
162 {
163 // Testing C::emplace(value_type&)"
164 Container c;
165 ValueTp v(42);
166 cc->expect<ValueTp&>();
167 assert(c.emplace(v).second);
168 assert(!cc->unchecked());
169 {
170 DisableAllocationGuard g;
171 ValueTp v2(42);
172 assert(c.emplace(v2).second == false);
173 }
174 }
175 {
176 // Testing C::emplace(value_type&&)"
177 Container c;
178 ValueTp v(42);
179 cc->expect<ValueTp&&>();
180 assert(c.emplace(std::move(v)).second);
181 assert(!cc->unchecked());
182 {
183 DisableAllocationGuard g;
184 ValueTp v2(42);
185 assert(c.emplace(std::move(v2)).second == false);
186 }
187 }
188 {
189 // Testing C::emplace(const value_type&&)"
190 Container c;
191 const ValueTp v(42);
192 cc->expect<const ValueTp&&>();
193 assert(c.emplace(std::move(v)).second);
194 assert(!cc->unchecked());
195 {
196 DisableAllocationGuard g;
197 const ValueTp v2(42);
198 assert(c.emplace(std::move(v2)).second == false);
199 }
200 }
201}
202
203
204template <class Container>
205void testSetEmplaceHint()
206{
207 typedef typename Container::value_type ValueTp;
208 typedef Container C;
209 typedef typename C::iterator It;
210 ConstructController* cc = getConstructController();
211 cc->reset();
212 {
213 // Testing C::emplace_hint(p, const value_type&)"
214 Container c;
215 const ValueTp v(42);
216 cc->expect<const ValueTp&>();
217 It ret = c.emplace_hint(c.end(), v);
218 assert(ret != c.end());
219 assert(c.size() == 1);
220 assert(!cc->unchecked());
221 {
222 DisableAllocationGuard g;
223 const ValueTp v2(42);
224 It ret2 = c.emplace_hint(c.begin(), v2);
225 assert(&(*ret2) == &(*ret));
226 assert(c.size() == 1);
227 }
228 }
229 {
230 // Testing C::emplace_hint(p, value_type&)"
231 Container c;
232 ValueTp v(42);
233 cc->expect<ValueTp&>();
234 It ret = c.emplace_hint(c.end(), v);
235 assert(ret != c.end());
236 assert(c.size() == 1);
237 assert(!cc->unchecked());
238 {
239 DisableAllocationGuard g;
240 ValueTp v2(42);
241 It ret2 = c.emplace_hint(c.begin(), v2);
242 assert(&(*ret2) == &(*ret));
243 assert(c.size() == 1);
244 }
245 }
246 {
247 // Testing C::emplace_hint(p, value_type&&)"
248 Container c;
249 ValueTp v(42);
250 cc->expect<ValueTp&&>();
251 It ret = c.emplace_hint(c.end(), std::move(v));
252 assert(ret != c.end());
253 assert(c.size() == 1);
254 assert(!cc->unchecked());
255 {
256 DisableAllocationGuard g;
257 ValueTp v2(42);
258 It ret2 = c.emplace_hint(c.begin(), std::move(v2));
259 assert(&(*ret2) == &(*ret));
260 assert(c.size() == 1);
261 }
262 }
263 {
264 // Testing C::emplace_hint(p, const value_type&&)"
265 Container c;
266 const ValueTp v(42);
267 cc->expect<const ValueTp&&>();
268 It ret = c.emplace_hint(c.end(), std::move(v));
269 assert(ret != c.end());
270 assert(c.size() == 1);
271 assert(!cc->unchecked());
272 {
273 DisableAllocationGuard g;
274 const ValueTp v2(42);
275 It ret2 = c.emplace_hint(c.begin(), std::move(v2));
276 assert(&(*ret2) == &(*ret));
277 assert(c.size() == 1);
278 }
279 }
280}
281
282
283template <class Container>
284void testMultisetInsert()
285{
286 typedef typename Container::value_type ValueTp;
287 ConstructController* cc = getConstructController();
288 cc->reset();
289 {
290 // Testing C::insert(const value_type&)"
291 Container c;
292 const ValueTp v(42);
293 cc->expect<const ValueTp&>();
294 c.insert(v);
295 assert(!cc->unchecked());
296 }
297 {
298 // Testing C::insert(value_type&)"
299 Container c;
300 ValueTp v(42);
301 cc->expect<const ValueTp&>();
302 c.insert(v);
303 assert(!cc->unchecked());
304 }
305 {
306 // Testing C::insert(value_type&&)"
307 Container c;
308 ValueTp v(42);
309 cc->expect<ValueTp&&>();
310 c.insert(std::move(v));
311 assert(!cc->unchecked());
312 }
313 {
314 // Testing C::insert(std::initializer_list<ValueTp>)"
315 Container c;
316 std::initializer_list<ValueTp> il = { ValueTp(1), ValueTp(2) };
317 cc->expect<ValueTp const&>(2);
318 c.insert(il);
319 assert(!cc->unchecked());
320 }
321 {
322 // Testing C::insert(Iter, Iter) for *Iter = value_type const&"
323 Container c;
324 const ValueTp ValueList[] = { ValueTp(1), ValueTp(2), ValueTp(3) };
325 cc->expect<ValueTp const&>(3);
326 c.insert(std::begin(ValueList), std::end(ValueList));
327 assert(!cc->unchecked());
328 }
329 {
330 // Testing C::insert(Iter, Iter) for *Iter = value_type&&"
331 Container c;
332 ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
333 cc->expect<ValueTp&&>(3);
334 c.insert(std::move_iterator<ValueTp*>(std::begin(ValueList)),
335 std::move_iterator<ValueTp*>(std::end(ValueList)));
336 assert(!cc->unchecked());
337 }
338 {
339 // Testing C::insert(Iter, Iter) for *Iter = value_type&"
340 Container c;
341 ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(1) };
342 cc->expect<ValueTp&>(3);
343 c.insert(std::begin(ValueList), std::end(ValueList));
344 assert(!cc->unchecked());
345 }
346}
347
348
349template <class Container>
350void testMultisetEmplace()
351{
352 typedef typename Container::value_type ValueTp;
353 ConstructController* cc = getConstructController();
354 cc->reset();
355 {
356 // Testing C::emplace(const value_type&)"
357 Container c;
358 const ValueTp v(42);
359 cc->expect<const ValueTp&>();
360 c.emplace(v);
361 assert(!cc->unchecked());
362 }
363 {
364 // Testing C::emplace(value_type&)"
365 Container c;
366 ValueTp v(42);
367 cc->expect<ValueTp&>();
368 c.emplace(v);
369 assert(!cc->unchecked());
370 }
371 {
372 // Testing C::emplace(value_type&&)"
373 Container c;
374 ValueTp v(42);
375 cc->expect<ValueTp&&>();
376 c.emplace(std::move(v));
377 assert(!cc->unchecked());
378 }
379 {
380 // Testing C::emplace(const value_type&&)"
381 Container c;
382 const ValueTp v(42);
383 cc->expect<const ValueTp&&>();
384 c.emplace(std::move(v));
385 assert(!cc->unchecked());
386 }
387}
388
389#endif
390

source code of libcxx/test/std/containers/set_allocator_requirement_test_templates.h