1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 #ifndef MAP_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
9 #define MAP_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
15 // class unordered_map
25 #include "test_macros.h"
26 #include "count_new.h"
27 #include "container_test_types.h"
30 template <class Container
>
33 typedef typename
Container::value_type ValueTp
;
34 ConstructController
* cc
= getConstructController();
37 // Testing C::insert(const value_type&)
39 const ValueTp
v(42, 1);
40 cc
->expect
<const ValueTp
&>();
41 assert(c
.insert(v
).second
);
42 assert(!cc
->unchecked());
44 DisableAllocationGuard g
;
45 const ValueTp
v2(42, 1);
46 assert(c
.insert(v2
).second
== false);
50 // Testing C::insert(value_type&)
53 cc
->expect
<const ValueTp
&>();
54 assert(c
.insert(v
).second
);
55 assert(!cc
->unchecked());
57 DisableAllocationGuard g
;
59 assert(c
.insert(v2
).second
== false);
63 // Testing C::insert(value_type&&)
66 cc
->expect
<ValueTp
&&>();
67 assert(c
.insert(std::move(v
)).second
);
68 assert(!cc
->unchecked());
70 DisableAllocationGuard g
;
72 assert(c
.insert(std::move(v2
)).second
== false);
76 // Testing C::insert(const value_type&&)
78 const ValueTp
v(42, 1);
79 cc
->expect
<const ValueTp
&>();
80 assert(c
.insert(std::move(v
)).second
);
81 assert(!cc
->unchecked());
83 DisableAllocationGuard g
;
84 const ValueTp
v2(42, 1);
85 assert(c
.insert(std::move(v2
)).second
== false);
89 // Testing C::insert({key, value})
91 cc
->expect
<ValueTp
&&>();
92 assert(c
.insert({42, 1}).second
);
93 assert(!cc
->unchecked());
95 DisableAllocationGuard g
;
96 const ValueTp
v2(42, 1);
97 assert(c
.insert(std::move(v2
)).second
== false);
101 // Testing C::insert(std::initializer_list<ValueTp>)
103 std::initializer_list
<ValueTp
> il
= { ValueTp(1, 1), ValueTp(2, 1) };
104 cc
->expect
<ValueTp
const&>(2);
106 assert(!cc
->unchecked());
108 DisableAllocationGuard g
;
113 // Testing C::insert(Iter, Iter) for *Iter = value_type const&
115 const ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1), ValueTp(3, 1) };
116 cc
->expect
<ValueTp
const&>(3);
117 c
.insert(std::begin(ValueList
), std::end(ValueList
));
118 assert(!cc
->unchecked());
120 DisableAllocationGuard g
;
121 c
.insert(std::begin(ValueList
), std::end(ValueList
));
125 // Testing C::insert(Iter, Iter) for *Iter = value_type&&
127 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
128 cc
->expect
<ValueTp
&&>(3);
129 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList
)),
130 std::move_iterator
<ValueTp
*>(std::end(ValueList
)));
131 assert(!cc
->unchecked());
133 DisableAllocationGuard g
;
134 ValueTp ValueList2
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
135 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList2
)),
136 std::move_iterator
<ValueTp
*>(std::end(ValueList2
)));
140 // Testing C::insert(Iter, Iter) for *Iter = value_type&
142 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
143 cc
->expect
<ValueTp
const&>(3);
144 c
.insert(std::begin(ValueList
), std::end(ValueList
));
145 assert(!cc
->unchecked());
147 DisableAllocationGuard g
;
148 c
.insert(std::begin(ValueList
), std::end(ValueList
));
154 template <class Container
>
155 void testMapInsertHint()
157 typedef typename
Container::value_type ValueTp
;
158 typedef typename
Container::key_type Key
;
159 typedef typename
Container::mapped_type Mapped
;
160 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
162 typedef typename
C::iterator It
;
163 ConstructController
* cc
= getConstructController();
166 // Testing C::insert(p, const value_type&)
168 const ValueTp
v(42, 1);
169 cc
->expect
<const ValueTp
&>();
170 It ret
= c
.insert(c
.end(), v
);
171 assert(ret
!= c
.end());
172 assert(c
.size() == 1);
173 assert(!cc
->unchecked());
175 DisableAllocationGuard g
;
176 const ValueTp
v2(42, 1);
177 It ret2
= c
.insert(c
.begin(), v2
);
178 assert(&(*ret2
) == &(*ret
));
179 assert(c
.size() == 1);
183 // Testing C::insert(p, value_type&)
186 cc
->expect
<ValueTp
const&>();
187 It ret
= c
.insert(c
.end(), v
);
188 assert(ret
!= c
.end());
189 assert(c
.size() == 1);
190 assert(!cc
->unchecked());
192 DisableAllocationGuard g
;
194 It ret2
= c
.insert(c
.begin(), v2
);
195 assert(&(*ret2
) == &(*ret
));
196 assert(c
.size() == 1);
200 // Testing C::insert(p, value_type&&)
203 cc
->expect
<ValueTp
&&>();
204 It ret
= c
.insert(c
.end(), std::move(v
));
205 assert(ret
!= c
.end());
206 assert(c
.size() == 1);
207 assert(!cc
->unchecked());
209 DisableAllocationGuard g
;
211 It ret2
= c
.insert(c
.begin(), std::move(v2
));
212 assert(&(*ret2
) == &(*ret
));
213 assert(c
.size() == 1);
217 // Testing C::insert(p, {key, value})
219 cc
->expect
<ValueTp
&&>();
220 It ret
= c
.insert(c
.end(), {42, 1});
221 assert(ret
!= c
.end());
222 assert(c
.size() == 1);
223 assert(!cc
->unchecked());
225 DisableAllocationGuard g
;
226 It ret2
= c
.insert(c
.begin(), {42, 1});
227 assert(&(*ret2
) == &(*ret
));
228 assert(c
.size() == 1);
232 // Testing C::insert(p, const value_type&&)
234 const ValueTp
v(42, 1);
235 cc
->expect
<const ValueTp
&>();
236 It ret
= c
.insert(c
.end(), std::move(v
));
237 assert(ret
!= c
.end());
238 assert(c
.size() == 1);
239 assert(!cc
->unchecked());
241 DisableAllocationGuard g
;
242 const ValueTp
v2(42, 1);
243 It ret2
= c
.insert(c
.begin(), std::move(v2
));
244 assert(&(*ret2
) == &(*ret
));
245 assert(c
.size() == 1);
249 // Testing C::insert(p, pair<Key, Mapped> const&)
251 const NonConstKeyPair
v(42, 1);
252 cc
->expect
<const NonConstKeyPair
&>();
253 It ret
= c
.insert(c
.end(), v
);
254 assert(ret
!= c
.end());
255 assert(c
.size() == 1);
256 assert(!cc
->unchecked());
258 DisableAllocationGuard g
;
259 const NonConstKeyPair
v2(42, 1);
260 It ret2
= c
.insert(c
.begin(), v2
);
261 assert(&(*ret2
) == &(*ret
));
262 assert(c
.size() == 1);
266 // Testing C::insert(p, pair<Key, Mapped>&&)
268 NonConstKeyPair
v(42, 1);
269 cc
->expect
<NonConstKeyPair
&&>();
270 It ret
= c
.insert(c
.end(), std::move(v
));
271 assert(ret
!= c
.end());
272 assert(c
.size() == 1);
273 assert(!cc
->unchecked());
275 DisableAllocationGuard g
;
276 NonConstKeyPair
v2(42, 1);
277 It ret2
= c
.insert(c
.begin(), std::move(v2
));
278 assert(&(*ret2
) == &(*ret
));
279 assert(c
.size() == 1);
287 template <class Container
>
288 void testMapEmplace()
290 typedef typename
Container::value_type ValueTp
;
291 typedef typename
Container::key_type Key
;
292 typedef typename
Container::mapped_type Mapped
;
293 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
294 ConstructController
* cc
= getConstructController();
297 // Testing C::emplace(const value_type&)
299 const ValueTp
v(42, 1);
300 cc
->expect
<const ValueTp
&>();
301 assert(c
.emplace(v
).second
);
302 assert(!cc
->unchecked());
304 DisableAllocationGuard g
;
305 const ValueTp
v2(42, 1);
306 assert(c
.emplace(v2
).second
== false);
310 // Testing C::emplace(value_type&)
313 cc
->expect
<ValueTp
&>();
314 assert(c
.emplace(v
).second
);
315 assert(!cc
->unchecked());
317 DisableAllocationGuard g
;
319 assert(c
.emplace(v2
).second
== false);
323 // Testing C::emplace(value_type&&)
326 cc
->expect
<ValueTp
&&>();
327 assert(c
.emplace(std::move(v
)).second
);
328 assert(!cc
->unchecked());
330 DisableAllocationGuard g
;
332 assert(c
.emplace(std::move(v2
)).second
== false);
336 // Testing C::emplace(const value_type&&)
338 const ValueTp
v(42, 1);
339 cc
->expect
<const ValueTp
&&>();
340 assert(c
.emplace(std::move(v
)).second
);
341 assert(!cc
->unchecked());
343 DisableAllocationGuard g
;
344 const ValueTp
v2(42, 1);
345 assert(c
.emplace(std::move(v2
)).second
== false);
349 // Testing C::emplace(pair<Key, Mapped> const&)
351 const NonConstKeyPair
v(42, 1);
352 cc
->expect
<const NonConstKeyPair
&>();
353 assert(c
.emplace(v
).second
);
354 assert(!cc
->unchecked());
356 DisableAllocationGuard g
;
357 const NonConstKeyPair
v2(42, 1);
358 assert(c
.emplace(v2
).second
== false);
362 // Testing C::emplace(pair<Key, Mapped> &&)
364 NonConstKeyPair
v(42, 1);
365 cc
->expect
<NonConstKeyPair
&&>();
366 assert(c
.emplace(std::move(v
)).second
);
367 assert(!cc
->unchecked());
369 DisableAllocationGuard g
;
370 NonConstKeyPair
v2(42, 1);
371 assert(c
.emplace(std::move(v2
)).second
== false);
375 // Testing C::emplace(const Key&, ConvertibleToMapped&&)
378 cc
->expect
<Key
const&, int&&>();
379 assert(c
.emplace(k
, 1).second
);
380 assert(!cc
->unchecked());
382 DisableAllocationGuard g
;
384 assert(c
.emplace(k2
, 2).second
== false);
388 // Testing C::emplace(Key&, Mapped&)
392 cc
->expect
<Key
&, Mapped
&>();
393 assert(c
.emplace(k
, m
).second
);
394 assert(!cc
->unchecked());
396 DisableAllocationGuard g
;
398 assert(c
.emplace(k2
, m
).second
== false);
402 // Testing C::emplace(Key&&, Mapped&&)
406 cc
->expect
<Key
&&, Mapped
&&>();
407 assert(c
.emplace(std::move(k
), std::move(m
)).second
);
408 assert(!cc
->unchecked());
410 DisableAllocationGuard g
;
413 assert(c
.emplace(std::move(k2
), std::move(m2
)).second
== false);
417 // Testing C::emplace(ConvertibleToKey&&, ConvertibleToMapped&&)
419 cc
->expect
<int&&, int&&>();
420 assert(c
.emplace(42, 1).second
);
421 assert(!cc
->unchecked());
423 // test that emplacing a duplicate item allocates. We cannot optimize
424 // this case because int&& does not match the type of key exactly.
425 cc
->expect
<int&&, int&&>();
426 assert(c
.emplace(42, 1).second
== false);
427 assert(!cc
->unchecked());
433 template <class Container
>
434 void testMapEmplaceHint()
436 typedef typename
Container::value_type ValueTp
;
437 typedef typename
Container::key_type Key
;
438 typedef typename
Container::mapped_type Mapped
;
439 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
441 typedef typename
C::iterator It
;
442 ConstructController
* cc
= getConstructController();
445 // Testing C::emplace_hint(p, const value_type&)
447 const ValueTp
v(42, 1);
448 cc
->expect
<const ValueTp
&>();
449 It ret
= c
.emplace_hint(c
.end(), v
);
450 assert(ret
!= c
.end());
451 assert(c
.size() == 1);
452 assert(!cc
->unchecked());
454 DisableAllocationGuard g
;
455 const ValueTp
v2(42, 1);
456 It ret2
= c
.emplace_hint(c
.begin(), v2
);
457 assert(&(*ret2
) == &(*ret
));
458 assert(c
.size() == 1);
462 // Testing C::emplace_hint(p, value_type&)
465 cc
->expect
<ValueTp
&>();
466 It ret
= c
.emplace_hint(c
.end(), v
);
467 assert(ret
!= c
.end());
468 assert(c
.size() == 1);
469 assert(!cc
->unchecked());
471 DisableAllocationGuard g
;
473 It ret2
= c
.emplace_hint(c
.begin(), v2
);
474 assert(&(*ret2
) == &(*ret
));
475 assert(c
.size() == 1);
479 // Testing C::emplace_hint(p, value_type&&)
482 cc
->expect
<ValueTp
&&>();
483 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
484 assert(ret
!= c
.end());
485 assert(c
.size() == 1);
486 assert(!cc
->unchecked());
488 DisableAllocationGuard g
;
490 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
491 assert(&(*ret2
) == &(*ret
));
492 assert(c
.size() == 1);
496 // Testing C::emplace_hint(p, const value_type&&)
498 const ValueTp
v(42, 1);
499 cc
->expect
<const ValueTp
&&>();
500 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
501 assert(ret
!= c
.end());
502 assert(c
.size() == 1);
503 assert(!cc
->unchecked());
505 DisableAllocationGuard g
;
506 const ValueTp
v2(42, 1);
507 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
508 assert(&(*ret2
) == &(*ret
));
509 assert(c
.size() == 1);
513 // Testing C::emplace_hint(p, pair<Key, Mapped> const&)
515 const NonConstKeyPair
v(42, 1);
516 cc
->expect
<const NonConstKeyPair
&>();
517 It ret
= c
.emplace_hint(c
.end(), v
);
518 assert(ret
!= c
.end());
519 assert(c
.size() == 1);
520 assert(!cc
->unchecked());
522 DisableAllocationGuard g
;
523 const NonConstKeyPair
v2(42, 1);
524 It ret2
= c
.emplace_hint(c
.begin(), v2
);
525 assert(&(*ret2
) == &(*ret
));
526 assert(c
.size() == 1);
530 // Testing C::emplace_hint(p, pair<Key, Mapped>&&)
532 NonConstKeyPair
v(42, 1);
533 cc
->expect
<NonConstKeyPair
&&>();
534 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
535 assert(ret
!= c
.end());
536 assert(c
.size() == 1);
537 assert(!cc
->unchecked());
539 DisableAllocationGuard g
;
540 NonConstKeyPair
v2(42, 1);
541 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
542 assert(&(*ret2
) == &(*ret
));
543 assert(c
.size() == 1);
547 // Testing C::emplace_hint(p, const Key&, ConvertibleToMapped&&)
550 cc
->expect
<Key
const&, int&&>();
551 It ret
= c
.emplace_hint(c
.end(), k
, 42);
552 assert(ret
!= c
.end());
553 assert(c
.size() == 1);
554 assert(!cc
->unchecked());
556 DisableAllocationGuard g
;
558 It ret2
= c
.emplace_hint(c
.begin(), k2
, 1);
559 assert(&(*ret2
) == &(*ret
));
560 assert(c
.size() == 1);
564 // Testing C::emplace_hint(p, Key&, Mapped&)
568 cc
->expect
<Key
&, Mapped
&>();
569 It ret
= c
.emplace_hint(c
.end(), k
, m
);
570 assert(ret
!= c
.end());
571 assert(c
.size() == 1);
572 assert(!cc
->unchecked());
574 DisableAllocationGuard g
;
577 It ret2
= c
.emplace_hint(c
.begin(), k2
, m2
);
578 assert(&(*ret2
) == &(*ret
));
579 assert(c
.size() == 1);
583 // Testing C::emplace_hint(p, Key&&, Mapped&&)
587 cc
->expect
<Key
&&, Mapped
&&>();
588 It ret
= c
.emplace_hint(c
.end(), std::move(k
), std::move(m
));
589 assert(ret
!= c
.end());
590 assert(c
.size() == 1);
591 assert(!cc
->unchecked());
593 DisableAllocationGuard g
;
596 It ret2
= c
.emplace_hint(c
.begin(), std::move(k2
), std::move(m2
));
597 assert(&(*ret2
) == &(*ret
));
598 assert(c
.size() == 1);
602 // Testing C::emplace_hint(p, ConvertibleToKey&&, ConvertibleToMapped&&)
604 cc
->expect
<int&&, int&&>();
605 It ret
= c
.emplace_hint(c
.end(), 42, 1);
606 assert(ret
!= c
.end());
607 assert(c
.size() == 1);
608 assert(!cc
->unchecked());
610 cc
->expect
<int&&, int&&>();
611 It ret2
= c
.emplace_hint(c
.begin(), 42, 2);
612 assert(&(*ret2
) == &(*ret
));
613 assert(c
.size() == 1);
614 assert(!cc
->unchecked());
621 template <class Container
>
622 void testMultimapInsert()
624 typedef typename
Container::value_type ValueTp
;
625 ConstructController
* cc
= getConstructController();
628 // Testing C::insert(const value_type&)
630 const ValueTp
v(42, 1);
631 cc
->expect
<const ValueTp
&>();
633 assert(!cc
->unchecked());
636 // Testing C::insert(value_type&)
639 cc
->expect
<ValueTp
&>();
641 assert(!cc
->unchecked());
644 // Testing C::insert(value_type&&)
647 cc
->expect
<ValueTp
&&>();
648 c
.insert(std::move(v
));
649 assert(!cc
->unchecked());
652 // Testing C::insert({key, value})
654 cc
->expect
<ValueTp
&&>();
656 assert(!cc
->unchecked());
659 // Testing C::insert(std::initializer_list<ValueTp>)
661 std::initializer_list
<ValueTp
> il
= { ValueTp(1, 1), ValueTp(2, 1) };
662 cc
->expect
<ValueTp
const&>(2);
664 assert(!cc
->unchecked());
667 // Testing C::insert(Iter, Iter) for *Iter = value_type const&
669 const ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1), ValueTp(3, 1) };
670 cc
->expect
<ValueTp
const&>(3);
671 c
.insert(std::begin(ValueList
), std::end(ValueList
));
672 assert(!cc
->unchecked());
675 // Testing C::insert(Iter, Iter) for *Iter = value_type&&
677 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
678 cc
->expect
<ValueTp
&&>(3);
679 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList
)),
680 std::move_iterator
<ValueTp
*>(std::end(ValueList
)));
681 assert(!cc
->unchecked());
684 // Testing C::insert(Iter, Iter) for *Iter = value_type&
686 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
687 cc
->expect
<ValueTp
&>(3);
688 c
.insert(std::begin(ValueList
), std::end(ValueList
));
689 assert(!cc
->unchecked());
694 template <class Container
>
695 void testMultimapInsertHint()
697 typedef typename
Container::value_type ValueTp
;
698 ConstructController
* cc
= getConstructController();
701 // Testing C::insert(p, const value_type&)
703 const ValueTp
v(42, 1);
704 cc
->expect
<const ValueTp
&>();
705 c
.insert(c
.begin(), v
);
706 assert(!cc
->unchecked());
709 // Testing C::insert(p, value_type&)
712 cc
->expect
<ValueTp
&>();
713 c
.insert(c
.begin(), v
);
714 assert(!cc
->unchecked());
717 // Testing C::insert(p, value_type&&)
720 cc
->expect
<ValueTp
&&>();
721 c
.insert(c
.begin(), std::move(v
));
722 assert(!cc
->unchecked());
725 // Testing C::insert(p, {key, value})
727 cc
->expect
<ValueTp
&&>();
728 c
.insert(c
.begin(), {42, 1});
729 assert(!cc
->unchecked());