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// UNSUPPORTED: availability-pmr-missing
11
12// <flat_set>
13
14// Test various constructors with pmr
15
16#include <algorithm>
17#include <cassert>
18#include <deque>
19#include <flat_set>
20#include <functional>
21#include <memory_resource>
22#include <ranges>
23#include <vector>
24#include <string>
25
26#include "test_iterators.h"
27#include "test_macros.h"
28#include "test_allocator.h"
29#include "../../../test_compare.h"
30
31void test() {
32 {
33 // flat_set(const Allocator& a);
34 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
35 std::pmr::monotonic_buffer_resource mr;
36 std::pmr::polymorphic_allocator<int> pa = &mr;
37 auto m1 = M(pa);
38 assert(m1.empty());
39 assert(std::move(m1).extract().get_allocator() == pa);
40 auto m2 = M(&mr);
41 assert(m2.empty());
42 assert(std::move(m2).extract().get_allocator() == pa);
43 }
44 {
45 // flat_set(const key_compare& comp, const Alloc& a);
46 using M = std::flat_set<int, std::function<bool(int, int)>, std::pmr::vector<int>>;
47 std::pmr::monotonic_buffer_resource mr;
48 std::pmr::vector<M> vm(&mr);
49 vm.emplace_back(std::greater<int>());
50 assert(vm[0] == M{});
51 assert(vm[0].key_comp()(2, 1) == true);
52 assert(vm[0].value_comp()(2, 1) == true);
53 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
54 }
55 {
56 // flat_set(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
57 // const Allocator& a);
58 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
59 std::pmr::monotonic_buffer_resource mr;
60 std::pmr::vector<M> vm(&mr);
61 std::pmr::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
62 assert(ks.get_allocator().resource() != &mr);
63 vm.emplace_back(ks);
64 assert(ks.size() == 9); // ks' value is unchanged, since it was an lvalue above
65 assert((vm[0] == M{1, 2, 3}));
66 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
67 }
68 {
69 // flat_set(const flat_set&, const allocator_type&);
70 using C = test_less<int>;
71 using M = std::flat_set<int, C, std::pmr::vector<int>>;
72 std::pmr::monotonic_buffer_resource mr1;
73 std::pmr::monotonic_buffer_resource mr2;
74 M mo = M({1, 2, 3}, C(5), &mr1);
75 M m = {mo, &mr2}; // also test the implicitness of this constructor
76
77 assert(m.key_comp() == C(5));
78 auto keys = std::move(m).extract();
79 assert((keys == std::pmr::vector<int>{1, 2, 3}));
80 assert(keys.get_allocator().resource() == &mr2);
81
82 // mo is unchanged
83 assert(mo.key_comp() == C(5));
84 auto keys2 = std::move(mo).extract();
85 assert((keys2 == std::pmr::vector<int>{1, 2, 3}));
86 assert(keys2.get_allocator().resource() == &mr1);
87 }
88 {
89 // flat_set(const flat_set&, const allocator_type&);
90 using M = std::flat_set<int, std::less<>, std::pmr::vector<int>>;
91 std::pmr::vector<M> vs;
92 M m = {1, 2, 3};
93 vs.push_back(m);
94 assert(vs[0] == m);
95 }
96 {
97 // flat_set& operator=(const flat_set& m);
98 // pmr allocator is not propagated
99 using M = std::flat_set<int, std::less<>, std::pmr::deque<int>>;
100 std::pmr::monotonic_buffer_resource mr1;
101 std::pmr::monotonic_buffer_resource mr2;
102 M mo = M({1, 2, 3}, &mr1);
103 M m = M({4, 5}, &mr2);
104 m = mo;
105 assert((m == M{1, 2, 3}));
106 assert(std::move(m).extract().get_allocator().resource() == &mr2);
107
108 // mo is unchanged
109 assert((mo == M{1, 2, 3}));
110 assert(std::move(mo).extract().get_allocator().resource() == &mr1);
111 }
112 {
113 // flat_set(const flat_set& m);
114 using C = test_less<int>;
115 std::pmr::monotonic_buffer_resource mr;
116 using M = std::flat_set<int, C, std::pmr::vector<int>>;
117 auto mo = M({1, 2, 3}, C(5), &mr);
118 auto m = mo;
119
120 assert(m.key_comp() == C(5));
121 assert((m == M{1, 2, 3}));
122 auto ks = std::move(m).extract();
123 assert(ks.get_allocator().resource() == std::pmr::get_default_resource());
124
125 // mo is unchanged
126 assert(mo.key_comp() == C(5));
127 assert((mo == M{1, 2, 3}));
128 auto kso = std::move(mo).extract();
129 assert(kso.get_allocator().resource() == &mr);
130 }
131 {
132 // flat_set(initializer_list<value_type> il, const Alloc& a);
133 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
134 std::pmr::monotonic_buffer_resource mr;
135 std::pmr::vector<M> vm(&mr);
136 std::initializer_list<M::value_type> il = {3, 1, 4, 1, 5};
137 vm.emplace_back(il);
138 assert((vm[0] == M{1, 3, 4, 5}));
139 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
140 }
141 {
142 // flat_set(initializer_list<value_type> il, const key_compare& comp, const Alloc& a);
143 using C = test_less<int>;
144 using M = std::flat_set<int, C, std::pmr::deque<int>>;
145 std::pmr::monotonic_buffer_resource mr;
146 std::pmr::vector<M> vm(&mr);
147 std::initializer_list<M::value_type> il = {3, 1, 4, 1, 5};
148 vm.emplace_back(il, C(5));
149 assert((vm[0] == M{1, 3, 4, 5}));
150 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
151 assert(vm[0].key_comp() == C(5));
152 }
153 {
154 // flat_set(InputIterator first, InputIterator last, const Allocator& a);
155 int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3};
156 int expected[] = {1, 2, 3};
157 {
158 // cpp17 iterator
159 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
160 std::pmr::monotonic_buffer_resource mr;
161 std::pmr::vector<M> vm(&mr);
162 vm.emplace_back(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));
163 assert(std::ranges::equal(vm[0], expected));
164 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
165 }
166 {
167 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
168 std::pmr::monotonic_buffer_resource mr;
169 std::pmr::vector<M> vm(&mr);
170 vm.emplace_back(ar, ar);
171 assert(vm[0].empty());
172 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
173 }
174 }
175 {
176 // flat_set(flat_set&&, const allocator_type&);
177 int expected[] = {1, 2, 3};
178 using C = test_less<int>;
179 using M = std::flat_set<int, C, std::pmr::vector<int>>;
180 std::pmr::monotonic_buffer_resource mr1;
181 std::pmr::monotonic_buffer_resource mr2;
182 M mo = M({1, 3, 1, 2}, C(5), &mr1);
183 M m = {std::move(mo), &mr2}; // also test the implicitness of this constructor
184
185 assert(m.key_comp() == C(5));
186 assert(m.size() == 3);
187 assert(std::equal(m.begin(), m.end(), expected, expected + 3));
188 assert(std::move(m).extract().get_allocator().resource() == &mr2);
189
190 // The original flat_set is moved-from.
191 assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
192 assert(mo.key_comp() == C(5));
193 assert(std::move(mo).extract().get_allocator().resource() == &mr1);
194 }
195 {
196 // flat_set(flat_set&&, const allocator_type&);
197 using M = std::flat_set<int, std::less<>, std::pmr::deque<int>>;
198 std::pmr::vector<M> vs;
199 M m = {1, 3, 1, 2};
200 vs.push_back(std::move(m));
201 assert((std::move(vs[0]).extract() == std::pmr::deque<int>{1, 2, 3}));
202 }
203 {
204 // flat_set& operator=(flat_set&&);
205 using M = std::flat_set<std::pmr::string, std::less<>, std::pmr::vector<std::pmr::string>>;
206 std::pmr::monotonic_buffer_resource mr1;
207 std::pmr::monotonic_buffer_resource mr2;
208 M mo =
209 M({"short", "very long string that definitely won't fit in the SSO buffer and therefore becomes empty on move"},
210 &mr1);
211 M m = M({"don't care"}, &mr2);
212 m = std::move(mo);
213 assert(m.size() == 2);
214 assert(std::is_sorted(m.begin(), m.end(), m.value_comp()));
215 assert(m.begin()->get_allocator().resource() == &mr2);
216
217 assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
218 mo.insert("foo");
219 assert(mo.begin()->get_allocator().resource() == &mr1);
220 }
221 {
222 // flat_set(from_range_t, R&&, const Alloc&);
223 int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3};
224 int expected[] = {1, 2, 3};
225 {
226 // input_range
227 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
228 using Iter = cpp20_input_iterator<const int*>;
229 using Sent = sentinel_wrapper<Iter>;
230 using R = std::ranges::subrange<Iter, Sent>;
231 std::pmr::monotonic_buffer_resource mr;
232 std::pmr::vector<M> vm(&mr);
233 vm.emplace_back(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));
234 assert(std::ranges::equal(vm[0], expected));
235 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
236 }
237 {
238 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
239 using R = std::ranges::subrange<const int*>;
240 std::pmr::monotonic_buffer_resource mr;
241 std::pmr::vector<M> vm(&mr);
242 vm.emplace_back(std::from_range, R(ar, ar));
243 assert(vm[0].empty());
244 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
245 }
246 }
247 {
248 // flat_set(sorted_unique_t, const container_type& key_cont, const Alloc& a);
249 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
250 std::pmr::monotonic_buffer_resource mr;
251 std::pmr::vector<M> vm(&mr);
252 std::pmr::vector<int> ks = {1, 2, 4, 10};
253 vm.emplace_back(std::sorted_unique, ks);
254 assert(!ks.empty()); // it was an lvalue above
255 assert((vm[0] == M{1, 2, 4, 10}));
256 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
257 }
258 {
259 // flat_set(sorted_unique_t, const container_type& key_cont,const Alloc& a);
260 using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
261 std::pmr::monotonic_buffer_resource mr;
262 std::pmr::vector<M> vm(&mr);
263 std::pmr::vector<int> ks({1, 2, 4, 10}, &mr);
264 vm.emplace_back(std::sorted_unique, ks);
265 assert((vm[0] == M{1, 2, 4, 10}));
266 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
267 }
268 {
269 // flat_set(sorted_unique_t, initializer_list<value_type> il, const Alloc& a);
270 // cpp_17
271 using C = test_less<int>;
272 using M = std::flat_set<int, C, std::pmr::vector<int>>;
273 std::pmr::monotonic_buffer_resource mr;
274 std::pmr::vector<M> vm(&mr);
275 int ar[] = {1, 2, 4, 5};
276 vm.emplace_back(
277 std::sorted_unique, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4), C(3));
278 assert((vm[0] == M{1, 2, 4, 5}));
279 assert(vm[0].key_comp() == C(3));
280 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
281 }
282 {
283 // flat_set(sorted_unique_t, initializer_list<value_type> il, const Alloc& a);
284 using C = test_less<int>;
285 using M = std::flat_set<int, C, std::pmr::vector<int>>;
286 std::pmr::monotonic_buffer_resource mr;
287 std::pmr::vector<M> vm(&mr);
288 int ar[1] = {42};
289 vm.emplace_back(std::sorted_unique, ar, ar, C(4));
290 assert(vm[0] == M{});
291 assert(vm[0].key_comp() == C(4));
292 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
293 }
294 {
295 // flat_set(InputIterator first, InputIterator last, const Alloc& a);
296 // cpp_17
297 using C = test_less<int>;
298 using M = std::flat_set<int, C, std::pmr::vector<int>>;
299 std::pmr::monotonic_buffer_resource mr;
300 std::pmr::vector<M> vm(&mr);
301 int ar[] = {1, 2, 4, 5};
302 vm.emplace_back(
303 std::sorted_unique, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4), C(3));
304 assert((vm[0] == M{1, 2, 4, 5}));
305 assert(vm[0].key_comp() == C(3));
306 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
307 }
308 {
309 // flat_set(InputIterator first, InputIterator last, const Alloc& a);
310 using C = test_less<int>;
311 using M = std::flat_set<int, C, std::pmr::vector<int>>;
312 std::pmr::monotonic_buffer_resource mr;
313 std::pmr::vector<M> vm(&mr);
314 int ar[1] = {42};
315 vm.emplace_back(std::sorted_unique, ar, ar, C(4));
316 assert(vm[0] == M{});
317 assert(vm[0].key_comp() == C(4));
318 assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
319 }
320}
321
322int main(int, char**) {
323 test();
324
325 return 0;
326}
327

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