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 // map& operator=(const map& m);
14 
15 #include <map>
16 #include <cassert>
17 #include <vector>
18 #include <algorithm>
19 #include <iterator>
20 
21 #include <iostream>
22 
23 #include "../../../test_compare.h"
24 #include "test_allocator.h"
25 #include "min_allocator.h"
26 
27 #if TEST_STD_VER >= 11
28 std::vector<int> ca_allocs;
29 std::vector<int> ca_deallocs;
30 
31 template <class T>
32 class counting_allocatorT {
33 public:
34     typedef T value_type;
35     int foo{0};
36     counting_allocatorT(int f) noexcept : foo(f) {}
37 
38     using propagate_on_container_copy_assignment = std::true_type;
39     template <class U> counting_allocatorT(const counting_allocatorT<U>& other) noexcept {foo = other.foo;}
40     template <class U> bool operator==(const counting_allocatorT<U>& other) const noexcept { return foo == other.foo; }
41     template <class U> bool operator!=(const counting_allocatorT<U>& other) const noexcept { return foo != other.foo; }
42 
43     T * allocate(const size_t n) const {
44         ca_allocs.push_back(foo);
45         void * const pv = ::malloc(n * sizeof(T));
46         return static_cast<T *>(pv);
47     }
48     void deallocate(T * const p, size_t) const noexcept {
49         ca_deallocs.push_back(foo);
50         free(p);
51     }
52 };
53 
54 template <class T>
55 class counting_allocatorF {
56 public:
57     typedef T value_type;
58     int foo{0};
59     counting_allocatorF(int f) noexcept : foo(f) {}
60 
61     using propagate_on_container_copy_assignment = std::false_type;
62     template <class U> counting_allocatorF(const counting_allocatorF<U>& other) noexcept {foo = other.foo;}
63     template <class U> bool operator==(const counting_allocatorF<U>& other) const noexcept { return foo == other.foo; }
64     template <class U> bool operator!=(const counting_allocatorF<U>& other) const noexcept { return foo != other.foo; }
65 
66     T * allocate(const size_t n) const {
67         ca_allocs.push_back(foo);
68         void * const pv = ::malloc(n * sizeof(T));
69         return static_cast<T *>(pv);
70     }
71     void deallocate(T * const p, size_t) const noexcept {
72         ca_deallocs.push_back(foo);
73         free(p);
74     }
75 };
76 
77 bool balanced_allocs() {
78     std::vector<int> temp1, temp2;
79 
80     std::cout << "Allocations = " << ca_allocs.size() << ", deallocatons = " << ca_deallocs.size() << std::endl;
81     if (ca_allocs.size() != ca_deallocs.size())
82         return false;
83 
84     temp1 = ca_allocs;
85     std::sort(temp1.begin(), temp1.end());
86     temp2.clear();
87     std::unique_copy(temp1.begin(), temp1.end(), std::back_inserter<std::vector<int>>(temp2));
88     std::cout << "There were " << temp2.size() << " different allocators\n";
89 
90     for (std::vector<int>::const_iterator it = temp2.begin(); it != temp2.end(); ++it ) {
91         std::cout << *it << ": " << std::count(ca_allocs.begin(), ca_allocs.end(), *it) << " vs " << std::count(ca_deallocs.begin(), ca_deallocs.end(), *it) << std::endl;
92         if ( std::count(ca_allocs.begin(), ca_allocs.end(), *it) != std::count(ca_deallocs.begin(), ca_deallocs.end(), *it))
93             return false;
94         }
95 
96     temp1 = ca_allocs;
97     std::sort(temp1.begin(), temp1.end());
98     temp2.clear();
99     std::unique_copy(temp1.begin(), temp1.end(), std::back_inserter<std::vector<int>>(temp2));
100     std::cout << "There were " << temp2.size() << " different (de)allocators\n";
101     for (std::vector<int>::const_iterator it = ca_deallocs.begin(); it != ca_deallocs.end(); ++it ) {
102         std::cout << *it << ": " << std::count(ca_allocs.begin(), ca_allocs.end(), *it) << " vs " << std::count(ca_deallocs.begin(), ca_deallocs.end(), *it) << std::endl;
103         if ( std::count(ca_allocs.begin(), ca_allocs.end(), *it) != std::count(ca_deallocs.begin(), ca_deallocs.end(), *it))
104             return false;
105         }
106 
107     return true;
108     }
109 #endif
110 
111 int main(int, char**)
112 {
113     {
114         typedef std::pair<const int, double> V;
115         V ar[] =
116         {
117             V(1, 1),
118             V(1, 1.5),
119             V(1, 2),
120             V(2, 1),
121             V(2, 1.5),
122             V(2, 2),
123             V(3, 1),
124             V(3, 1.5),
125             V(3, 2)
126         };
127         typedef test_compare<std::less<int> > C;
128         typedef test_allocator<V> A;
129         std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(2));
130         std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(7));
131         m = mo;
132         assert(m.get_allocator() == A(7));
133         assert(m.key_comp() == C(5));
134         assert(m.size() == 3);
135         assert(distance(m.begin(), m.end()) == 3);
136         assert(*m.begin() == V(1, 1));
137         assert(*next(m.begin()) == V(2, 1));
138         assert(*next(m.begin(), 2) == V(3, 1));
139 
140         assert(mo.get_allocator() == A(2));
141         assert(mo.key_comp() == C(5));
142         assert(mo.size() == 3);
143         assert(distance(mo.begin(), mo.end()) == 3);
144         assert(*mo.begin() == V(1, 1));
145         assert(*next(mo.begin()) == V(2, 1));
146         assert(*next(mo.begin(), 2) == V(3, 1));
147     }
148     {
149         typedef std::pair<const int, double> V;
150         const V ar[] =
151         {
152             V(1, 1),
153             V(2, 1),
154             V(3, 1),
155         };
156         std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
157         std::map<int, double> *p = &m;
158         m = *p;
159 
160         assert(m.size() == 3);
161         assert(std::equal(m.begin(), m.end(), ar));
162     }
163     {
164         typedef std::pair<const int, double> V;
165         V ar[] =
166         {
167             V(1, 1),
168             V(1, 1.5),
169             V(1, 2),
170             V(2, 1),
171             V(2, 1.5),
172             V(2, 2),
173             V(3, 1),
174             V(3, 1.5),
175             V(3, 2)
176         };
177         typedef test_compare<std::less<int> > C;
178         typedef other_allocator<V> A;
179         std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(2));
180         std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(7));
181         m = mo;
182         assert(m.get_allocator() == A(2));
183         assert(m.key_comp() == C(5));
184         assert(m.size() == 3);
185         assert(distance(m.begin(), m.end()) == 3);
186         assert(*m.begin() == V(1, 1));
187         assert(*next(m.begin()) == V(2, 1));
188         assert(*next(m.begin(), 2) == V(3, 1));
189 
190         assert(mo.get_allocator() == A(2));
191         assert(mo.key_comp() == C(5));
192         assert(mo.size() == 3);
193         assert(distance(mo.begin(), mo.end()) == 3);
194         assert(*mo.begin() == V(1, 1));
195         assert(*next(mo.begin()) == V(2, 1));
196         assert(*next(mo.begin(), 2) == V(3, 1));
197     }
198 #if TEST_STD_VER >= 11
199     {
200         typedef std::pair<const int, double> V;
201         V ar[] =
202         {
203             V(1, 1),
204             V(1, 1.5),
205             V(1, 2),
206             V(2, 1),
207             V(2, 1.5),
208             V(2, 2),
209             V(3, 1),
210             V(3, 1.5),
211             V(3, 2)
212         };
213         typedef test_compare<std::less<int> > C;
214         typedef min_allocator<V> A;
215         std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A());
216         std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A());
217         m = mo;
218         assert(m.get_allocator() == A());
219         assert(m.key_comp() == C(5));
220         assert(m.size() == 3);
221         assert(distance(m.begin(), m.end()) == 3);
222         assert(*m.begin() == V(1, 1));
223         assert(*next(m.begin()) == V(2, 1));
224         assert(*next(m.begin(), 2) == V(3, 1));
225 
226         assert(mo.get_allocator() == A());
227         assert(mo.key_comp() == C(5));
228         assert(mo.size() == 3);
229         assert(distance(mo.begin(), mo.end()) == 3);
230         assert(*mo.begin() == V(1, 1));
231         assert(*next(mo.begin()) == V(2, 1));
232         assert(*next(mo.begin(), 2) == V(3, 1));
233     }
234     {
235         typedef std::pair<const int, double> V;
236         V ar[] =
237         {
238             V(1, 1),
239             V(1, 1.5),
240             V(1, 2),
241             V(2, 1),
242             V(2, 1.5),
243             V(2, 2),
244             V(3, 1),
245             V(3, 1.5),
246             V(3, 2)
247         };
248         typedef test_compare<std::less<int> > C;
249         typedef min_allocator<V> A;
250         std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A());
251         std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A());
252         m = mo;
253         assert(m.get_allocator() == A());
254         assert(m.key_comp() == C(5));
255         assert(m.size() == 3);
256         assert(distance(m.begin(), m.end()) == 3);
257         assert(*m.begin() == V(1, 1));
258         assert(*next(m.begin()) == V(2, 1));
259         assert(*next(m.begin(), 2) == V(3, 1));
260 
261         assert(mo.get_allocator() == A());
262         assert(mo.key_comp() == C(5));
263         assert(mo.size() == 3);
264         assert(distance(mo.begin(), mo.end()) == 3);
265         assert(*mo.begin() == V(1, 1));
266         assert(*next(mo.begin()) == V(2, 1));
267         assert(*next(mo.begin(), 2) == V(3, 1));
268     }
269 
270     assert(balanced_allocs());
271     {
272         typedef std::pair<const int, double> V;
273         V ar[] =
274         {
275             V(1, 1),
276             V(1, 1.5),
277             V(1, 2),
278             V(2, 1),
279             V(2, 1.5),
280             V(2, 2),
281             V(3, 1),
282             V(3, 1.5),
283             V(3, 2)
284         };
285         typedef test_compare<std::less<int> > C;
286         typedef counting_allocatorT<V> A;
287         std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(1));
288         std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(2));
289         m = mo;
290         assert(m.key_comp() == C(5));
291         assert(m.size() == 3);
292         assert(distance(m.begin(), m.end()) == 3);
293         assert(*m.begin() == V(1, 1));
294         assert(*next(m.begin()) == V(2, 1));
295         assert(*next(m.begin(), 2) == V(3, 1));
296 
297         assert(mo.key_comp() == C(5));
298         assert(mo.size() == 3);
299         assert(distance(mo.begin(), mo.end()) == 3);
300         assert(*mo.begin() == V(1, 1));
301         assert(*next(mo.begin()) == V(2, 1));
302         assert(*next(mo.begin(), 2) == V(3, 1));
303     }
304     assert(balanced_allocs());
305     {
306         typedef std::pair<const int, double> V;
307         V ar[] =
308         {
309             V(1, 1),
310             V(1, 1.5),
311             V(1, 2),
312             V(2, 1),
313             V(2, 1.5),
314             V(2, 2),
315             V(3, 1),
316             V(3, 1.5),
317             V(3, 2)
318         };
319         typedef test_compare<std::less<int> > C;
320         typedef counting_allocatorF<V> A;
321         std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(100));
322         std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(200));
323         m = mo;
324         assert(m.key_comp() == C(5));
325         assert(m.size() == 3);
326         assert(distance(m.begin(), m.end()) == 3);
327         assert(*m.begin() == V(1, 1));
328         assert(*next(m.begin()) == V(2, 1));
329         assert(*next(m.begin(), 2) == V(3, 1));
330 
331         assert(mo.key_comp() == C(5));
332         assert(mo.size() == 3);
333         assert(distance(mo.begin(), mo.end()) == 3);
334         assert(*mo.begin() == V(1, 1));
335         assert(*next(mo.begin()) == V(2, 1));
336         assert(*next(mo.begin(), 2) == V(3, 1));
337     }
338     assert(balanced_allocs());
339 #endif
340 
341   return 0;
342 }
343