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_map& 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>::is_always_equal::value && |
22 | // noexcept(swap(declval<Hash&>(), declval<Hash&>())) && |
23 | // noexcept(swap(declval<Pred&>(), declval<Pred&>()))); |
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 | |
122 | int main(int, char**) |
123 | { |
124 | typedef std::pair<const MoveOnly, MoveOnly> MapType; |
125 | { |
126 | typedef std::unordered_map<MoveOnly, MoveOnly> C; |
127 | static_assert(noexcept(swap(a&: std::declval<C&>(), b&: std::declval<C&>())), "" ); |
128 | } |
129 | #if defined(_LIBCPP_VERSION) |
130 | { |
131 | typedef std::unordered_map<MoveOnly, MoveOnly, std::hash<MoveOnly>, |
132 | std::equal_to<MoveOnly>, test_allocator<MapType>> C; |
133 | static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
134 | } |
135 | { |
136 | typedef std::unordered_map<MoveOnly, MoveOnly, std::hash<MoveOnly>, |
137 | std::equal_to<MoveOnly>, other_allocator<MapType>> C; |
138 | static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
139 | } |
140 | #endif // _LIBCPP_VERSION |
141 | { |
142 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash<MoveOnly>> C; |
143 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
144 | } |
145 | { |
146 | typedef std::unordered_map<MoveOnly, MoveOnly, std::hash<MoveOnly>, |
147 | some_comp<MoveOnly>> C; |
148 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
149 | } |
150 | |
151 | #if TEST_STD_VER >= 14 |
152 | { // POCS allocator, throwable swap for hash, throwable swap for comp |
153 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc <MapType>> C; |
154 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
155 | } |
156 | { // always equal allocator, throwable swap for hash, throwable swap for comp |
157 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc2<MapType>> C; |
158 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
159 | } |
160 | { // POCS allocator, throwable swap for hash, nothrow swap for comp |
161 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MapType>> C; |
162 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
163 | } |
164 | { // always equal allocator, throwable swap for hash, nothrow swap for comp |
165 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MapType>> C; |
166 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
167 | } |
168 | { // POCS allocator, nothrow swap for hash, throwable swap for comp |
169 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc <MapType>> C; |
170 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
171 | } |
172 | { // always equal allocator, nothrow swap for hash, throwable swap for comp |
173 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc2<MapType>> C; |
174 | static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
175 | } |
176 | { // POCS allocator, nothrow swap for hash, nothrow swap for comp |
177 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MapType>> C; |
178 | static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
179 | } |
180 | { // always equal allocator, nothrow swap for hash, nothrow swap for comp |
181 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MapType>> C; |
182 | static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
183 | } |
184 | #if defined(_LIBCPP_VERSION) |
185 | { // NOT always equal allocator, nothrow swap for hash, nothrow swap for comp |
186 | typedef std::unordered_map<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc3<MapType>> C; |
187 | static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "" ); |
188 | } |
189 | #endif // _LIBCPP_VERSION |
190 | #endif |
191 | |
192 | return 0; |
193 | } |
194 | |