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

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace.pass.cpp