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