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