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