1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 // map& operator=(const map& m);
24 #include "../../../test_compare.h"
25 #include "test_allocator.h"
26 #include "min_allocator.h"
28 #if TEST_STD_VER >= 11
29 std::vector<int> ca_allocs;
30 std::vector<int> ca_deallocs;
33 class counting_allocatorT {
37 counting_allocatorT(int f) noexcept : foo(f) {}
39 using propagate_on_container_copy_assignment = std::true_type;
40 template <class U> counting_allocatorT(const counting_allocatorT<U>& other) noexcept {foo = other.foo;}
41 template <class U> bool operator==(const counting_allocatorT<U>& other) const noexcept { return foo == other.foo; }
42 template <class U> bool operator!=(const counting_allocatorT<U>& other) const noexcept { return foo != other.foo; }
44 T * allocate(const size_t n) const {
45 ca_allocs.push_back(foo);
46 void * const pv = ::malloc(n * sizeof(T));
47 return static_cast<T *>(pv);
49 void deallocate(T * const p, size_t) const noexcept {
50 ca_deallocs.push_back(foo);
56 class counting_allocatorF {
60 counting_allocatorF(int f) noexcept : foo(f) {}
62 using propagate_on_container_copy_assignment = std::false_type;
63 template <class U> counting_allocatorF(const counting_allocatorF<U>& other) noexcept {foo = other.foo;}
64 template <class U> bool operator==(const counting_allocatorF<U>& other) const noexcept { return foo == other.foo; }
65 template <class U> bool operator!=(const counting_allocatorF<U>& other) const noexcept { return foo != other.foo; }
67 T * allocate(const size_t n) const {
68 ca_allocs.push_back(foo);
69 void * const pv = ::malloc(n * sizeof(T));
70 return static_cast<T *>(pv);
72 void deallocate(T * const p, size_t) const noexcept {
73 ca_deallocs.push_back(foo);
78 bool balanced_allocs() {
79 std::vector<int> temp1, temp2;
81 std::cout << "Allocations = " << ca_allocs.size() << ", deallocatons = " << ca_deallocs.size() << std::endl;
82 if (ca_allocs.size() != ca_deallocs.size())
86 std::sort(temp1.begin(), temp1.end());
88 std::unique_copy(temp1.begin(), temp1.end(), std::back_inserter<std::vector<int>>(temp2));
89 std::cout << "There were " << temp2.size() << " different allocators\n";
91 for (std::vector<int>::const_iterator it = temp2.begin(); it != temp2.end(); ++it ) {
92 std::cout << *it << ": " << std::count(ca_allocs.begin(), ca_allocs.end(), *it) << " vs " << std::count(ca_deallocs.begin(), ca_deallocs.end(), *it) << std::endl;
93 if ( std::count(ca_allocs.begin(), ca_allocs.end(), *it) != std::count(ca_deallocs.begin(), ca_deallocs.end(), *it))
98 std::sort(temp1.begin(), temp1.end());
100 std::unique_copy(temp1.begin(), temp1.end(), std::back_inserter<std::vector<int>>(temp2));
101 std::cout << "There were " << temp2.size() << " different (de)allocators\n";
102 for (std::vector<int>::const_iterator it = ca_deallocs.begin(); it != ca_deallocs.end(); ++it ) {
103 std::cout << *it << ": " << std::count(ca_allocs.begin(), ca_allocs.end(), *it) << " vs " << std::count(ca_deallocs.begin(), ca_deallocs.end(), *it) << std::endl;
104 if ( std::count(ca_allocs.begin(), ca_allocs.end(), *it) != std::count(ca_deallocs.begin(), ca_deallocs.end(), *it))
115 typedef std::pair<const int, double> V;
128 typedef test_compare<std::less<int> > C;
129 typedef test_allocator<V> A;
130 std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(2));
131 std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(7));
133 assert(m.get_allocator() == A(7));
134 assert(m.key_comp() == C(5));
135 assert(m.size() == 3);
136 assert(distance(m.begin(), m.end()) == 3);
137 assert(*m.begin() == V(1, 1));
138 assert(*next(m.begin()) == V(2, 1));
139 assert(*next(m.begin(), 2) == V(3, 1));
141 assert(mo.get_allocator() == A(2));
142 assert(mo.key_comp() == C(5));
143 assert(mo.size() == 3);
144 assert(distance(mo.begin(), mo.end()) == 3);
145 assert(*mo.begin() == V(1, 1));
146 assert(*next(mo.begin()) == V(2, 1));
147 assert(*next(mo.begin(), 2) == V(3, 1));
150 typedef std::pair<const int, double> V;
157 std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
158 std::map<int, double> *p = &m;
161 assert(m.size() == 3);
162 assert(std::equal(m.begin(), m.end(), ar));
165 typedef std::pair<const int, double> V;
178 typedef test_compare<std::less<int> > C;
179 typedef other_allocator<V> A;
180 std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(2));
181 std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(7));
183 assert(m.get_allocator() == A(2));
184 assert(m.key_comp() == C(5));
185 assert(m.size() == 3);
186 assert(distance(m.begin(), m.end()) == 3);
187 assert(*m.begin() == V(1, 1));
188 assert(*next(m.begin()) == V(2, 1));
189 assert(*next(m.begin(), 2) == V(3, 1));
191 assert(mo.get_allocator() == A(2));
192 assert(mo.key_comp() == C(5));
193 assert(mo.size() == 3);
194 assert(distance(mo.begin(), mo.end()) == 3);
195 assert(*mo.begin() == V(1, 1));
196 assert(*next(mo.begin()) == V(2, 1));
197 assert(*next(mo.begin(), 2) == V(3, 1));
199 #if TEST_STD_VER >= 11
201 typedef std::pair<const int, double> V;
214 typedef test_compare<std::less<int> > C;
215 typedef min_allocator<V> A;
216 std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A());
217 std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A());
219 assert(m.get_allocator() == A());
220 assert(m.key_comp() == C(5));
221 assert(m.size() == 3);
222 assert(distance(m.begin(), m.end()) == 3);
223 assert(*m.begin() == V(1, 1));
224 assert(*next(m.begin()) == V(2, 1));
225 assert(*next(m.begin(), 2) == V(3, 1));
227 assert(mo.get_allocator() == A());
228 assert(mo.key_comp() == C(5));
229 assert(mo.size() == 3);
230 assert(distance(mo.begin(), mo.end()) == 3);
231 assert(*mo.begin() == V(1, 1));
232 assert(*next(mo.begin()) == V(2, 1));
233 assert(*next(mo.begin(), 2) == V(3, 1));
236 typedef std::pair<const int, double> V;
249 typedef test_compare<std::less<int> > C;
250 typedef min_allocator<V> A;
251 std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A());
252 std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A());
254 assert(m.get_allocator() == A());
255 assert(m.key_comp() == C(5));
256 assert(m.size() == 3);
257 assert(distance(m.begin(), m.end()) == 3);
258 assert(*m.begin() == V(1, 1));
259 assert(*next(m.begin()) == V(2, 1));
260 assert(*next(m.begin(), 2) == V(3, 1));
262 assert(mo.get_allocator() == A());
263 assert(mo.key_comp() == C(5));
264 assert(mo.size() == 3);
265 assert(distance(mo.begin(), mo.end()) == 3);
266 assert(*mo.begin() == V(1, 1));
267 assert(*next(mo.begin()) == V(2, 1));
268 assert(*next(mo.begin(), 2) == V(3, 1));
271 assert(balanced_allocs());
273 typedef std::pair<const int, double> V;
286 typedef test_compare<std::less<int> > C;
287 typedef counting_allocatorT<V> A;
288 std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(1));
289 std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(2));
291 assert(m.key_comp() == C(5));
292 assert(m.size() == 3);
293 assert(distance(m.begin(), m.end()) == 3);
294 assert(*m.begin() == V(1, 1));
295 assert(*next(m.begin()) == V(2, 1));
296 assert(*next(m.begin(), 2) == V(3, 1));
298 assert(mo.key_comp() == C(5));
299 assert(mo.size() == 3);
300 assert(distance(mo.begin(), mo.end()) == 3);
301 assert(*mo.begin() == V(1, 1));
302 assert(*next(mo.begin()) == V(2, 1));
303 assert(*next(mo.begin(), 2) == V(3, 1));
305 assert(balanced_allocs());
307 typedef std::pair<const int, double> V;
320 typedef test_compare<std::less<int> > C;
321 typedef counting_allocatorF<V> A;
322 std::map<int, double, C, A> mo(ar, ar+sizeof(ar)/sizeof(ar[0]), C(5), A(100));
323 std::map<int, double, C, A> m(ar, ar+sizeof(ar)/sizeof(ar[0])/2, C(3), A(200));
325 assert(m.key_comp() == C(5));
326 assert(m.size() == 3);
327 assert(distance(m.begin(), m.end()) == 3);
328 assert(*m.begin() == V(1, 1));
329 assert(*next(m.begin()) == V(2, 1));
330 assert(*next(m.begin(), 2) == V(3, 1));
332 assert(mo.key_comp() == C(5));
333 assert(mo.size() == 3);
334 assert(distance(mo.begin(), mo.end()) == 3);
335 assert(*mo.begin() == V(1, 1));
336 assert(*next(mo.begin()) == V(2, 1));
337 assert(*next(mo.begin(), 2) == V(3, 1));
339 assert(balanced_allocs());