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 //===----------------------------------------------------------------------===//
11 // C++2a[container.requirements.general]p8
12 // Move constructors obtain an allocator by move construction from the allocator
13 // belonging to the container being moved. Such move construction of the
14 // allocator shall not exit via an exception.
19 #include <forward_list>
22 #include <unordered_map>
23 #include <unordered_set>
25 #include "test_macros.h"
26 #include "test_allocator.h"
29 void test(int expected_num_allocs
= 1) {
31 test_alloc_base::clear();
32 using AllocT
= typename
C::allocator_type
;
35 assert(test_alloc_base::count
== expected_num_allocs
);
37 const int num_stored_allocs
= test_alloc_base::count
;
39 const AllocT
& a
= v
.get_allocator();
40 assert(test_alloc_base::count
== 1 + num_stored_allocs
);
41 assert(a
.get_data() == 42);
42 assert(a
.get_id() == 101);
44 assert(test_alloc_base::count
== num_stored_allocs
);
45 test_alloc_base::clear_ctor_counters();
48 assert(test_alloc_base::count
== num_stored_allocs
* 2);
49 assert(test_alloc_base::copied
== 0);
50 assert(test_alloc_base::moved
== num_stored_allocs
);
52 const AllocT
& a
= v
.get_allocator();
53 assert(a
.get_id() == test_alloc_base::moved_value
);
54 assert(a
.get_data() == test_alloc_base::moved_value
);
57 const AllocT
& a
= v2
.get_allocator();
58 assert(a
.get_id() == 101);
59 assert(a
.get_data() == 42);
64 int main(int, char**) {
65 { // test sequence containers
66 test
<std::vector
<int, test_allocator
<int> > >();
67 test
<std::vector
<bool, test_allocator
<bool> > >();
68 test
<std::list
<int, test_allocator
<int> > >();
69 test
<std::forward_list
<int, test_allocator
<int> > >();
71 // libc++ stores two allocators in deque
72 #ifdef _LIBCPP_VERSION
73 int stored_allocators
= 2;
75 int stored_allocators
= 1;
77 test
<std::deque
<int, test_allocator
<int> > >(stored_allocators
);
79 { // test associative containers
80 test
<std::set
<int, std::less
<int>, test_allocator
<int> > >();
81 test
<std::multiset
<int, std::less
<int>, test_allocator
<int> > >();
83 using KV
= std::pair
<const int, int>;
84 test
<std::map
<int, int, std::less
<int>, test_allocator
<KV
> > >();
85 test
<std::multimap
<int, int, std::less
<int>, test_allocator
<KV
> > >();
87 { // test unordered containers
88 // libc++ stores two allocators in the unordered containers.
89 #ifdef _LIBCPP_VERSION
90 int stored_allocators
= 2;
92 int stored_allocators
= 1;
94 test
<std::unordered_set
<int, std::hash
<int>, std::equal_to
<int>,
95 test_allocator
<int> > >(stored_allocators
);
96 test
<std::unordered_multiset
<int, std::hash
<int>, std::equal_to
<int>,
97 test_allocator
<int> > >(stored_allocators
);
99 using KV
= std::pair
<const int, int>;
100 test
<std::unordered_map
<int, int, std::hash
<int>, std::equal_to
<int>,
101 test_allocator
<KV
> > >(stored_allocators
);
102 test
<std::unordered_multimap
<int, int, std::hash
<int>, std::equal_to
<int>,
103 test_allocator
<KV
> > >(stored_allocators
);