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& operator=(flat_set&&);
14
15#include <algorithm>
16#include <deque>
17#include <flat_set>
18#include <functional>
19#include <string>
20#include <utility>
21#include <vector>
22
23#include "test_macros.h"
24#include "MoveOnly.h"
25#include "../helpers.h"
26#include "../../../test_compare.h"
27#include "test_allocator.h"
28#include "min_allocator.h"
29
30struct MoveNegates {
31 int value_ = 0;
32 MoveNegates() = default;
33 MoveNegates(int v) : value_(v) {}
34 MoveNegates(MoveNegates&& rhs) : value_(rhs.value_) { rhs.value_ = -rhs.value_; }
35 MoveNegates& operator=(MoveNegates&& rhs) {
36 value_ = rhs.value_;
37 rhs.value_ = -rhs.value_;
38 return *this;
39 }
40 ~MoveNegates() = default;
41 auto operator<=>(const MoveNegates&) const = default;
42};
43
44struct MoveClears {
45 int value_ = 0;
46 MoveClears() = default;
47 MoveClears(int v) : value_(v) {}
48 MoveClears(MoveClears&& rhs) : value_(rhs.value_) { rhs.value_ = 0; }
49 MoveClears& operator=(MoveClears&& rhs) {
50 value_ = rhs.value_;
51 rhs.value_ = 0;
52 return *this;
53 }
54 ~MoveClears() = default;
55 auto operator<=>(const MoveClears&) const = default;
56};
57
58#if !defined(TEST_HAS_NO_EXCEPTIONS)
59struct MoveAssignThrows : std::vector<int> {
60 using std::vector<int>::vector;
61 MoveAssignThrows& operator=(MoveAssignThrows&& other) {
62 push_back(x: 0);
63 push_back(x: 0);
64 other.push_back(x: 0);
65 other.push_back(x: 0);
66 throw 42;
67 }
68};
69#endif // TEST_HAS_NO_EXCEPTIONS
70
71void test_move_assign_clears() {
72 // Preserves the class invariant for the moved-from flat_set.
73 {
74 const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
75 using M = std::flat_set<MoveNegates, std::less<MoveNegates>>;
76 M m = M(expected, expected + 8);
77 M m2 = M(expected, expected + 3);
78
79 m2 = std::move(m);
80
81 assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
82 LIBCPP_ASSERT(m.empty());
83 assert(std::is_sorted(m.begin(), m.end(), m.key_comp())); // still sorted
84 assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
85 m.insert(1);
86 m.insert(2);
87 assert(m.contains(1));
88 assert(m.find(2) != m.end());
89 }
90 {
91 const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
92 using M = std::flat_set<MoveClears, std::less<MoveClears>>;
93 M m = M(expected, expected + 8);
94 M m2 = M(expected, expected + 3);
95
96 m2 = std::move(m);
97
98 assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
99 LIBCPP_ASSERT(m.empty());
100 assert(std::is_sorted(m.begin(), m.end(), m.key_comp())); // still sorted
101 assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
102 m.insert(1);
103 m.insert(2);
104 assert(m.contains(1));
105 assert(m.find(2) != m.end());
106 }
107 {
108 // moved-from object maintains invariant if one of underlying container does not clear after move
109 using M = std::flat_set<int, std::less<>, std::vector<int>>;
110 M m1 = M({1, 2, 3});
111 M m2 = M({1, 2});
112 m2 = std::move(m1);
113 assert(m2.size() == 3);
114 check_invariant(m1);
115 LIBCPP_ASSERT(m1.empty());
116 }
117#if !defined(TEST_HAS_NO_EXCEPTIONS)
118 {
119 using M = std::flat_set<int, std::less<>, MoveAssignThrows>;
120 M m1 = {1, 2, 3};
121 M m2 = {1, 2};
122 try {
123 m2 = std::move(m1);
124 assert(false);
125 } catch (int e) {
126 assert(e == 42);
127 }
128 check_invariant(m1);
129 check_invariant(m2);
130 LIBCPP_ASSERT(m1.empty());
131 LIBCPP_ASSERT(m2.empty());
132 }
133#endif // TEST_HAS_NO_EXCEPTIONS
134}
135
136struct MoveSensitiveComp {
137 MoveSensitiveComp() noexcept(false) = default;
138 MoveSensitiveComp(const MoveSensitiveComp&) noexcept(false) = default;
139 MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
140 MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept = default;
141 MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
142 rhs.is_moved_from_ = true;
143 return *this;
144 }
145 bool operator()(const auto&, const auto&) const { return false; }
146 bool is_moved_from_ = false;
147};
148
149struct MoveThrowsComp {
150 MoveThrowsComp(MoveThrowsComp&&) noexcept(false);
151 MoveThrowsComp(const MoveThrowsComp&) noexcept(true);
152 MoveThrowsComp& operator=(MoveThrowsComp&&) noexcept(false);
153 MoveThrowsComp& operator=(const MoveThrowsComp&) noexcept(true);
154 bool operator()(const auto&, const auto&) const;
155};
156
157void test_move_assign_no_except() {
158 // This tests a conforming extension
159
160 {
161 using C = std::flat_set<int, int>;
162 LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
163 }
164 {
165 using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
166 static_assert(!std::is_nothrow_move_assignable_v<C>);
167 }
168 {
169 using C = std::flat_set<int, std::less<int>, std::vector<int, test_allocator<int>>>;
170 static_assert(!std::is_nothrow_move_assignable_v<C>);
171 }
172 {
173 using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
174 LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
175 }
176 {
177 using C = std::flat_set<int, std::less<int>, std::vector<int, other_allocator<int>>>;
178 LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
179 }
180 {
181 // Test with a comparator that throws on move-assignment.
182 using C = std::flat_set<int, MoveThrowsComp>;
183 LIBCPP_STATIC_ASSERT(!std::is_nothrow_move_assignable_v<C>);
184 }
185 {
186 // Test with a container that throws on move-assignment.
187 using C = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
188 static_assert(!std::is_nothrow_move_assignable_v<C>);
189 }
190}
191
192void test() {
193 {
194 using C = test_less<int>;
195 using A1 = test_allocator<int>;
196 using M = std::flat_set<int, C, std::vector<int, A1>>;
197 M mo = M({1, 2, 3}, C(5), A1(7));
198 M m = M({}, C(3), A1(7));
199 std::same_as<M&> decltype(auto) r = m = std::move(mo);
200 assert(&r == &m);
201 assert((m == M{1, 2, 3}));
202 assert(m.key_comp() == C(5));
203 auto ks = std::move(m).extract();
204 assert(ks.get_allocator() == A1(7));
205 assert(mo.empty());
206 }
207 {
208 using C = test_less<int>;
209 using A1 = other_allocator<int>;
210 using M = std::flat_set<int, C, std::deque<int, A1>>;
211 M mo = M({4, 5}, C(5), A1(7));
212 M m = M({1, 2, 3, 4}, C(3), A1(7));
213 std::same_as<M&> decltype(auto) r = m = std::move(mo);
214 assert(&r == &m);
215 assert((m == M{4, 5}));
216 assert(m.key_comp() == C(5));
217 auto ks = std::move(m).extract();
218 assert(ks.get_allocator() == A1(7));
219 assert(mo.empty());
220 }
221 {
222 using A = min_allocator<int>;
223 using M = std::flat_set<int, std::greater<int>, std::vector<int, A>>;
224 M mo = M({5, 4, 3}, A());
225 M m = M({4, 3, 2, 1}, A());
226 std::same_as<M&> decltype(auto) r = m = std::move(mo);
227 assert(&r == &m);
228 assert((m == M{5, 4, 3}));
229 auto ks = std::move(m).extract();
230 assert(ks.get_allocator() == A());
231 assert(mo.empty());
232 }
233}
234
235int main(int, char**) {
236 test();
237 test_move_assign_clears();
238 test_move_assign_no_except();
239
240 return 0;
241}
242

source code of libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign.pass.cpp