llvm-project
205 строк · 6.2 Кб
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// <set>
10
11// class set
12
13// iterator erase(const_iterator position);
14
15#include <set>
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::set<int> M;
33typedef int V;
34typedef M::iterator I;
35V ar[] =
36{
371,
382,
393,
404,
415,
426,
437,
448
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(*std::next(m.begin(), 0) == 1);
52assert(*std::next(m.begin(), 1) == 2);
53assert(*std::next(m.begin(), 2) == 3);
54assert(*std::next(m.begin(), 3) == 5);
55assert(*std::next(m.begin(), 4) == 6);
56assert(*std::next(m.begin(), 5) == 7);
57assert(*std::next(m.begin(), 6) == 8);
58
59i = m.erase(std::next(m.cbegin(), 0));
60assert(m.size() == 6);
61assert(i == m.begin());
62assert(*std::next(m.begin(), 0) == 2);
63assert(*std::next(m.begin(), 1) == 3);
64assert(*std::next(m.begin(), 2) == 5);
65assert(*std::next(m.begin(), 3) == 6);
66assert(*std::next(m.begin(), 4) == 7);
67assert(*std::next(m.begin(), 5) == 8);
68
69i = m.erase(std::next(m.cbegin(), 5));
70assert(m.size() == 5);
71assert(i == m.end());
72assert(*std::next(m.begin(), 0) == 2);
73assert(*std::next(m.begin(), 1) == 3);
74assert(*std::next(m.begin(), 2) == 5);
75assert(*std::next(m.begin(), 3) == 6);
76assert(*std::next(m.begin(), 4) == 7);
77
78i = m.erase(std::next(m.cbegin(), 1));
79assert(m.size() == 4);
80assert(i == std::next(m.begin()));
81assert(*std::next(m.begin(), 0) == 2);
82assert(*std::next(m.begin(), 1) == 5);
83assert(*std::next(m.begin(), 2) == 6);
84assert(*std::next(m.begin(), 3) == 7);
85
86i = m.erase(std::next(m.cbegin(), 2));
87assert(m.size() == 3);
88assert(i == std::next(m.begin(), 2));
89assert(*std::next(m.begin(), 0) == 2);
90assert(*std::next(m.begin(), 1) == 5);
91assert(*std::next(m.begin(), 2) == 7);
92
93i = m.erase(std::next(m.cbegin(), 2));
94assert(m.size() == 2);
95assert(i == std::next(m.begin(), 2));
96assert(*std::next(m.begin(), 0) == 2);
97assert(*std::next(m.begin(), 1) == 5);
98
99i = m.erase(std::next(m.cbegin(), 0));
100assert(m.size() == 1);
101assert(i == std::next(m.begin(), 0));
102assert(*std::next(m.begin(), 0) == 5);
103
104i = m.erase(m.cbegin());
105assert(m.size() == 0);
106assert(i == m.begin());
107assert(i == m.end());
108}
109#if TEST_STD_VER >= 11
110{
111typedef std::set<int, std::less<int>, min_allocator<int>> M;
112typedef int V;
113typedef M::iterator I;
114V ar[] =
115{
1161,
1172,
1183,
1194,
1205,
1216,
1227,
1238
124};
125M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
126assert(m.size() == 8);
127I i = m.erase(std::next(m.cbegin(), 3));
128assert(m.size() == 7);
129assert(i == std::next(m.begin(), 3));
130assert(*std::next(m.begin(), 0) == 1);
131assert(*std::next(m.begin(), 1) == 2);
132assert(*std::next(m.begin(), 2) == 3);
133assert(*std::next(m.begin(), 3) == 5);
134assert(*std::next(m.begin(), 4) == 6);
135assert(*std::next(m.begin(), 5) == 7);
136assert(*std::next(m.begin(), 6) == 8);
137
138i = m.erase(std::next(m.cbegin(), 0));
139assert(m.size() == 6);
140assert(i == m.begin());
141assert(*std::next(m.begin(), 0) == 2);
142assert(*std::next(m.begin(), 1) == 3);
143assert(*std::next(m.begin(), 2) == 5);
144assert(*std::next(m.begin(), 3) == 6);
145assert(*std::next(m.begin(), 4) == 7);
146assert(*std::next(m.begin(), 5) == 8);
147
148i = m.erase(std::next(m.cbegin(), 5));
149assert(m.size() == 5);
150assert(i == m.end());
151assert(*std::next(m.begin(), 0) == 2);
152assert(*std::next(m.begin(), 1) == 3);
153assert(*std::next(m.begin(), 2) == 5);
154assert(*std::next(m.begin(), 3) == 6);
155assert(*std::next(m.begin(), 4) == 7);
156
157i = m.erase(std::next(m.cbegin(), 1));
158assert(m.size() == 4);
159assert(i == std::next(m.begin()));
160assert(*std::next(m.begin(), 0) == 2);
161assert(*std::next(m.begin(), 1) == 5);
162assert(*std::next(m.begin(), 2) == 6);
163assert(*std::next(m.begin(), 3) == 7);
164
165i = m.erase(std::next(m.cbegin(), 2));
166assert(m.size() == 3);
167assert(i == std::next(m.begin(), 2));
168assert(*std::next(m.begin(), 0) == 2);
169assert(*std::next(m.begin(), 1) == 5);
170assert(*std::next(m.begin(), 2) == 7);
171
172i = m.erase(std::next(m.cbegin(), 2));
173assert(m.size() == 2);
174assert(i == std::next(m.begin(), 2));
175assert(*std::next(m.begin(), 0) == 2);
176assert(*std::next(m.begin(), 1) == 5);
177
178i = m.erase(std::next(m.cbegin(), 0));
179assert(m.size() == 1);
180assert(i == std::next(m.begin(), 0));
181assert(*std::next(m.begin(), 0) == 5);
182
183i = m.erase(m.cbegin());
184assert(m.size() == 0);
185assert(i == m.begin());
186assert(i == m.end());
187}
188#endif
189#if TEST_STD_VER >= 14
190{
191// This is LWG #2059
192typedef TemplateConstructor T;
193typedef std::set<T> C;
194typedef C::iterator I;
195
196C c;
197T a{0};
198I it = c.find(a);
199if (it != c.end())
200c.erase(it);
201}
202#endif
203
204return 0;
205}
206