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
21 // UNSUPPORTED: c++98, c++03
25 #include "test_macros.h"
26 #include "count_new.h"
27 #include "container_test_types.h"
28 #include "assert_checkpoint.h"
31 template <class Container
>
34 typedef typename
Container::value_type ValueTp
;
35 ConstructController
* cc
= getConstructController();
38 CHECKPOINT("Testing C::insert(const value_type&)");
40 const ValueTp
v(42, 1);
41 cc
->expect
<const ValueTp
&>();
42 assert(c
.insert(v
).second
);
43 assert(!cc
->unchecked());
45 DisableAllocationGuard g
;
46 const ValueTp
v2(42, 1);
47 assert(c
.insert(v2
).second
== false);
51 CHECKPOINT("Testing C::insert(value_type&)");
54 cc
->expect
<const ValueTp
&>();
55 assert(c
.insert(v
).second
);
56 assert(!cc
->unchecked());
58 DisableAllocationGuard g
;
60 assert(c
.insert(v2
).second
== false);
64 CHECKPOINT("Testing C::insert(value_type&&)");
67 cc
->expect
<ValueTp
&&>();
68 assert(c
.insert(std::move(v
)).second
);
69 assert(!cc
->unchecked());
71 DisableAllocationGuard g
;
73 assert(c
.insert(std::move(v2
)).second
== false);
77 CHECKPOINT("Testing C::insert(const value_type&&)");
79 const ValueTp
v(42, 1);
80 cc
->expect
<const ValueTp
&>();
81 assert(c
.insert(std::move(v
)).second
);
82 assert(!cc
->unchecked());
84 DisableAllocationGuard g
;
85 const ValueTp
v2(42, 1);
86 assert(c
.insert(std::move(v2
)).second
== false);
90 CHECKPOINT("Testing C::insert({key, value})");
92 cc
->expect
<ValueTp
&&>();
93 assert(c
.insert({42, 1}).second
);
94 assert(!cc
->unchecked());
96 DisableAllocationGuard g
;
97 const ValueTp
v2(42, 1);
98 assert(c
.insert(std::move(v2
)).second
== false);
102 CHECKPOINT("Testing C::insert(std::initializer_list<ValueTp>)");
104 std::initializer_list
<ValueTp
> il
= { ValueTp(1, 1), ValueTp(2, 1) };
105 cc
->expect
<ValueTp
const&>(2);
107 assert(!cc
->unchecked());
109 DisableAllocationGuard g
;
114 CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type const&");
116 const ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1), ValueTp(3, 1) };
117 cc
->expect
<ValueTp
const&>(3);
118 c
.insert(std::begin(ValueList
), std::end(ValueList
));
119 assert(!cc
->unchecked());
121 DisableAllocationGuard g
;
122 c
.insert(std::begin(ValueList
), std::end(ValueList
));
126 CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&&");
128 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
129 cc
->expect
<ValueTp
&&>(3);
130 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList
)),
131 std::move_iterator
<ValueTp
*>(std::end(ValueList
)));
132 assert(!cc
->unchecked());
134 DisableAllocationGuard g
;
135 ValueTp ValueList2
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
136 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList2
)),
137 std::move_iterator
<ValueTp
*>(std::end(ValueList2
)));
141 CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&");
143 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
144 cc
->expect
<ValueTp
const&>(3);
145 c
.insert(std::begin(ValueList
), std::end(ValueList
));
146 assert(!cc
->unchecked());
148 DisableAllocationGuard g
;
149 c
.insert(std::begin(ValueList
), std::end(ValueList
));
155 template <class Container
>
156 void testMapInsertHint()
158 typedef typename
Container::value_type ValueTp
;
159 typedef typename
Container::key_type Key
;
160 typedef typename
Container::mapped_type Mapped
;
161 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
163 typedef typename
C::iterator It
;
164 ConstructController
* cc
= getConstructController();
167 CHECKPOINT("Testing C::insert(p, const value_type&)");
169 const ValueTp
v(42, 1);
170 cc
->expect
<const ValueTp
&>();
171 It ret
= c
.insert(c
.end(), v
);
172 assert(ret
!= c
.end());
173 assert(c
.size() == 1);
174 assert(!cc
->unchecked());
176 DisableAllocationGuard g
;
177 const ValueTp
v2(42, 1);
178 It ret2
= c
.insert(c
.begin(), v2
);
179 assert(&(*ret2
) == &(*ret
));
180 assert(c
.size() == 1);
184 CHECKPOINT("Testing C::insert(p, value_type&)");
187 cc
->expect
<ValueTp
const&>();
188 It ret
= c
.insert(c
.end(), v
);
189 assert(ret
!= c
.end());
190 assert(c
.size() == 1);
191 assert(!cc
->unchecked());
193 DisableAllocationGuard g
;
195 It ret2
= c
.insert(c
.begin(), v2
);
196 assert(&(*ret2
) == &(*ret
));
197 assert(c
.size() == 1);
201 CHECKPOINT("Testing C::insert(p, value_type&&)");
204 cc
->expect
<ValueTp
&&>();
205 It ret
= c
.insert(c
.end(), std::move(v
));
206 assert(ret
!= c
.end());
207 assert(c
.size() == 1);
208 assert(!cc
->unchecked());
210 DisableAllocationGuard g
;
212 It ret2
= c
.insert(c
.begin(), std::move(v2
));
213 assert(&(*ret2
) == &(*ret
));
214 assert(c
.size() == 1);
218 CHECKPOINT("Testing C::insert(p, {key, value})");
220 cc
->expect
<ValueTp
&&>();
221 It ret
= c
.insert(c
.end(), {42, 1});
222 assert(ret
!= c
.end());
223 assert(c
.size() == 1);
224 assert(!cc
->unchecked());
226 DisableAllocationGuard g
;
227 It ret2
= c
.insert(c
.begin(), {42, 1});
228 assert(&(*ret2
) == &(*ret
));
229 assert(c
.size() == 1);
233 CHECKPOINT("Testing C::insert(p, const value_type&&)");
235 const ValueTp
v(42, 1);
236 cc
->expect
<const ValueTp
&>();
237 It ret
= c
.insert(c
.end(), std::move(v
));
238 assert(ret
!= c
.end());
239 assert(c
.size() == 1);
240 assert(!cc
->unchecked());
242 DisableAllocationGuard g
;
243 const ValueTp
v2(42, 1);
244 It ret2
= c
.insert(c
.begin(), std::move(v2
));
245 assert(&(*ret2
) == &(*ret
));
246 assert(c
.size() == 1);
250 CHECKPOINT("Testing C::insert(p, pair<Key, Mapped> const&)");
252 const NonConstKeyPair
v(42, 1);
253 cc
->expect
<const NonConstKeyPair
&>();
254 It ret
= c
.insert(c
.end(), v
);
255 assert(ret
!= c
.end());
256 assert(c
.size() == 1);
257 assert(!cc
->unchecked());
259 DisableAllocationGuard g
;
260 const NonConstKeyPair
v2(42, 1);
261 It ret2
= c
.insert(c
.begin(), v2
);
262 assert(&(*ret2
) == &(*ret
));
263 assert(c
.size() == 1);
267 CHECKPOINT("Testing C::insert(p, pair<Key, Mapped>&&)");
269 NonConstKeyPair
v(42, 1);
270 cc
->expect
<NonConstKeyPair
&&>();
271 It ret
= c
.insert(c
.end(), std::move(v
));
272 assert(ret
!= c
.end());
273 assert(c
.size() == 1);
274 assert(!cc
->unchecked());
276 DisableAllocationGuard g
;
277 NonConstKeyPair
v2(42, 1);
278 It ret2
= c
.insert(c
.begin(), std::move(v2
));
279 assert(&(*ret2
) == &(*ret
));
280 assert(c
.size() == 1);
288 template <class Container
>
289 void testMapEmplace()
291 typedef typename
Container::value_type ValueTp
;
292 typedef typename
Container::key_type Key
;
293 typedef typename
Container::mapped_type Mapped
;
294 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
295 ConstructController
* cc
= getConstructController();
298 CHECKPOINT("Testing C::emplace(const value_type&)");
300 const ValueTp
v(42, 1);
301 cc
->expect
<const ValueTp
&>();
302 assert(c
.emplace(v
).second
);
303 assert(!cc
->unchecked());
305 DisableAllocationGuard g
;
306 const ValueTp
v2(42, 1);
307 assert(c
.emplace(v2
).second
== false);
311 CHECKPOINT("Testing C::emplace(value_type&)");
314 cc
->expect
<ValueTp
&>();
315 assert(c
.emplace(v
).second
);
316 assert(!cc
->unchecked());
318 DisableAllocationGuard g
;
320 assert(c
.emplace(v2
).second
== false);
324 CHECKPOINT("Testing C::emplace(value_type&&)");
327 cc
->expect
<ValueTp
&&>();
328 assert(c
.emplace(std::move(v
)).second
);
329 assert(!cc
->unchecked());
331 DisableAllocationGuard g
;
333 assert(c
.emplace(std::move(v2
)).second
== false);
337 CHECKPOINT("Testing C::emplace(const value_type&&)");
339 const ValueTp
v(42, 1);
340 cc
->expect
<const ValueTp
&&>();
341 assert(c
.emplace(std::move(v
)).second
);
342 assert(!cc
->unchecked());
344 DisableAllocationGuard g
;
345 const ValueTp
v2(42, 1);
346 assert(c
.emplace(std::move(v2
)).second
== false);
350 CHECKPOINT("Testing C::emplace(pair<Key, Mapped> const&)");
352 const NonConstKeyPair
v(42, 1);
353 cc
->expect
<const NonConstKeyPair
&>();
354 assert(c
.emplace(v
).second
);
355 assert(!cc
->unchecked());
357 DisableAllocationGuard g
;
358 const NonConstKeyPair
v2(42, 1);
359 assert(c
.emplace(v2
).second
== false);
363 CHECKPOINT("Testing C::emplace(pair<Key, Mapped> &&)");
365 NonConstKeyPair
v(42, 1);
366 cc
->expect
<NonConstKeyPair
&&>();
367 assert(c
.emplace(std::move(v
)).second
);
368 assert(!cc
->unchecked());
370 DisableAllocationGuard g
;
371 NonConstKeyPair
v2(42, 1);
372 assert(c
.emplace(std::move(v2
)).second
== false);
376 CHECKPOINT("Testing C::emplace(const Key&, ConvertibleToMapped&&)");
379 cc
->expect
<Key
const&, int&&>();
380 assert(c
.emplace(k
, 1).second
);
381 assert(!cc
->unchecked());
383 DisableAllocationGuard g
;
385 assert(c
.emplace(k2
, 2).second
== false);
389 CHECKPOINT("Testing C::emplace(Key&, Mapped&)");
393 cc
->expect
<Key
&, Mapped
&>();
394 assert(c
.emplace(k
, m
).second
);
395 assert(!cc
->unchecked());
397 DisableAllocationGuard g
;
399 assert(c
.emplace(k2
, m
).second
== false);
403 CHECKPOINT("Testing C::emplace(Key&&, Mapped&&)");
407 cc
->expect
<Key
&&, Mapped
&&>();
408 assert(c
.emplace(std::move(k
), std::move(m
)).second
);
409 assert(!cc
->unchecked());
411 DisableAllocationGuard g
;
414 assert(c
.emplace(std::move(k2
), std::move(m2
)).second
== false);
418 CHECKPOINT("Testing C::emplace(ConvertibleToKey&&, ConvertibleToMapped&&)");
420 cc
->expect
<int&&, int&&>();
421 assert(c
.emplace(42, 1).second
);
422 assert(!cc
->unchecked());
424 // test that emplacing a duplicate item allocates. We cannot optimize
425 // this case because int&& does not match the type of key exactly.
426 cc
->expect
<int&&, int&&>();
427 assert(c
.emplace(42, 1).second
== false);
428 assert(!cc
->unchecked());
434 template <class Container
>
435 void testMapEmplaceHint()
437 typedef typename
Container::value_type ValueTp
;
438 typedef typename
Container::key_type Key
;
439 typedef typename
Container::mapped_type Mapped
;
440 typedef typename
std::pair
<Key
, Mapped
> NonConstKeyPair
;
442 typedef typename
C::iterator It
;
443 ConstructController
* cc
= getConstructController();
446 CHECKPOINT("Testing C::emplace_hint(p, const value_type&)");
448 const ValueTp
v(42, 1);
449 cc
->expect
<const ValueTp
&>();
450 It ret
= c
.emplace_hint(c
.end(), v
);
451 assert(ret
!= c
.end());
452 assert(c
.size() == 1);
453 assert(!cc
->unchecked());
455 DisableAllocationGuard g
;
456 const ValueTp
v2(42, 1);
457 It ret2
= c
.emplace_hint(c
.begin(), v2
);
458 assert(&(*ret2
) == &(*ret
));
459 assert(c
.size() == 1);
463 CHECKPOINT("Testing C::emplace_hint(p, value_type&)");
466 cc
->expect
<ValueTp
&>();
467 It ret
= c
.emplace_hint(c
.end(), v
);
468 assert(ret
!= c
.end());
469 assert(c
.size() == 1);
470 assert(!cc
->unchecked());
472 DisableAllocationGuard g
;
474 It ret2
= c
.emplace_hint(c
.begin(), v2
);
475 assert(&(*ret2
) == &(*ret
));
476 assert(c
.size() == 1);
480 CHECKPOINT("Testing C::emplace_hint(p, value_type&&)");
483 cc
->expect
<ValueTp
&&>();
484 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
485 assert(ret
!= c
.end());
486 assert(c
.size() == 1);
487 assert(!cc
->unchecked());
489 DisableAllocationGuard g
;
491 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
492 assert(&(*ret2
) == &(*ret
));
493 assert(c
.size() == 1);
497 CHECKPOINT("Testing C::emplace_hint(p, const value_type&&)");
499 const ValueTp
v(42, 1);
500 cc
->expect
<const ValueTp
&&>();
501 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
502 assert(ret
!= c
.end());
503 assert(c
.size() == 1);
504 assert(!cc
->unchecked());
506 DisableAllocationGuard g
;
507 const ValueTp
v2(42, 1);
508 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
509 assert(&(*ret2
) == &(*ret
));
510 assert(c
.size() == 1);
514 CHECKPOINT("Testing C::emplace_hint(p, pair<Key, Mapped> const&)");
516 const NonConstKeyPair
v(42, 1);
517 cc
->expect
<const NonConstKeyPair
&>();
518 It ret
= c
.emplace_hint(c
.end(), v
);
519 assert(ret
!= c
.end());
520 assert(c
.size() == 1);
521 assert(!cc
->unchecked());
523 DisableAllocationGuard g
;
524 const NonConstKeyPair
v2(42, 1);
525 It ret2
= c
.emplace_hint(c
.begin(), v2
);
526 assert(&(*ret2
) == &(*ret
));
527 assert(c
.size() == 1);
531 CHECKPOINT("Testing C::emplace_hint(p, pair<Key, Mapped>&&)");
533 NonConstKeyPair
v(42, 1);
534 cc
->expect
<NonConstKeyPair
&&>();
535 It ret
= c
.emplace_hint(c
.end(), std::move(v
));
536 assert(ret
!= c
.end());
537 assert(c
.size() == 1);
538 assert(!cc
->unchecked());
540 DisableAllocationGuard g
;
541 NonConstKeyPair
v2(42, 1);
542 It ret2
= c
.emplace_hint(c
.begin(), std::move(v2
));
543 assert(&(*ret2
) == &(*ret
));
544 assert(c
.size() == 1);
548 CHECKPOINT("Testing C::emplace_hint(p, const Key&, ConvertibleToMapped&&)");
551 cc
->expect
<Key
const&, int&&>();
552 It ret
= c
.emplace_hint(c
.end(), k
, 42);
553 assert(ret
!= c
.end());
554 assert(c
.size() == 1);
555 assert(!cc
->unchecked());
557 DisableAllocationGuard g
;
559 It ret2
= c
.emplace_hint(c
.begin(), k2
, 1);
560 assert(&(*ret2
) == &(*ret
));
561 assert(c
.size() == 1);
565 CHECKPOINT("Testing C::emplace_hint(p, Key&, Mapped&)");
569 cc
->expect
<Key
&, Mapped
&>();
570 It ret
= c
.emplace_hint(c
.end(), k
, m
);
571 assert(ret
!= c
.end());
572 assert(c
.size() == 1);
573 assert(!cc
->unchecked());
575 DisableAllocationGuard g
;
578 It ret2
= c
.emplace_hint(c
.begin(), k2
, m2
);
579 assert(&(*ret2
) == &(*ret
));
580 assert(c
.size() == 1);
584 CHECKPOINT("Testing C::emplace_hint(p, Key&&, Mapped&&)");
588 cc
->expect
<Key
&&, Mapped
&&>();
589 It ret
= c
.emplace_hint(c
.end(), std::move(k
), std::move(m
));
590 assert(ret
!= c
.end());
591 assert(c
.size() == 1);
592 assert(!cc
->unchecked());
594 DisableAllocationGuard g
;
597 It ret2
= c
.emplace_hint(c
.begin(), std::move(k2
), std::move(m2
));
598 assert(&(*ret2
) == &(*ret
));
599 assert(c
.size() == 1);
603 CHECKPOINT("Testing C::emplace_hint(p, ConvertibleToKey&&, ConvertibleToMapped&&)");
605 cc
->expect
<int&&, int&&>();
606 It ret
= c
.emplace_hint(c
.end(), 42, 1);
607 assert(ret
!= c
.end());
608 assert(c
.size() == 1);
609 assert(!cc
->unchecked());
611 cc
->expect
<int&&, int&&>();
612 It ret2
= c
.emplace_hint(c
.begin(), 42, 2);
613 assert(&(*ret2
) == &(*ret
));
614 assert(c
.size() == 1);
615 assert(!cc
->unchecked());
622 template <class Container
>
623 void testMultimapInsert()
625 typedef typename
Container::value_type ValueTp
;
626 ConstructController
* cc
= getConstructController();
629 CHECKPOINT("Testing C::insert(const value_type&)");
631 const ValueTp
v(42, 1);
632 cc
->expect
<const ValueTp
&>();
634 assert(!cc
->unchecked());
637 CHECKPOINT("Testing C::insert(value_type&)");
640 cc
->expect
<ValueTp
&>();
642 assert(!cc
->unchecked());
645 CHECKPOINT("Testing C::insert(value_type&&)");
648 cc
->expect
<ValueTp
&&>();
649 c
.insert(std::move(v
));
650 assert(!cc
->unchecked());
653 CHECKPOINT("Testing C::insert({key, value})");
655 cc
->expect
<ValueTp
&&>();
657 assert(!cc
->unchecked());
660 CHECKPOINT("Testing C::insert(std::initializer_list<ValueTp>)");
662 std::initializer_list
<ValueTp
> il
= { ValueTp(1, 1), ValueTp(2, 1) };
663 cc
->expect
<ValueTp
const&>(2);
665 assert(!cc
->unchecked());
668 CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type const&");
670 const ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1), ValueTp(3, 1) };
671 cc
->expect
<ValueTp
const&>(3);
672 c
.insert(std::begin(ValueList
), std::end(ValueList
));
673 assert(!cc
->unchecked());
676 CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&&");
678 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
679 cc
->expect
<ValueTp
&&>(3);
680 c
.insert(std::move_iterator
<ValueTp
*>(std::begin(ValueList
)),
681 std::move_iterator
<ValueTp
*>(std::end(ValueList
)));
682 assert(!cc
->unchecked());
685 CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&");
687 ValueTp ValueList
[] = { ValueTp(1, 1), ValueTp(2, 1) , ValueTp(3, 1) };
688 cc
->expect
<ValueTp
&>(3);
689 c
.insert(std::begin(ValueList
), std::end(ValueList
));
690 assert(!cc
->unchecked());
695 template <class Container
>
696 void testMultimapInsertHint()
698 typedef typename
Container::value_type ValueTp
;
699 ConstructController
* cc
= getConstructController();
702 CHECKPOINT("Testing C::insert(p, const value_type&)");
704 const ValueTp
v(42, 1);
705 cc
->expect
<const ValueTp
&>();
706 c
.insert(c
.begin(), v
);
707 assert(!cc
->unchecked());
710 CHECKPOINT("Testing C::insert(p, value_type&)");
713 cc
->expect
<ValueTp
&>();
714 c
.insert(c
.begin(), v
);
715 assert(!cc
->unchecked());
718 CHECKPOINT("Testing C::insert(p, value_type&&)");
721 cc
->expect
<ValueTp
&&>();
722 c
.insert(c
.begin(), std::move(v
));
723 assert(!cc
->unchecked());
726 CHECKPOINT("Testing C::insert(p, {key, value})");
728 cc
->expect
<ValueTp
&&>();
729 c
.insert(c
.begin(), {42, 1});
730 assert(!cc
->unchecked());