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// template <class T, class Allocator>
13//     void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y);
14
15#include <forward_list>
16#include <cassert>
17
18#include "test_allocator.h"
19#include "min_allocator.h"
20
21int main()
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));
29        const T t2[] = {10, 11, 12};
30        C c2(std::begin(t2), std::end(t2), A(2));
31        swap(c1, c2);
32
33        assert(distance(c1.begin(), c1.end()) == 3);
34        assert(*next(c1.begin(), 0) == 10);
35        assert(*next(c1.begin(), 1) == 11);
36        assert(*next(c1.begin(), 2) == 12);
37        assert(c1.get_allocator() == A(1));
38
39        assert(distance(c2.begin(), c2.end()) == 6);
40        assert(*next(c2.begin(), 0) == 0);
41        assert(*next(c2.begin(), 1) == 1);
42        assert(*next(c2.begin(), 2) == 2);
43        assert(*next(c2.begin(), 3) == 3);
44        assert(*next(c2.begin(), 4) == 4);
45        assert(*next(c2.begin(), 5) == 5);
46        assert(c2.get_allocator() == A(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(t1), std::end(t1), A(1));
54        C c2(A(2));
55        swap(c1, c2);
56
57        assert(distance(c1.begin(), c1.end()) == 0);
58        assert(c1.get_allocator() == A(1));
59
60        assert(distance(c2.begin(), c2.end()) == 6);
61        assert(*next(c2.begin(), 0) == 0);
62        assert(*next(c2.begin(), 1) == 1);
63        assert(*next(c2.begin(), 2) == 2);
64        assert(*next(c2.begin(), 3) == 3);
65        assert(*next(c2.begin(), 4) == 4);
66        assert(*next(c2.begin(), 5) == 5);
67        assert(c2.get_allocator() == A(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));
74        const T t2[] = {10, 11, 12};
75        C c2(std::begin(t2), std::end(t2), A(2));
76        swap(c1, c2);
77
78        assert(distance(c1.begin(), c1.end()) == 3);
79        assert(*next(c1.begin(), 0) == 10);
80        assert(*next(c1.begin(), 1) == 11);
81        assert(*next(c1.begin(), 2) == 12);
82        assert(c1.get_allocator() == A(1));
83
84        assert(distance(c2.begin(), c2.end()) == 0);
85        assert(c2.get_allocator() == A(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));
92        C c2(A(2));
93        swap(c1, c2);
94
95        assert(distance(c1.begin(), c1.end()) == 0);
96        assert(c1.get_allocator() == A(1));
97
98        assert(distance(c2.begin(), c2.end()) == 0);
99        assert(c2.get_allocator() == A(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(t1), std::end(t1), A(1));
108        const T t2[] = {10, 11, 12};
109        C c2(std::begin(t2), std::end(t2), A(2));
110        swap(c1, c2);
111
112        assert(distance(c1.begin(), c1.end()) == 3);
113        assert(*next(c1.begin(), 0) == 10);
114        assert(*next(c1.begin(), 1) == 11);
115        assert(*next(c1.begin(), 2) == 12);
116        assert(c1.get_allocator() == A(2));
117
118        assert(distance(c2.begin(), c2.end()) == 6);
119        assert(*next(c2.begin(), 0) == 0);
120        assert(*next(c2.begin(), 1) == 1);
121        assert(*next(c2.begin(), 2) == 2);
122        assert(*next(c2.begin(), 3) == 3);
123        assert(*next(c2.begin(), 4) == 4);
124        assert(*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(t1), std::end(t1), A(1));
133        C c2(A(2));
134        swap(c1, c2);
135
136        assert(distance(c1.begin(), c1.end()) == 0);
137        assert(c1.get_allocator() == A(2));
138
139        assert(distance(c2.begin(), c2.end()) == 6);
140        assert(*next(c2.begin(), 0) == 0);
141        assert(*next(c2.begin(), 1) == 1);
142        assert(*next(c2.begin(), 2) == 2);
143        assert(*next(c2.begin(), 3) == 3);
144        assert(*next(c2.begin(), 4) == 4);
145        assert(*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(t2), std::end(t2), A(2));
155        swap(c1, c2);
156
157        assert(distance(c1.begin(), c1.end()) == 3);
158        assert(*next(c1.begin(), 0) == 10);
159        assert(*next(c1.begin(), 1) == 11);
160        assert(*next(c1.begin(), 2) == 12);
161        assert(c1.get_allocator() == A(2));
162
163        assert(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        swap(c1, c2);
173
174        assert(distance(c1.begin(), c1.end()) == 0);
175        assert(c1.get_allocator() == A(2));
176
177        assert(distance(c2.begin(), c2.end()) == 0);
178        assert(c2.get_allocator() == A(1));
179    }
180#if __cplusplus >= 201103L
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        swap(c1, c2);
190
191        assert(distance(c1.begin(), c1.end()) == 3);
192        assert(*next(c1.begin(), 0) == 10);
193        assert(*next(c1.begin(), 1) == 11);
194        assert(*next(c1.begin(), 2) == 12);
195        assert(c1.get_allocator() == A());
196
197        assert(distance(c2.begin(), c2.end()) == 6);
198        assert(*next(c2.begin(), 0) == 0);
199        assert(*next(c2.begin(), 1) == 1);
200        assert(*next(c2.begin(), 2) == 2);
201        assert(*next(c2.begin(), 3) == 3);
202        assert(*next(c2.begin(), 4) == 4);
203        assert(*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        swap(c1, c2);
214
215        assert(distance(c1.begin(), c1.end()) == 0);
216        assert(c1.get_allocator() == A());
217
218        assert(distance(c2.begin(), c2.end()) == 6);
219        assert(*next(c2.begin(), 0) == 0);
220        assert(*next(c2.begin(), 1) == 1);
221        assert(*next(c2.begin(), 2) == 2);
222        assert(*next(c2.begin(), 3) == 3);
223        assert(*next(c2.begin(), 4) == 4);
224        assert(*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        swap(c1, c2);
235
236        assert(distance(c1.begin(), c1.end()) == 3);
237        assert(*next(c1.begin(), 0) == 10);
238        assert(*next(c1.begin(), 1) == 11);
239        assert(*next(c1.begin(), 2) == 12);
240        assert(c1.get_allocator() == A());
241
242        assert(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        swap(c1, c2);
252
253        assert(distance(c1.begin(), c1.end()) == 0);
254        assert(c1.get_allocator() == A());
255
256        assert(distance(c2.begin(), c2.end()) == 0);
257        assert(c2.get_allocator() == A());
258    }
259#endif
260}
261