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// explicit flat_set(container_type key_cont, const key_compare& comp = key_compare());
14// template<class Allocator>
15// flat_set(const container_type& key_cont, const Allocator& a);
16// template<class Alloc>
17// flat_set(const container_type& key_cont, const key_compare& comp, const Alloc& a);
18
19#include <algorithm>
20#include <deque>
21#include <flat_set>
22#include <functional>
23#include <type_traits>
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
33template <class T>
34void conversion_test(T);
35
36template <class T, class... Args>
37concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test<T>({std::forward<Args>(args)...}); };
38
39void test() {
40 {
41 // The constructors in this subclause shall not participate in overload
42 // resolution unless uses_allocator_v<container_type, Alloc> is true.
43
44 using C = test_less<int>;
45 using A1 = test_allocator<int>;
46 using A2 = other_allocator<int>;
47 using V1 = std::vector<int, A1>;
48 using V2 = std::vector<int, A2>;
49 using M1 = std::flat_set<int, C, V1>;
50 using M2 = std::flat_set<int, C, V2>;
51 static_assert(std::is_constructible_v<M1, const V1&, const A1&>);
52 static_assert(std::is_constructible_v<M2, const V2&, const A2&>);
53 static_assert(!std::is_constructible_v<M1, const V1&, const A2&>);
54 static_assert(!std::is_constructible_v<M2, const V2&, const A1&>);
55
56 static_assert(std::is_constructible_v<M1, const V1&, const C&, const A1&>);
57 static_assert(std::is_constructible_v<M2, const V2&, const C&, const A2&>);
58 static_assert(!std::is_constructible_v<M1, const V1&, const C&, const A2&>);
59 static_assert(!std::is_constructible_v<M2, const V2&, const C&, const A1&>);
60 }
61 {
62 // flat_set(container_type)
63 using M = std::flat_set<int>;
64 std::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
65 auto m = M(ks);
66 assert(std::ranges::equal(m, std::vector<int>{1, 2, 3}));
67
68 // explicit
69 static_assert(std::is_constructible_v<M, const std::vector<int>&>);
70 static_assert(!ImplicitlyConstructible<M, const std::vector<int>&>);
71 }
72 {
73 // flat_set(container_type)
74 // move-only
75 MoveOnly expected[] = {3, 2, 1};
76 using Ks = std::deque<MoveOnly, min_allocator<MoveOnly>>;
77 using M = std::flat_set<MoveOnly, std::greater<MoveOnly>, Ks>;
78 Ks ks;
79 ks.push_back(1);
80 ks.push_back(3);
81 ks.push_back(2);
82 auto m = M(std::move(ks));
83 assert(ks.empty()); // it was moved-from
84 assert(std::ranges::equal(m, expected));
85 }
86 {
87 // flat_set(container_type)
88 // container's allocator is used
89 using A = test_allocator<int>;
90 using M = std::flat_set<int, std::less<int>, std::deque<int, A>>;
91 auto ks = std::deque<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));
92 auto m = M(std::move(ks));
93 assert(ks.empty()); // it was moved-from
94 assert((m == M{1, 2, 3}));
95 auto keys = std::move(m).extract();
96 assert(keys.get_allocator() == A(5));
97 }
98 {
99 // flat_set(container_type , key_compare)
100 using C = test_less<int>;
101 using M = std::flat_set<int, C>;
102 std::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
103 auto m = M(ks, C(4));
104 assert(std::ranges::equal(m, std::vector<int>{1, 2, 3}));
105 assert(m.key_comp() == C(4));
106
107 // explicit
108 static_assert(std::is_constructible_v<M, const std::vector<int>&, const C&>);
109 static_assert(!ImplicitlyConstructible<M, const std::vector<int>&, const C&>);
110 }
111 {
112 // flat_set(container_type , const Allocator&)
113 using A = test_allocator<int>;
114 using M = std::flat_set<int, std::less<int>, std::deque<int, A>>;
115 auto ks = std::deque<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));
116 auto m = M(ks, A(4)); // replaces the allocators
117 assert(!ks.empty()); // it was an lvalue above
118 assert((m == M{1, 2, 3}));
119 auto keys = M(m).extract();
120 assert(keys.get_allocator() == A(4));
121
122 // explicit(false)
123 static_assert(ImplicitlyConstructible<M, const std::deque<int, A>&, const A&>);
124 M m2 = {ks, A(4)}; // implicit ctor
125 assert(!ks.empty()); // it was an lvalue above
126 assert(m2 == m);
127 auto keys2 = std::move(m).extract();
128 assert(keys2.get_allocator() == A(4));
129 }
130 {
131 // flat_set(container_type , key_compare, const Allocator&)
132 using C = test_less<int>;
133 using A = test_allocator<int>;
134 using M = std::flat_set<int, C, std::vector<int, A>>;
135 std::vector<int, A> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
136 auto m = M(ks, C(4), A(5));
137 assert(std::ranges::equal(m, std::vector<int, A>{1, 2, 3}));
138 assert(m.key_comp() == C(4));
139 auto m_copy = m;
140 auto keys = std::move(m_copy).extract();
141 assert(keys.get_allocator() == A(5));
142
143 // explicit(false)
144 static_assert(ImplicitlyConstructible<M, const std::vector<int, A>&, const A&>);
145 M m2 = {ks, C(4), A(5)};
146 assert(m2 == m);
147 assert(m2.key_comp() == C(4));
148 keys = std::move(m2).extract();
149 assert(keys.get_allocator() == A(5));
150 }
151}
152
153int main(int, char**) {
154 test();
155
156 return 0;
157}
158

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