xref: /llvm-project/libcxx/test/std/containers/unord/unord.set/unord.set.swap/swap_noexcept.pass.cpp (revision 31cbe0f240f660f15602c96b787c58a26f17e179)
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_set>
12 
13 // void swap(unordered_set& 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_set>
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 
some_compsome_comp40     some_comp() {}
some_compsome_comp41     some_comp(const some_comp&) {}
operator ()some_comp42     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 
some_comp2some_comp250     some_comp2() {}
some_comp2some_comp251     some_comp2(const some_comp2&) {}
operator ()some_comp252     bool operator()(const T&, const T&) const { return false; }
53 };
54 
55 #if TEST_STD_VER >= 14
56 template <typename T>
swap(some_comp2<T> &,some_comp2<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;
some_hashsome_hash64     some_hash() {}
65     some_hash(const some_hash&);
operator ()some_hash66     std::size_t operator()(const T&) const { return 0; }
67 };
68 
69 template <class T>
70 struct some_hash2
71 {
72     typedef T value_type;
some_hash2some_hash273     some_hash2() {}
74     some_hash2(const some_hash2&);
operator ()some_hash275     std::size_t operator()(const T&) const { return 0; }
76 };
77 
78 #if TEST_STD_VER >= 14
79 template <typename T>
swap(some_hash2<T> &,some_hash2<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 
some_allocsome_alloc88     some_alloc() {}
89     some_alloc(const some_alloc&);
deallocatesome_alloc90     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 
some_alloc2some_alloc2100     some_alloc2() {}
101     some_alloc2(const some_alloc2&);
deallocatesome_alloc2102     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 
some_alloc3some_alloc3113     some_alloc3() {}
114     some_alloc3(const some_alloc3&);
deallocatesome_alloc3115     void deallocate(void*, unsigned) {}
116 
117     typedef std::false_type propagate_on_container_swap;
118     typedef std::false_type is_always_equal;
119 };
120 
main(int,char **)121 int main(int, char**)
122 {
123     {
124         typedef std::unordered_set<MoveOnly> C;
125         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
126     }
127 #if defined(_LIBCPP_VERSION)
128     {
129         typedef std::unordered_set<MoveOnly, std::hash<MoveOnly>,
130                            std::equal_to<MoveOnly>, test_allocator<MoveOnly>> C;
131         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
132     }
133     {
134         typedef std::unordered_set<MoveOnly, std::hash<MoveOnly>,
135                           std::equal_to<MoveOnly>, other_allocator<MoveOnly>> C;
136         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
137     }
138 #endif // _LIBCPP_VERSION
139     {
140         typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>> C;
141         static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
142     }
143     {
144         typedef std::unordered_set<MoveOnly, std::hash<MoveOnly>,
145                                                          some_comp<MoveOnly>> C;
146         static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
147     }
148 
149 #if TEST_STD_VER >= 14
150     { // POCS allocator, throwable swap for hash, throwable swap for comp
151     typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc <MoveOnly>> C;
152     static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
153     }
154     { // always equal allocator, throwable swap for hash, throwable swap for comp
155     typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc2<MoveOnly>> C;
156     static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
157     }
158     { // POCS allocator, throwable swap for hash, nothrow swap for comp
159     typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MoveOnly>> C;
160     static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
161     }
162     { // always equal allocator, throwable swap for hash, nothrow swap for comp
163     typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MoveOnly>> C;
164     static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
165     }
166     { // POCS allocator, nothrow swap for hash, throwable swap for comp
167     typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc <MoveOnly>> C;
168     static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
169     }
170     { // always equal allocator, nothrow swap for hash, throwable swap for comp
171     typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc2<MoveOnly>> C;
172     static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
173     }
174     { // POCS allocator, nothrow swap for hash, nothrow swap for comp
175     typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MoveOnly>> C;
176     static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
177     }
178     { // always equal allocator, nothrow swap for hash, nothrow swap for comp
179     typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MoveOnly>> C;
180     static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
181     }
182 #if defined(_LIBCPP_VERSION)
183     { // NOT always equal allocator, nothrow swap for hash, nothrow swap for comp
184     typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc3<MoveOnly>> C;
185     static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
186     }
187 #endif // _LIBCPP_VERSION
188 #endif
189 
190   return 0;
191 }
192