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// <map>
10
11// class map
12
13// size_type erase(const key_type& k);
14
15#include <map>
16#include <cassert>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20
21int main(int, char**)
22{
23 {
24 typedef std::map<int, double> M;
25 typedef std::pair<int, double> P;
26 typedef M::size_type R;
27 P ar[] =
28 {
29 P(1, 1.5),
30 P(2, 2.5),
31 P(3, 3.5),
32 P(4, 4.5),
33 P(5, 5.5),
34 P(6, 6.5),
35 P(7, 7.5),
36 P(8, 8.5),
37 };
38 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
39 assert(m.size() == 8);
40 R s = m.erase(x: 9);
41 assert(s == 0);
42 assert(m.size() == 8);
43 assert(m.begin()->first == 1);
44 assert(m.begin()->second == 1.5);
45 assert(std::next(m.begin())->first == 2);
46 assert(std::next(m.begin())->second == 2.5);
47 assert(std::next(m.begin(), 2)->first == 3);
48 assert(std::next(m.begin(), 2)->second == 3.5);
49 assert(std::next(m.begin(), 3)->first == 4);
50 assert(std::next(m.begin(), 3)->second == 4.5);
51 assert(std::next(m.begin(), 4)->first == 5);
52 assert(std::next(m.begin(), 4)->second == 5.5);
53 assert(std::next(m.begin(), 5)->first == 6);
54 assert(std::next(m.begin(), 5)->second == 6.5);
55 assert(std::next(m.begin(), 6)->first == 7);
56 assert(std::next(m.begin(), 6)->second == 7.5);
57 assert(std::next(m.begin(), 7)->first == 8);
58 assert(std::next(m.begin(), 7)->second == 8.5);
59
60 s = m.erase(x: 4);
61 assert(m.size() == 7);
62 assert(s == 1);
63 assert(m.begin()->first == 1);
64 assert(m.begin()->second == 1.5);
65 assert(std::next(m.begin())->first == 2);
66 assert(std::next(m.begin())->second == 2.5);
67 assert(std::next(m.begin(), 2)->first == 3);
68 assert(std::next(m.begin(), 2)->second == 3.5);
69 assert(std::next(m.begin(), 3)->first == 5);
70 assert(std::next(m.begin(), 3)->second == 5.5);
71 assert(std::next(m.begin(), 4)->first == 6);
72 assert(std::next(m.begin(), 4)->second == 6.5);
73 assert(std::next(m.begin(), 5)->first == 7);
74 assert(std::next(m.begin(), 5)->second == 7.5);
75 assert(std::next(m.begin(), 6)->first == 8);
76 assert(std::next(m.begin(), 6)->second == 8.5);
77
78 s = m.erase(x: 1);
79 assert(m.size() == 6);
80 assert(s == 1);
81 assert(m.begin()->first == 2);
82 assert(m.begin()->second == 2.5);
83 assert(std::next(m.begin())->first == 3);
84 assert(std::next(m.begin())->second == 3.5);
85 assert(std::next(m.begin(), 2)->first == 5);
86 assert(std::next(m.begin(), 2)->second == 5.5);
87 assert(std::next(m.begin(), 3)->first == 6);
88 assert(std::next(m.begin(), 3)->second == 6.5);
89 assert(std::next(m.begin(), 4)->first == 7);
90 assert(std::next(m.begin(), 4)->second == 7.5);
91 assert(std::next(m.begin(), 5)->first == 8);
92 assert(std::next(m.begin(), 5)->second == 8.5);
93
94 s = m.erase(x: 8);
95 assert(m.size() == 5);
96 assert(s == 1);
97 assert(m.begin()->first == 2);
98 assert(m.begin()->second == 2.5);
99 assert(std::next(m.begin())->first == 3);
100 assert(std::next(m.begin())->second == 3.5);
101 assert(std::next(m.begin(), 2)->first == 5);
102 assert(std::next(m.begin(), 2)->second == 5.5);
103 assert(std::next(m.begin(), 3)->first == 6);
104 assert(std::next(m.begin(), 3)->second == 6.5);
105 assert(std::next(m.begin(), 4)->first == 7);
106 assert(std::next(m.begin(), 4)->second == 7.5);
107
108 s = m.erase(x: 3);
109 assert(m.size() == 4);
110 assert(s == 1);
111 assert(m.begin()->first == 2);
112 assert(m.begin()->second == 2.5);
113 assert(std::next(m.begin())->first == 5);
114 assert(std::next(m.begin())->second == 5.5);
115 assert(std::next(m.begin(), 2)->first == 6);
116 assert(std::next(m.begin(), 2)->second == 6.5);
117 assert(std::next(m.begin(), 3)->first == 7);
118 assert(std::next(m.begin(), 3)->second == 7.5);
119
120 s = m.erase(x: 6);
121 assert(m.size() == 3);
122 assert(s == 1);
123 assert(m.begin()->first == 2);
124 assert(m.begin()->second == 2.5);
125 assert(std::next(m.begin())->first == 5);
126 assert(std::next(m.begin())->second == 5.5);
127 assert(std::next(m.begin(), 2)->first == 7);
128 assert(std::next(m.begin(), 2)->second == 7.5);
129
130 s = m.erase(x: 7);
131 assert(m.size() == 2);
132 assert(s == 1);
133 assert(m.begin()->first == 2);
134 assert(m.begin()->second == 2.5);
135 assert(std::next(m.begin())->first == 5);
136 assert(std::next(m.begin())->second == 5.5);
137
138 s = m.erase(x: 2);
139 assert(m.size() == 1);
140 assert(s == 1);
141 assert(m.begin()->first == 5);
142 assert(m.begin()->second == 5.5);
143
144 s = m.erase(x: 5);
145 assert(m.size() == 0);
146 assert(s == 1);
147 }
148#if TEST_STD_VER >= 11
149 {
150 typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
151 typedef std::pair<int, double> P;
152 typedef M::size_type R;
153 P ar[] =
154 {
155 P(1, 1.5),
156 P(2, 2.5),
157 P(3, 3.5),
158 P(4, 4.5),
159 P(5, 5.5),
160 P(6, 6.5),
161 P(7, 7.5),
162 P(8, 8.5),
163 };
164 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
165 assert(m.size() == 8);
166 R s = m.erase(9);
167 assert(s == 0);
168 assert(m.size() == 8);
169 assert(m.begin()->first == 1);
170 assert(m.begin()->second == 1.5);
171 assert(std::next(m.begin())->first == 2);
172 assert(std::next(m.begin())->second == 2.5);
173 assert(std::next(m.begin(), 2)->first == 3);
174 assert(std::next(m.begin(), 2)->second == 3.5);
175 assert(std::next(m.begin(), 3)->first == 4);
176 assert(std::next(m.begin(), 3)->second == 4.5);
177 assert(std::next(m.begin(), 4)->first == 5);
178 assert(std::next(m.begin(), 4)->second == 5.5);
179 assert(std::next(m.begin(), 5)->first == 6);
180 assert(std::next(m.begin(), 5)->second == 6.5);
181 assert(std::next(m.begin(), 6)->first == 7);
182 assert(std::next(m.begin(), 6)->second == 7.5);
183 assert(std::next(m.begin(), 7)->first == 8);
184 assert(std::next(m.begin(), 7)->second == 8.5);
185
186 s = m.erase(4);
187 assert(m.size() == 7);
188 assert(s == 1);
189 assert(m.begin()->first == 1);
190 assert(m.begin()->second == 1.5);
191 assert(std::next(m.begin())->first == 2);
192 assert(std::next(m.begin())->second == 2.5);
193 assert(std::next(m.begin(), 2)->first == 3);
194 assert(std::next(m.begin(), 2)->second == 3.5);
195 assert(std::next(m.begin(), 3)->first == 5);
196 assert(std::next(m.begin(), 3)->second == 5.5);
197 assert(std::next(m.begin(), 4)->first == 6);
198 assert(std::next(m.begin(), 4)->second == 6.5);
199 assert(std::next(m.begin(), 5)->first == 7);
200 assert(std::next(m.begin(), 5)->second == 7.5);
201 assert(std::next(m.begin(), 6)->first == 8);
202 assert(std::next(m.begin(), 6)->second == 8.5);
203
204 s = m.erase(1);
205 assert(m.size() == 6);
206 assert(s == 1);
207 assert(m.begin()->first == 2);
208 assert(m.begin()->second == 2.5);
209 assert(std::next(m.begin())->first == 3);
210 assert(std::next(m.begin())->second == 3.5);
211 assert(std::next(m.begin(), 2)->first == 5);
212 assert(std::next(m.begin(), 2)->second == 5.5);
213 assert(std::next(m.begin(), 3)->first == 6);
214 assert(std::next(m.begin(), 3)->second == 6.5);
215 assert(std::next(m.begin(), 4)->first == 7);
216 assert(std::next(m.begin(), 4)->second == 7.5);
217 assert(std::next(m.begin(), 5)->first == 8);
218 assert(std::next(m.begin(), 5)->second == 8.5);
219
220 s = m.erase(8);
221 assert(m.size() == 5);
222 assert(s == 1);
223 assert(m.begin()->first == 2);
224 assert(m.begin()->second == 2.5);
225 assert(std::next(m.begin())->first == 3);
226 assert(std::next(m.begin())->second == 3.5);
227 assert(std::next(m.begin(), 2)->first == 5);
228 assert(std::next(m.begin(), 2)->second == 5.5);
229 assert(std::next(m.begin(), 3)->first == 6);
230 assert(std::next(m.begin(), 3)->second == 6.5);
231 assert(std::next(m.begin(), 4)->first == 7);
232 assert(std::next(m.begin(), 4)->second == 7.5);
233
234 s = m.erase(3);
235 assert(m.size() == 4);
236 assert(s == 1);
237 assert(m.begin()->first == 2);
238 assert(m.begin()->second == 2.5);
239 assert(std::next(m.begin())->first == 5);
240 assert(std::next(m.begin())->second == 5.5);
241 assert(std::next(m.begin(), 2)->first == 6);
242 assert(std::next(m.begin(), 2)->second == 6.5);
243 assert(std::next(m.begin(), 3)->first == 7);
244 assert(std::next(m.begin(), 3)->second == 7.5);
245
246 s = m.erase(6);
247 assert(m.size() == 3);
248 assert(s == 1);
249 assert(m.begin()->first == 2);
250 assert(m.begin()->second == 2.5);
251 assert(std::next(m.begin())->first == 5);
252 assert(std::next(m.begin())->second == 5.5);
253 assert(std::next(m.begin(), 2)->first == 7);
254 assert(std::next(m.begin(), 2)->second == 7.5);
255
256 s = m.erase(7);
257 assert(m.size() == 2);
258 assert(s == 1);
259 assert(m.begin()->first == 2);
260 assert(m.begin()->second == 2.5);
261 assert(std::next(m.begin())->first == 5);
262 assert(std::next(m.begin())->second == 5.5);
263
264 s = m.erase(2);
265 assert(m.size() == 1);
266 assert(s == 1);
267 assert(m.begin()->first == 5);
268 assert(m.begin()->second == 5.5);
269
270 s = m.erase(5);
271 assert(m.size() == 0);
272 assert(s == 1);
273 }
274#endif
275
276 return 0;
277}
278

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