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#include <algorithm>
15#include <cassert>
16#include <climits>
17#include <deque>
18#include <initializer_list>
19#include <list>
20#include <flat_set>
21#include <functional>
22#include <memory_resource>
23#include <ranges>
24#include <type_traits>
25#include <utility>
26#include <vector>
27
28#include "test_allocator.h"
29
30using P = std::pair<int, long>;
31using PC = std::pair<const int, long>;
32
33int main(int, char**) {
34 {
35 std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
36 std::deque<int, test_allocator<int>> sorted_ks({1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
37 const int expected[] = {1, 2, 3, INT_MAX};
38 {
39 // template<class KeyContainer, class Allocator>
40 // flat_set(KeyContainer, Allocator)
41 // -> flat_set<typename KeyContainer::value_type,
42 // less<typename KeyContainer::value_type>, KeyContainer>;
43 std::pmr::monotonic_buffer_resource mr;
44 std::pmr::monotonic_buffer_resource mr2;
45 std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
46 std::flat_set s(std::move(pks), &mr2);
47
48 ASSERT_SAME_TYPE(decltype(s), std::flat_set<int, std::less<int>, std::pmr::deque<int>>);
49 assert(std::ranges::equal(s, expected));
50 auto keys = std::move(s).extract();
51 assert(keys.get_allocator().resource() == &mr2);
52 }
53 {
54 // template<class KeyContainer, class Allocator>
55 // flat_set(sorted_unique_t, KeyContainer, Allocator)
56 // -> flat_set<typename KeyContainer::value_type,
57 // less<typename KeyContainer::value_type>, KeyContainer>;
58 std::pmr::monotonic_buffer_resource mr;
59 std::pmr::monotonic_buffer_resource mr2;
60 std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
61 std::flat_set s(std::sorted_unique, std::move(pks), &mr2);
62
63 ASSERT_SAME_TYPE(decltype(s), std::flat_set<int, std::less<int>, std::pmr::deque<int>>);
64 assert(std::ranges::equal(s, expected));
65 auto keys = std::move(s).extract();
66 assert(keys.get_allocator().resource() == &mr2);
67 }
68 }
69 {
70 std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
71 std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1}, test_allocator<int>(0, 42));
72 const int expected[] = {INT_MAX, 3, 2, 1};
73 {
74 // template<class KeyContainer, class Compare, class Allocator>
75 // flat_set(KeyContainer, Compare, Allocator)
76 // -> flat_set<typename KeyContainer::value_type, Compare, KeyContainer>;
77 std::pmr::monotonic_buffer_resource mr;
78 std::pmr::monotonic_buffer_resource mr2;
79 std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
80 std::flat_set s(std::move(pks), std::greater<int>(), &mr2);
81
82 ASSERT_SAME_TYPE(decltype(s), std::flat_set<int, std::greater<int>, std::pmr::deque<int>>);
83 assert(std::ranges::equal(s, expected));
84 auto keys = std::move(s).extract();
85 assert(keys.get_allocator().resource() == &mr2);
86 }
87 {
88 // template<class KeyContainer, class Compare, class Allocator>
89 // flat_set(sorted_unique_t, KeyContainer, Compare, Allocator)
90 // -> flat_set<typename KeyContainer::value_type, Compare, KeyContainer>;
91 std::pmr::monotonic_buffer_resource mr;
92 std::pmr::monotonic_buffer_resource mr2;
93 std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
94 std::flat_set s(std::sorted_unique, std::move(pks), std::greater<int>(), &mr2);
95
96 ASSERT_SAME_TYPE(decltype(s), std::flat_set<int, std::greater<int>, std::pmr::deque<int>>);
97 assert(std::ranges::equal(s, expected));
98 auto keys = std::move(s).extract();
99 assert(keys.get_allocator().resource() == &mr2);
100 }
101 }
102
103 return 0;
104}
105

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