1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
13 // template<class InputIterator,
14 // class Compare = less<iter-value-type<InputIterator>>,
15 // class Allocator = allocator<iter-value-type<InputIterator>>>
16 // set(InputIterator, InputIterator,
17 // Compare = Compare(), Allocator = Allocator())
18 // -> set<iter-value-type<InputIterator>, Compare, Allocator>;
19 // template<class Key, class Compare = less<Key>,
20 // class Allocator = allocator<Key>>
21 // set(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
22 // -> set<Key, Compare, Allocator>;
23 // template<class InputIterator, class Allocator>
24 // set(InputIterator, InputIterator, Allocator)
25 // -> set<iter-value-type<InputIterator>,
26 // less<iter-value-type<InputIterator>>, Allocator>;
27 // template<class Key, class Allocator>
28 // set(initializer_list<Key>, Allocator)
29 // -> set<Key, less<Key>, Allocator>;
31 // template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>,
32 // class Allocator = allocator<ranges::range_value_t<R>>>
33 // set(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
34 // -> set<ranges::range_value_t<R>, Compare, Allocator>; // C++23
36 // template<ranges::input_range R, class Allocator>
37 // set(from_range_t, R&&, Allocator)
38 // -> set<ranges::range_value_t<R>, less<ranges::range_value_t<R>>, Allocator>; // C++23
40 #include <algorithm> // std::equal
43 #include <climits> // INT_MAX
46 #include <type_traits>
48 #include "deduction_guides_sfinae_checks.h"
49 #include "test_allocator.h"
51 struct NotAnAllocator
{
52 friend bool operator<(NotAnAllocator
, NotAnAllocator
) { return false; }
55 int main(int, char **) {
57 const int arr
[] = { 1, 2, 1, INT_MAX
, 3 };
58 std::set
s(std::begin(arr
), std::end(arr
));
60 ASSERT_SAME_TYPE(decltype(s
), std::set
<int>);
61 const int expected_s
[] = { 1, 2, 3, INT_MAX
};
62 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
63 std::end(expected_s
)));
67 const int arr
[] = { 1, 2, 1, INT_MAX
, 3 };
68 std::set
s(std::begin(arr
), std::end(arr
), std::greater
<int>());
70 ASSERT_SAME_TYPE(decltype(s
), std::set
<int, std::greater
<int> >);
71 const int expected_s
[] = { INT_MAX
, 3, 2, 1 };
72 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
73 std::end(expected_s
)));
77 const int arr
[] = { 1, 2, 1, INT_MAX
, 3 };
78 std::set
s(std::begin(arr
), std::end(arr
), std::greater
<int>(),
79 test_allocator
<int>(0, 42));
81 ASSERT_SAME_TYPE(decltype(s
),
82 std::set
<int, std::greater
<int>, test_allocator
<int> >);
83 const int expected_s
[] = { INT_MAX
, 3, 2, 1 };
84 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
85 std::end(expected_s
)));
86 assert(s
.get_allocator().get_id() == 42);
90 std::set
<long> source
;
92 ASSERT_SAME_TYPE(decltype(s
), std::set
<long>);
93 assert(s
.size() == 0);
97 std::set
<long> source
;
98 std::set s
{ source
}; // braces instead of parens
99 ASSERT_SAME_TYPE(decltype(s
), std::set
<long>);
100 assert(s
.size() == 0);
104 std::set
<long> source
;
105 std::set
s(source
, std::set
<long>::allocator_type());
106 ASSERT_SAME_TYPE(decltype(s
), std::set
<long>);
107 assert(s
.size() == 0);
111 std::set s
{ 1, 2, 1, INT_MAX
, 3 };
113 ASSERT_SAME_TYPE(decltype(s
), std::set
<int>);
114 const int expected_s
[] = { 1, 2, 3, INT_MAX
};
115 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
116 std::end(expected_s
)));
120 std::set
s({ 1, 2, 1, INT_MAX
, 3 }, std::greater
<int>());
122 ASSERT_SAME_TYPE(decltype(s
), std::set
<int, std::greater
<int> >);
123 const int expected_s
[] = { INT_MAX
, 3, 2, 1 };
124 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
125 std::end(expected_s
)));
129 std::set
s({ 1, 2, 1, INT_MAX
, 3 }, std::greater
<int>(),
130 test_allocator
<int>(0, 43));
132 ASSERT_SAME_TYPE(decltype(s
),
133 std::set
<int, std::greater
<int>, test_allocator
<int> >);
134 const int expected_s
[] = { INT_MAX
, 3, 2, 1 };
135 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
136 std::end(expected_s
)));
137 assert(s
.get_allocator().get_id() == 43);
141 const int arr
[] = { 1, 2, 1, INT_MAX
, 3 };
142 std::set
s(std::begin(arr
), std::end(arr
), test_allocator
<int>(0, 44));
144 ASSERT_SAME_TYPE(decltype(s
),
145 std::set
<int, std::less
<int>, test_allocator
<int> >);
146 const int expected_s
[] = { 1, 2, 3, INT_MAX
};
147 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
148 std::end(expected_s
)));
149 assert(s
.get_allocator().get_id() == 44);
153 std::set
s({ 1, 2, 1, INT_MAX
, 3 }, test_allocator
<int>(0, 45));
155 ASSERT_SAME_TYPE(decltype(s
),
156 std::set
<int, std::less
<int>, test_allocator
<int> >);
157 const int expected_s
[] = { 1, 2, 3, INT_MAX
};
158 assert(std::equal(s
.begin(), s
.end(), std::begin(expected_s
),
159 std::end(expected_s
)));
160 assert(s
.get_allocator().get_id() == 45);
165 std::set s
{ a
}; // set(initializer_list<NotAnAllocator>)
166 ASSERT_SAME_TYPE(decltype(s
), std::set
<NotAnAllocator
>);
167 assert(s
.size() == 1);
171 std::set
<long> source
;
172 std::set s
{ source
, source
}; // set(initializer_list<set<long>>)
173 ASSERT_SAME_TYPE(decltype(s
), std::set
<std::set
<long> >);
174 assert(s
.size() == 1);
179 std::set s
{ a
, a
}; // set(initializer_list<NotAnAllocator>)
180 ASSERT_SAME_TYPE(decltype(s
), std::set
<NotAnAllocator
>);
181 assert(s
.size() == 1);
185 int source
[3] = { 3, 4, 5 };
186 std::set
s(source
, source
+ 3); // set(InputIterator, InputIterator)
187 ASSERT_SAME_TYPE(decltype(s
), std::set
<int>);
188 assert(s
.size() == 3);
192 int source
[3] = { 3, 4, 5 };
193 std::set s
{ source
, source
+ 3 }; // set(initializer_list<int*>)
194 ASSERT_SAME_TYPE(decltype(s
), std::set
<int *>);
195 assert(s
.size() == 2);
198 #if TEST_STD_VER >= 23
200 using Range
= std::array
<int, 0>;
201 using Comp
= std::greater
<int>;
202 using DefaultComp
= std::less
<int>;
203 using Alloc
= test_allocator
<int>;
205 { // (from_range, range)
206 std::set
c(std::from_range
, Range());
207 static_assert(std::is_same_v
<decltype(c
), std::set
<int>>);
210 { // (from_range, range, comp)
211 std::set
c(std::from_range
, Range(), Comp());
212 static_assert(std::is_same_v
<decltype(c
), std::set
<int, Comp
>>);
215 { // (from_range, range, comp, alloc)
216 std::set
c(std::from_range
, Range(), Comp(), Alloc());
217 static_assert(std::is_same_v
<decltype(c
), std::set
<int, Comp
, Alloc
>>);
220 { // (from_range, range, alloc)
221 std::set
c(std::from_range
, Range(), Alloc());
222 static_assert(std::is_same_v
<decltype(c
), std::set
<int, DefaultComp
, Alloc
>>);
227 AssociativeContainerDeductionGuidesSfinaeAway
<std::set
, std::set
<int>>();