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 | |
9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
10 | |
11 | // <flat_map> |
12 | |
13 | // class flat_multimap |
14 | |
15 | // iterator insert( value_type&& v); |
16 | |
17 | #include <flat_map> |
18 | #include <cassert> |
19 | #include <deque> |
20 | |
21 | #include "MinSequenceContainer.h" |
22 | #include "MoveOnly.h" |
23 | #include "min_allocator.h" |
24 | #include "test_macros.h" |
25 | #include "../helpers.h" |
26 | |
27 | template <class Container, class Pair> |
28 | void do_insert_rv_test() { |
29 | using M = Container; |
30 | using P = Pair; |
31 | using R = typename M::iterator; |
32 | M m; |
33 | std::same_as<R> decltype(auto) r = m.insert(P(2, 2)); |
34 | assert(r == m.begin()); |
35 | assert(m.size() == 1); |
36 | assert(r->first == 2); |
37 | assert(r->second == 2); |
38 | |
39 | r = m.insert(P(1, 1)); |
40 | assert(r == m.begin()); |
41 | assert(m.size() == 2); |
42 | assert(r->first == 1); |
43 | assert(r->second == 1); |
44 | |
45 | r = m.insert(P(3, 3)); |
46 | assert(r == std::ranges::prev(m.end())); |
47 | assert(m.size() == 3); |
48 | assert(r->first == 3); |
49 | assert(r->second == 3); |
50 | |
51 | r = m.insert(P(3, 3)); |
52 | assert(r == std::ranges::prev(m.end())); |
53 | assert(m.size() == 4); |
54 | assert(r->first == 3); |
55 | assert(r->second == 3); |
56 | } |
57 | |
58 | template <class KeyContainer, class ValueContainer> |
59 | void test() { |
60 | using Key = typename KeyContainer::value_type; |
61 | using Value = typename ValueContainer::value_type; |
62 | using M = std::flat_multimap<Key, Value, TransparentComparator, KeyContainer, ValueContainer>; |
63 | |
64 | using P = std::pair<Key, Value>; |
65 | using CP = std::pair<const Key, Value>; |
66 | |
67 | do_insert_rv_test<M, P>(); |
68 | do_insert_rv_test<M, CP>(); |
69 | } |
70 | |
71 | int main(int, char**) { |
72 | test<std::vector<int>, std::vector<MoveOnly>>(); |
73 | test<std::deque<int>, std::vector<MoveOnly>>(); |
74 | test<MinSequenceContainer<int>, MinSequenceContainer<MoveOnly>>(); |
75 | test<std::vector<int, min_allocator<int>>, std::vector<MoveOnly, min_allocator<MoveOnly>>>(); |
76 | |
77 | { |
78 | using M = std::flat_multimap<int, MoveOnly>; |
79 | using R = M::iterator; |
80 | M m; |
81 | R r = m.insert({2, MoveOnly(2)}); |
82 | assert(r == m.begin()); |
83 | assert(m.size() == 1); |
84 | assert(r->first == 2); |
85 | assert(r->second == 2); |
86 | |
87 | r = m.insert({1, MoveOnly(1)}); |
88 | assert(r == m.begin()); |
89 | assert(m.size() == 2); |
90 | assert(r->first == 1); |
91 | assert(r->second == 1); |
92 | |
93 | r = m.insert({3, MoveOnly(3)}); |
94 | assert(r == std::ranges::prev(m.end())); |
95 | assert(m.size() == 3); |
96 | assert(r->first == 3); |
97 | assert(r->second == 3); |
98 | |
99 | r = m.insert({3, MoveOnly(3)}); |
100 | assert(r == std::ranges::prev(m.end())); |
101 | assert(m.size() == 4); |
102 | assert(r->first == 3); |
103 | assert(r->second == 3); |
104 | } |
105 | { |
106 | auto insert_func = [](auto& m, auto key_arg, auto value_arg) { |
107 | using FlatMap = std::decay_t<decltype(m)>; |
108 | using value_type = typename FlatMap::value_type; |
109 | value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); |
110 | m.insert(std::move(p)); |
111 | }; |
112 | test_emplace_exception_guarantee(emplace_function&: insert_func); |
113 | } |
114 | |
115 | return 0; |
116 | } |
117 | |