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

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