| 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 | // template <class... Args> |
| 14 | // iterator emplace(Args&&... args); |
| 15 | |
| 16 | #include <flat_set> |
| 17 | #include <cassert> |
| 18 | #include <deque> |
| 19 | #include <tuple> |
| 20 | #include <functional> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "MinSequenceContainer.h" |
| 24 | #include "../helpers.h" |
| 25 | #include "test_macros.h" |
| 26 | #include "../../../Emplaceable.h" |
| 27 | #include "DefaultOnly.h" |
| 28 | #include "min_allocator.h" |
| 29 | |
| 30 | template <class KeyContainer> |
| 31 | void test_one() { |
| 32 | using Key = typename KeyContainer::value_type; |
| 33 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
| 34 | using R = typename M::iterator; |
| 35 | { |
| 36 | // was empty |
| 37 | M m; |
| 38 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2)); |
| 39 | assert(r == m.begin()); |
| 40 | assert(m.size() == 1); |
| 41 | assert(*r == 2); |
| 42 | } |
| 43 | { |
| 44 | // key does not exist and inserted at the begin |
| 45 | M m = {3, 3, 3, 7}; |
| 46 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2)); |
| 47 | assert(r == m.begin()); |
| 48 | assert(m.size() == 5); |
| 49 | assert(*r == 2); |
| 50 | } |
| 51 | { |
| 52 | // key does not exist and inserted in the middle |
| 53 | M m = {1, 1, 3, 4}; |
| 54 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2)); |
| 55 | assert(r == m.begin() + 2); |
| 56 | assert(m.size() == 5); |
| 57 | assert(*r == 2); |
| 58 | } |
| 59 | { |
| 60 | // key does not exist and inserted at the end |
| 61 | M m = {1, 1}; |
| 62 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2)); |
| 63 | assert(r == m.begin() + 2); |
| 64 | assert(m.size() == 3); |
| 65 | assert(*r == 2); |
| 66 | } |
| 67 | { |
| 68 | // key already exists and original at the begin |
| 69 | M m = {2, 2, 5, 6}; |
| 70 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2)); |
| 71 | assert(r == m.begin() + 2); |
| 72 | assert(m.size() == 5); |
| 73 | assert(*r == 2); |
| 74 | } |
| 75 | { |
| 76 | // key already exists and original in the middle |
| 77 | M m = {0, 2, 2, 4}; |
| 78 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2)); |
| 79 | assert(r == m.begin() + 3); |
| 80 | assert(m.size() == 5); |
| 81 | assert(*r == 2); |
| 82 | } |
| 83 | { |
| 84 | // key already exists and original at the end |
| 85 | M m = {0, 1, 2}; |
| 86 | std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2)); |
| 87 | assert(r == m.begin() + 3); |
| 88 | assert(m.size() == 4); |
| 89 | assert(*r == 2); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | template <class KeyContainer> |
| 94 | void test_emplaceable() { |
| 95 | using M = std::flat_multiset<Emplaceable, std::less<Emplaceable>, KeyContainer>; |
| 96 | using R = typename M::iterator; |
| 97 | |
| 98 | M m; |
| 99 | ASSERT_SAME_TYPE(decltype(m.emplace()), R); |
| 100 | R r = m.emplace(2, 0.0); |
| 101 | assert(r == m.begin()); |
| 102 | assert(m.size() == 1); |
| 103 | assert(*r == Emplaceable(2, 0.0)); |
| 104 | r = m.emplace(1, 3.5); |
| 105 | assert(r == m.begin()); |
| 106 | assert(m.size() == 2); |
| 107 | assert(*r == Emplaceable(1, 3.5)); |
| 108 | r = m.emplace(1, 3.5); |
| 109 | assert(r == m.begin() + 1); |
| 110 | assert(m.size() == 3); |
| 111 | assert(*r == Emplaceable(1, 3.5)); |
| 112 | } |
| 113 | |
| 114 | void test() { |
| 115 | test_one<std::vector<int>>(); |
| 116 | test_one<std::deque<int>>(); |
| 117 | test_one<MinSequenceContainer<int>>(); |
| 118 | test_one<std::vector<int, min_allocator<int>>>(); |
| 119 | |
| 120 | test_emplaceable<std::vector<Emplaceable>>(); |
| 121 | test_emplaceable<std::deque<Emplaceable>>(); |
| 122 | test_emplaceable<MinSequenceContainer<Emplaceable>>(); |
| 123 | test_emplaceable<std::vector<Emplaceable, min_allocator<Emplaceable>>>(); |
| 124 | } |
| 125 | |
| 126 | void test_exception() { |
| 127 | auto emplace_func = [](auto& m, auto key_arg) { m.emplace(key_arg); }; |
| 128 | test_emplace_exception_guarantee(emplace_function&: emplace_func); |
| 129 | } |
| 130 | |
| 131 | int main(int, char**) { |
| 132 | test(); |
| 133 | test_exception(); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |