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