| 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 | // iterator erase(iterator position); |
| 14 | // iterator erase(const_iterator position); |
| 15 | |
| 16 | #include <compare> |
| 17 | #include <concepts> |
| 18 | #include <deque> |
| 19 | #include <flat_map> |
| 20 | #include <functional> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "MinSequenceContainer.h" |
| 25 | #include "../helpers.h" |
| 26 | #include "test_macros.h" |
| 27 | #include "min_allocator.h" |
| 28 | |
| 29 | template <class KeyContainer, class ValueContainer> |
| 30 | void test() { |
| 31 | using Key = typename KeyContainer::value_type; |
| 32 | using Value = typename ValueContainer::value_type; |
| 33 | using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; |
| 34 | using P = std::pair<Key, Value>; |
| 35 | using I = M::iterator; |
| 36 | |
| 37 | P ar[] = { |
| 38 | P(1, 1.5), |
| 39 | P(2, 2.5), |
| 40 | P(3, 3.5), |
| 41 | P(4, 4.5), |
| 42 | P(5, 5.5), |
| 43 | P(6, 6.5), |
| 44 | P(7, 7.5), |
| 45 | P(8, 8.5), |
| 46 | }; |
| 47 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 48 | assert(m.size() == 8); |
| 49 | std::same_as<I> decltype(auto) i1 = m.erase(std::next(m.cbegin(), 3)); |
| 50 | assert(m.size() == 7); |
| 51 | assert(i1 == std::next(m.begin(), 3)); |
| 52 | assert(m.begin()->first == 1); |
| 53 | assert(m.begin()->second == 1.5); |
| 54 | assert(std::next(m.begin())->first == 2); |
| 55 | assert(std::next(m.begin())->second == 2.5); |
| 56 | assert(std::next(m.begin(), 2)->first == 3); |
| 57 | assert(std::next(m.begin(), 2)->second == 3.5); |
| 58 | assert(std::next(m.begin(), 3)->first == 5); |
| 59 | assert(std::next(m.begin(), 3)->second == 5.5); |
| 60 | assert(std::next(m.begin(), 4)->first == 6); |
| 61 | assert(std::next(m.begin(), 4)->second == 6.5); |
| 62 | assert(std::next(m.begin(), 5)->first == 7); |
| 63 | assert(std::next(m.begin(), 5)->second == 7.5); |
| 64 | assert(std::next(m.begin(), 6)->first == 8); |
| 65 | assert(std::next(m.begin(), 6)->second == 8.5); |
| 66 | |
| 67 | std::same_as<I> decltype(auto) i2 = m.erase(std::next(m.begin(), 0)); |
| 68 | assert(m.size() == 6); |
| 69 | assert(i2 == m.begin()); |
| 70 | assert(m.begin()->first == 2); |
| 71 | assert(m.begin()->second == 2.5); |
| 72 | assert(std::next(m.begin())->first == 3); |
| 73 | assert(std::next(m.begin())->second == 3.5); |
| 74 | assert(std::next(m.begin(), 2)->first == 5); |
| 75 | assert(std::next(m.begin(), 2)->second == 5.5); |
| 76 | assert(std::next(m.begin(), 3)->first == 6); |
| 77 | assert(std::next(m.begin(), 3)->second == 6.5); |
| 78 | assert(std::next(m.begin(), 4)->first == 7); |
| 79 | assert(std::next(m.begin(), 4)->second == 7.5); |
| 80 | assert(std::next(m.begin(), 5)->first == 8); |
| 81 | assert(std::next(m.begin(), 5)->second == 8.5); |
| 82 | |
| 83 | std::same_as<I> decltype(auto) i3 = m.erase(std::next(m.cbegin(), 5)); |
| 84 | assert(m.size() == 5); |
| 85 | assert(i3 == m.end()); |
| 86 | assert(m.begin()->first == 2); |
| 87 | assert(m.begin()->second == 2.5); |
| 88 | assert(std::next(m.begin())->first == 3); |
| 89 | assert(std::next(m.begin())->second == 3.5); |
| 90 | assert(std::next(m.begin(), 2)->first == 5); |
| 91 | assert(std::next(m.begin(), 2)->second == 5.5); |
| 92 | assert(std::next(m.begin(), 3)->first == 6); |
| 93 | assert(std::next(m.begin(), 3)->second == 6.5); |
| 94 | assert(std::next(m.begin(), 4)->first == 7); |
| 95 | assert(std::next(m.begin(), 4)->second == 7.5); |
| 96 | |
| 97 | std::same_as<I> decltype(auto) i4 = m.erase(std::next(m.begin(), 1)); |
| 98 | assert(m.size() == 4); |
| 99 | assert(i4 == std::next(m.begin())); |
| 100 | assert(m.begin()->first == 2); |
| 101 | assert(m.begin()->second == 2.5); |
| 102 | assert(std::next(m.begin())->first == 5); |
| 103 | assert(std::next(m.begin())->second == 5.5); |
| 104 | assert(std::next(m.begin(), 2)->first == 6); |
| 105 | assert(std::next(m.begin(), 2)->second == 6.5); |
| 106 | assert(std::next(m.begin(), 3)->first == 7); |
| 107 | assert(std::next(m.begin(), 3)->second == 7.5); |
| 108 | |
| 109 | std::same_as<I> decltype(auto) i5 = m.erase(std::next(m.cbegin(), 2)); |
| 110 | assert(m.size() == 3); |
| 111 | assert(i5 == std::next(m.begin(), 2)); |
| 112 | assert(m.begin()->first == 2); |
| 113 | assert(m.begin()->second == 2.5); |
| 114 | assert(std::next(m.begin())->first == 5); |
| 115 | assert(std::next(m.begin())->second == 5.5); |
| 116 | assert(std::next(m.begin(), 2)->first == 7); |
| 117 | assert(std::next(m.begin(), 2)->second == 7.5); |
| 118 | |
| 119 | std::same_as<I> decltype(auto) i6 = m.erase(std::next(m.begin(), 2)); |
| 120 | assert(m.size() == 2); |
| 121 | assert(i6 == std::next(m.begin(), 2)); |
| 122 | assert(m.begin()->first == 2); |
| 123 | assert(m.begin()->second == 2.5); |
| 124 | assert(std::next(m.begin())->first == 5); |
| 125 | assert(std::next(m.begin())->second == 5.5); |
| 126 | |
| 127 | std::same_as<I> decltype(auto) i7 = m.erase(std::next(m.cbegin(), 0)); |
| 128 | assert(m.size() == 1); |
| 129 | assert(i7 == std::next(m.begin(), 0)); |
| 130 | assert(m.begin()->first == 5); |
| 131 | assert(m.begin()->second == 5.5); |
| 132 | |
| 133 | std::same_as<I> decltype(auto) i8 = m.erase(m.begin()); |
| 134 | assert(m.size() == 0); |
| 135 | assert(i8 == m.begin()); |
| 136 | assert(i8 == m.end()); |
| 137 | } |
| 138 | |
| 139 | int main(int, char**) { |
| 140 | test<std::vector<int>, std::vector<double>>(); |
| 141 | test<std::deque<int>, std::vector<double>>(); |
| 142 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 143 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 144 | |
| 145 | { |
| 146 | auto erase_function = [](auto& m, auto) { m.erase(m.begin() + 2); }; |
| 147 | test_erase_exception_guarantee(erase_function); |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |