| 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 |
{ |
| 28 |
typedef std::unordered_map<int, std::string> C; |
| 29 |
typedef std::pair<int, std::string> P; |
| 30 |
P a[] = |
| 31 |
{ |
| 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, 1); |
| 42 |
C::iterator k = c.erase(i, i); |
| 43 |
assert(k == i); |
| 44 |
assert(c.size() == 4); |
| 45 |
assert(c.at(1) == "one"); |
| 46 |
assert(c.at(2) == "two"); |
| 47 |
assert(c.at(3) == "three"); |
| 48 |
assert(c.at(4) == "four"); |
| 49 |
|
| 50 |
k = c.erase(i, j); |
| 51 |
assert(c.size() == 3); |
| 52 |
assert(k == j); |
| 53 |
assert(c.at(1) == "one"); |
| 54 |
assert(c.at(3) == "three"); |
| 55 |
assert(c.at(4) == "four"); |
| 56 |
|
| 57 |
k = c.erase(c.cbegin(), c.cend()); |
| 58 |
assert(k == c.cend()); |
| 59 |
assert(c.size() == 0); |
| 60 |
assert(k == c.end()); |
| 61 |
} |
| 62 |
#if TEST_STD_VER >= 11 |
| 63 |
{ |
| 64 |
typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>, |
| 65 |
min_allocator<std::pair<const int, std::string>>> C; |
| 66 |
typedef std::pair<int, std::string> P; |
| 67 |
P a[] = |
| 68 |
{ |
| 69 |
P(1, "one"), |
| 70 |
P(2, "two"), |
| 71 |
P(3, "three"), |
| 72 |
P(4, "four"), |
| 73 |
P(1, "four"), |
| 74 |
P(2, "four"), |
| 75 |
}; |
| 76 |
C c(a, a + sizeof(a)/sizeof(a[0])); |
| 77 |
C::const_iterator i = c.find(2); |
| 78 |
C::const_iterator j = std::next(i, 1); |
| 79 |
C::iterator k = c.erase(i, i); |
| 80 |
assert(k == i); |
| 81 |
assert(c.size() == 4); |
| 82 |
assert(c.at(1) == "one"); |
| 83 |
assert(c.at(2) == "two"); |
| 84 |
assert(c.at(3) == "three"); |
| 85 |
assert(c.at(4) == "four"); |
| 86 |
|
| 87 |
k = c.erase(i, j); |
| 88 |
assert(c.size() == 3); |
| 89 |
assert(k == j); |
| 90 |
assert(c.at(1) == "one"); |
| 91 |
assert(c.at(3) == "three"); |
| 92 |
assert(c.at(4) == "four"); |
| 93 |
|
| 94 |
k = c.erase(c.cbegin(), c.cend()); |
| 95 |
assert(k == c.cend()); |
| 96 |
assert(c.size() == 0); |
| 97 |
assert(k == c.end()); |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |