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 //===----------------------------------------------------------------------===//
9 #ifndef MAP_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
10 #define MAP_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
16 // class unordered_map
27 #include "test_macros.h"
28 #include "count_new.h"
29 #include "container_test_types.h"
32 template <class Container
>
35 typedef typename
Container::value_type ValueTp
;
36 ConstructController
* cc
= getConstructController();
39 // Testing C::insert(const value_type&)
41 const ValueTp
v(42, 1);
42 cc
->expect
<const ValueTp
&>();
43 assert(c
.insert(v
).second
);
44 assert(!cc
->unchecked());
46 DisableAllocationGuard g
;
47 const ValueTp
v2(42, 1);
48 assert(c
.insert(v2
).second
== false);
52 // Testing C::insert(value_type&)
55 cc
->expect
<const ValueTp
&>();
56 assert(c
.insert(v
).second
);
57 assert(!cc
->unchecked());
59 DisableAllocationGuard g
;
61 assert(c
.insert(v2
).second
== false);
65 // Testing C::insert(value_type&&)
68 cc
->expect
<ValueTp
&&>();
69 assert(c
.insert(std::move(v
)).second
);
70 assert(!cc
->unchecked());
72 DisableAllocationGuard g
;
74 assert(c
.insert(std::move(v2
)).second
== false);
78 // Testing C::insert(const value_type&&)
80 const ValueTp
v(42, 1);
81 cc
->expect
<const ValueTp
&>();
82 assert(c
.insert(std::move(v
)).second
);
83 assert(!cc
->unchecked());
85 DisableAllocationGuard g
;
86 const ValueTp
v2(42, 1);
87 assert(c
.insert(std::move(v2
)).second
== false);
91 // Testing C::insert({key, value})
93 cc
->expect
<ValueTp
&&>();
94 assert(c
.insert({42, 1}).second
);
95 assert(!cc
->unchecked());
97 DisableAllocationGuard g
;
98 const ValueTp
v2(42, 1);
99 assert(c
.insert(std::move(v2
)).second
== false);
103 // Testing C::insert(std::initializer_list<ValueTp>)
105 std::initializer_list
<ValueTp
> il
= { ValueTp(1, 1), ValueTp(2, 1) };
106 cc
->expect
<ValueTp
const&>(2);
108 assert(!cc
->unchecked());
110 DisableAllocationGuard g
;
115 // Testing C::insert(Iter, Iter) for *Iter = value_type const&
117 const ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1), ValueTp(3, 1) };
118 cc
->expect
<ValueTp
const&>(3);
119 c
.insert(std::begin(ValueList
), std::end(ValueList
));
120 assert(!cc
->unchecked());
122 DisableAllocationGuard g
;
123 c
.insert(std::begin(ValueList
), std::end(ValueList
));
127 // Testing C::insert(Iter, Iter) for *Iter = value_type&&
129 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
130 cc
->expect
<ValueTp
&&>(3);
131 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList
)),
132 std::move_iterator
<ValueTp
*>(std::end(ValueList
)));
133 assert(!cc
->unchecked());
135 DisableAllocationGuard g
;
136 ValueTp ValueList2
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
137 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList2
)),
138 std::move_iterator
<ValueTp
*>(std::end(ValueList2
)));
142 // Testing C::insert(Iter, Iter) for *Iter = value_type&
144 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
145 cc
->expect
<ValueTp
const&>(3);
146 c
.insert(std::begin(ValueList
), std::end(ValueList
));
147 assert(!cc
->unchecked());
149 DisableAllocationGuard g
;
150 c
.insert(std::begin(ValueList
), std::end(ValueList
));
156 template <class Container
>
157 void testMapInsertHint()
159 typedef typename
Container::value_type ValueTp
;
160 typedef typename
Container::key_type Key
;
161 typedef typename
Container::mapped_type Mapped
;
162 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
164 typedef typename
C::iterator It
;
165 ConstructController
* cc
= getConstructController();
168 // Testing C::insert(p, const value_type&)
170 const ValueTp
v(42, 1);
171 cc
->expect
<const ValueTp
&>();
172 It ret
= c
.insert(c
.end(), v
);
173 assert(ret
!= c
.end());
174 assert(c
.size() == 1);
175 assert(!cc
->unchecked());
177 DisableAllocationGuard g
;
178 const ValueTp
v2(42, 1);
179 It ret2
= c
.insert(c
.begin(), v2
);
180 assert(&(*ret2
) == &(*ret
));
181 assert(c
.size() == 1);
185 // Testing C::insert(p, value_type&)
188 cc
->expect
<ValueTp
const&>();
189 It ret
= c
.insert(c
.end(), v
);
190 assert(ret
!= c
.end());
191 assert(c
.size() == 1);
192 assert(!cc
->unchecked());
194 DisableAllocationGuard g
;
196 It ret2
= c
.insert(c
.begin(), v2
);
197 assert(&(*ret2
) == &(*ret
));
198 assert(c
.size() == 1);
202 // Testing C::insert(p, value_type&&)
205 cc
->expect
<ValueTp
&&>();
206 It ret
= c
.insert(c
.end(), std::move(v
));
207 assert(ret
!= c
.end());
208 assert(c
.size() == 1);
209 assert(!cc
->unchecked());
211 DisableAllocationGuard g
;
213 It ret2
= c
.insert(c
.begin(), std::move(v2
));
214 assert(&(*ret2
) == &(*ret
));
215 assert(c
.size() == 1);
219 // Testing C::insert(p, {key, value})
221 cc
->expect
<ValueTp
&&>();
222 It ret
= c
.insert(c
.end(), {42, 1});
223 assert(ret
!= c
.end());
224 assert(c
.size() == 1);
225 assert(!cc
->unchecked());
227 DisableAllocationGuard g
;
228 It ret2
= c
.insert(c
.begin(), {42, 1});
229 assert(&(*ret2
) == &(*ret
));
230 assert(c
.size() == 1);
234 // Testing C::insert(p, const value_type&&)
236 const ValueTp
v(42, 1);
237 cc
->expect
<const ValueTp
&>();
238 It ret
= c
.insert(c
.end(), std::move(v
));
239 assert(ret
!= c
.end());
240 assert(c
.size() == 1);
241 assert(!cc
->unchecked());
243 DisableAllocationGuard g
;
244 const ValueTp
v2(42, 1);
245 It ret2
= c
.insert(c
.begin(), std::move(v2
));
246 assert(&(*ret2
) == &(*ret
));
247 assert(c
.size() == 1);
251 // Testing C::insert(p, pair<Key, Mapped> const&)
253 const NonConstKeyPair
v(42, 1);
254 cc
->expect
<const NonConstKeyPair
&>();
255 It ret
= c
.insert(c
.end(), v
);
256 assert(ret
!= c
.end());
257 assert(c
.size() == 1);
258 assert(!cc
->unchecked());
260 DisableAllocationGuard g
;
261 const NonConstKeyPair
v2(42, 1);
262 It ret2
= c
.insert(c
.begin(), v2
);
263 assert(&(*ret2
) == &(*ret
));
264 assert(c
.size() == 1);
268 // Testing C::insert(p, pair<Key, Mapped>&&)
270 NonConstKeyPair
v(42, 1);
271 cc
->expect
<NonConstKeyPair
&&>();
272 It ret
= c
.insert(c
.end(), std::move(v
));
273 assert(ret
!= c
.end());
274 assert(c
.size() == 1);
275 assert(!cc
->unchecked());
277 DisableAllocationGuard g
;
278 NonConstKeyPair
v2(42, 1);
279 It ret2
= c
.insert(c
.begin(), std::move(v2
));
280 assert(&(*ret2
) == &(*ret
));
281 assert(c
.size() == 1);
289 template <class Container
>
290 void testMapEmplace()
292 typedef typename
Container::value_type ValueTp
;
293 typedef typename
Container::key_type Key
;
294 typedef typename
Container::mapped_type Mapped
;
295 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
296 ConstructController
* cc
= getConstructController();
299 // Testing C::emplace(const value_type&)
301 const ValueTp
v(42, 1);
302 cc
->expect
<const ValueTp
&>();
303 assert(c
.emplace(v
).second
);
304 assert(!cc
->unchecked());
306 DisableAllocationGuard g
;
307 const ValueTp
v2(42, 1);
308 assert(c
.emplace(v2
).second
== false);
312 // Testing C::emplace(value_type&)
315 cc
->expect
<ValueTp
&>();
316 assert(c
.emplace(v
).second
);
317 assert(!cc
->unchecked());
319 DisableAllocationGuard g
;
321 assert(c
.emplace(v2
).second
== false);
325 // Testing C::emplace(value_type&&)
328 cc
->expect
<ValueTp
&&>();
329 assert(c
.emplace(std::move(v
)).second
);
330 assert(!cc
->unchecked());
332 DisableAllocationGuard g
;
334 assert(c
.emplace(std::move(v2
)).second
== false);
338 // Testing C::emplace(const value_type&&)
340 const ValueTp
v(42, 1);
341 cc
->expect
<const ValueTp
&&>();
342 assert(c
.emplace(std::move(v
)).second
);
343 assert(!cc
->unchecked());
345 DisableAllocationGuard g
;
346 const ValueTp
v2(42, 1);
347 assert(c
.emplace(std::move(v2
)).second
== false);
351 // Testing C::emplace(pair<Key, Mapped> const&)
353 const NonConstKeyPair
v(42, 1);
354 cc
->expect
<const NonConstKeyPair
&>();
355 assert(c
.emplace(v
).second
);
356 assert(!cc
->unchecked());
358 DisableAllocationGuard g
;
359 const NonConstKeyPair
v2(42, 1);
360 assert(c
.emplace(v2
).second
== false);
364 // Testing C::emplace(pair<Key, Mapped> &&)
366 NonConstKeyPair
v(42, 1);
367 cc
->expect
<NonConstKeyPair
&&>();
368 assert(c
.emplace(std::move(v
)).second
);
369 assert(!cc
->unchecked());
371 DisableAllocationGuard g
;
372 NonConstKeyPair
v2(42, 1);
373 assert(c
.emplace(std::move(v2
)).second
== false);
377 // Testing C::emplace(const Key&, ConvertibleToMapped&&)
380 cc
->expect
<Key
const&, int&&>();
381 assert(c
.emplace(k
, 1).second
);
382 assert(!cc
->unchecked());
384 DisableAllocationGuard g
;
386 assert(c
.emplace(k2
, 2).second
== false);
390 // Testing C::emplace(Key&, Mapped&)
394 cc
->expect
<Key
&, Mapped
&>();
395 assert(c
.emplace(k
, m
).second
);
396 assert(!cc
->unchecked());
398 DisableAllocationGuard g
;
400 assert(c
.emplace(k2
, m
).second
== false);
404 // Testing C::emplace(Key&&, Mapped&&)
408 cc
->expect
<Key
&&, Mapped
&&>();
409 assert(c
.emplace(std::move(k
), std::move(m
)).second
);
410 assert(!cc
->unchecked());
412 DisableAllocationGuard g
;
415 assert(c
.emplace(std::move(k2
), std::move(m2
)).second
== false);
419 // Testing C::emplace(ConvertibleToKey&&, ConvertibleToMapped&&)
421 cc
->expect
<int&&, int&&>();
422 assert(c
.emplace(42, 1).second
);
423 assert(!cc
->unchecked());
425 // test that emplacing a duplicate item allocates. We cannot optimize
426 // this case because int&& does not match the type of key exactly.
427 cc
->expect
<int&&, int&&>();
428 assert(c
.emplace(42, 1).second
== false);
429 assert(!cc
->unchecked());
435 template <class Container
>
436 void testMapEmplaceHint()
438 typedef typename
Container::value_type ValueTp
;
439 typedef typename
Container::key_type Key
;
440 typedef typename
Container::mapped_type Mapped
;
441 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
443 typedef typename
C::iterator It
;
444 ConstructController
* cc
= getConstructController();
447 // Testing C::emplace_hint(p, const value_type&)
449 const ValueTp
v(42, 1);
450 cc
->expect
<const ValueTp
&>();
451 It ret
= c
.emplace_hint(c
.end(), v
);
452 assert(ret
!= c
.end());
453 assert(c
.size() == 1);
454 assert(!cc
->unchecked());
456 DisableAllocationGuard g
;
457 const ValueTp
v2(42, 1);
458 It ret2
= c
.emplace_hint(c
.begin(), v2
);
459 assert(&(*ret2
) == &(*ret
));
460 assert(c
.size() == 1);
464 // Testing C::emplace_hint(p, value_type&)
467 cc
->expect
<ValueTp
&>();
468 It ret
= c
.emplace_hint(c
.end(), v
);
469 assert(ret
!= c
.end());
470 assert(c
.size() == 1);
471 assert(!cc
->unchecked());
473 DisableAllocationGuard g
;
475 It ret2
= c
.emplace_hint(c
.begin(), v2
);
476 assert(&(*ret2
) == &(*ret
));
477 assert(c
.size() == 1);
481 // Testing C::emplace_hint(p, value_type&&)
484 cc
->expect
<ValueTp
&&>();
485 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
486 assert(ret
!= c
.end());
487 assert(c
.size() == 1);
488 assert(!cc
->unchecked());
490 DisableAllocationGuard g
;
492 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
493 assert(&(*ret2
) == &(*ret
));
494 assert(c
.size() == 1);
498 // Testing C::emplace_hint(p, const value_type&&)
500 const ValueTp
v(42, 1);
501 cc
->expect
<const ValueTp
&&>();
502 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
503 assert(ret
!= c
.end());
504 assert(c
.size() == 1);
505 assert(!cc
->unchecked());
507 DisableAllocationGuard g
;
508 const ValueTp
v2(42, 1);
509 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
510 assert(&(*ret2
) == &(*ret
));
511 assert(c
.size() == 1);
515 // Testing C::emplace_hint(p, pair<Key, Mapped> const&)
517 const NonConstKeyPair
v(42, 1);
518 cc
->expect
<const NonConstKeyPair
&>();
519 It ret
= c
.emplace_hint(c
.end(), v
);
520 assert(ret
!= c
.end());
521 assert(c
.size() == 1);
522 assert(!cc
->unchecked());
524 DisableAllocationGuard g
;
525 const NonConstKeyPair
v2(42, 1);
526 It ret2
= c
.emplace_hint(c
.begin(), v2
);
527 assert(&(*ret2
) == &(*ret
));
528 assert(c
.size() == 1);
532 // Testing C::emplace_hint(p, pair<Key, Mapped>&&)
534 NonConstKeyPair
v(42, 1);
535 cc
->expect
<NonConstKeyPair
&&>();
536 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
537 assert(ret
!= c
.end());
538 assert(c
.size() == 1);
539 assert(!cc
->unchecked());
541 DisableAllocationGuard g
;
542 NonConstKeyPair
v2(42, 1);
543 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
544 assert(&(*ret2
) == &(*ret
));
545 assert(c
.size() == 1);
549 // Testing C::emplace_hint(p, const Key&, ConvertibleToMapped&&)
552 cc
->expect
<Key
const&, int&&>();
553 It ret
= c
.emplace_hint(c
.end(), k
, 42);
554 assert(ret
!= c
.end());
555 assert(c
.size() == 1);
556 assert(!cc
->unchecked());
558 DisableAllocationGuard g
;
560 It ret2
= c
.emplace_hint(c
.begin(), k2
, 1);
561 assert(&(*ret2
) == &(*ret
));
562 assert(c
.size() == 1);
566 // Testing C::emplace_hint(p, Key&, Mapped&)
570 cc
->expect
<Key
&, Mapped
&>();
571 It ret
= c
.emplace_hint(c
.end(), k
, m
);
572 assert(ret
!= c
.end());
573 assert(c
.size() == 1);
574 assert(!cc
->unchecked());
576 DisableAllocationGuard g
;
579 It ret2
= c
.emplace_hint(c
.begin(), k2
, m2
);
580 assert(&(*ret2
) == &(*ret
));
581 assert(c
.size() == 1);
585 // Testing C::emplace_hint(p, Key&&, Mapped&&)
589 cc
->expect
<Key
&&, Mapped
&&>();
590 It ret
= c
.emplace_hint(c
.end(), std::move(k
), std::move(m
));
591 assert(ret
!= c
.end());
592 assert(c
.size() == 1);
593 assert(!cc
->unchecked());
595 DisableAllocationGuard g
;
598 It ret2
= c
.emplace_hint(c
.begin(), std::move(k2
), std::move(m2
));
599 assert(&(*ret2
) == &(*ret
));
600 assert(c
.size() == 1);
604 // Testing C::emplace_hint(p, ConvertibleToKey&&, ConvertibleToMapped&&)
606 cc
->expect
<int&&, int&&>();
607 It ret
= c
.emplace_hint(c
.end(), 42, 1);
608 assert(ret
!= c
.end());
609 assert(c
.size() == 1);
610 assert(!cc
->unchecked());
612 cc
->expect
<int&&, int&&>();
613 It ret2
= c
.emplace_hint(c
.begin(), 42, 2);
614 assert(&(*ret2
) == &(*ret
));
615 assert(c
.size() == 1);
616 assert(!cc
->unchecked());
623 template <class Container
>
624 void testMultimapInsert()
626 typedef typename
Container::value_type ValueTp
;
627 ConstructController
* cc
= getConstructController();
630 // Testing C::insert(const value_type&)
632 const ValueTp
v(42, 1);
633 cc
->expect
<const ValueTp
&>();
635 assert(!cc
->unchecked());
638 // Testing C::insert(value_type&)
641 cc
->expect
<ValueTp
&>();
643 assert(!cc
->unchecked());
646 // Testing C::insert(value_type&&)
649 cc
->expect
<ValueTp
&&>();
650 c
.insert(std::move(v
));
651 assert(!cc
->unchecked());
654 // Testing C::insert({key, value})
656 cc
->expect
<ValueTp
&&>();
658 assert(!cc
->unchecked());
661 // Testing C::insert(std::initializer_list<ValueTp>)
663 std::initializer_list
<ValueTp
> il
= { ValueTp(1, 1), ValueTp(2, 1) };
664 cc
->expect
<ValueTp
const&>(2);
666 assert(!cc
->unchecked());
669 // Testing C::insert(Iter, Iter) for *Iter = value_type const&
671 const ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1), ValueTp(3, 1) };
672 cc
->expect
<ValueTp
const&>(3);
673 c
.insert(std::begin(ValueList
), std::end(ValueList
));
674 assert(!cc
->unchecked());
677 // Testing C::insert(Iter, Iter) for *Iter = value_type&&
679 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
680 cc
->expect
<ValueTp
&&>(3);
681 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList
)),
682 std::move_iterator
<ValueTp
*>(std::end(ValueList
)));
683 assert(!cc
->unchecked());
686 // Testing C::insert(Iter, Iter) for *Iter = value_type&
688 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
689 cc
->expect
<ValueTp
&>(3);
690 c
.insert(std::begin(ValueList
), std::end(ValueList
));
691 assert(!cc
->unchecked());
696 template <class Container
>
697 void testMultimapInsertHint()
699 typedef typename
Container::value_type ValueTp
;
700 ConstructController
* cc
= getConstructController();
703 // Testing C::insert(p, const value_type&)
705 const ValueTp
v(42, 1);
706 cc
->expect
<const ValueTp
&>();
707 c
.insert(c
.begin(), v
);
708 assert(!cc
->unchecked());
711 // Testing C::insert(p, value_type&)
714 cc
->expect
<ValueTp
&>();
715 c
.insert(c
.begin(), v
);
716 assert(!cc
->unchecked());
719 // Testing C::insert(p, value_type&&)
722 cc
->expect
<ValueTp
&&>();
723 c
.insert(c
.begin(), std::move(v
));
724 assert(!cc
->unchecked());
727 // Testing C::insert(p, {key, value})
729 cc
->expect
<ValueTp
&&>();
730 c
.insert(c
.begin(), {42, 1});
731 assert(!cc
->unchecked());