| 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_map |
| 14 |
|
| 15 |
// iterator erase(const_iterator first, const_iterator last) |
| 16 |
|
| 17 |
#include <unordered_map> |
| 18 |
#include <string> |
| 19 |
#include <cassert> |
| 20 |
#include <iterator> |
| 21 |
|
| 22 |
#include "test_macros.h" |
| 23 |
#include "min_allocator.h" |
| 24 |
|
| 25 |
int main(int, char**) { |
| 26 |
{ |
| 27 |
typedef std::unordered_map<int, std::string> C; |
| 28 |
typedef std::pair<int, std::string> P; |
| 29 |
P a[] = { |
| 30 |
P(1, "one"), |
| 31 |
P(2, "two"), |
| 32 |
P(3, "three"), |
| 33 |
P(4, "four"), |
| 34 |
P(1, "four"), |
| 35 |
P(2, "four"), |
| 36 |
}; |
| 37 |
C c(a, a + sizeof(a) / sizeof(a[0])); |
| 38 |
C::const_iterator i = c.find(2); |
| 39 |
C::const_iterator j = std::next(i, 1); |
| 40 |
C::iterator k = c.erase(i, i); |
| 41 |
assert(k == i); |
| 42 |
assert(c.size() == 4); |
| 43 |
assert(c.at(1) == "one"); |
| 44 |
assert(c.at(2) == "two"); |
| 45 |
assert(c.at(3) == "three"); |
| 46 |
assert(c.at(4) == "four"); |
| 47 |
|
| 48 |
k = c.erase(i, j); |
| 49 |
assert(c.size() == 3); |
| 50 |
assert(k == j); |
| 51 |
assert(c.at(1) == "one"); |
| 52 |
assert(c.at(3) == "three"); |
| 53 |
assert(c.at(4) == "four"); |
| 54 |
|
| 55 |
k = c.erase(c.cbegin(), c.cend()); |
| 56 |
assert(k == c.cend()); |
| 57 |
assert(c.size() == 0); |
| 58 |
assert(k == c.end()); |
| 59 |
} |
| 60 |
{ // Make sure that we're properly updating the bucket list when we're erasing to the end |
| 61 |
std::unordered_map<int, int> m; |
| 62 |
m.insert(std::make_pair(1, 1)); |
| 63 |
m.insert(std::make_pair(2, 2)); |
| 64 |
|
| 65 |
{ |
| 66 |
auto pair = m.equal_range(1); |
| 67 |
assert(pair.first != pair.second); |
| 68 |
m.erase(pair.first, pair.second); |
| 69 |
} |
| 70 |
|
| 71 |
{ |
| 72 |
auto pair = m.equal_range(2); |
| 73 |
assert(pair.first != pair.second); |
| 74 |
m.erase(pair.first, pair.second); |
| 75 |
} |
| 76 |
|
| 77 |
m.insert(std::make_pair(3, 3)); |
| 78 |
assert(m.size() == 1); |
| 79 |
assert(*m.begin() == std::make_pair(3, 3)); |
| 80 |
assert(++m.begin() == m.end()); |
| 81 |
} |
| 82 |
#if TEST_STD_VER >= 11 |
| 83 |
{ |
| 84 |
typedef std::unordered_map<int, |
| 85 |
std::string, |
| 86 |
std::hash<int>, |
| 87 |
std::equal_to<int>, |
| 88 |
min_allocator<std::pair<const int, std::string>>> |
| 89 |
C; |
| 90 |
typedef std::pair<int, std::string> P; |
| 91 |
P a[] = { |
| 92 |
P(1, "one"), |
| 93 |
P(2, "two"), |
| 94 |
P(3, "three"), |
| 95 |
P(4, "four"), |
| 96 |
P(1, "four"), |
| 97 |
P(2, "four"), |
| 98 |
}; |
| 99 |
C c(a, a + sizeof(a) / sizeof(a[0])); |
| 100 |
C::const_iterator i = c.find(2); |
| 101 |
C::const_iterator j = std::next(i, 1); |
| 102 |
C::iterator k = c.erase(i, i); |
| 103 |
assert(k == i); |
| 104 |
assert(c.size() == 4); |
| 105 |
assert(c.at(1) == "one"); |
| 106 |
assert(c.at(2) == "two"); |
| 107 |
assert(c.at(3) == "three"); |
| 108 |
assert(c.at(4) == "four"); |
| 109 |
|
| 110 |
k = c.erase(i, j); |
| 111 |
assert(c.size() == 3); |
| 112 |
assert(k == j); |
| 113 |
assert(c.at(1) == "one"); |
| 114 |
assert(c.at(3) == "three"); |
| 115 |
assert(c.at(4) == "four"); |
| 116 |
|
| 117 |
k = c.erase(c.cbegin(), c.cend()); |
| 118 |
assert(k == c.cend()); |
| 119 |
assert(c.size() == 0); |
| 120 |
assert(k == c.end()); |
| 121 |
} |
| 122 |
#endif |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |