Howard Hinnant | 04dae1d | 2011-06-04 20:18:37 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <unordered_set> |
| 11 | |
| 12 | // void swap(unordered_set& c) |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 13 | // noexcept( |
| 14 | // (!allocator_type::propagate_on_container_swap::value || |
| 15 | // __is_nothrow_swappable<allocator_type>::value) && |
| 16 | // __is_nothrow_swappable<hasher>::value && |
| 17 | // __is_nothrow_swappable<key_equal>::value); |
| 18 | // |
| 19 | // In C++17, the standard says that swap shall have: |
| 20 | // noexcept(allocator_traits<Allocator>::is_always_equal::value && |
| 21 | // noexcept(swap(declval<Hash&>(), declval<Hash&>())) && |
| 22 | // noexcept(swap(declval<Pred&>(), declval<Pred&>()))); |
Howard Hinnant | 04dae1d | 2011-06-04 20:18:37 +0000 | [diff] [blame] | 23 | |
| 24 | // This tests a conforming extension |
| 25 | |
| 26 | #include <unordered_set> |
| 27 | #include <cassert> |
| 28 | |
Marshall Clow | df00d5e | 2015-01-28 21:22:53 +0000 | [diff] [blame] | 29 | #include "MoveOnly.h" |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 30 | #include "test_allocator.h" |
Howard Hinnant | 04dae1d | 2011-06-04 20:18:37 +0000 | [diff] [blame] | 31 | |
| 32 | template <class T> |
| 33 | struct some_comp |
| 34 | { |
| 35 | typedef T value_type; |
| 36 | |
| 37 | some_comp() {} |
| 38 | some_comp(const some_comp&) {} |
| 39 | }; |
| 40 | |
| 41 | template <class T> |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 42 | struct some_comp2 |
| 43 | { |
| 44 | typedef T value_type; |
| 45 | |
| 46 | some_comp2() {} |
| 47 | some_comp2(const some_comp2&) {} |
| 48 | void deallocate(void*, unsigned) {} |
| 49 | typedef std::true_type propagate_on_container_swap; |
| 50 | }; |
| 51 | |
| 52 | #if TEST_STD_VER >= 14 |
| 53 | template <typename T> |
| 54 | void swap(some_comp2<T>&, some_comp2<T>&) noexcept {} |
| 55 | #endif |
| 56 | |
| 57 | template <class T> |
Howard Hinnant | 04dae1d | 2011-06-04 20:18:37 +0000 | [diff] [blame] | 58 | struct some_hash |
| 59 | { |
| 60 | typedef T value_type; |
| 61 | some_hash() {} |
| 62 | some_hash(const some_hash&); |
| 63 | }; |
| 64 | |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 65 | template <class T> |
| 66 | struct some_hash2 |
| 67 | { |
| 68 | typedef T value_type; |
| 69 | some_hash2() {} |
| 70 | some_hash2(const some_hash2&); |
| 71 | }; |
| 72 | |
| 73 | #if TEST_STD_VER >= 14 |
| 74 | template <typename T> |
| 75 | void swap(some_hash2<T>&, some_hash2<T>&) noexcept {} |
| 76 | #endif |
| 77 | |
| 78 | template <class T> |
| 79 | struct some_alloc |
| 80 | { |
| 81 | typedef T value_type; |
| 82 | |
| 83 | some_alloc() {} |
| 84 | some_alloc(const some_alloc&); |
| 85 | void deallocate(void*, unsigned) {} |
| 86 | |
| 87 | typedef std::true_type propagate_on_container_swap; |
| 88 | }; |
| 89 | |
| 90 | template <class T> |
| 91 | struct some_alloc2 |
| 92 | { |
| 93 | typedef T value_type; |
| 94 | |
| 95 | some_alloc2() {} |
| 96 | some_alloc2(const some_alloc2&); |
| 97 | void deallocate(void*, unsigned) {} |
| 98 | |
| 99 | typedef std::false_type propagate_on_container_swap; |
| 100 | typedef std::true_type is_always_equal; |
| 101 | }; |
| 102 | |
| 103 | template <class T> |
| 104 | struct some_alloc3 |
| 105 | { |
| 106 | typedef T value_type; |
| 107 | |
| 108 | some_alloc3() {} |
| 109 | some_alloc3(const some_alloc3&); |
| 110 | void deallocate(void*, unsigned) {} |
| 111 | |
| 112 | typedef std::false_type propagate_on_container_swap; |
| 113 | typedef std::false_type is_always_equal; |
| 114 | }; |
| 115 | |
Howard Hinnant | 04dae1d | 2011-06-04 20:18:37 +0000 | [diff] [blame] | 116 | int main() |
| 117 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 118 | #if __has_feature(cxx_noexcept) |
Howard Hinnant | 04dae1d | 2011-06-04 20:18:37 +0000 | [diff] [blame] | 119 | { |
| 120 | typedef std::unordered_set<MoveOnly> C; |
| 121 | C c1, c2; |
| 122 | static_assert(noexcept(swap(c1, c2)), ""); |
| 123 | } |
| 124 | { |
| 125 | typedef std::unordered_set<MoveOnly, std::hash<MoveOnly>, |
| 126 | std::equal_to<MoveOnly>, test_allocator<MoveOnly>> C; |
| 127 | C c1, c2; |
| 128 | static_assert(noexcept(swap(c1, c2)), ""); |
| 129 | } |
| 130 | { |
| 131 | typedef std::unordered_set<MoveOnly, std::hash<MoveOnly>, |
| 132 | std::equal_to<MoveOnly>, other_allocator<MoveOnly>> C; |
| 133 | C c1, c2; |
| 134 | static_assert(noexcept(swap(c1, c2)), ""); |
| 135 | } |
| 136 | { |
| 137 | typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>> C; |
| 138 | C c1, c2; |
| 139 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 140 | } |
| 141 | { |
| 142 | typedef std::unordered_set<MoveOnly, std::hash<MoveOnly>, |
| 143 | some_comp<MoveOnly>> C; |
| 144 | C c1, c2; |
| 145 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 146 | } |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 147 | |
| 148 | #if TEST_STD_VER >= 14 |
| 149 | { // POCS allocator, throwable swap for hash, throwable swap for comp |
| 150 | typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc <MoveOnly>> C; |
| 151 | C c1, c2; |
| 152 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 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 | C c1, c2; |
| 157 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 158 | } |
| 159 | { // POCS allocator, throwable swap for hash, nothrow swap for comp |
| 160 | typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MoveOnly>> C; |
| 161 | C c1, c2; |
| 162 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 163 | } |
| 164 | { // always equal allocator, throwable swap for hash, nothrow swap for comp |
| 165 | typedef std::unordered_set<MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MoveOnly>> C; |
| 166 | C c1, c2; |
| 167 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 168 | } |
| 169 | { // POCS allocator, nothrow swap for hash, throwable swap for comp |
| 170 | typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc <MoveOnly>> C; |
| 171 | C c1, c2; |
| 172 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 173 | } |
| 174 | { // always equal allocator, nothrow swap for hash, throwable swap for comp |
| 175 | typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc2<MoveOnly>> C; |
| 176 | C c1, c2; |
| 177 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 178 | } |
| 179 | { // POCS allocator, nothrow swap for hash, nothrow swap for comp |
| 180 | typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MoveOnly>> C; |
| 181 | C c1, c2; |
| 182 | static_assert( noexcept(swap(c1, c2)), ""); |
| 183 | } |
| 184 | { // always equal allocator, nothrow swap for hash, nothrow swap for comp |
| 185 | typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MoveOnly>> C; |
| 186 | C c1, c2; |
| 187 | static_assert( noexcept(swap(c1, c2)), ""); |
| 188 | } |
| 189 | |
| 190 | { // NOT always equal allocator, nothrow swap for hash, nothrow swap for comp |
| 191 | typedef std::unordered_set<MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc3<MoveOnly>> C; |
| 192 | C c1, c2; |
| 193 | static_assert( noexcept(swap(c1, c2)), ""); |
| 194 | } |
| 195 | #endif |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 196 | |
| 197 | #endif |
Howard Hinnant | 04dae1d | 2011-06-04 20:18:37 +0000 | [diff] [blame] | 198 | } |