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// template<container-compatible-range<value_type> R>
14// flat_multiset(from_range_t, R&&)
15// template<container-compatible-range<value_type> R>
16// flat_multiset(from_range_t, R&&, const key_compare&)
17// template<container-compatible-range<value_type> R, class Alloc>
18// flat_multiset(from_range_t, R&&, const Alloc&);
19// template<container-compatible-range<value_type> R, class Alloc>
20// flat_multiset(from_range_t, R&&, const key_compare&, const Alloc&);
21
22#include <algorithm>
23#include <deque>
24#include <flat_set>
25#include <functional>
26#include <string>
27#include <vector>
28
29#include "min_allocator.h"
30#include "test_allocator.h"
31#include "test_iterators.h"
32#include "test_macros.h"
33#include "../../../test_compare.h"
34
35// test constraint container-compatible-range
36
37template <class V>
38using RangeOf = std::ranges::subrange<V*>;
39using Set = std::flat_multiset<int>;
40
41static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>>);
42static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>>);
43static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>>);
44
45static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>>);
46static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::less<int>>);
47static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>>);
48
49static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::allocator<int>>);
50static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::allocator<int>>);
51static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::allocator<int>>);
52
53static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>);
54static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>);
55static_assert(
56 !std::
57 is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>, std::allocator<int>>);
58
59void test() {
60 {
61 // The constructors in this subclause shall not participate in overload
62 // resolution unless uses_allocator_v<container_type, Alloc> is true.
63
64 using C = test_less<int>;
65 using A1 = test_allocator<int>;
66 using A2 = other_allocator<int>;
67 using V1 = std::vector<int, A1>;
68 using V2 = std::vector<int, A2>;
69 using M1 = std::flat_multiset<int, C, V1>;
70 using M2 = std::flat_multiset<int, C, V2>;
71 static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const A1&>);
72 static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const A2&>);
73 static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const A2&>);
74 static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const A1&>);
75
76 static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A1&>);
77 static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A2&>);
78 static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A2&>);
79 static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A1&>);
80 }
81
82 int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3};
83 int expected[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
84 {
85 // flat_multiset(from_range_t, R&&)
86 // input_range && !common
87 using M = std::flat_multiset<int>;
88 using Iter = cpp20_input_iterator<const int*>;
89 using Sent = sentinel_wrapper<Iter>;
90 using R = std::ranges::subrange<Iter, Sent>;
91 auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));
92 assert(std::ranges::equal(m, expected));
93
94 // explicit(false)
95 M m2 = {std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))};
96 assert(m2 == m);
97 }
98 {
99 // flat_multiset(from_range_t, R&&)
100 // greater
101 using M = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
102 using Iter = cpp20_input_iterator<const int*>;
103 using Sent = sentinel_wrapper<Iter>;
104 using R = std::ranges::subrange<Iter, Sent>;
105 auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));
106 assert(std::ranges::equal(m, std::deque<int, min_allocator<int>>{3, 3, 3, 2, 2, 2, 1, 1, 1}));
107 }
108 {
109 // flat_multiset(from_range_t, R&&)
110 // contiguous range
111 using M = std::flat_multiset<int>;
112 using R = std::ranges::subrange<const int*>;
113 auto m = M(std::from_range, R(ar, ar + 9));
114 assert(std::ranges::equal(m, expected));
115 }
116 {
117 // flat_multiset(from_range_t, R&&, const key_compare&)
118 using C = test_less<int>;
119 using M = std::flat_multiset<int, C, std::vector<int>>;
120 using R = std::ranges::subrange<const int*>;
121 auto m = M(std::from_range, R(ar, ar + 9), C(3));
122 assert(std::ranges::equal(m, expected));
123 assert(m.key_comp() == C(3));
124
125 // explicit(false)
126 M m2 = {std::from_range, R(ar, ar + 9), C(3)};
127 assert(m2 == m);
128 assert(m2.key_comp() == C(3));
129 }
130 {
131 // flat_multiset(from_range_t, R&&, const Allocator&)
132 using A1 = test_allocator<int>;
133 using M = std::flat_multiset<int, std::less<int>, std::vector<int, A1>>;
134 using R = std::ranges::subrange<const int*>;
135 auto m = M(std::from_range, R(ar, ar + 9), A1(5));
136 assert(std::ranges::equal(m, expected));
137 assert(std::move(m).extract().get_allocator() == A1(5));
138 }
139 {
140 // flat_multiset(from_range_t, R&&, const Allocator&)
141 // explicit(false)
142 using A1 = test_allocator<int>;
143 using M = std::flat_multiset<int, std::less<int>, std::deque<int, A1>>;
144 using R = std::ranges::subrange<const int*>;
145 M m = {std::from_range, R(ar, ar + 9), A1(5)}; // implicit ctor
146 assert(std::ranges::equal(m, expected));
147 assert(std::move(m).extract().get_allocator() == A1(5));
148 }
149 {
150 // flat_multiset(from_range_t, R&&, const key_compare&, const Allocator&)
151 using C = test_less<int>;
152 using A1 = test_allocator<int>;
153 using M = std::flat_multiset<int, C, std::vector<int, A1>>;
154 using R = std::ranges::subrange<const int*>;
155 auto m = M(std::from_range, R(ar, ar + 9), C(3), A1(5));
156 assert(std::ranges::equal(m, expected));
157 assert(m.key_comp() == C(3));
158 assert(std::move(m).extract().get_allocator() == A1(5));
159 }
160 {
161 // flat_multiset(from_range_t, R&&, const key_compare&, const Allocator&)
162 // explicit(false)
163 using A1 = test_allocator<int>;
164 using M = std::flat_multiset<int, std::less<int>, std::deque<int, A1>>;
165 using R = std::ranges::subrange<const int*>;
166 M m = {std::from_range, R(ar, ar + 9), {}, A1(5)}; // implicit ctor
167 assert(std::ranges::equal(m, expected));
168 assert(std::move(m).extract().get_allocator() == A1(5));
169 }
170}
171
172int main(int, char**) {
173 test();
174
175 return 0;
176}
177

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