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

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