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