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_set> |
12 | |
13 | // iterator insert(const_iterator position, const value_type& v); |
14 | |
15 | #include <flat_set> |
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 | |
25 | template <class KeyContainer> |
26 | void test_one() { |
27 | using Key = typename KeyContainer::value_type; |
28 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
29 | using R = typename M::iterator; |
30 | using VT = typename M::value_type; |
31 | |
32 | M m; |
33 | const VT v1(2); |
34 | std::same_as<R> decltype(auto) r = m.insert(m.end(), v1); |
35 | assert(r == m.begin()); |
36 | assert(m.size() == 1); |
37 | assert(*r == 2); |
38 | |
39 | const VT v2(1); |
40 | r = m.insert(m.end(), v2); |
41 | assert(r == m.begin()); |
42 | assert(m.size() == 2); |
43 | assert(*r == 1); |
44 | |
45 | const VT v3(3); |
46 | r = m.insert(m.end(), v3); |
47 | assert(r == std::ranges::prev(m.end())); |
48 | assert(m.size() == 3); |
49 | assert(*r == 3); |
50 | |
51 | const VT v4(3); |
52 | r = m.insert(m.end(), v4); |
53 | assert(r == std::ranges::prev(m.end())); |
54 | assert(m.size() == 4); |
55 | assert(*r == 3); |
56 | |
57 | const VT v5(1); |
58 | r = m.insert(m.begin() + 2, v5); |
59 | assert(r == m.begin() + 1); |
60 | assert(m.size() == 5); |
61 | assert(*r == 1); |
62 | } |
63 | |
64 | void test() { |
65 | test_one<std::vector<int>>(); |
66 | test_one<std::deque<int>>(); |
67 | test_one<MinSequenceContainer<int>>(); |
68 | test_one<std::vector<int, min_allocator<int>>>(); |
69 | } |
70 | |
71 | void test_exception() { |
72 | auto insert_func = [](auto& m, auto key_arg) { |
73 | using FlatSet = std::decay_t<decltype(m)>; |
74 | using value_type = typename FlatSet::value_type; |
75 | const value_type p(key_arg); |
76 | m.insert(m.begin(), p); |
77 | }; |
78 | test_emplace_exception_guarantee(emplace_function&: insert_func); |
79 | } |
80 | |
81 | int main(int, char**) { |
82 | test(); |
83 | test_exception(); |
84 | |
85 | return 0; |
86 | } |
87 | |