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(const_iterator position, value_type&&); |
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 "../helpers.h" |
25 | #include "test_macros.h" |
26 | |
27 | template <class Container, class Pair> |
28 | void do_insert_iter_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(m.end(), 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(m.end(), 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(m.end(), 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(m.end(), P(3, 4)); |
52 | assert(r == std::ranges::prev(m.end())); |
53 | assert(m.size() == 4); |
54 | assert(r->first == 3); |
55 | assert(r->second == 4); |
56 | |
57 | r = m.insert(m.end(), P(2, 5)); |
58 | assert(r == m.begin() + 2); |
59 | assert(m.size() == 5); |
60 | assert(r->first == 2); |
61 | assert(r->second == 5); |
62 | |
63 | r = m.insert(m.begin(), P(2, 6)); |
64 | assert(r == m.begin() + 1); |
65 | assert(m.size() == 6); |
66 | assert(r->first == 2); |
67 | assert(r->second == 6); |
68 | } |
69 | |
70 | template <class KeyContainer, class ValueContainer> |
71 | void test() { |
72 | using Key = typename KeyContainer::value_type; |
73 | using Value = typename ValueContainer::value_type; |
74 | using M = std::flat_multimap<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; |
75 | using P = std::pair<Key, Value>; |
76 | using CP = std::pair<const Key, Value>; |
77 | |
78 | do_insert_iter_rv_test<M, P>(); |
79 | do_insert_iter_rv_test<M, CP>(); |
80 | } |
81 | |
82 | int main(int, char**) { |
83 | test<std::vector<int>, std::vector<double>>(); |
84 | test<std::vector<int>, std::vector<MoveOnly>>(); |
85 | test<std::deque<int>, std::deque<double>>(); |
86 | test<std::deque<int>, std::deque<MoveOnly>>(); |
87 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
88 | test<MinSequenceContainer<int>, MinSequenceContainer<MoveOnly>>(); |
89 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
90 | test<std::vector<int, min_allocator<int>>, std::vector<MoveOnly, min_allocator<MoveOnly>>>(); |
91 | |
92 | { |
93 | auto insert_func = [](auto& m, auto key_arg, auto value_arg) { |
94 | using FlatMap = std::decay_t<decltype(m)>; |
95 | using value_type = typename FlatMap::value_type; |
96 | value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); |
97 | m.insert(m.begin(), std::move(p)); |
98 | }; |
99 | test_emplace_exception_guarantee(emplace_function&: insert_func); |
100 | } |
101 | |
102 | return 0; |
103 | } |
104 | |