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 // <map>
11
12 // class map
13
14 // template <class Key, class T, class Compare, class Allocator>
15 // void
16 // swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y);
17
18 #include <map>
19 #include <cassert>
20 #include "test_allocator.h"
21 #include "../../../test_compare.h"
22 #include "min_allocator.h"
23
main()24 int main()
25 {
26 {
27 typedef std::pair<const int, double> V;
28 typedef std::map<int, double> M;
29 {
30 V ar1[] =
31 {
32 };
33 V ar2[] =
34 {
35 };
36 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
37 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
38 M m1_save = m1;
39 M m2_save = m2;
40 swap(m1, m2);
41 assert(m1 == m2_save);
42 assert(m2 == m1_save);
43 }
44 {
45 V ar1[] =
46 {
47 };
48 V ar2[] =
49 {
50 V(5, 5),
51 V(6, 6),
52 V(7, 7),
53 V(8, 8),
54 V(9, 9),
55 V(10, 10),
56 V(11, 11),
57 V(12, 12)
58 };
59 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
60 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
61 M m1_save = m1;
62 M m2_save = m2;
63 swap(m1, m2);
64 assert(m1 == m2_save);
65 assert(m2 == m1_save);
66 }
67 {
68 V ar1[] =
69 {
70 V(1, 1),
71 V(2, 2),
72 V(3, 3),
73 V(4, 4)
74 };
75 V ar2[] =
76 {
77 };
78 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
79 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
80 M m1_save = m1;
81 M m2_save = m2;
82 swap(m1, m2);
83 assert(m1 == m2_save);
84 assert(m2 == m1_save);
85 }
86 {
87 V ar1[] =
88 {
89 V(1, 1),
90 V(2, 2),
91 V(3, 3),
92 V(4, 4)
93 };
94 V ar2[] =
95 {
96 V(5, 5),
97 V(6, 6),
98 V(7, 7),
99 V(8, 8),
100 V(9, 9),
101 V(10, 10),
102 V(11, 11),
103 V(12, 12)
104 };
105 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
106 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
107 M m1_save = m1;
108 M m2_save = m2;
109 swap(m1, m2);
110 assert(m1 == m2_save);
111 assert(m2 == m1_save);
112 }
113 {
114 typedef test_allocator<V> A;
115 typedef test_compare<std::less<int> > C;
116 typedef std::map<int, double, C, A> M;
117 V ar1[] =
118 {
119 V(1, 1),
120 V(2, 2),
121 V(3, 3),
122 V(4, 4)
123 };
124 V ar2[] =
125 {
126 V(5, 5),
127 V(6, 6),
128 V(7, 7),
129 V(8, 8),
130 V(9, 9),
131 V(10, 10),
132 V(11, 11),
133 V(12, 12)
134 };
135 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A(1));
136 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A(2));
137 M m1_save = m1;
138 M m2_save = m2;
139 swap(m1, m2);
140 assert(m1 == m2_save);
141 assert(m2 == m1_save);
142 assert(m1.key_comp() == C(2));
143 assert(m1.get_allocator() == A(1));
144 assert(m2.key_comp() == C(1));
145 assert(m2.get_allocator() == A(2));
146 }
147 {
148 typedef other_allocator<V> A;
149 typedef test_compare<std::less<int> > C;
150 typedef std::map<int, double, C, A> M;
151 V ar1[] =
152 {
153 V(1, 1),
154 V(2, 2),
155 V(3, 3),
156 V(4, 4)
157 };
158 V ar2[] =
159 {
160 V(5, 5),
161 V(6, 6),
162 V(7, 7),
163 V(8, 8),
164 V(9, 9),
165 V(10, 10),
166 V(11, 11),
167 V(12, 12)
168 };
169 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A(1));
170 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A(2));
171 M m1_save = m1;
172 M m2_save = m2;
173 swap(m1, m2);
174 assert(m1 == m2_save);
175 assert(m2 == m1_save);
176 assert(m1.key_comp() == C(2));
177 assert(m1.get_allocator() == A(2));
178 assert(m2.key_comp() == C(1));
179 assert(m2.get_allocator() == A(1));
180 }
181 }
182 #if __cplusplus >= 201103L
183 {
184 typedef std::pair<const int, double> V;
185 typedef std::map<int, double, std::less<int>, min_allocator<V>> M;
186 {
187 V ar1[] =
188 {
189 };
190 V ar2[] =
191 {
192 };
193 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
194 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
195 M m1_save = m1;
196 M m2_save = m2;
197 swap(m1, m2);
198 assert(m1 == m2_save);
199 assert(m2 == m1_save);
200 }
201 {
202 V ar1[] =
203 {
204 };
205 V ar2[] =
206 {
207 V(5, 5),
208 V(6, 6),
209 V(7, 7),
210 V(8, 8),
211 V(9, 9),
212 V(10, 10),
213 V(11, 11),
214 V(12, 12)
215 };
216 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
217 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
218 M m1_save = m1;
219 M m2_save = m2;
220 swap(m1, m2);
221 assert(m1 == m2_save);
222 assert(m2 == m1_save);
223 }
224 {
225 V ar1[] =
226 {
227 V(1, 1),
228 V(2, 2),
229 V(3, 3),
230 V(4, 4)
231 };
232 V ar2[] =
233 {
234 };
235 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
236 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
237 M m1_save = m1;
238 M m2_save = m2;
239 swap(m1, m2);
240 assert(m1 == m2_save);
241 assert(m2 == m1_save);
242 }
243 {
244 V ar1[] =
245 {
246 V(1, 1),
247 V(2, 2),
248 V(3, 3),
249 V(4, 4)
250 };
251 V ar2[] =
252 {
253 V(5, 5),
254 V(6, 6),
255 V(7, 7),
256 V(8, 8),
257 V(9, 9),
258 V(10, 10),
259 V(11, 11),
260 V(12, 12)
261 };
262 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));
263 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));
264 M m1_save = m1;
265 M m2_save = m2;
266 swap(m1, m2);
267 assert(m1 == m2_save);
268 assert(m2 == m1_save);
269 }
270 {
271 typedef min_allocator<V> A;
272 typedef test_compare<std::less<int> > C;
273 typedef std::map<int, double, C, A> M;
274 V ar1[] =
275 {
276 V(1, 1),
277 V(2, 2),
278 V(3, 3),
279 V(4, 4)
280 };
281 V ar2[] =
282 {
283 V(5, 5),
284 V(6, 6),
285 V(7, 7),
286 V(8, 8),
287 V(9, 9),
288 V(10, 10),
289 V(11, 11),
290 V(12, 12)
291 };
292 M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A());
293 M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A());
294 M m1_save = m1;
295 M m2_save = m2;
296 swap(m1, m2);
297 assert(m1 == m2_save);
298 assert(m2 == m1_save);
299 assert(m1.key_comp() == C(2));
300 assert(m1.get_allocator() == A());
301 assert(m2.key_comp() == C(1));
302 assert(m2.get_allocator() == A());
303 }
304 }
305 #endif
306 }
307