| 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 set |
| 12 | |
| 13 | // iterator erase(const_iterator first, const_iterator last); |
| 14 | |
| 15 | #include <set> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "min_allocator.h" |
| 20 | |
| 21 | int main(int, char**) { |
| 22 | { |
| 23 | typedef std::set<int> M; |
| 24 | typedef int V; |
| 25 | typedef M::iterator I; |
| 26 | V ar[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 27 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 28 | assert(m.size() == 8); |
| 29 | I i = m.erase(first: std::next(x: m.cbegin(), n: 5), last: std::next(x: m.cbegin(), n: 5)); |
| 30 | assert(m.size() == 8); |
| 31 | assert(i == std::next(m.begin(), 5)); |
| 32 | assert(*std::next(m.begin(), 0) == 1); |
| 33 | assert(*std::next(m.begin(), 1) == 2); |
| 34 | assert(*std::next(m.begin(), 2) == 3); |
| 35 | assert(*std::next(m.begin(), 3) == 4); |
| 36 | assert(*std::next(m.begin(), 4) == 5); |
| 37 | assert(*std::next(m.begin(), 5) == 6); |
| 38 | assert(*std::next(m.begin(), 6) == 7); |
| 39 | assert(*std::next(m.begin(), 7) == 8); |
| 40 | |
| 41 | i = m.erase(first: std::next(x: m.cbegin(), n: 3), last: std::next(x: m.cbegin(), n: 4)); |
| 42 | assert(m.size() == 7); |
| 43 | assert(i == std::next(m.begin(), 3)); |
| 44 | assert(*std::next(m.begin(), 0) == 1); |
| 45 | assert(*std::next(m.begin(), 1) == 2); |
| 46 | assert(*std::next(m.begin(), 2) == 3); |
| 47 | assert(*std::next(m.begin(), 3) == 5); |
| 48 | assert(*std::next(m.begin(), 4) == 6); |
| 49 | assert(*std::next(m.begin(), 5) == 7); |
| 50 | assert(*std::next(m.begin(), 6) == 8); |
| 51 | |
| 52 | i = m.erase(first: std::next(x: m.cbegin(), n: 2), last: std::next(x: m.cbegin(), n: 5)); |
| 53 | assert(m.size() == 4); |
| 54 | assert(i == std::next(m.begin(), 2)); |
| 55 | assert(*std::next(m.begin(), 0) == 1); |
| 56 | assert(*std::next(m.begin(), 1) == 2); |
| 57 | assert(*std::next(m.begin(), 2) == 7); |
| 58 | assert(*std::next(m.begin(), 3) == 8); |
| 59 | |
| 60 | i = m.erase(first: std::next(x: m.cbegin(), n: 0), last: std::next(x: m.cbegin(), n: 2)); |
| 61 | assert(m.size() == 2); |
| 62 | assert(i == std::next(m.begin(), 0)); |
| 63 | assert(*std::next(m.begin(), 0) == 7); |
| 64 | assert(*std::next(m.begin(), 1) == 8); |
| 65 | |
| 66 | i = m.erase(first: m.cbegin(), last: m.cend()); |
| 67 | assert(m.size() == 0); |
| 68 | assert(i == m.end()); |
| 69 | } |
| 70 | #if TEST_STD_VER >= 11 |
| 71 | { |
| 72 | typedef std::set<int, std::less<int>, min_allocator<int>> M; |
| 73 | typedef int V; |
| 74 | typedef M::iterator I; |
| 75 | V ar[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 76 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 77 | assert(m.size() == 8); |
| 78 | I i = m.erase(std::next(m.cbegin(), 5), std::next(m.cbegin(), 5)); |
| 79 | assert(m.size() == 8); |
| 80 | assert(i == std::next(m.begin(), 5)); |
| 81 | assert(*std::next(m.begin(), 0) == 1); |
| 82 | assert(*std::next(m.begin(), 1) == 2); |
| 83 | assert(*std::next(m.begin(), 2) == 3); |
| 84 | assert(*std::next(m.begin(), 3) == 4); |
| 85 | assert(*std::next(m.begin(), 4) == 5); |
| 86 | assert(*std::next(m.begin(), 5) == 6); |
| 87 | assert(*std::next(m.begin(), 6) == 7); |
| 88 | assert(*std::next(m.begin(), 7) == 8); |
| 89 | |
| 90 | i = m.erase(std::next(m.cbegin(), 3), std::next(m.cbegin(), 4)); |
| 91 | assert(m.size() == 7); |
| 92 | assert(i == std::next(m.begin(), 3)); |
| 93 | assert(*std::next(m.begin(), 0) == 1); |
| 94 | assert(*std::next(m.begin(), 1) == 2); |
| 95 | assert(*std::next(m.begin(), 2) == 3); |
| 96 | assert(*std::next(m.begin(), 3) == 5); |
| 97 | assert(*std::next(m.begin(), 4) == 6); |
| 98 | assert(*std::next(m.begin(), 5) == 7); |
| 99 | assert(*std::next(m.begin(), 6) == 8); |
| 100 | |
| 101 | i = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 5)); |
| 102 | assert(m.size() == 4); |
| 103 | assert(i == std::next(m.begin(), 2)); |
| 104 | assert(*std::next(m.begin(), 0) == 1); |
| 105 | assert(*std::next(m.begin(), 1) == 2); |
| 106 | assert(*std::next(m.begin(), 2) == 7); |
| 107 | assert(*std::next(m.begin(), 3) == 8); |
| 108 | |
| 109 | i = m.erase(std::next(m.cbegin(), 0), std::next(m.cbegin(), 2)); |
| 110 | assert(m.size() == 2); |
| 111 | assert(i == std::next(m.begin(), 0)); |
| 112 | assert(*std::next(m.begin(), 0) == 7); |
| 113 | assert(*std::next(m.begin(), 1) == 8); |
| 114 | |
| 115 | i = m.erase(m.cbegin(), m.cend()); |
| 116 | assert(m.size() == 0); |
| 117 | assert(i == m.end()); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |