1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 // map& operator=(const map& m);
22 #include "test_macros.h"
23 #include "../../../test_compare.h"
24 #include "test_allocator.h"
25 #include "min_allocator.h"
27 #if TEST_STD_VER >= 11
28 std::vector
<int> ca_allocs
;
29 std::vector
<int> ca_deallocs
;
32 class counting_allocatorT
{
36 counting_allocatorT(int f
) noexcept
: foo(f
) {}
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
; }
43 T
* allocate(std::size_t n
) const {
44 ca_allocs
.push_back(foo
);
45 void * const pv
= ::malloc(n
* sizeof(T
));
46 return static_cast<T
*>(pv
);
48 void deallocate(T
* p
, std::size_t) const noexcept
{
49 ca_deallocs
.push_back(foo
);
55 class counting_allocatorF
{
59 counting_allocatorF(int f
) noexcept
: foo(f
) {}
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
; }
66 T
* allocate(std::size_t n
) const {
67 ca_allocs
.push_back(foo
);
68 void * const pv
= ::malloc(n
* sizeof(T
));
69 return static_cast<T
*>(pv
);
71 void deallocate(T
* p
, std::size_t) const noexcept
{
72 ca_deallocs
.push_back(foo
);
77 bool balanced_allocs() {
78 std::vector
<int> temp1
, temp2
;
80 std::printf("Allocations = %zu, deallocations = %zu\n", ca_allocs
.size(),
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::printf("There were %zu different allocators\n", temp2
.size());
91 for (std::vector
<int>::const_iterator it
= temp2
.begin(); it
!= temp2
.end(); ++it
) {
92 std::ptrdiff_t const allocs
= std::count(ca_allocs
.begin(), ca_allocs
.end(), *it
);
93 std::ptrdiff_t const deallocs
= std::count(ca_deallocs
.begin(), ca_deallocs
.end(), *it
);
94 std::printf("%d: %td vs %td\n", *it
, allocs
, deallocs
);
95 if (allocs
!= deallocs
)
100 std::sort(temp1
.begin(), temp1
.end());
102 std::unique_copy(temp1
.begin(), temp1
.end(), std::back_inserter
<std::vector
<int>>(temp2
));
103 std::printf("There were %zu different (de)allocators\n", temp2
.size());
105 for (std::vector
<int>::const_iterator it
= ca_deallocs
.begin(); it
!= ca_deallocs
.end(); ++it
) {
106 std::ptrdiff_t const allocs
= std::count(ca_allocs
.begin(), ca_allocs
.end(), *it
);
107 std::ptrdiff_t const deallocs
= std::count(ca_deallocs
.begin(), ca_deallocs
.end(), *it
);
108 std::printf("%d: %td vs %td\n", *it
, allocs
, deallocs
);
109 if (allocs
!= deallocs
)
117 int main(int, char**)
120 typedef std::pair
<const int, double> V
;
133 typedef test_less
<int> C
;
134 typedef test_allocator
<V
> A
;
135 std::map
<int, double, C
, A
> mo(ar
, ar
+sizeof(ar
)/sizeof(ar
[0]), C(5), A(2));
136 std::map
<int, double, C
, A
> m(ar
, ar
+sizeof(ar
)/sizeof(ar
[0])/2, C(3), A(7));
138 assert(m
.get_allocator() == A(7));
139 assert(m
.key_comp() == C(5));
140 assert(m
.size() == 3);
141 assert(std::distance(m
.begin(), m
.end()) == 3);
142 assert(*m
.begin() == V(1, 1));
143 assert(*std::next(m
.begin()) == V(2, 1));
144 assert(*std::next(m
.begin(), 2) == V(3, 1));
146 assert(mo
.get_allocator() == A(2));
147 assert(mo
.key_comp() == C(5));
148 assert(mo
.size() == 3);
149 assert(std::distance(mo
.begin(), mo
.end()) == 3);
150 assert(*mo
.begin() == V(1, 1));
151 assert(*std::next(mo
.begin()) == V(2, 1));
152 assert(*std::next(mo
.begin(), 2) == V(3, 1));
155 typedef std::pair
<const int, double> V
;
162 std::map
<int, double> m(ar
, ar
+sizeof(ar
)/sizeof(ar
[0]));
163 std::map
<int, double> *p
= &m
;
166 assert(m
.size() == 3);
167 assert(std::equal(m
.begin(), m
.end(), ar
));
170 typedef std::pair
<const int, double> V
;
183 typedef test_less
<int> C
;
184 typedef other_allocator
<V
> A
;
185 std::map
<int, double, C
, A
> mo(ar
, ar
+sizeof(ar
)/sizeof(ar
[0]), C(5), A(2));
186 std::map
<int, double, C
, A
> m(ar
, ar
+sizeof(ar
)/sizeof(ar
[0])/2, C(3), A(7));
188 assert(m
.get_allocator() == A(2));
189 assert(m
.key_comp() == C(5));
190 assert(m
.size() == 3);
191 assert(std::distance(m
.begin(), m
.end()) == 3);
192 assert(*m
.begin() == V(1, 1));
193 assert(*std::next(m
.begin()) == V(2, 1));
194 assert(*std::next(m
.begin(), 2) == V(3, 1));
196 assert(mo
.get_allocator() == A(2));
197 assert(mo
.key_comp() == C(5));
198 assert(mo
.size() == 3);
199 assert(std::distance(mo
.begin(), mo
.end()) == 3);
200 assert(*mo
.begin() == V(1, 1));
201 assert(*std::next(mo
.begin()) == V(2, 1));
202 assert(*std::next(mo
.begin(), 2) == V(3, 1));
204 #if TEST_STD_VER >= 11
206 typedef std::pair
<const int, double> V
;
219 typedef test_less
<int> C
;
220 typedef min_allocator
<V
> A
;
221 std::map
<int, double, C
, A
> mo(ar
, ar
+sizeof(ar
)/sizeof(ar
[0]), C(5), A());
222 std::map
<int, double, C
, A
> m(ar
, ar
+sizeof(ar
)/sizeof(ar
[0])/2, C(3), A());
224 assert(m
.get_allocator() == A());
225 assert(m
.key_comp() == C(5));
226 assert(m
.size() == 3);
227 assert(std::distance(m
.begin(), m
.end()) == 3);
228 assert(*m
.begin() == V(1, 1));
229 assert(*std::next(m
.begin()) == V(2, 1));
230 assert(*std::next(m
.begin(), 2) == V(3, 1));
232 assert(mo
.get_allocator() == A());
233 assert(mo
.key_comp() == C(5));
234 assert(mo
.size() == 3);
235 assert(std::distance(mo
.begin(), mo
.end()) == 3);
236 assert(*mo
.begin() == V(1, 1));
237 assert(*std::next(mo
.begin()) == V(2, 1));
238 assert(*std::next(mo
.begin(), 2) == V(3, 1));
241 typedef std::pair
<const int, double> V
;
254 typedef test_less
<int> C
;
255 typedef min_allocator
<V
> A
;
256 std::map
<int, double, C
, A
> mo(ar
, ar
+sizeof(ar
)/sizeof(ar
[0]), C(5), A());
257 std::map
<int, double, C
, A
> m(ar
, ar
+sizeof(ar
)/sizeof(ar
[0])/2, C(3), A());
259 assert(m
.get_allocator() == A());
260 assert(m
.key_comp() == C(5));
261 assert(m
.size() == 3);
262 assert(std::distance(m
.begin(), m
.end()) == 3);
263 assert(*m
.begin() == V(1, 1));
264 assert(*std::next(m
.begin()) == V(2, 1));
265 assert(*std::next(m
.begin(), 2) == V(3, 1));
267 assert(mo
.get_allocator() == A());
268 assert(mo
.key_comp() == C(5));
269 assert(mo
.size() == 3);
270 assert(std::distance(mo
.begin(), mo
.end()) == 3);
271 assert(*mo
.begin() == V(1, 1));
272 assert(*std::next(mo
.begin()) == V(2, 1));
273 assert(*std::next(mo
.begin(), 2) == V(3, 1));
276 assert(balanced_allocs());
278 typedef std::pair
<const int, double> V
;
291 typedef test_less
<int> C
;
292 typedef counting_allocatorT
<V
> A
;
293 std::map
<int, double, C
, A
> mo(ar
, ar
+sizeof(ar
)/sizeof(ar
[0]), C(5), A(1));
294 std::map
<int, double, C
, A
> m(ar
, ar
+sizeof(ar
)/sizeof(ar
[0])/2, C(3), A(2));
296 assert(m
.key_comp() == C(5));
297 assert(m
.size() == 3);
298 assert(std::distance(m
.begin(), m
.end()) == 3);
299 assert(*m
.begin() == V(1, 1));
300 assert(*std::next(m
.begin()) == V(2, 1));
301 assert(*std::next(m
.begin(), 2) == V(3, 1));
303 assert(mo
.key_comp() == C(5));
304 assert(mo
.size() == 3);
305 assert(std::distance(mo
.begin(), mo
.end()) == 3);
306 assert(*mo
.begin() == V(1, 1));
307 assert(*std::next(mo
.begin()) == V(2, 1));
308 assert(*std::next(mo
.begin(), 2) == V(3, 1));
310 assert(balanced_allocs());
312 typedef std::pair
<const int, double> V
;
325 typedef test_less
<int> C
;
326 typedef counting_allocatorF
<V
> A
;
327 std::map
<int, double, C
, A
> mo(ar
, ar
+sizeof(ar
)/sizeof(ar
[0]), C(5), A(100));
328 std::map
<int, double, C
, A
> m(ar
, ar
+sizeof(ar
)/sizeof(ar
[0])/2, C(3), A(200));
330 assert(m
.key_comp() == C(5));
331 assert(m
.size() == 3);
332 assert(std::distance(m
.begin(), m
.end()) == 3);
333 assert(*m
.begin() == V(1, 1));
334 assert(*std::next(m
.begin()) == V(2, 1));
335 assert(*std::next(m
.begin(), 2) == V(3, 1));
337 assert(mo
.key_comp() == C(5));
338 assert(mo
.size() == 3);
339 assert(std::distance(mo
.begin(), mo
.end()) == 3);
340 assert(*mo
.begin() == V(1, 1));
341 assert(*std::next(mo
.begin()) == V(2, 1));
342 assert(*std::next(mo
.begin(), 2) == V(3, 1));
344 assert(balanced_allocs());