llvm-project
261 строка · 9.3 Кб
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// 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::map<int, double> M;
33typedef std::pair<int, double> P;
34typedef M::iterator I;
35P ar[] =
36{
37P(1, 1.5),
38P(2, 2.5),
39P(3, 3.5),
40P(4, 4.5),
41P(5, 5.5),
42P(6, 6.5),
43P(7, 7.5),
44P(8, 8.5),
45};
46M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
47assert(m.size() == 8);
48I i = m.erase(std::next(m.cbegin(), 3));
49assert(m.size() == 7);
50assert(i == std::next(m.begin(), 3));
51assert(m.begin()->first == 1);
52assert(m.begin()->second == 1.5);
53assert(std::next(m.begin())->first == 2);
54assert(std::next(m.begin())->second == 2.5);
55assert(std::next(m.begin(), 2)->first == 3);
56assert(std::next(m.begin(), 2)->second == 3.5);
57assert(std::next(m.begin(), 3)->first == 5);
58assert(std::next(m.begin(), 3)->second == 5.5);
59assert(std::next(m.begin(), 4)->first == 6);
60assert(std::next(m.begin(), 4)->second == 6.5);
61assert(std::next(m.begin(), 5)->first == 7);
62assert(std::next(m.begin(), 5)->second == 7.5);
63assert(std::next(m.begin(), 6)->first == 8);
64assert(std::next(m.begin(), 6)->second == 8.5);
65
66i = m.erase(std::next(m.cbegin(), 0));
67assert(m.size() == 6);
68assert(i == m.begin());
69assert(m.begin()->first == 2);
70assert(m.begin()->second == 2.5);
71assert(std::next(m.begin())->first == 3);
72assert(std::next(m.begin())->second == 3.5);
73assert(std::next(m.begin(), 2)->first == 5);
74assert(std::next(m.begin(), 2)->second == 5.5);
75assert(std::next(m.begin(), 3)->first == 6);
76assert(std::next(m.begin(), 3)->second == 6.5);
77assert(std::next(m.begin(), 4)->first == 7);
78assert(std::next(m.begin(), 4)->second == 7.5);
79assert(std::next(m.begin(), 5)->first == 8);
80assert(std::next(m.begin(), 5)->second == 8.5);
81
82i = m.erase(std::next(m.cbegin(), 5));
83assert(m.size() == 5);
84assert(i == m.end());
85assert(m.begin()->first == 2);
86assert(m.begin()->second == 2.5);
87assert(std::next(m.begin())->first == 3);
88assert(std::next(m.begin())->second == 3.5);
89assert(std::next(m.begin(), 2)->first == 5);
90assert(std::next(m.begin(), 2)->second == 5.5);
91assert(std::next(m.begin(), 3)->first == 6);
92assert(std::next(m.begin(), 3)->second == 6.5);
93assert(std::next(m.begin(), 4)->first == 7);
94assert(std::next(m.begin(), 4)->second == 7.5);
95
96i = m.erase(std::next(m.cbegin(), 1));
97assert(m.size() == 4);
98assert(i == std::next(m.begin()));
99assert(m.begin()->first == 2);
100assert(m.begin()->second == 2.5);
101assert(std::next(m.begin())->first == 5);
102assert(std::next(m.begin())->second == 5.5);
103assert(std::next(m.begin(), 2)->first == 6);
104assert(std::next(m.begin(), 2)->second == 6.5);
105assert(std::next(m.begin(), 3)->first == 7);
106assert(std::next(m.begin(), 3)->second == 7.5);
107
108i = m.erase(std::next(m.cbegin(), 2));
109assert(m.size() == 3);
110assert(i == std::next(m.begin(), 2));
111assert(m.begin()->first == 2);
112assert(m.begin()->second == 2.5);
113assert(std::next(m.begin())->first == 5);
114assert(std::next(m.begin())->second == 5.5);
115assert(std::next(m.begin(), 2)->first == 7);
116assert(std::next(m.begin(), 2)->second == 7.5);
117
118i = m.erase(std::next(m.cbegin(), 2));
119assert(m.size() == 2);
120assert(i == std::next(m.begin(), 2));
121assert(m.begin()->first == 2);
122assert(m.begin()->second == 2.5);
123assert(std::next(m.begin())->first == 5);
124assert(std::next(m.begin())->second == 5.5);
125
126i = m.erase(std::next(m.cbegin(), 0));
127assert(m.size() == 1);
128assert(i == std::next(m.begin(), 0));
129assert(m.begin()->first == 5);
130assert(m.begin()->second == 5.5);
131
132i = m.erase(m.cbegin());
133assert(m.size() == 0);
134assert(i == m.begin());
135assert(i == m.end());
136}
137#if TEST_STD_VER >= 11
138{
139typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
140typedef std::pair<int, double> P;
141typedef M::iterator I;
142P ar[] =
143{
144P(1, 1.5),
145P(2, 2.5),
146P(3, 3.5),
147P(4, 4.5),
148P(5, 5.5),
149P(6, 6.5),
150P(7, 7.5),
151P(8, 8.5),
152};
153M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
154assert(m.size() == 8);
155I i = m.erase(std::next(m.cbegin(), 3));
156assert(m.size() == 7);
157assert(i == std::next(m.begin(), 3));
158assert(m.begin()->first == 1);
159assert(m.begin()->second == 1.5);
160assert(std::next(m.begin())->first == 2);
161assert(std::next(m.begin())->second == 2.5);
162assert(std::next(m.begin(), 2)->first == 3);
163assert(std::next(m.begin(), 2)->second == 3.5);
164assert(std::next(m.begin(), 3)->first == 5);
165assert(std::next(m.begin(), 3)->second == 5.5);
166assert(std::next(m.begin(), 4)->first == 6);
167assert(std::next(m.begin(), 4)->second == 6.5);
168assert(std::next(m.begin(), 5)->first == 7);
169assert(std::next(m.begin(), 5)->second == 7.5);
170assert(std::next(m.begin(), 6)->first == 8);
171assert(std::next(m.begin(), 6)->second == 8.5);
172
173i = m.erase(std::next(m.cbegin(), 0));
174assert(m.size() == 6);
175assert(i == m.begin());
176assert(m.begin()->first == 2);
177assert(m.begin()->second == 2.5);
178assert(std::next(m.begin())->first == 3);
179assert(std::next(m.begin())->second == 3.5);
180assert(std::next(m.begin(), 2)->first == 5);
181assert(std::next(m.begin(), 2)->second == 5.5);
182assert(std::next(m.begin(), 3)->first == 6);
183assert(std::next(m.begin(), 3)->second == 6.5);
184assert(std::next(m.begin(), 4)->first == 7);
185assert(std::next(m.begin(), 4)->second == 7.5);
186assert(std::next(m.begin(), 5)->first == 8);
187assert(std::next(m.begin(), 5)->second == 8.5);
188
189i = m.erase(std::next(m.cbegin(), 5));
190assert(m.size() == 5);
191assert(i == m.end());
192assert(m.begin()->first == 2);
193assert(m.begin()->second == 2.5);
194assert(std::next(m.begin())->first == 3);
195assert(std::next(m.begin())->second == 3.5);
196assert(std::next(m.begin(), 2)->first == 5);
197assert(std::next(m.begin(), 2)->second == 5.5);
198assert(std::next(m.begin(), 3)->first == 6);
199assert(std::next(m.begin(), 3)->second == 6.5);
200assert(std::next(m.begin(), 4)->first == 7);
201assert(std::next(m.begin(), 4)->second == 7.5);
202
203i = m.erase(std::next(m.cbegin(), 1));
204assert(m.size() == 4);
205assert(i == std::next(m.begin()));
206assert(m.begin()->first == 2);
207assert(m.begin()->second == 2.5);
208assert(std::next(m.begin())->first == 5);
209assert(std::next(m.begin())->second == 5.5);
210assert(std::next(m.begin(), 2)->first == 6);
211assert(std::next(m.begin(), 2)->second == 6.5);
212assert(std::next(m.begin(), 3)->first == 7);
213assert(std::next(m.begin(), 3)->second == 7.5);
214
215i = m.erase(std::next(m.cbegin(), 2));
216assert(m.size() == 3);
217assert(i == std::next(m.begin(), 2));
218assert(m.begin()->first == 2);
219assert(m.begin()->second == 2.5);
220assert(std::next(m.begin())->first == 5);
221assert(std::next(m.begin())->second == 5.5);
222assert(std::next(m.begin(), 2)->first == 7);
223assert(std::next(m.begin(), 2)->second == 7.5);
224
225i = m.erase(std::next(m.cbegin(), 2));
226assert(m.size() == 2);
227assert(i == std::next(m.begin(), 2));
228assert(m.begin()->first == 2);
229assert(m.begin()->second == 2.5);
230assert(std::next(m.begin())->first == 5);
231assert(std::next(m.begin())->second == 5.5);
232
233i = m.erase(std::next(m.cbegin(), 0));
234assert(m.size() == 1);
235assert(i == std::next(m.begin(), 0));
236assert(m.begin()->first == 5);
237assert(m.begin()->second == 5.5);
238
239i = m.erase(m.cbegin());
240assert(m.size() == 0);
241assert(i == m.begin());
242assert(i == m.end());
243}
244#endif
245#if TEST_STD_VER >= 14
246{
247// This is LWG #2059
248typedef TemplateConstructor T;
249typedef std::map<T, int> C;
250typedef C::iterator I;
251
252C c;
253T a{0};
254I it = c.find(a);
255if (it != c.end())
256c.erase(it);
257}
258#endif
259
260return 0;
261}
262