| 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, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <flat_set> |
| 12 | |
| 13 | // flat_set(flat_set&&); |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <deque> |
| 17 | #include <flat_set> |
| 18 | #include <functional> |
| 19 | #include <utility> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "../helpers.h" |
| 23 | #include "test_macros.h" |
| 24 | #include "../../../test_compare.h" |
| 25 | #include "test_allocator.h" |
| 26 | #include "min_allocator.h" |
| 27 | |
| 28 | template <template <class...> class KeyContainer> |
| 29 | constexpr void test() { |
| 30 | { |
| 31 | using C = test_less<int>; |
| 32 | using A = test_allocator<int>; |
| 33 | using M = std::flat_set<int, C, KeyContainer<int, A>>; |
| 34 | M mo = M({1, 2, 3}, C(5), A(7)); |
| 35 | M m = std::move(mo); |
| 36 | assert((m == M{1, 2, 3})); |
| 37 | assert(m.key_comp() == C(5)); |
| 38 | assert(std::move(m).extract().get_allocator() == A(7)); |
| 39 | |
| 40 | assert(mo.empty()); |
| 41 | assert(mo.key_comp() == C(5)); |
| 42 | assert(std::move(mo).extract().get_allocator().get_id() == test_alloc_base::moved_value); |
| 43 | } |
| 44 | { |
| 45 | using C = test_less<int>; |
| 46 | using A = min_allocator<int>; |
| 47 | using M = std::flat_set<int, C, KeyContainer<int, A>>; |
| 48 | M mo = M({1, 2, 3}, C(5), A()); |
| 49 | M m = std::move(mo); |
| 50 | assert((m == M{1, 2, 3})); |
| 51 | assert(m.key_comp() == C(5)); |
| 52 | assert(std::move(m).extract().get_allocator() == A()); |
| 53 | |
| 54 | assert(mo.empty()); |
| 55 | assert(mo.key_comp() == C(5)); |
| 56 | assert(std::move(mo).extract().get_allocator() == A()); |
| 57 | } |
| 58 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 59 | // A moved-from flat_set maintains its class invariant in the presence of moved-from comparators. |
| 60 | using M = std::flat_set<int, std::function<bool(int, int)>, KeyContainer<int>>; |
| 61 | M mo = M({1, 2, 3}, std::less<int>()); |
| 62 | M m = std::move(mo); |
| 63 | assert(m.size() == 3); |
| 64 | assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); |
| 65 | assert(m.key_comp()(1, 2) == true); |
| 66 | |
| 67 | assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp())); |
| 68 | LIBCPP_ASSERT(m.key_comp()(1, 2) == true); |
| 69 | LIBCPP_ASSERT(mo.empty()); |
| 70 | mo.insert({1, 2, 3}); // insert has no preconditions |
| 71 | assert(m == mo); |
| 72 | } |
| 73 | { |
| 74 | // moved-from object maintains invariant if the underlying container does not clear after move |
| 75 | using M = std::flat_set<int, std::less<>, CopyOnlyVector<int>>; |
| 76 | M m1 = M({1, 2, 3}); |
| 77 | M m2 = std::move(m1); |
| 78 | assert(m2.size() == 3); |
| 79 | check_invariant(m1); |
| 80 | LIBCPP_ASSERT(m1.empty()); |
| 81 | LIBCPP_ASSERT(m1.size() == 0); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | template <class T> |
| 86 | struct ThrowingMoveAllocator { |
| 87 | using value_type = T; |
| 88 | explicit ThrowingMoveAllocator() = default; |
| 89 | ThrowingMoveAllocator(const ThrowingMoveAllocator&) = default; |
| 90 | constexpr ThrowingMoveAllocator(ThrowingMoveAllocator&&) noexcept(false) {} |
| 91 | constexpr T* allocate(std::ptrdiff_t n) { return std::allocator<T>().allocate(n); } |
| 92 | constexpr void deallocate(T* p, std::ptrdiff_t n) { return std::allocator<T>().deallocate(p, n); } |
| 93 | friend bool operator==(ThrowingMoveAllocator, ThrowingMoveAllocator) = default; |
| 94 | }; |
| 95 | |
| 96 | struct ThrowingMoveComp { |
| 97 | ThrowingMoveComp() = default; |
| 98 | constexpr ThrowingMoveComp(const ThrowingMoveComp&) noexcept(true) {} |
| 99 | constexpr ThrowingMoveComp(ThrowingMoveComp&&) noexcept(false) {} |
| 100 | constexpr bool operator()(const auto&, const auto&) const { return false; } |
| 101 | }; |
| 102 | |
| 103 | template <template <class...> class KeyContainer> |
| 104 | constexpr void test_move_noexcept() { |
| 105 | { |
| 106 | using C = std::flat_set<int, std::less<int>, KeyContainer<int>>; |
| 107 | LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>); |
| 108 | C c; |
| 109 | C d = std::move(c); |
| 110 | } |
| 111 | { |
| 112 | using C = std::flat_set<int, std::less<int>, KeyContainer<int, test_allocator<int>>>; |
| 113 | LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>); |
| 114 | C c; |
| 115 | C d = std::move(c); |
| 116 | } |
| 117 | #if _LIBCPP_VERSION |
| 118 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 119 | // Container fails to be nothrow-move-constructible; this relies on libc++'s support for non-nothrow-copyable allocators |
| 120 | using C = std::flat_set<int, std::less<int>, std::deque<int, ThrowingMoveAllocator<int>>>; |
| 121 | static_assert(!std::is_nothrow_move_constructible_v<std::deque<int, ThrowingMoveAllocator<int>>>); |
| 122 | static_assert(!std::is_nothrow_move_constructible_v<C>); |
| 123 | C c; |
| 124 | C d = std::move(c); |
| 125 | } |
| 126 | #endif // _LIBCPP_VERSION |
| 127 | { |
| 128 | // Comparator fails to be nothrow-move-constructible |
| 129 | using C = std::flat_set<int, ThrowingMoveComp, KeyContainer<int>>; |
| 130 | static_assert(!std::is_nothrow_move_constructible_v<C>); |
| 131 | C c; |
| 132 | C d = std::move(c); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | constexpr bool test() { |
| 137 | test<std::vector>(); |
| 138 | test_move_noexcept<std::vector>(); |
| 139 | #ifndef __cpp_lib_constexpr_deque |
| 140 | if (!TEST_IS_CONSTANT_EVALUATED) |
| 141 | #endif |
| 142 | { |
| 143 | test<std::deque>(); |
| 144 | test_move_noexcept<std::deque>(); |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | #if !defined(TEST_HAS_NO_EXCEPTIONS) |
| 150 | static int countdown = 0; |
| 151 | |
| 152 | struct EvilContainer : std::vector<int> { |
| 153 | EvilContainer() = default; |
| 154 | EvilContainer(EvilContainer&& rhs) { |
| 155 | // Throw on move-construction. |
| 156 | if (--countdown == 0) { |
| 157 | rhs.insert(position: rhs.end(), x: 0); |
| 158 | rhs.insert(position: rhs.end(), x: 0); |
| 159 | throw 42; |
| 160 | } |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | void test_move_exception() { |
| 165 | { |
| 166 | using M = std::flat_set<int, std::less<int>, EvilContainer>; |
| 167 | M mo = {1, 2, 3}; |
| 168 | countdown = 1; |
| 169 | try { |
| 170 | M m = std::move(mo); |
| 171 | assert(false); // not reached |
| 172 | } catch (int x) { |
| 173 | assert(x == 42); |
| 174 | } |
| 175 | // The source flat_set maintains its class invariant. |
| 176 | check_invariant(mo); |
| 177 | LIBCPP_ASSERT(mo.empty()); |
| 178 | } |
| 179 | } |
| 180 | #endif // !defined(TEST_HAS_NO_EXCEPTIONS) |
| 181 | |
| 182 | int main(int, char**) { |
| 183 | test(); |
| 184 | #if !defined(TEST_HAS_NO_EXCEPTIONS) |
| 185 | test_move_exception(); |
| 186 | #endif |
| 187 | #if TEST_STD_VER >= 26 |
| 188 | static_assert(test()); |
| 189 | #endif |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |