| 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 | // <unordered_map> |
| 10 | |
| 11 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 12 | // class Alloc = allocator<pair<const Key, T>>> |
| 13 | // class unordered_multimap |
| 14 | |
| 15 | // iterator erase(const_iterator first, const_iterator last) |
| 16 | |
| 17 | #include <unordered_map> |
| 18 | #include <string> |
| 19 | #include <set> |
| 20 | #include <cassert> |
| 21 | #include <cstddef> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | #include "../../../check_consecutive.h" |
| 25 | #include "min_allocator.h" |
| 26 | |
| 27 | int main(int, char**) { |
| 28 | { |
| 29 | typedef std::unordered_multimap<int, std::string> C; |
| 30 | typedef std::pair<int, std::string> P; |
| 31 | P a[] = { |
| 32 | P(1, "one" ), |
| 33 | P(2, "two" ), |
| 34 | P(3, "three" ), |
| 35 | P(4, "four" ), |
| 36 | P(1, "four" ), |
| 37 | P(2, "four" ), |
| 38 | }; |
| 39 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 40 | C::const_iterator i = c.find(2); |
| 41 | C::const_iterator j = std::next(i, 2); |
| 42 | C::iterator k = c.erase(i, i); |
| 43 | assert(k == i); |
| 44 | assert(c.size() == 6); |
| 45 | typedef std::pair<C::iterator, C::iterator> Eq; |
| 46 | Eq eq = c.equal_range(1); |
| 47 | assert(std::distance(eq.first, eq.second) == 2); |
| 48 | std::multiset<std::string> s; |
| 49 | s.insert("one" ); |
| 50 | s.insert("four" ); |
| 51 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 52 | eq = c.equal_range(2); |
| 53 | assert(std::distance(eq.first, eq.second) == 2); |
| 54 | s.insert("two" ); |
| 55 | s.insert("four" ); |
| 56 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 57 | eq = c.equal_range(3); |
| 58 | assert(std::distance(eq.first, eq.second) == 1); |
| 59 | k = eq.first; |
| 60 | assert(k->first == 3); |
| 61 | assert(k->second == "three" ); |
| 62 | eq = c.equal_range(4); |
| 63 | assert(std::distance(eq.first, eq.second) == 1); |
| 64 | k = eq.first; |
| 65 | assert(k->first == 4); |
| 66 | assert(k->second == "four" ); |
| 67 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 68 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 69 | |
| 70 | k = c.erase(i, j); |
| 71 | assert(c.size() == 4); |
| 72 | eq = c.equal_range(1); |
| 73 | assert(std::distance(eq.first, eq.second) == 2); |
| 74 | s.insert("one" ); |
| 75 | s.insert("four" ); |
| 76 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 77 | eq = c.equal_range(3); |
| 78 | assert(std::distance(eq.first, eq.second) == 1); |
| 79 | k = eq.first; |
| 80 | assert(k->first == 3); |
| 81 | assert(k->second == "three" ); |
| 82 | eq = c.equal_range(4); |
| 83 | assert(std::distance(eq.first, eq.second) == 1); |
| 84 | k = eq.first; |
| 85 | assert(k->first == 4); |
| 86 | assert(k->second == "four" ); |
| 87 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 88 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 89 | |
| 90 | k = c.erase(c.cbegin(), c.cend()); |
| 91 | assert(c.size() == 0); |
| 92 | assert(k == c.end()); |
| 93 | } |
| 94 | #if TEST_STD_VER >= 11 |
| 95 | { |
| 96 | typedef std::unordered_multimap<int, |
| 97 | std::string, |
| 98 | std::hash<int>, |
| 99 | std::equal_to<int>, |
| 100 | min_allocator<std::pair<const int, std::string>>> |
| 101 | C; |
| 102 | typedef std::pair<int, std::string> P; |
| 103 | P a[] = { |
| 104 | P(1, "one" ), |
| 105 | P(2, "two" ), |
| 106 | P(3, "three" ), |
| 107 | P(4, "four" ), |
| 108 | P(1, "four" ), |
| 109 | P(2, "four" ), |
| 110 | }; |
| 111 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 112 | C::const_iterator i = c.find(2); |
| 113 | C::const_iterator j = std::next(i, 2); |
| 114 | C::iterator k = c.erase(i, i); |
| 115 | assert(k == i); |
| 116 | assert(c.size() == 6); |
| 117 | typedef std::pair<C::iterator, C::iterator> Eq; |
| 118 | Eq eq = c.equal_range(1); |
| 119 | assert(std::distance(eq.first, eq.second) == 2); |
| 120 | std::multiset<std::string> s; |
| 121 | s.insert("one" ); |
| 122 | s.insert("four" ); |
| 123 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 124 | eq = c.equal_range(2); |
| 125 | assert(std::distance(eq.first, eq.second) == 2); |
| 126 | s.insert("two" ); |
| 127 | s.insert("four" ); |
| 128 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 129 | eq = c.equal_range(3); |
| 130 | assert(std::distance(eq.first, eq.second) == 1); |
| 131 | k = eq.first; |
| 132 | assert(k->first == 3); |
| 133 | assert(k->second == "three" ); |
| 134 | eq = c.equal_range(4); |
| 135 | assert(std::distance(eq.first, eq.second) == 1); |
| 136 | k = eq.first; |
| 137 | assert(k->first == 4); |
| 138 | assert(k->second == "four" ); |
| 139 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 140 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 141 | |
| 142 | k = c.erase(i, j); |
| 143 | assert(c.size() == 4); |
| 144 | eq = c.equal_range(1); |
| 145 | assert(std::distance(eq.first, eq.second) == 2); |
| 146 | s.insert("one" ); |
| 147 | s.insert("four" ); |
| 148 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 149 | eq = c.equal_range(3); |
| 150 | assert(std::distance(eq.first, eq.second) == 1); |
| 151 | k = eq.first; |
| 152 | assert(k->first == 3); |
| 153 | assert(k->second == "three" ); |
| 154 | eq = c.equal_range(4); |
| 155 | assert(std::distance(eq.first, eq.second) == 1); |
| 156 | k = eq.first; |
| 157 | assert(k->first == 4); |
| 158 | assert(k->second == "four" ); |
| 159 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 160 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 161 | |
| 162 | k = c.erase(c.cbegin(), c.cend()); |
| 163 | assert(c.size() == 0); |
| 164 | assert(k == c.end()); |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |