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 | // <list> |
10 | |
11 | // void splice(const_iterator position, list<T,Allocator>& x, iterator i); |
12 | |
13 | #include <list> |
14 | #include <cassert> |
15 | |
16 | #include "test_macros.h" |
17 | #include "min_allocator.h" |
18 | |
19 | int main(int, char**) |
20 | { |
21 | int a1[] = {1, 2, 3}; |
22 | int a2[] = {4, 5, 6}; |
23 | { |
24 | std::list<int> l1; |
25 | std::list<int> l2(a2, a2+1); |
26 | l1.splice(position: l1.end(), x&: l2, i: l2.begin()); |
27 | assert(l1.size() == 1); |
28 | assert(std::distance(l1.begin(), l1.end()) == 1); |
29 | assert(l2.size() == 0); |
30 | assert(std::distance(l2.begin(), l2.end()) == 0); |
31 | std::list<int>::const_iterator i = l1.begin(); |
32 | assert(*i == 4); |
33 | } |
34 | { |
35 | std::list<int> l1; |
36 | std::list<int> l2(a2, a2+2); |
37 | l1.splice(position: l1.end(), x&: l2, i: l2.begin()); |
38 | assert(l1.size() == 1); |
39 | assert(std::distance(l1.begin(), l1.end()) == 1); |
40 | assert(l2.size() == 1); |
41 | assert(std::distance(l2.begin(), l2.end()) == 1); |
42 | std::list<int>::const_iterator i = l1.begin(); |
43 | assert(*i == 4); |
44 | i = l2.begin(); |
45 | assert(*i == 5); |
46 | } |
47 | { |
48 | std::list<int> l1; |
49 | std::list<int> l2(a2, a2+2); |
50 | l1.splice(position: l1.end(), x&: l2, i: std::next(x: l2.begin())); |
51 | assert(l1.size() == 1); |
52 | assert(std::distance(l1.begin(), l1.end()) == 1); |
53 | assert(l2.size() == 1); |
54 | assert(std::distance(l2.begin(), l2.end()) == 1); |
55 | std::list<int>::const_iterator i = l1.begin(); |
56 | assert(*i == 5); |
57 | i = l2.begin(); |
58 | assert(*i == 4); |
59 | } |
60 | { |
61 | std::list<int> l1; |
62 | std::list<int> l2(a2, a2+3); |
63 | l1.splice(position: l1.end(), x&: l2, i: l2.begin()); |
64 | assert(l1.size() == 1); |
65 | assert(std::distance(l1.begin(), l1.end()) == 1); |
66 | assert(l2.size() == 2); |
67 | assert(std::distance(l2.begin(), l2.end()) == 2); |
68 | std::list<int>::const_iterator i = l1.begin(); |
69 | assert(*i == 4); |
70 | i = l2.begin(); |
71 | assert(*i == 5); |
72 | ++i; |
73 | assert(*i == 6); |
74 | } |
75 | { |
76 | std::list<int> l1; |
77 | std::list<int> l2(a2, a2+3); |
78 | l1.splice(position: l1.end(), x&: l2, i: std::next(x: l2.begin())); |
79 | assert(l1.size() == 1); |
80 | assert(std::distance(l1.begin(), l1.end()) == 1); |
81 | assert(l2.size() == 2); |
82 | assert(std::distance(l2.begin(), l2.end()) == 2); |
83 | std::list<int>::const_iterator i = l1.begin(); |
84 | assert(*i == 5); |
85 | i = l2.begin(); |
86 | assert(*i == 4); |
87 | ++i; |
88 | assert(*i == 6); |
89 | } |
90 | { |
91 | std::list<int> l1; |
92 | std::list<int> l2(a2, a2+3); |
93 | l1.splice(position: l1.end(), x&: l2, i: std::next(x: l2.begin(), n: 2)); |
94 | assert(l1.size() == 1); |
95 | assert(std::distance(l1.begin(), l1.end()) == 1); |
96 | assert(l2.size() == 2); |
97 | assert(std::distance(l2.begin(), l2.end()) == 2); |
98 | std::list<int>::const_iterator i = l1.begin(); |
99 | assert(*i == 6); |
100 | i = l2.begin(); |
101 | assert(*i == 4); |
102 | ++i; |
103 | assert(*i == 5); |
104 | } |
105 | { |
106 | std::list<int> l1(a1, a1+1); |
107 | l1.splice(position: l1.begin(), x&: l1, i: l1.begin()); |
108 | assert(l1.size() == 1); |
109 | assert(std::distance(l1.begin(), l1.end()) == 1); |
110 | std::list<int>::const_iterator i = l1.begin(); |
111 | assert(*i == 1); |
112 | } |
113 | { |
114 | std::list<int> l1(a1, a1+1); |
115 | std::list<int> l2(a2, a2+1); |
116 | l1.splice(position: l1.begin(), x&: l2, i: l2.begin()); |
117 | assert(l1.size() == 2); |
118 | assert(std::distance(l1.begin(), l1.end()) == 2); |
119 | assert(l2.size() == 0); |
120 | assert(std::distance(l2.begin(), l2.end()) == 0); |
121 | std::list<int>::const_iterator i = l1.begin(); |
122 | assert(*i == 4); |
123 | ++i; |
124 | assert(*i == 1); |
125 | } |
126 | { |
127 | std::list<int> l1(a1, a1+1); |
128 | std::list<int> l2(a2, a2+1); |
129 | l1.splice(position: std::next(x: l1.begin()), x&: l2, i: l2.begin()); |
130 | assert(l1.size() == 2); |
131 | assert(std::distance(l1.begin(), l1.end()) == 2); |
132 | assert(l2.size() == 0); |
133 | assert(std::distance(l2.begin(), l2.end()) == 0); |
134 | std::list<int>::const_iterator i = l1.begin(); |
135 | assert(*i == 1); |
136 | ++i; |
137 | assert(*i == 4); |
138 | } |
139 | { |
140 | std::list<int> l1(a1, a1+2); |
141 | l1.splice(position: l1.begin(), x&: l1, i: l1.begin()); |
142 | assert(l1.size() == 2); |
143 | assert(std::distance(l1.begin(), l1.end()) == 2); |
144 | std::list<int>::const_iterator i = l1.begin(); |
145 | assert(*i == 1); |
146 | ++i; |
147 | assert(*i == 2); |
148 | } |
149 | { |
150 | std::list<int> l1(a1, a1+2); |
151 | l1.splice(position: l1.begin(), x&: l1, i: std::next(x: l1.begin())); |
152 | assert(l1.size() == 2); |
153 | assert(std::distance(l1.begin(), l1.end()) == 2); |
154 | std::list<int>::const_iterator i = l1.begin(); |
155 | assert(*i == 2); |
156 | ++i; |
157 | assert(*i == 1); |
158 | } |
159 | { |
160 | std::list<int> l1(a1, a1+2); |
161 | l1.splice(position: std::next(x: l1.begin()), x&: l1, i: l1.begin()); |
162 | assert(l1.size() == 2); |
163 | assert(std::distance(l1.begin(), l1.end()) == 2); |
164 | std::list<int>::const_iterator i = l1.begin(); |
165 | assert(*i == 1); |
166 | ++i; |
167 | assert(*i == 2); |
168 | } |
169 | { |
170 | std::list<int> l1(a1, a1+2); |
171 | l1.splice(position: std::next(x: l1.begin()), x&: l1, i: std::next(x: l1.begin())); |
172 | assert(l1.size() == 2); |
173 | assert(std::distance(l1.begin(), l1.end()) == 2); |
174 | std::list<int>::const_iterator i = l1.begin(); |
175 | assert(*i == 1); |
176 | ++i; |
177 | assert(*i == 2); |
178 | } |
179 | #if TEST_STD_VER >= 11 |
180 | { |
181 | std::list<int, min_allocator<int>> l1; |
182 | std::list<int, min_allocator<int>> l2(a2, a2+1); |
183 | l1.splice(l1.end(), l2, l2.begin()); |
184 | assert(l1.size() == 1); |
185 | assert(std::distance(l1.begin(), l1.end()) == 1); |
186 | assert(l2.size() == 0); |
187 | assert(std::distance(l2.begin(), l2.end()) == 0); |
188 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
189 | assert(*i == 4); |
190 | } |
191 | { |
192 | std::list<int, min_allocator<int>> l1; |
193 | std::list<int, min_allocator<int>> l2(a2, a2+2); |
194 | l1.splice(l1.end(), l2, l2.begin()); |
195 | assert(l1.size() == 1); |
196 | assert(std::distance(l1.begin(), l1.end()) == 1); |
197 | assert(l2.size() == 1); |
198 | assert(std::distance(l2.begin(), l2.end()) == 1); |
199 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
200 | assert(*i == 4); |
201 | i = l2.begin(); |
202 | assert(*i == 5); |
203 | } |
204 | { |
205 | std::list<int, min_allocator<int>> l1; |
206 | std::list<int, min_allocator<int>> l2(a2, a2+2); |
207 | l1.splice(l1.end(), l2, std::next(l2.begin())); |
208 | assert(l1.size() == 1); |
209 | assert(std::distance(l1.begin(), l1.end()) == 1); |
210 | assert(l2.size() == 1); |
211 | assert(std::distance(l2.begin(), l2.end()) == 1); |
212 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
213 | assert(*i == 5); |
214 | i = l2.begin(); |
215 | assert(*i == 4); |
216 | } |
217 | { |
218 | std::list<int, min_allocator<int>> l1; |
219 | std::list<int, min_allocator<int>> l2(a2, a2+3); |
220 | l1.splice(l1.end(), l2, l2.begin()); |
221 | assert(l1.size() == 1); |
222 | assert(std::distance(l1.begin(), l1.end()) == 1); |
223 | assert(l2.size() == 2); |
224 | assert(std::distance(l2.begin(), l2.end()) == 2); |
225 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
226 | assert(*i == 4); |
227 | i = l2.begin(); |
228 | assert(*i == 5); |
229 | ++i; |
230 | assert(*i == 6); |
231 | } |
232 | { |
233 | std::list<int, min_allocator<int>> l1; |
234 | std::list<int, min_allocator<int>> l2(a2, a2+3); |
235 | l1.splice(l1.end(), l2, std::next(l2.begin())); |
236 | assert(l1.size() == 1); |
237 | assert(std::distance(l1.begin(), l1.end()) == 1); |
238 | assert(l2.size() == 2); |
239 | assert(std::distance(l2.begin(), l2.end()) == 2); |
240 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
241 | assert(*i == 5); |
242 | i = l2.begin(); |
243 | assert(*i == 4); |
244 | ++i; |
245 | assert(*i == 6); |
246 | } |
247 | { |
248 | std::list<int, min_allocator<int>> l1; |
249 | std::list<int, min_allocator<int>> l2(a2, a2+3); |
250 | l1.splice(l1.end(), l2, std::next(l2.begin(), 2)); |
251 | assert(l1.size() == 1); |
252 | assert(std::distance(l1.begin(), l1.end()) == 1); |
253 | assert(l2.size() == 2); |
254 | assert(std::distance(l2.begin(), l2.end()) == 2); |
255 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
256 | assert(*i == 6); |
257 | i = l2.begin(); |
258 | assert(*i == 4); |
259 | ++i; |
260 | assert(*i == 5); |
261 | } |
262 | { |
263 | std::list<int, min_allocator<int>> l1(a1, a1+1); |
264 | l1.splice(l1.begin(), l1, l1.begin()); |
265 | assert(l1.size() == 1); |
266 | assert(std::distance(l1.begin(), l1.end()) == 1); |
267 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
268 | assert(*i == 1); |
269 | } |
270 | { |
271 | std::list<int, min_allocator<int>> l1(a1, a1+1); |
272 | std::list<int, min_allocator<int>> l2(a2, a2+1); |
273 | l1.splice(l1.begin(), l2, l2.begin()); |
274 | assert(l1.size() == 2); |
275 | assert(std::distance(l1.begin(), l1.end()) == 2); |
276 | assert(l2.size() == 0); |
277 | assert(std::distance(l2.begin(), l2.end()) == 0); |
278 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
279 | assert(*i == 4); |
280 | ++i; |
281 | assert(*i == 1); |
282 | } |
283 | { |
284 | std::list<int, min_allocator<int>> l1(a1, a1+1); |
285 | std::list<int, min_allocator<int>> l2(a2, a2+1); |
286 | l1.splice(std::next(l1.begin()), l2, l2.begin()); |
287 | assert(l1.size() == 2); |
288 | assert(std::distance(l1.begin(), l1.end()) == 2); |
289 | assert(l2.size() == 0); |
290 | assert(std::distance(l2.begin(), l2.end()) == 0); |
291 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
292 | assert(*i == 1); |
293 | ++i; |
294 | assert(*i == 4); |
295 | } |
296 | { |
297 | std::list<int, min_allocator<int>> l1(a1, a1+2); |
298 | l1.splice(l1.begin(), l1, l1.begin()); |
299 | assert(l1.size() == 2); |
300 | assert(std::distance(l1.begin(), l1.end()) == 2); |
301 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
302 | assert(*i == 1); |
303 | ++i; |
304 | assert(*i == 2); |
305 | } |
306 | { |
307 | std::list<int, min_allocator<int>> l1(a1, a1+2); |
308 | l1.splice(l1.begin(), l1, std::next(l1.begin())); |
309 | assert(l1.size() == 2); |
310 | assert(std::distance(l1.begin(), l1.end()) == 2); |
311 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
312 | assert(*i == 2); |
313 | ++i; |
314 | assert(*i == 1); |
315 | } |
316 | { |
317 | std::list<int, min_allocator<int>> l1(a1, a1+2); |
318 | l1.splice(std::next(l1.begin()), l1, l1.begin()); |
319 | assert(l1.size() == 2); |
320 | assert(std::distance(l1.begin(), l1.end()) == 2); |
321 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
322 | assert(*i == 1); |
323 | ++i; |
324 | assert(*i == 2); |
325 | } |
326 | { |
327 | std::list<int, min_allocator<int>> l1(a1, a1+2); |
328 | l1.splice(std::next(l1.begin()), l1, std::next(l1.begin())); |
329 | assert(l1.size() == 2); |
330 | assert(std::distance(l1.begin(), l1.end()) == 2); |
331 | std::list<int, min_allocator<int>>::const_iterator i = l1.begin(); |
332 | assert(*i == 1); |
333 | ++i; |
334 | assert(*i == 2); |
335 | } |
336 | #endif |
337 | |
338 | return 0; |
339 | } |
340 | |