| 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 | // <set> |
| 10 | |
| 11 | // class multiset |
| 12 | |
| 13 | // iterator erase(const_iterator position); |
| 14 | |
| 15 | #include <set> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "min_allocator.h" |
| 20 | |
| 21 | struct TemplateConstructor { |
| 22 | template <typename T> |
| 23 | TemplateConstructor(const T&) {} |
| 24 | }; |
| 25 | |
| 26 | bool operator<(const TemplateConstructor&, const TemplateConstructor&) { return false; } |
| 27 | |
| 28 | int main(int, char**) { |
| 29 | { |
| 30 | typedef std::multiset<int> M; |
| 31 | typedef int V; |
| 32 | typedef M::iterator I; |
| 33 | V ar[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 34 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 35 | assert(m.size() == 8); |
| 36 | I i = m.erase(position: std::next(x: m.cbegin(), n: 3)); |
| 37 | assert(m.size() == 7); |
| 38 | assert(i == std::next(m.begin(), 3)); |
| 39 | assert(*std::next(m.begin(), 0) == 1); |
| 40 | assert(*std::next(m.begin(), 1) == 2); |
| 41 | assert(*std::next(m.begin(), 2) == 3); |
| 42 | assert(*std::next(m.begin(), 3) == 5); |
| 43 | assert(*std::next(m.begin(), 4) == 6); |
| 44 | assert(*std::next(m.begin(), 5) == 7); |
| 45 | assert(*std::next(m.begin(), 6) == 8); |
| 46 | |
| 47 | i = m.erase(position: std::next(x: m.cbegin(), n: 0)); |
| 48 | assert(m.size() == 6); |
| 49 | assert(i == m.begin()); |
| 50 | assert(*std::next(m.begin(), 0) == 2); |
| 51 | assert(*std::next(m.begin(), 1) == 3); |
| 52 | assert(*std::next(m.begin(), 2) == 5); |
| 53 | assert(*std::next(m.begin(), 3) == 6); |
| 54 | assert(*std::next(m.begin(), 4) == 7); |
| 55 | assert(*std::next(m.begin(), 5) == 8); |
| 56 | |
| 57 | i = m.erase(position: std::next(x: m.cbegin(), n: 5)); |
| 58 | assert(m.size() == 5); |
| 59 | assert(i == m.end()); |
| 60 | assert(*std::next(m.begin(), 0) == 2); |
| 61 | assert(*std::next(m.begin(), 1) == 3); |
| 62 | assert(*std::next(m.begin(), 2) == 5); |
| 63 | assert(*std::next(m.begin(), 3) == 6); |
| 64 | assert(*std::next(m.begin(), 4) == 7); |
| 65 | |
| 66 | i = m.erase(position: std::next(x: m.cbegin(), n: 1)); |
| 67 | assert(m.size() == 4); |
| 68 | assert(i == std::next(m.begin())); |
| 69 | assert(*std::next(m.begin(), 0) == 2); |
| 70 | assert(*std::next(m.begin(), 1) == 5); |
| 71 | assert(*std::next(m.begin(), 2) == 6); |
| 72 | assert(*std::next(m.begin(), 3) == 7); |
| 73 | |
| 74 | i = m.erase(position: std::next(x: m.cbegin(), n: 2)); |
| 75 | assert(m.size() == 3); |
| 76 | assert(i == std::next(m.begin(), 2)); |
| 77 | assert(*std::next(m.begin(), 0) == 2); |
| 78 | assert(*std::next(m.begin(), 1) == 5); |
| 79 | assert(*std::next(m.begin(), 2) == 7); |
| 80 | |
| 81 | i = m.erase(position: std::next(x: m.cbegin(), n: 2)); |
| 82 | assert(m.size() == 2); |
| 83 | assert(i == std::next(m.begin(), 2)); |
| 84 | assert(*std::next(m.begin(), 0) == 2); |
| 85 | assert(*std::next(m.begin(), 1) == 5); |
| 86 | |
| 87 | i = m.erase(position: std::next(x: m.cbegin(), n: 0)); |
| 88 | assert(m.size() == 1); |
| 89 | assert(i == std::next(m.begin(), 0)); |
| 90 | assert(*std::next(m.begin(), 0) == 5); |
| 91 | |
| 92 | i = m.erase(position: m.cbegin()); |
| 93 | assert(m.size() == 0); |
| 94 | assert(i == m.begin()); |
| 95 | assert(i == m.end()); |
| 96 | } |
| 97 | #if TEST_STD_VER >= 11 |
| 98 | { |
| 99 | typedef std::multiset<int, std::less<int>, min_allocator<int>> M; |
| 100 | typedef int V; |
| 101 | typedef M::iterator I; |
| 102 | V ar[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 103 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 104 | assert(m.size() == 8); |
| 105 | I i = m.erase(std::next(m.cbegin(), 3)); |
| 106 | assert(m.size() == 7); |
| 107 | assert(i == std::next(m.begin(), 3)); |
| 108 | assert(*std::next(m.begin(), 0) == 1); |
| 109 | assert(*std::next(m.begin(), 1) == 2); |
| 110 | assert(*std::next(m.begin(), 2) == 3); |
| 111 | assert(*std::next(m.begin(), 3) == 5); |
| 112 | assert(*std::next(m.begin(), 4) == 6); |
| 113 | assert(*std::next(m.begin(), 5) == 7); |
| 114 | assert(*std::next(m.begin(), 6) == 8); |
| 115 | |
| 116 | i = m.erase(std::next(m.cbegin(), 0)); |
| 117 | assert(m.size() == 6); |
| 118 | assert(i == m.begin()); |
| 119 | assert(*std::next(m.begin(), 0) == 2); |
| 120 | assert(*std::next(m.begin(), 1) == 3); |
| 121 | assert(*std::next(m.begin(), 2) == 5); |
| 122 | assert(*std::next(m.begin(), 3) == 6); |
| 123 | assert(*std::next(m.begin(), 4) == 7); |
| 124 | assert(*std::next(m.begin(), 5) == 8); |
| 125 | |
| 126 | i = m.erase(std::next(m.cbegin(), 5)); |
| 127 | assert(m.size() == 5); |
| 128 | assert(i == m.end()); |
| 129 | assert(*std::next(m.begin(), 0) == 2); |
| 130 | assert(*std::next(m.begin(), 1) == 3); |
| 131 | assert(*std::next(m.begin(), 2) == 5); |
| 132 | assert(*std::next(m.begin(), 3) == 6); |
| 133 | assert(*std::next(m.begin(), 4) == 7); |
| 134 | |
| 135 | i = m.erase(std::next(m.cbegin(), 1)); |
| 136 | assert(m.size() == 4); |
| 137 | assert(i == std::next(m.begin())); |
| 138 | assert(*std::next(m.begin(), 0) == 2); |
| 139 | assert(*std::next(m.begin(), 1) == 5); |
| 140 | assert(*std::next(m.begin(), 2) == 6); |
| 141 | assert(*std::next(m.begin(), 3) == 7); |
| 142 | |
| 143 | i = m.erase(std::next(m.cbegin(), 2)); |
| 144 | assert(m.size() == 3); |
| 145 | assert(i == std::next(m.begin(), 2)); |
| 146 | assert(*std::next(m.begin(), 0) == 2); |
| 147 | assert(*std::next(m.begin(), 1) == 5); |
| 148 | assert(*std::next(m.begin(), 2) == 7); |
| 149 | |
| 150 | i = m.erase(std::next(m.cbegin(), 2)); |
| 151 | assert(m.size() == 2); |
| 152 | assert(i == std::next(m.begin(), 2)); |
| 153 | assert(*std::next(m.begin(), 0) == 2); |
| 154 | assert(*std::next(m.begin(), 1) == 5); |
| 155 | |
| 156 | i = m.erase(std::next(m.cbegin(), 0)); |
| 157 | assert(m.size() == 1); |
| 158 | assert(i == std::next(m.begin(), 0)); |
| 159 | assert(*std::next(m.begin(), 0) == 5); |
| 160 | |
| 161 | i = m.erase(m.cbegin()); |
| 162 | assert(m.size() == 0); |
| 163 | assert(i == m.begin()); |
| 164 | assert(i == m.end()); |
| 165 | } |
| 166 | #endif |
| 167 | #if TEST_STD_VER >= 14 |
| 168 | { |
| 169 | // This is LWG #2059 |
| 170 | typedef TemplateConstructor T; |
| 171 | typedef std::multiset<T> C; |
| 172 | typedef C::iterator I; |
| 173 | |
| 174 | C c; |
| 175 | T a{0}; |
| 176 | I it = c.find(a); |
| 177 | if (it != c.end()) |
| 178 | c.erase(it); |
| 179 | } |
| 180 | #endif |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |