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 | // <unordered_map> |
12 | |
13 | // void swap(unordered_multimap& c) |
14 | // noexcept( |
15 | // (!allocator_type::propagate_on_container_swap::value || |
16 | // __is_nothrow_swappable<allocator_type>::value) && |
17 | // __is_nothrow_swappable<hasher>::value && |
18 | // __is_nothrow_swappable<key_equal>::value); |
19 | // |
20 | // In C++17, the standard says that swap shall have: |
21 | // noexcept(allocator_traits<allocator_type>::is_always_equal::value && |
22 | // __is_nothrow_swappable<hasher>::value && |
23 | // __is_nothrow_swappable<key_equal>::value); |
24 | |
25 | // This tests a conforming extension |
26 | |
27 | #include <unordered_map> |
28 | #include <utility> |
29 | #include <cassert> |
30 | |
31 | #include "test_macros.h" |
32 | #include "MoveOnly.h" |
33 | #include "test_allocator.h" |
34 | |
35 | template <class T> |
36 | struct some_comp |
37 | { |
38 | typedef T value_type; |
39 | |
40 | some_comp() {} |
41 | some_comp(const some_comp&) {} |
42 | bool operator()(const T&, const T&) const { return false; } |
43 | }; |
44 | |
45 | template <class T> |
46 | struct some_comp2 |
47 | { |
48 | typedef T value_type; |
49 | |
50 | some_comp2() {} |
51 | some_comp2(const some_comp2&) {} |
52 | bool operator()(const T&, const T&) const { return false; } |
53 | }; |
54 | |
55 | #if TEST_STD_VER >= 14 |
56 | template <typename T> |
57 | void swap(some_comp2<T>&, some_comp2<T>&) noexcept {} |
58 | #endif |
59 | |
60 | template <class T> |
61 | struct some_hash |
62 | { |
63 | typedef T value_type; |
64 | some_hash() {} |
65 | some_hash(const some_hash&); |
66 | std::size_t operator()(T const&) const; |
67 | }; |
68 | |
69 | template <class T> |
70 | struct some_hash2 |
71 | { |
72 | typedef T value_type; |
73 | some_hash2() {} |
74 | some_hash2(const some_hash2&); |
75 | std::size_t operator()(T const&) const; |
76 | }; |
77 | |
78 | #if TEST_STD_VER >= 14 |
79 | template <typename T> |
80 | void swap(some_hash2<T>&, some_hash2<T>&) noexcept {} |
81 | #endif |
82 | |
83 | template <class T> |
84 | struct some_alloc |
85 | { |
86 | typedef T value_type; |
87 | |
88 | some_alloc() {} |
89 | some_alloc(const some_alloc&); |
90 | void deallocate(void*, unsigned) {} |
91 | |
92 | typedef std::true_type propagate_on_container_swap; |
93 | }; |
94 | |
95 | template <class T> |
96 | struct some_alloc2 |
97 | { |
98 | typedef T value_type; |
99 | |
100 | some_alloc2() {} |
101 | some_alloc2(const some_alloc2&); |
102 | void deallocate(void*, unsigned) {} |
103 | |
104 | typedef std::false_type propagate_on_container_swap; |
105 | typedef std::true_type is_always_equal; |
106 | }; |
107 | |
108 | template <class T> |
109 | struct some_alloc3 |
110 | { |
111 | typedef T value_type; |
112 | |
113 | some_alloc3() {} |
114 | some_alloc3(const some_alloc3&); |
115 | void deallocate(void*, unsigned) {} |
116 | |
117 | typedef std::false_type propagate_on_container_swap; |
118 | typedef std::false_type is_always_equal; |
119 | }; |
120 | |
121 | int main(int, char**) |
122 | { |
123 | typedef std::pair<const MoveOnly, MoveOnly> V; |
124 | { |
125 | typedef std::unordered_multimap<MoveOnly, MoveOnly> C; |
126 | static_assert(noexcept(swap(a&: std::declval<C&>(), b&: std::declval<C&>())), "" ); |
127 | } |
128 | #if defined(_LIBCPP_VERSION) |
129 | { |
130 | typedef std::unordered_multimap<MoveOnly, MoveOnly, std::hash<MoveOnly>, |
131 | std::equal_to<MoveOnly>, test_allocator<V>> C; |
132 | static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
133 | } |
134 | { |
135 | typedef std::unordered_multimap<MoveOnly, MoveOnly, std::hash<MoveOnly>, |
136 | std::equal_to<MoveOnly>, other_allocator<V>> C; |
137 | static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
138 | } |
139 | #endif // _LIBCPP_VERSION |
140 | { |
141 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>> C; |
142 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
143 | } |
144 | { |
145 | typedef std::unordered_multimap<MoveOnly, MoveOnly, std::hash<MoveOnly>, |
146 | some_comp<MoveOnly>> C; |
147 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
148 | } |
149 | |
150 | #if TEST_STD_VER >= 14 |
151 | { // POCS allocator, throwable swap for hash, throwable swap for comp |
152 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc <V>> C; |
153 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
154 | } |
155 | { // always equal allocator, throwable swap for hash, throwable swap for comp |
156 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc2<V>> C; |
157 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
158 | } |
159 | { // POCS allocator, throwable swap for hash, nothrow swap for comp |
160 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc <V>> C; |
161 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
162 | } |
163 | { // always equal allocator, throwable swap for hash, nothrow swap for comp |
164 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<V>> C; |
165 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
166 | } |
167 | { // POCS allocator, nothrow swap for hash, throwable swap for comp |
168 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc <V>> C; |
169 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
170 | } |
171 | { // always equal allocator, nothrow swap for hash, throwable swap for comp |
172 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc2<V>> C; |
173 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
174 | } |
175 | { // POCS allocator, nothrow swap for hash, nothrow swap for comp |
176 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc <V>> C; |
177 | static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
178 | } |
179 | { // always equal allocator, nothrow swap for hash, nothrow swap for comp |
180 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<V>> C; |
181 | static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
182 | } |
183 | #if defined(_LIBCPP_VERSION) |
184 | { // NOT always equal allocator, nothrow swap for hash, nothrow swap for comp |
185 | typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc3<V>> C; |
186 | static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
187 | } |
188 | #endif // _LIBCPP_VERSION |
189 | #endif |
190 | |
191 | return 0; |
192 | } |
193 | |