1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <set>
11
12 // class set
13
14 // size_type erase(const key_type& k);
15
16 #include <set>
17 #include <cassert>
18
19 #include "min_allocator.h"
20
main()21 int main()
22 {
23 {
24 typedef std::set<int> M;
25 typedef int V;
26 typedef M::size_type I;
27 V ar[] =
28 {
29 1,
30 2,
31 3,
32 4,
33 5,
34 6,
35 7,
36 8
37 };
38 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
39 assert(m.size() == 8);
40 I i = m.erase(9);
41 assert(m.size() == 8);
42 assert(i == 0);
43 assert(*next(m.begin(), 0) == 1);
44 assert(*next(m.begin(), 1) == 2);
45 assert(*next(m.begin(), 2) == 3);
46 assert(*next(m.begin(), 3) == 4);
47 assert(*next(m.begin(), 4) == 5);
48 assert(*next(m.begin(), 5) == 6);
49 assert(*next(m.begin(), 6) == 7);
50 assert(*next(m.begin(), 7) == 8);
51
52 i = m.erase(4);
53 assert(m.size() == 7);
54 assert(i == 1);
55 assert(*next(m.begin(), 0) == 1);
56 assert(*next(m.begin(), 1) == 2);
57 assert(*next(m.begin(), 2) == 3);
58 assert(*next(m.begin(), 3) == 5);
59 assert(*next(m.begin(), 4) == 6);
60 assert(*next(m.begin(), 5) == 7);
61 assert(*next(m.begin(), 6) == 8);
62
63 i = m.erase(1);
64 assert(m.size() == 6);
65 assert(i == 1);
66 assert(*next(m.begin(), 0) == 2);
67 assert(*next(m.begin(), 1) == 3);
68 assert(*next(m.begin(), 2) == 5);
69 assert(*next(m.begin(), 3) == 6);
70 assert(*next(m.begin(), 4) == 7);
71 assert(*next(m.begin(), 5) == 8);
72
73 i = m.erase(8);
74 assert(m.size() == 5);
75 assert(i == 1);
76 assert(*next(m.begin(), 0) == 2);
77 assert(*next(m.begin(), 1) == 3);
78 assert(*next(m.begin(), 2) == 5);
79 assert(*next(m.begin(), 3) == 6);
80 assert(*next(m.begin(), 4) == 7);
81
82 i = m.erase(3);
83 assert(m.size() == 4);
84 assert(i == 1);
85 assert(*next(m.begin(), 0) == 2);
86 assert(*next(m.begin(), 1) == 5);
87 assert(*next(m.begin(), 2) == 6);
88 assert(*next(m.begin(), 3) == 7);
89
90 i = m.erase(6);
91 assert(m.size() == 3);
92 assert(i == 1);
93 assert(*next(m.begin(), 0) == 2);
94 assert(*next(m.begin(), 1) == 5);
95 assert(*next(m.begin(), 2) == 7);
96
97 i = m.erase(7);
98 assert(m.size() == 2);
99 assert(i == 1);
100 assert(*next(m.begin(), 0) == 2);
101 assert(*next(m.begin(), 1) == 5);
102
103 i = m.erase(2);
104 assert(m.size() == 1);
105 assert(i == 1);
106 assert(*next(m.begin(), 0) == 5);
107
108 i = m.erase(5);
109 assert(m.size() == 0);
110 assert(i == 1);
111 }
112 #if __cplusplus >= 201103L
113 {
114 typedef std::set<int, std::less<int>, min_allocator<int>> M;
115 typedef int V;
116 typedef M::size_type I;
117 V ar[] =
118 {
119 1,
120 2,
121 3,
122 4,
123 5,
124 6,
125 7,
126 8
127 };
128 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
129 assert(m.size() == 8);
130 I i = m.erase(9);
131 assert(m.size() == 8);
132 assert(i == 0);
133 assert(*next(m.begin(), 0) == 1);
134 assert(*next(m.begin(), 1) == 2);
135 assert(*next(m.begin(), 2) == 3);
136 assert(*next(m.begin(), 3) == 4);
137 assert(*next(m.begin(), 4) == 5);
138 assert(*next(m.begin(), 5) == 6);
139 assert(*next(m.begin(), 6) == 7);
140 assert(*next(m.begin(), 7) == 8);
141
142 i = m.erase(4);
143 assert(m.size() == 7);
144 assert(i == 1);
145 assert(*next(m.begin(), 0) == 1);
146 assert(*next(m.begin(), 1) == 2);
147 assert(*next(m.begin(), 2) == 3);
148 assert(*next(m.begin(), 3) == 5);
149 assert(*next(m.begin(), 4) == 6);
150 assert(*next(m.begin(), 5) == 7);
151 assert(*next(m.begin(), 6) == 8);
152
153 i = m.erase(1);
154 assert(m.size() == 6);
155 assert(i == 1);
156 assert(*next(m.begin(), 0) == 2);
157 assert(*next(m.begin(), 1) == 3);
158 assert(*next(m.begin(), 2) == 5);
159 assert(*next(m.begin(), 3) == 6);
160 assert(*next(m.begin(), 4) == 7);
161 assert(*next(m.begin(), 5) == 8);
162
163 i = m.erase(8);
164 assert(m.size() == 5);
165 assert(i == 1);
166 assert(*next(m.begin(), 0) == 2);
167 assert(*next(m.begin(), 1) == 3);
168 assert(*next(m.begin(), 2) == 5);
169 assert(*next(m.begin(), 3) == 6);
170 assert(*next(m.begin(), 4) == 7);
171
172 i = m.erase(3);
173 assert(m.size() == 4);
174 assert(i == 1);
175 assert(*next(m.begin(), 0) == 2);
176 assert(*next(m.begin(), 1) == 5);
177 assert(*next(m.begin(), 2) == 6);
178 assert(*next(m.begin(), 3) == 7);
179
180 i = m.erase(6);
181 assert(m.size() == 3);
182 assert(i == 1);
183 assert(*next(m.begin(), 0) == 2);
184 assert(*next(m.begin(), 1) == 5);
185 assert(*next(m.begin(), 2) == 7);
186
187 i = m.erase(7);
188 assert(m.size() == 2);
189 assert(i == 1);
190 assert(*next(m.begin(), 0) == 2);
191 assert(*next(m.begin(), 1) == 5);
192
193 i = m.erase(2);
194 assert(m.size() == 1);
195 assert(i == 1);
196 assert(*next(m.begin(), 0) == 5);
197
198 i = m.erase(5);
199 assert(m.size() == 0);
200 assert(i == 1);
201 }
202 #endif
203 }
204