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&&, const allocator_type&);
14
15#include <algorithm>
16#include <deque>
17#include <flat_set>
18#include <functional>
19#include <ranges>
20#include <vector>
21
22#include "../helpers.h"
23#include "test_macros.h"
24#include "../../../test_compare.h"
25#include "test_allocator.h"
26
27void test() {
28 {
29 // The constructors in this subclause shall not participate in overload
30 // resolution unless uses_allocator_v<container_type, Alloc> is true.
31
32 using C = test_less<int>;
33 using A1 = test_allocator<int>;
34 using A2 = other_allocator<int>;
35 using V1 = std::vector<int, A1>;
36 using V2 = std::vector<int, A2>;
37 using M1 = std::flat_set<int, C, V1>;
38 using M2 = std::flat_set<int, C, V2>;
39 static_assert(std::is_constructible_v<M1, M1&&, const A1&>);
40 static_assert(std::is_constructible_v<M2, M2&&, const A2&>);
41 static_assert(!std::is_constructible_v<M1, M1&&, const A2&>);
42 static_assert(!std::is_constructible_v<M2, M2&&, const A1&>);
43 }
44 {
45 int expected[] = {1, 2, 3};
46 using C = test_less<int>;
47 using A = test_allocator<int>;
48 using M = std::flat_set<int, C, std::deque<int, A>>;
49 auto mo = M(expected, expected + 3, C(5), A(7));
50 auto m = M(std::move(mo), A(3));
51
52 assert(m.key_comp() == C(5));
53 assert(m.size() == 3);
54 auto keys = std::move(m).extract();
55 assert(keys.get_allocator() == A(3));
56 assert(std::ranges::equal(keys, expected));
57
58 // The original flat_set is moved-from.
59 assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
60 assert(mo.empty());
61 assert(mo.key_comp() == C(5));
62 assert(std::move(mo).extract().get_allocator() == A(7));
63 }
64 {
65 // moved-from object maintains invariant if one of underlying container does not clear after move
66 using M = std::flat_set<int, std::less<>, CopyOnlyVector<int>>;
67 M m1 = M({1, 2, 3});
68 M m2(std::move(m1), std::allocator<int>{});
69 assert(m2.size() == 3);
70 check_invariant(m1);
71 LIBCPP_ASSERT(m1.empty());
72 }
73}
74
75int main(int, char**) {
76 test();
77
78 return 0;
79}
80

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