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(sorted_unique_t, container_type key_cont, const key_compare& comp = key_compare());
14//
15// template<class Alloc>
16// flat_set(sorted_unique_t, const container_type& key_cont, const Alloc& a);
17// template<class Alloc>
18// flat_set(sorted_unique_t, const container_type& key_cont,
19// const key_compare& comp, const Alloc& a);
20
21#include <deque>
22#include <flat_set>
23#include <functional>
24#include <vector>
25
26#include "min_allocator.h"
27#include "MoveOnly.h"
28#include "test_allocator.h"
29#include "test_iterators.h"
30#include "test_macros.h"
31#include "../../../test_compare.h"
32
33void test() {
34 {
35 // The constructors in this subclause shall not participate in overload
36 // resolution unless uses_allocator_v<container_type, Alloc> is true.
37
38 using C = test_less<int>;
39 using A1 = test_allocator<int>;
40 using A2 = other_allocator<int>;
41 using V1 = std::vector<int, A1>;
42 using V2 = std::vector<int, A2>;
43 using M1 = std::flat_set<int, C, V1>;
44 using M2 = std::flat_set<int, C, V2>;
45 static_assert(std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const A1&>);
46 static_assert(std::is_constructible_v<M2, std::sorted_unique_t, const V2&, const A2&>);
47 static_assert(!std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const A2&>);
48 static_assert(!std::is_constructible_v<M2, std::sorted_unique_t, const V2&, const A1&>);
49
50 static_assert(std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const C&, const A1&>);
51 static_assert(std::is_constructible_v<M2, std::sorted_unique_t, const V2&, const C&, const A2&>);
52 static_assert(!std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const C&, const A2&>);
53 static_assert(!std::is_constructible_v<M2, std::sorted_unique_t, const V2&, const C&, const A1&>);
54 }
55 {
56 // flat_set(sorted_unique_t, container_type)
57 using M = std::flat_set<int>;
58 std::vector<int> ks = {1, 2, 4, 10};
59 auto ks2 = ks;
60
61 auto m = M(std::sorted_unique, ks);
62 assert((m == M{1, 2, 4, 10}));
63 m = M(std::sorted_unique, std::move(ks));
64 assert(ks.empty()); // it was moved-from
65 assert((m == M{1, 2, 4, 10}));
66
67 // explicit(false)
68 M m2 = {std::sorted_unique, std::move(ks2)};
69 assert(m == m2);
70 }
71 {
72 // flat_set(sorted_unique_t, container_type)
73 // non-default container, comparator and allocator type
74 using Ks = std::deque<int, min_allocator<int>>;
75 using M = std::flat_set<int, std::greater<int>, Ks>;
76 Ks ks = {10, 4, 2, 1};
77 auto m = M(std::sorted_unique, ks);
78 assert((m == M{1, 2, 4, 10}));
79 m = M(std::sorted_unique, std::move(ks));
80 assert(ks.empty()); // it was moved-from
81 assert((m == M{1, 2, 4, 10}));
82 }
83 {
84 // flat_set(sorted_unique_t, container_type)
85 // allocator copied into the containers
86 using A = test_allocator<int>;
87 using M = std::flat_set<int, std::less<int>, std::deque<int, A>>;
88 auto ks = std::deque<int, A>({1, 2, 4, 10}, A(4));
89 auto m = M(std::sorted_unique, std::move(ks));
90 assert(ks.empty()); // it was moved-from
91 assert((m == M{1, 2, 4, 10}));
92 assert(std::move(m).extract().get_allocator() == A(4));
93 }
94 {
95 // flat_set(sorted_unique_t, container_type , key_compare)
96 using C = test_less<int>;
97 using M = std::flat_set<int, C>;
98 std::vector<int> ks = {1, 2, 4, 10};
99
100 auto m = M(std::sorted_unique, ks, C(4));
101 assert((m == M{1, 2, 4, 10}));
102 assert(m.key_comp() == C(4));
103
104 // explicit(false)
105 M m2 = {std::sorted_unique, ks, C(4)};
106 assert(m2 == m);
107 assert(m2.key_comp() == C(4));
108 }
109 {
110 // flat_set(sorted_unique_t, container_type , key_compare, const Allocator&)
111 using C = test_less<int>;
112 using A = test_allocator<int>;
113 using M = std::flat_set<int, C, std::vector<int, A>>;
114 std::vector<int, A> ks = {1, 2, 4, 10};
115 auto m = M(std::sorted_unique, ks, C(4), A(5));
116 assert((m == M{1, 2, 4, 10}));
117 assert(m.key_comp() == C(4));
118 assert(M(m).extract().get_allocator() == A(5));
119
120 // explicit(false)
121 M m2 = {ks, C(4), A(5)};
122 assert(m2 == m);
123 assert(m2.key_comp() == C(4));
124 assert(std::move(m2).extract().get_allocator() == A(5));
125 }
126 {
127 // flat_set(sorted_unique_t, container_type , const Allocator&)
128 using A = test_allocator<int>;
129 using M = std::flat_set<int, std::less<int>, std::deque<int, A>>;
130 auto ks = std::deque<int, A>({1, 2, 4, 10}, A(4));
131 auto m = M(std::sorted_unique, ks, A(6)); // replaces the allocators
132 assert(!ks.empty()); // it was an lvalue above
133 assert((m == M{1, 2, 4, 10}));
134 assert(M(m).extract().get_allocator() == A(6));
135
136 // explicit(false)
137 M m2 = {std::sorted_unique, ks, A(6)};
138 assert(m2 == m);
139 assert(std::move(m2).extract().get_allocator() == A(6));
140 }
141}
142
143int main(int, char**) {
144 test();
145
146 return 0;
147}
148

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