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