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