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// <forward_list>
10
11// void swap(forward_list& x);
12
13#include <forward_list>
14#include <cassert>
15#include <iterator>
16
17#include "test_macros.h"
18#include "test_allocator.h"
19#include "min_allocator.h"
20
21int main(int, char**)
22{
23 {
24 typedef int T;
25 typedef test_allocator<T> A;
26 typedef std::forward_list<T, A> C;
27 const T t1[] = {0, 1, 2, 3, 4, 5};
28 C c1(std::begin(t1), std::end(t1), A(1, 1));
29 const T t2[] = {10, 11, 12};
30 C c2(std::begin(arr: t2), std::end(arr: t2), A(1, 2));
31 c1.swap(c2);
32
33 assert(std::distance(c1.begin(), c1.end()) == 3);
34 assert(*std::next(c1.begin(), 0) == 10);
35 assert(*std::next(c1.begin(), 1) == 11);
36 assert(*std::next(c1.begin(), 2) == 12);
37 assert(c1.get_allocator().get_id() == 1);
38
39 assert(std::distance(c2.begin(), c2.end()) == 6);
40 assert(*std::next(c2.begin(), 0) == 0);
41 assert(*std::next(c2.begin(), 1) == 1);
42 assert(*std::next(c2.begin(), 2) == 2);
43 assert(*std::next(c2.begin(), 3) == 3);
44 assert(*std::next(c2.begin(), 4) == 4);
45 assert(*std::next(c2.begin(), 5) == 5);
46 assert(c2.get_allocator().get_id() == 2);
47 }
48 {
49 typedef int T;
50 typedef test_allocator<T> A;
51 typedef std::forward_list<T, A> C;
52 const T t1[] = {0, 1, 2, 3, 4, 5};
53 C c1(std::begin(arr: t1), std::end(arr: t1), A(1, 1));
54 C c2(A(1, 2));
55 c1.swap(list&: c2);
56
57 assert(std::distance(c1.begin(), c1.end()) == 0);
58 assert(c1.get_allocator().get_id() == 1);
59
60 assert(std::distance(c2.begin(), c2.end()) == 6);
61 assert(*std::next(c2.begin(), 0) == 0);
62 assert(*std::next(c2.begin(), 1) == 1);
63 assert(*std::next(c2.begin(), 2) == 2);
64 assert(*std::next(c2.begin(), 3) == 3);
65 assert(*std::next(c2.begin(), 4) == 4);
66 assert(*std::next(c2.begin(), 5) == 5);
67 assert(c2.get_allocator().get_id() == 2);
68 }
69 {
70 typedef int T;
71 typedef test_allocator<T> A;
72 typedef std::forward_list<T, A> C;
73 C c1(A(1, 1));
74 const T t2[] = {10, 11, 12};
75 C c2(std::begin(arr: t2), std::end(arr: t2), A(1, 2));
76 c1.swap(list&: c2);
77
78 assert(std::distance(c1.begin(), c1.end()) == 3);
79 assert(*std::next(c1.begin(), 0) == 10);
80 assert(*std::next(c1.begin(), 1) == 11);
81 assert(*std::next(c1.begin(), 2) == 12);
82 assert(c1.get_allocator().get_id() == 1);
83
84 assert(std::distance(c2.begin(), c2.end()) == 0);
85 assert(c2.get_allocator().get_id() == 2);
86 }
87 {
88 typedef int T;
89 typedef test_allocator<T> A;
90 typedef std::forward_list<T, A> C;
91 C c1(A(1, 1));
92 C c2(A(1, 2));
93 c1.swap(list&: c2);
94
95 assert(std::distance(c1.begin(), c1.end()) == 0);
96 assert(c1.get_allocator().get_id() == 1);
97
98 assert(std::distance(c2.begin(), c2.end()) == 0);
99 assert(c2.get_allocator().get_id() == 2);
100 }
101
102 {
103 typedef int T;
104 typedef other_allocator<T> A;
105 typedef std::forward_list<T, A> C;
106 const T t1[] = {0, 1, 2, 3, 4, 5};
107 C c1(std::begin(arr: t1), std::end(arr: t1), A(1));
108 const T t2[] = {10, 11, 12};
109 C c2(std::begin(arr: t2), std::end(arr: t2), A(2));
110 c1.swap(list&: c2);
111
112 assert(std::distance(c1.begin(), c1.end()) == 3);
113 assert(*std::next(c1.begin(), 0) == 10);
114 assert(*std::next(c1.begin(), 1) == 11);
115 assert(*std::next(c1.begin(), 2) == 12);
116 assert(c1.get_allocator() == A(2));
117
118 assert(std::distance(c2.begin(), c2.end()) == 6);
119 assert(*std::next(c2.begin(), 0) == 0);
120 assert(*std::next(c2.begin(), 1) == 1);
121 assert(*std::next(c2.begin(), 2) == 2);
122 assert(*std::next(c2.begin(), 3) == 3);
123 assert(*std::next(c2.begin(), 4) == 4);
124 assert(*std::next(c2.begin(), 5) == 5);
125 assert(c2.get_allocator() == A(1));
126 }
127 {
128 typedef int T;
129 typedef other_allocator<T> A;
130 typedef std::forward_list<T, A> C;
131 const T t1[] = {0, 1, 2, 3, 4, 5};
132 C c1(std::begin(arr: t1), std::end(arr: t1), A(1));
133 C c2(A(2));
134 c1.swap(list&: c2);
135
136 assert(std::distance(c1.begin(), c1.end()) == 0);
137 assert(c1.get_allocator() == A(2));
138
139 assert(std::distance(c2.begin(), c2.end()) == 6);
140 assert(*std::next(c2.begin(), 0) == 0);
141 assert(*std::next(c2.begin(), 1) == 1);
142 assert(*std::next(c2.begin(), 2) == 2);
143 assert(*std::next(c2.begin(), 3) == 3);
144 assert(*std::next(c2.begin(), 4) == 4);
145 assert(*std::next(c2.begin(), 5) == 5);
146 assert(c2.get_allocator() == A(1));
147 }
148 {
149 typedef int T;
150 typedef other_allocator<T> A;
151 typedef std::forward_list<T, A> C;
152 C c1(A(1));
153 const T t2[] = {10, 11, 12};
154 C c2(std::begin(arr: t2), std::end(arr: t2), A(2));
155 c1.swap(list&: c2);
156
157 assert(std::distance(c1.begin(), c1.end()) == 3);
158 assert(*std::next(c1.begin(), 0) == 10);
159 assert(*std::next(c1.begin(), 1) == 11);
160 assert(*std::next(c1.begin(), 2) == 12);
161 assert(c1.get_allocator() == A(2));
162
163 assert(std::distance(c2.begin(), c2.end()) == 0);
164 assert(c2.get_allocator() == A(1));
165 }
166 {
167 typedef int T;
168 typedef other_allocator<T> A;
169 typedef std::forward_list<T, A> C;
170 C c1(A(1));
171 C c2(A(2));
172 c1.swap(list&: c2);
173
174 assert(std::distance(c1.begin(), c1.end()) == 0);
175 assert(c1.get_allocator() == A(2));
176
177 assert(std::distance(c2.begin(), c2.end()) == 0);
178 assert(c2.get_allocator() == A(1));
179 }
180#if TEST_STD_VER >= 11
181 {
182 typedef int T;
183 typedef min_allocator<T> A;
184 typedef std::forward_list<T, A> C;
185 const T t1[] = {0, 1, 2, 3, 4, 5};
186 C c1(std::begin(t1), std::end(t1), A());
187 const T t2[] = {10, 11, 12};
188 C c2(std::begin(t2), std::end(t2), A());
189 c1.swap(c2);
190
191 assert(std::distance(c1.begin(), c1.end()) == 3);
192 assert(*std::next(c1.begin(), 0) == 10);
193 assert(*std::next(c1.begin(), 1) == 11);
194 assert(*std::next(c1.begin(), 2) == 12);
195 assert(c1.get_allocator() == A());
196
197 assert(std::distance(c2.begin(), c2.end()) == 6);
198 assert(*std::next(c2.begin(), 0) == 0);
199 assert(*std::next(c2.begin(), 1) == 1);
200 assert(*std::next(c2.begin(), 2) == 2);
201 assert(*std::next(c2.begin(), 3) == 3);
202 assert(*std::next(c2.begin(), 4) == 4);
203 assert(*std::next(c2.begin(), 5) == 5);
204 assert(c2.get_allocator() == A());
205 }
206 {
207 typedef int T;
208 typedef min_allocator<T> A;
209 typedef std::forward_list<T, A> C;
210 const T t1[] = {0, 1, 2, 3, 4, 5};
211 C c1(std::begin(t1), std::end(t1), A());
212 C c2(A{});
213 c1.swap(c2);
214
215 assert(std::distance(c1.begin(), c1.end()) == 0);
216 assert(c1.get_allocator() == A());
217
218 assert(std::distance(c2.begin(), c2.end()) == 6);
219 assert(*std::next(c2.begin(), 0) == 0);
220 assert(*std::next(c2.begin(), 1) == 1);
221 assert(*std::next(c2.begin(), 2) == 2);
222 assert(*std::next(c2.begin(), 3) == 3);
223 assert(*std::next(c2.begin(), 4) == 4);
224 assert(*std::next(c2.begin(), 5) == 5);
225 assert(c2.get_allocator() == A());
226 }
227 {
228 typedef int T;
229 typedef min_allocator<T> A;
230 typedef std::forward_list<T, A> C;
231 C c1(A{});
232 const T t2[] = {10, 11, 12};
233 C c2(std::begin(t2), std::end(t2), A());
234 c1.swap(c2);
235
236 assert(std::distance(c1.begin(), c1.end()) == 3);
237 assert(*std::next(c1.begin(), 0) == 10);
238 assert(*std::next(c1.begin(), 1) == 11);
239 assert(*std::next(c1.begin(), 2) == 12);
240 assert(c1.get_allocator() == A());
241
242 assert(std::distance(c2.begin(), c2.end()) == 0);
243 assert(c2.get_allocator() == A());
244 }
245 {
246 typedef int T;
247 typedef min_allocator<T> A;
248 typedef std::forward_list<T, A> C;
249 C c1(A{});
250 C c2(A{});
251 c1.swap(c2);
252
253 assert(std::distance(c1.begin(), c1.end()) == 0);
254 assert(c1.get_allocator() == A());
255
256 assert(std::distance(c2.begin(), c2.end()) == 0);
257 assert(c2.get_allocator() == A());
258 }
259#endif
260
261 return 0;
262}
263

source code of libcxx/test/std/containers/sequences/forwardlist/forwardlist.spec/member_swap.pass.cpp