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 | // size_type erase(const key_type& k); |
14 | |
15 | #include <set> |
16 | #include <cassert> |
17 | |
18 | #include "test_macros.h" |
19 | #include "min_allocator.h" |
20 | |
21 | int main(int, char**) { |
22 | { |
23 | typedef std::set<int> M; |
24 | typedef int V; |
25 | typedef M::size_type I; |
26 | V ar[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
27 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
28 | assert(m.size() == 8); |
29 | I i = m.erase(x: 9); |
30 | assert(m.size() == 8); |
31 | assert(i == 0); |
32 | assert(*std::next(m.begin(), 0) == 1); |
33 | assert(*std::next(m.begin(), 1) == 2); |
34 | assert(*std::next(m.begin(), 2) == 3); |
35 | assert(*std::next(m.begin(), 3) == 4); |
36 | assert(*std::next(m.begin(), 4) == 5); |
37 | assert(*std::next(m.begin(), 5) == 6); |
38 | assert(*std::next(m.begin(), 6) == 7); |
39 | assert(*std::next(m.begin(), 7) == 8); |
40 | |
41 | i = m.erase(x: 4); |
42 | assert(m.size() == 7); |
43 | assert(i == 1); |
44 | assert(*std::next(m.begin(), 0) == 1); |
45 | assert(*std::next(m.begin(), 1) == 2); |
46 | assert(*std::next(m.begin(), 2) == 3); |
47 | assert(*std::next(m.begin(), 3) == 5); |
48 | assert(*std::next(m.begin(), 4) == 6); |
49 | assert(*std::next(m.begin(), 5) == 7); |
50 | assert(*std::next(m.begin(), 6) == 8); |
51 | |
52 | i = m.erase(x: 1); |
53 | assert(m.size() == 6); |
54 | assert(i == 1); |
55 | assert(*std::next(m.begin(), 0) == 2); |
56 | assert(*std::next(m.begin(), 1) == 3); |
57 | assert(*std::next(m.begin(), 2) == 5); |
58 | assert(*std::next(m.begin(), 3) == 6); |
59 | assert(*std::next(m.begin(), 4) == 7); |
60 | assert(*std::next(m.begin(), 5) == 8); |
61 | |
62 | i = m.erase(x: 8); |
63 | assert(m.size() == 5); |
64 | assert(i == 1); |
65 | assert(*std::next(m.begin(), 0) == 2); |
66 | assert(*std::next(m.begin(), 1) == 3); |
67 | assert(*std::next(m.begin(), 2) == 5); |
68 | assert(*std::next(m.begin(), 3) == 6); |
69 | assert(*std::next(m.begin(), 4) == 7); |
70 | |
71 | i = m.erase(x: 3); |
72 | assert(m.size() == 4); |
73 | assert(i == 1); |
74 | assert(*std::next(m.begin(), 0) == 2); |
75 | assert(*std::next(m.begin(), 1) == 5); |
76 | assert(*std::next(m.begin(), 2) == 6); |
77 | assert(*std::next(m.begin(), 3) == 7); |
78 | |
79 | i = m.erase(x: 6); |
80 | assert(m.size() == 3); |
81 | assert(i == 1); |
82 | assert(*std::next(m.begin(), 0) == 2); |
83 | assert(*std::next(m.begin(), 1) == 5); |
84 | assert(*std::next(m.begin(), 2) == 7); |
85 | |
86 | i = m.erase(x: 7); |
87 | assert(m.size() == 2); |
88 | assert(i == 1); |
89 | assert(*std::next(m.begin(), 0) == 2); |
90 | assert(*std::next(m.begin(), 1) == 5); |
91 | |
92 | i = m.erase(x: 2); |
93 | assert(m.size() == 1); |
94 | assert(i == 1); |
95 | assert(*std::next(m.begin(), 0) == 5); |
96 | |
97 | i = m.erase(x: 5); |
98 | assert(m.size() == 0); |
99 | assert(i == 1); |
100 | } |
101 | #if TEST_STD_VER >= 11 |
102 | { |
103 | typedef std::set<int, std::less<int>, min_allocator<int>> M; |
104 | typedef int V; |
105 | typedef M::size_type I; |
106 | V ar[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
107 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
108 | assert(m.size() == 8); |
109 | I i = m.erase(9); |
110 | assert(m.size() == 8); |
111 | assert(i == 0); |
112 | assert(*std::next(m.begin(), 0) == 1); |
113 | assert(*std::next(m.begin(), 1) == 2); |
114 | assert(*std::next(m.begin(), 2) == 3); |
115 | assert(*std::next(m.begin(), 3) == 4); |
116 | assert(*std::next(m.begin(), 4) == 5); |
117 | assert(*std::next(m.begin(), 5) == 6); |
118 | assert(*std::next(m.begin(), 6) == 7); |
119 | assert(*std::next(m.begin(), 7) == 8); |
120 | |
121 | i = m.erase(4); |
122 | assert(m.size() == 7); |
123 | assert(i == 1); |
124 | assert(*std::next(m.begin(), 0) == 1); |
125 | assert(*std::next(m.begin(), 1) == 2); |
126 | assert(*std::next(m.begin(), 2) == 3); |
127 | assert(*std::next(m.begin(), 3) == 5); |
128 | assert(*std::next(m.begin(), 4) == 6); |
129 | assert(*std::next(m.begin(), 5) == 7); |
130 | assert(*std::next(m.begin(), 6) == 8); |
131 | |
132 | i = m.erase(1); |
133 | assert(m.size() == 6); |
134 | assert(i == 1); |
135 | assert(*std::next(m.begin(), 0) == 2); |
136 | assert(*std::next(m.begin(), 1) == 3); |
137 | assert(*std::next(m.begin(), 2) == 5); |
138 | assert(*std::next(m.begin(), 3) == 6); |
139 | assert(*std::next(m.begin(), 4) == 7); |
140 | assert(*std::next(m.begin(), 5) == 8); |
141 | |
142 | i = m.erase(8); |
143 | assert(m.size() == 5); |
144 | assert(i == 1); |
145 | assert(*std::next(m.begin(), 0) == 2); |
146 | assert(*std::next(m.begin(), 1) == 3); |
147 | assert(*std::next(m.begin(), 2) == 5); |
148 | assert(*std::next(m.begin(), 3) == 6); |
149 | assert(*std::next(m.begin(), 4) == 7); |
150 | |
151 | i = m.erase(3); |
152 | assert(m.size() == 4); |
153 | assert(i == 1); |
154 | assert(*std::next(m.begin(), 0) == 2); |
155 | assert(*std::next(m.begin(), 1) == 5); |
156 | assert(*std::next(m.begin(), 2) == 6); |
157 | assert(*std::next(m.begin(), 3) == 7); |
158 | |
159 | i = m.erase(6); |
160 | assert(m.size() == 3); |
161 | assert(i == 1); |
162 | assert(*std::next(m.begin(), 0) == 2); |
163 | assert(*std::next(m.begin(), 1) == 5); |
164 | assert(*std::next(m.begin(), 2) == 7); |
165 | |
166 | i = m.erase(7); |
167 | assert(m.size() == 2); |
168 | assert(i == 1); |
169 | assert(*std::next(m.begin(), 0) == 2); |
170 | assert(*std::next(m.begin(), 1) == 5); |
171 | |
172 | i = m.erase(2); |
173 | assert(m.size() == 1); |
174 | assert(i == 1); |
175 | assert(*std::next(m.begin(), 0) == 5); |
176 | |
177 | i = m.erase(5); |
178 | assert(m.size() == 0); |
179 | assert(i == 1); |
180 | } |
181 | #endif |
182 | |
183 | return 0; |
184 | } |
185 | |