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 | |
21 | struct TemplateConstructor |
22 | { |
23 | template<typename T> |
24 | TemplateConstructor (const T&) {} |
25 | }; |
26 | |
27 | bool operator<(const TemplateConstructor&, const TemplateConstructor&) { return false; } |
28 | |
29 | int main(int, char**) |
30 | { |
31 | { |
32 | typedef std::set<int> M; |
33 | typedef int V; |
34 | typedef M::iterator I; |
35 | V ar[] = |
36 | { |
37 | 1, |
38 | 2, |
39 | 3, |
40 | 4, |
41 | 5, |
42 | 6, |
43 | 7, |
44 | 8 |
45 | }; |
46 | M m(ar, ar + sizeof(ar)/sizeof(ar[0])); |
47 | assert(m.size() == 8); |
48 | I i = m.erase(position: std::next(x: m.cbegin(), n: 3)); |
49 | assert(m.size() == 7); |
50 | assert(i == std::next(m.begin(), 3)); |
51 | assert(*std::next(m.begin(), 0) == 1); |
52 | assert(*std::next(m.begin(), 1) == 2); |
53 | assert(*std::next(m.begin(), 2) == 3); |
54 | assert(*std::next(m.begin(), 3) == 5); |
55 | assert(*std::next(m.begin(), 4) == 6); |
56 | assert(*std::next(m.begin(), 5) == 7); |
57 | assert(*std::next(m.begin(), 6) == 8); |
58 | |
59 | i = m.erase(position: std::next(x: m.cbegin(), n: 0)); |
60 | assert(m.size() == 6); |
61 | assert(i == m.begin()); |
62 | assert(*std::next(m.begin(), 0) == 2); |
63 | assert(*std::next(m.begin(), 1) == 3); |
64 | assert(*std::next(m.begin(), 2) == 5); |
65 | assert(*std::next(m.begin(), 3) == 6); |
66 | assert(*std::next(m.begin(), 4) == 7); |
67 | assert(*std::next(m.begin(), 5) == 8); |
68 | |
69 | i = m.erase(position: std::next(x: m.cbegin(), n: 5)); |
70 | assert(m.size() == 5); |
71 | assert(i == m.end()); |
72 | assert(*std::next(m.begin(), 0) == 2); |
73 | assert(*std::next(m.begin(), 1) == 3); |
74 | assert(*std::next(m.begin(), 2) == 5); |
75 | assert(*std::next(m.begin(), 3) == 6); |
76 | assert(*std::next(m.begin(), 4) == 7); |
77 | |
78 | i = m.erase(position: std::next(x: m.cbegin(), n: 1)); |
79 | assert(m.size() == 4); |
80 | assert(i == std::next(m.begin())); |
81 | assert(*std::next(m.begin(), 0) == 2); |
82 | assert(*std::next(m.begin(), 1) == 5); |
83 | assert(*std::next(m.begin(), 2) == 6); |
84 | assert(*std::next(m.begin(), 3) == 7); |
85 | |
86 | i = m.erase(position: std::next(x: m.cbegin(), n: 2)); |
87 | assert(m.size() == 3); |
88 | assert(i == std::next(m.begin(), 2)); |
89 | assert(*std::next(m.begin(), 0) == 2); |
90 | assert(*std::next(m.begin(), 1) == 5); |
91 | assert(*std::next(m.begin(), 2) == 7); |
92 | |
93 | i = m.erase(position: std::next(x: m.cbegin(), n: 2)); |
94 | assert(m.size() == 2); |
95 | assert(i == std::next(m.begin(), 2)); |
96 | assert(*std::next(m.begin(), 0) == 2); |
97 | assert(*std::next(m.begin(), 1) == 5); |
98 | |
99 | i = m.erase(position: std::next(x: m.cbegin(), n: 0)); |
100 | assert(m.size() == 1); |
101 | assert(i == std::next(m.begin(), 0)); |
102 | assert(*std::next(m.begin(), 0) == 5); |
103 | |
104 | i = m.erase(position: m.cbegin()); |
105 | assert(m.size() == 0); |
106 | assert(i == m.begin()); |
107 | assert(i == m.end()); |
108 | } |
109 | #if TEST_STD_VER >= 11 |
110 | { |
111 | typedef std::set<int, std::less<int>, min_allocator<int>> M; |
112 | typedef int V; |
113 | typedef M::iterator I; |
114 | V ar[] = |
115 | { |
116 | 1, |
117 | 2, |
118 | 3, |
119 | 4, |
120 | 5, |
121 | 6, |
122 | 7, |
123 | 8 |
124 | }; |
125 | M m(ar, ar + sizeof(ar)/sizeof(ar[0])); |
126 | assert(m.size() == 8); |
127 | I i = m.erase(std::next(m.cbegin(), 3)); |
128 | assert(m.size() == 7); |
129 | assert(i == std::next(m.begin(), 3)); |
130 | assert(*std::next(m.begin(), 0) == 1); |
131 | assert(*std::next(m.begin(), 1) == 2); |
132 | assert(*std::next(m.begin(), 2) == 3); |
133 | assert(*std::next(m.begin(), 3) == 5); |
134 | assert(*std::next(m.begin(), 4) == 6); |
135 | assert(*std::next(m.begin(), 5) == 7); |
136 | assert(*std::next(m.begin(), 6) == 8); |
137 | |
138 | i = m.erase(std::next(m.cbegin(), 0)); |
139 | assert(m.size() == 6); |
140 | assert(i == m.begin()); |
141 | assert(*std::next(m.begin(), 0) == 2); |
142 | assert(*std::next(m.begin(), 1) == 3); |
143 | assert(*std::next(m.begin(), 2) == 5); |
144 | assert(*std::next(m.begin(), 3) == 6); |
145 | assert(*std::next(m.begin(), 4) == 7); |
146 | assert(*std::next(m.begin(), 5) == 8); |
147 | |
148 | i = m.erase(std::next(m.cbegin(), 5)); |
149 | assert(m.size() == 5); |
150 | assert(i == m.end()); |
151 | assert(*std::next(m.begin(), 0) == 2); |
152 | assert(*std::next(m.begin(), 1) == 3); |
153 | assert(*std::next(m.begin(), 2) == 5); |
154 | assert(*std::next(m.begin(), 3) == 6); |
155 | assert(*std::next(m.begin(), 4) == 7); |
156 | |
157 | i = m.erase(std::next(m.cbegin(), 1)); |
158 | assert(m.size() == 4); |
159 | assert(i == std::next(m.begin())); |
160 | assert(*std::next(m.begin(), 0) == 2); |
161 | assert(*std::next(m.begin(), 1) == 5); |
162 | assert(*std::next(m.begin(), 2) == 6); |
163 | assert(*std::next(m.begin(), 3) == 7); |
164 | |
165 | i = m.erase(std::next(m.cbegin(), 2)); |
166 | assert(m.size() == 3); |
167 | assert(i == std::next(m.begin(), 2)); |
168 | assert(*std::next(m.begin(), 0) == 2); |
169 | assert(*std::next(m.begin(), 1) == 5); |
170 | assert(*std::next(m.begin(), 2) == 7); |
171 | |
172 | i = m.erase(std::next(m.cbegin(), 2)); |
173 | assert(m.size() == 2); |
174 | assert(i == std::next(m.begin(), 2)); |
175 | assert(*std::next(m.begin(), 0) == 2); |
176 | assert(*std::next(m.begin(), 1) == 5); |
177 | |
178 | i = m.erase(std::next(m.cbegin(), 0)); |
179 | assert(m.size() == 1); |
180 | assert(i == std::next(m.begin(), 0)); |
181 | assert(*std::next(m.begin(), 0) == 5); |
182 | |
183 | i = m.erase(m.cbegin()); |
184 | assert(m.size() == 0); |
185 | assert(i == m.begin()); |
186 | assert(i == m.end()); |
187 | } |
188 | #endif |
189 | #if TEST_STD_VER >= 14 |
190 | { |
191 | // This is LWG #2059 |
192 | typedef TemplateConstructor T; |
193 | typedef std::set<T> C; |
194 | typedef C::iterator I; |
195 | |
196 | C c; |
197 | T a{0}; |
198 | I it = c.find(a); |
199 | if (it != c.end()) |
200 | c.erase(it); |
201 | } |
202 | #endif |
203 | |
204 | return 0; |
205 | } |
206 | |