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

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