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// size_type erase(const key_type& k);
16
17#include <unordered_map>
18#include <string>
19#include <cassert>
20
21#include "test_macros.h"
22#include "min_allocator.h"
23
24#if TEST_STD_VER >= 11
25template <typename Unordered>
26bool only_deletions ( const Unordered &whole, const Unordered &part ) {
27 typename Unordered::const_iterator w = whole.begin();
28 typename Unordered::const_iterator p = part.begin();
29
30 while ( w != whole.end () && p != part.end()) {
31 if ( *w == *p )
32 p++;
33 w++;
34 }
35
36 return p == part.end();
37}
38#endif
39
40
41int main(int, char**)
42{
43 {
44 typedef std::unordered_map<int, std::string> C;
45 typedef std::pair<int, std::string> P;
46 P a[] =
47 {
48 P(1, "one"),
49 P(2, "two"),
50 P(3, "three"),
51 P(4, "four"),
52 P(1, "four"),
53 P(2, "four"),
54 };
55 C c(a, a + sizeof(a)/sizeof(a[0]));
56 assert(c.erase(5) == 0);
57 assert(c.size() == 4);
58 assert(c.at(1) == "one");
59 assert(c.at(2) == "two");
60 assert(c.at(3) == "three");
61 assert(c.at(4) == "four");
62
63 assert(c.erase(2) == 1);
64 assert(c.size() == 3);
65 assert(c.at(1) == "one");
66 assert(c.at(3) == "three");
67 assert(c.at(4) == "four");
68
69 assert(c.erase(2) == 0);
70 assert(c.size() == 3);
71 assert(c.at(1) == "one");
72 assert(c.at(3) == "three");
73 assert(c.at(4) == "four");
74
75 assert(c.erase(4) == 1);
76 assert(c.size() == 2);
77 assert(c.at(1) == "one");
78 assert(c.at(3) == "three");
79
80 assert(c.erase(4) == 0);
81 assert(c.size() == 2);
82 assert(c.at(1) == "one");
83 assert(c.at(3) == "three");
84
85 assert(c.erase(1) == 1);
86 assert(c.size() == 1);
87 assert(c.at(3) == "three");
88
89 assert(c.erase(1) == 0);
90 assert(c.size() == 1);
91 assert(c.at(3) == "three");
92
93 assert(c.erase(3) == 1);
94 assert(c.size() == 0);
95
96 assert(c.erase(3) == 0);
97 assert(c.size() == 0);
98 }
99#if TEST_STD_VER >= 11
100 {
101 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
102 min_allocator<std::pair<const int, std::string>>> C;
103 typedef std::pair<int, std::string> P;
104 P a[] =
105 {
106 P(1, "one"),
107 P(2, "two"),
108 P(3, "three"),
109 P(4, "four"),
110 P(1, "four"),
111 P(2, "four"),
112 };
113 C c(a, a + sizeof(a)/sizeof(a[0]));
114 assert(c.erase(5) == 0);
115 assert(c.size() == 4);
116 assert(c.at(1) == "one");
117 assert(c.at(2) == "two");
118 assert(c.at(3) == "three");
119 assert(c.at(4) == "four");
120
121 assert(c.erase(2) == 1);
122 assert(c.size() == 3);
123 assert(c.at(1) == "one");
124 assert(c.at(3) == "three");
125 assert(c.at(4) == "four");
126
127 assert(c.erase(2) == 0);
128 assert(c.size() == 3);
129 assert(c.at(1) == "one");
130 assert(c.at(3) == "three");
131 assert(c.at(4) == "four");
132
133 assert(c.erase(4) == 1);
134 assert(c.size() == 2);
135 assert(c.at(1) == "one");
136 assert(c.at(3) == "three");
137
138 assert(c.erase(4) == 0);
139 assert(c.size() == 2);
140 assert(c.at(1) == "one");
141 assert(c.at(3) == "three");
142
143 assert(c.erase(1) == 1);
144 assert(c.size() == 1);
145 assert(c.at(3) == "three");
146
147 assert(c.erase(1) == 0);
148 assert(c.size() == 1);
149 assert(c.at(3) == "three");
150
151 assert(c.erase(3) == 1);
152 assert(c.size() == 0);
153
154 assert(c.erase(3) == 0);
155 assert(c.size() == 0);
156 }
157 {
158 typedef std::unordered_map<int, int> C;
159 C m, m2;
160 for ( int i = 0; i < 10; ++i ) {
161 m[i] = i;
162 m2[i] = i;
163 }
164
165 C::iterator i = m2.begin();
166 int ctr = 0;
167 while (i != m2.end()) {
168 if (ctr++ % 2 == 0)
169 m2.erase(i++);
170 else
171 ++i;
172 }
173
174 assert (only_deletions (m, m2));
175 }
176#endif
177
178 return 0;
179}
180

source code of libcxx/test/std/containers/unord/unord.map/unord.map.modifiers/erase_key.pass.cpp