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