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