| 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 | // template <class... Args> |
| 16 | // iterator emplace(Args&&... args); |
| 17 | |
| 18 | #include <flat_map> |
| 19 | #include <cassert> |
| 20 | #include <deque> |
| 21 | #include <tuple> |
| 22 | #include <functional> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "MinSequenceContainer.h" |
| 26 | #include "../helpers.h" |
| 27 | #include "test_macros.h" |
| 28 | #include "../../../Emplaceable.h" |
| 29 | #include "DefaultOnly.h" |
| 30 | #include "min_allocator.h" |
| 31 | |
| 32 | // Constraints: is_constructible_v<pair<key_type, mapped_type>, Args...> is true. |
| 33 | template <class M, class... Args> |
| 34 | concept CanEmplace = requires(M m, Args&&... args) { m.emplace(std::forward<Args>(args)...); }; |
| 35 | |
| 36 | using Map = std::flat_multimap<Emplaceable, Emplaceable>; |
| 37 | static_assert(CanEmplace<Map>); |
| 38 | static_assert(CanEmplace<Map, Emplaceable, Emplaceable>); |
| 39 | static_assert(CanEmplace<Map, std::piecewise_construct_t, std::tuple<int, double>, std::tuple<int, double>>); |
| 40 | static_assert(!CanEmplace<Map, Emplaceable>); |
| 41 | static_assert(!CanEmplace<Map, int, double>); |
| 42 | |
| 43 | template <class KeyContainer, class ValueContainer> |
| 44 | void test() { |
| 45 | using Key = typename KeyContainer::value_type; |
| 46 | using Value = typename ValueContainer::value_type; |
| 47 | using M = std::flat_multimap<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; |
| 48 | using R = typename M::iterator; |
| 49 | |
| 50 | { |
| 51 | // was empty |
| 52 | M m; |
| 53 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 3.5)); |
| 54 | assert(r == m.begin()); |
| 55 | assert(m.size() == 1); |
| 56 | assert(r->first == 2); |
| 57 | assert(r->second == 3.5); |
| 58 | } |
| 59 | { |
| 60 | // key does not exist and inserted at the begin |
| 61 | M m = {{3, 4.0}, {3, 3.0}, {3, 1.0}, {7, 0.0}}; |
| 62 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0)); |
| 63 | assert(r == m.begin()); |
| 64 | assert(m.size() == 5); |
| 65 | assert(r->first == 2); |
| 66 | assert(r->second == 2.0); |
| 67 | } |
| 68 | { |
| 69 | // key does not exist and inserted in the middle |
| 70 | M m = {{1, 4.0}, {1, 3.0}, {3, 1.0}, {4, 0.0}}; |
| 71 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0)); |
| 72 | assert(r == m.begin() + 2); |
| 73 | assert(m.size() == 5); |
| 74 | assert(r->first == 2); |
| 75 | assert(r->second == 2.0); |
| 76 | } |
| 77 | { |
| 78 | // key does not exist and inserted at the end |
| 79 | M m = {{1, 4.0}, {1, 3.0}}; |
| 80 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0)); |
| 81 | assert(r == m.begin() + 2); |
| 82 | assert(m.size() == 3); |
| 83 | assert(r->first == 2); |
| 84 | assert(r->second == 2.0); |
| 85 | } |
| 86 | { |
| 87 | // key already exists and original at the begin |
| 88 | M m = {{2, 4.0}, {2, 3.0}, {5, 1.0}, {6, 0.0}}; |
| 89 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0)); |
| 90 | assert(r == m.begin() + 2); |
| 91 | assert(m.size() == 5); |
| 92 | assert(r->first == 2); |
| 93 | assert(r->second == 2.0); |
| 94 | } |
| 95 | { |
| 96 | // key already exists and original in the middle |
| 97 | M m = {{0, 4.0}, {2, 3.0}, {2, 1.0}, {4, 0.0}}; |
| 98 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0)); |
| 99 | assert(r == m.begin() + 3); |
| 100 | assert(m.size() == 5); |
| 101 | assert(r->first == 2); |
| 102 | assert(r->second == 2.0); |
| 103 | } |
| 104 | { |
| 105 | // key already exists and original at the end |
| 106 | M m = {{0, 4.0}, {1, 3.0}, {2, 1.0}}; |
| 107 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0)); |
| 108 | assert(r == m.begin() + 3); |
| 109 | assert(m.size() == 4); |
| 110 | assert(r->first == 2); |
| 111 | assert(r->second == 2.0); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | template <class KeyContainer, class ValueContainer> |
| 116 | void test_emplaceable() { |
| 117 | using M = std::flat_multimap<int, Emplaceable, std::less<int>, KeyContainer, ValueContainer>; |
| 118 | using R = typename M::iterator; |
| 119 | |
| 120 | M m; |
| 121 | std::same_as<R> decltype(auto) r = |
| 122 | m.emplace(std::piecewise_construct, std::forward_as_tuple(2), std::forward_as_tuple()); |
| 123 | assert(r == m.begin()); |
| 124 | assert(m.size() == 1); |
| 125 | assert(m.begin()->first == 2); |
| 126 | assert(m.begin()->second == Emplaceable()); |
| 127 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5)); |
| 128 | assert(r == m.begin()); |
| 129 | assert(m.size() == 2); |
| 130 | assert(m.begin()->first == 1); |
| 131 | assert(m.begin()->second == Emplaceable(2, 3.5)); |
| 132 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5)); |
| 133 | assert(r == m.begin() + 1); |
| 134 | assert(m.size() == 3); |
| 135 | assert(m.begin()->first == 1); |
| 136 | assert(m.begin()->second == Emplaceable(2, 3.5)); |
| 137 | } |
| 138 | |
| 139 | int main(int, char**) { |
| 140 | test<std::vector<int>, std::vector<double>>(); |
| 141 | test<std::deque<int>, std::vector<double>>(); |
| 142 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 143 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 144 | |
| 145 | test_emplaceable<std::vector<int>, std::vector<Emplaceable>>(); |
| 146 | test_emplaceable<std::deque<int>, std::vector<Emplaceable>>(); |
| 147 | test_emplaceable<MinSequenceContainer<int>, MinSequenceContainer<Emplaceable>>(); |
| 148 | test_emplaceable<std::vector<int, min_allocator<int>>, std::vector<Emplaceable, min_allocator<Emplaceable>>>(); |
| 149 | |
| 150 | { |
| 151 | auto emplace_func = [](auto& m, auto key_arg, auto value_arg) { |
| 152 | m.emplace(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); |
| 153 | }; |
| 154 | test_emplace_exception_guarantee(emplace_function&: emplace_func); |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |