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