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 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
15 // pair<iterator, bool> insert( value_type&& v);
21 #include "MinSequenceContainer.h"
23 #include "min_allocator.h"
24 #include "test_macros.h"
25 #include "../helpers.h"
27 template <class Container
, class Pair
>
28 void do_insert_rv_test() {
31 using R
= std::pair
<typename
M::iterator
, bool>;
33 std::same_as
<R
> decltype(auto) r
= m
.insert(P(2, 2));
35 assert(r
.first
== m
.begin());
36 assert(m
.size() == 1);
37 assert(r
.first
->first
== 2);
38 assert(r
.first
->second
== 2);
40 r
= m
.insert(P(1, 1));
42 assert(r
.first
== m
.begin());
43 assert(m
.size() == 2);
44 assert(r
.first
->first
== 1);
45 assert(r
.first
->second
== 1);
47 r
= m
.insert(P(3, 3));
49 assert(r
.first
== std::ranges::prev(m
.end()));
50 assert(m
.size() == 3);
51 assert(r
.first
->first
== 3);
52 assert(r
.first
->second
== 3);
54 r
= m
.insert(P(3, 3));
56 assert(r
.first
== std::ranges::prev(m
.end()));
57 assert(m
.size() == 3);
58 assert(r
.first
->first
== 3);
59 assert(r
.first
->second
== 3);
62 template <class KeyContainer
, class ValueContainer
>
64 using Key
= typename
KeyContainer::value_type
;
65 using Value
= typename
ValueContainer::value_type
;
66 using M
= std::flat_map
<Key
, Value
, TransparentComparator
, KeyContainer
, ValueContainer
>;
68 using P
= std::pair
<Key
, Value
>;
69 using CP
= std::pair
<const Key
, Value
>;
71 do_insert_rv_test
<M
, P
>();
72 do_insert_rv_test
<M
, CP
>();
75 int main(int, char**) {
76 test
<std::vector
<int>, std::vector
<MoveOnly
>>();
77 test
<std::deque
<int>, std::vector
<MoveOnly
>>();
78 test
<MinSequenceContainer
<int>, MinSequenceContainer
<MoveOnly
>>();
79 test
<std::vector
<int, min_allocator
<int>>, std::vector
<MoveOnly
, min_allocator
<MoveOnly
>>>();
82 using M
= std::flat_map
<int, MoveOnly
>;
83 using R
= std::pair
<M::iterator
, bool>;
85 R r
= m
.insert({2, MoveOnly(2)});
87 assert(r
.first
== m
.begin());
88 assert(m
.size() == 1);
89 assert(r
.first
->first
== 2);
90 assert(r
.first
->second
== 2);
92 r
= m
.insert({1, MoveOnly(1)});
94 assert(r
.first
== m
.begin());
95 assert(m
.size() == 2);
96 assert(r
.first
->first
== 1);
97 assert(r
.first
->second
== 1);
99 r
= m
.insert({3, MoveOnly(3)});
101 assert(r
.first
== std::ranges::prev(m
.end()));
102 assert(m
.size() == 3);
103 assert(r
.first
->first
== 3);
104 assert(r
.first
->second
== 3);
106 r
= m
.insert({3, MoveOnly(3)});
108 assert(r
.first
== std::ranges::prev(m
.end()));
109 assert(m
.size() == 3);
110 assert(r
.first
->first
== 3);
111 assert(r
.first
->second
== 3);
114 auto insert_func
= [](auto& m
, auto key_arg
, auto value_arg
) {
115 using FlatMap
= std::decay_t
<decltype(m
)>;
116 using value_type
= typename
FlatMap::value_type
;
117 value_type
p(std::piecewise_construct
, std::tuple(key_arg
), std::tuple(value_arg
));
118 m
.insert(std::move(p
));
120 test_emplace_exception_guarantee(insert_func
);