| 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 | // iterator insert(const_iterator position, value_type&&); |
| 13 | |
| 14 | #include <flat_map> |
| 15 | #include <cassert> |
| 16 | #include <deque> |
| 17 | |
| 18 | #include "MinSequenceContainer.h" |
| 19 | #include "MoveOnly.h" |
| 20 | #include "min_allocator.h" |
| 21 | #include "../helpers.h" |
| 22 | #include "test_macros.h" |
| 23 | |
| 24 | template <class Container, class Pair> |
| 25 | void do_insert_iter_rv_test() { |
| 26 | using M = Container; |
| 27 | using P = Pair; |
| 28 | using R = typename M::iterator; |
| 29 | M m; |
| 30 | std::same_as<R> decltype(auto) r = m.insert(m.end(), P(2, 2)); |
| 31 | assert(r == m.begin()); |
| 32 | assert(m.size() == 1); |
| 33 | assert(r->first == 2); |
| 34 | assert(r->second == 2); |
| 35 | |
| 36 | r = m.insert(m.end(), P(1, 1)); |
| 37 | assert(r == m.begin()); |
| 38 | assert(m.size() == 2); |
| 39 | assert(r->first == 1); |
| 40 | assert(r->second == 1); |
| 41 | |
| 42 | r = m.insert(m.end(), P(3, 3)); |
| 43 | assert(r == std::ranges::prev(m.end())); |
| 44 | assert(m.size() == 3); |
| 45 | assert(r->first == 3); |
| 46 | assert(r->second == 3); |
| 47 | |
| 48 | r = m.insert(m.end(), P(3, 4)); |
| 49 | assert(r == std::ranges::prev(m.end())); |
| 50 | assert(m.size() == 3); |
| 51 | assert(r->first == 3); |
| 52 | assert(r->second == 3); |
| 53 | } |
| 54 | |
| 55 | template <class KeyContainer, class ValueContainer> |
| 56 | void test() { |
| 57 | using Key = typename KeyContainer::value_type; |
| 58 | using Value = typename ValueContainer::value_type; |
| 59 | using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; |
| 60 | using P = std::pair<Key, Value>; |
| 61 | using CP = std::pair<const Key, Value>; |
| 62 | |
| 63 | do_insert_iter_rv_test<M, P>(); |
| 64 | do_insert_iter_rv_test<M, CP>(); |
| 65 | } |
| 66 | |
| 67 | int main(int, char**) { |
| 68 | test<std::vector<int>, std::vector<double>>(); |
| 69 | test<std::vector<int>, std::vector<MoveOnly>>(); |
| 70 | test<std::deque<int>, std::deque<double>>(); |
| 71 | test<std::deque<int>, std::deque<MoveOnly>>(); |
| 72 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 73 | test<MinSequenceContainer<int>, MinSequenceContainer<MoveOnly>>(); |
| 74 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 75 | test<std::vector<int, min_allocator<int>>, std::vector<MoveOnly, min_allocator<MoveOnly>>>(); |
| 76 | |
| 77 | { |
| 78 | auto insert_func = [](auto& m, auto key_arg, auto value_arg) { |
| 79 | using FlatMap = std::decay_t<decltype(m)>; |
| 80 | using value_type = typename FlatMap::value_type; |
| 81 | value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); |
| 82 | m.insert(m.begin(), std::move(p)); |
| 83 | }; |
| 84 | test_emplace_exception_guarantee(emplace_function&: insert_func); |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |