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 |
10 | |
11 | // <set> |
12 | |
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>; |
30 | // |
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 |
35 | // |
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 |
39 | |
40 | #include <algorithm> // std::equal |
41 | #include <array> |
42 | #include <cassert> |
43 | #include <climits> // INT_MAX |
44 | #include <functional> |
45 | #include <set> |
46 | #include <type_traits> |
47 | |
48 | #include "deduction_guides_sfinae_checks.h" |
49 | #include "test_allocator.h" |
50 | |
51 | struct NotAnAllocator { |
52 | friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; } |
53 | }; |
54 | |
55 | int main(int, char **) { |
56 | { |
57 | const int arr[] = { 1, 2, 1, INT_MAX, 3 }; |
58 | std::set s(std::begin(arr: arr), std::end(arr: arr)); |
59 | |
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))); |
64 | } |
65 | |
66 | { |
67 | const int arr[] = { 1, 2, 1, INT_MAX, 3 }; |
68 | std::set s(std::begin(arr: arr), std::end(arr: arr), std::greater<int>()); |
69 | |
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))); |
74 | } |
75 | |
76 | { |
77 | const int arr[] = { 1, 2, 1, INT_MAX, 3 }; |
78 | std::set s(std::begin(arr: arr), std::end(arr: arr), std::greater<int>(), |
79 | test_allocator<int>(0, 42)); |
80 | |
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); |
87 | } |
88 | |
89 | { |
90 | std::set<long> source; |
91 | std::set s(source); |
92 | ASSERT_SAME_TYPE(decltype(s), std::set<long>); |
93 | assert(s.size() == 0); |
94 | } |
95 | |
96 | { |
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); |
101 | } |
102 | |
103 | { |
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); |
108 | } |
109 | |
110 | { |
111 | std::set s{ 1, 2, 1, INT_MAX, 3 }; |
112 | |
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))); |
117 | } |
118 | |
119 | { |
120 | std::set s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>()); |
121 | |
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))); |
126 | } |
127 | |
128 | { |
129 | std::set s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>(), |
130 | test_allocator<int>(0, 43)); |
131 | |
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); |
138 | } |
139 | |
140 | { |
141 | const int arr[] = { 1, 2, 1, INT_MAX, 3 }; |
142 | std::set s(std::begin(arr: arr), std::end(arr: arr), test_allocator<int>(0, 44)); |
143 | |
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); |
150 | } |
151 | |
152 | { |
153 | std::set s({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 45)); |
154 | |
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); |
161 | } |
162 | |
163 | { |
164 | NotAnAllocator a; |
165 | std::set s{ a }; // set(initializer_list<NotAnAllocator>) |
166 | ASSERT_SAME_TYPE(decltype(s), std::set<NotAnAllocator>); |
167 | assert(s.size() == 1); |
168 | } |
169 | |
170 | { |
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); |
175 | } |
176 | |
177 | { |
178 | NotAnAllocator a; |
179 | std::set s{ a, a }; // set(initializer_list<NotAnAllocator>) |
180 | ASSERT_SAME_TYPE(decltype(s), std::set<NotAnAllocator>); |
181 | assert(s.size() == 1); |
182 | } |
183 | |
184 | { |
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); |
189 | } |
190 | |
191 | { |
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); |
196 | } |
197 | |
198 | #if TEST_STD_VER >= 23 |
199 | { |
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>; |
204 | |
205 | { // (from_range, range) |
206 | std::set c(std::from_range, Range()); |
207 | static_assert(std::is_same_v<decltype(c), std::set<int>>); |
208 | } |
209 | |
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>>); |
213 | } |
214 | |
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>>); |
218 | } |
219 | |
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>>); |
223 | } |
224 | } |
225 | #endif |
226 | |
227 | AssociativeContainerDeductionGuidesSfinaeAway<std::set, std::set<int>>(); |
228 | |
229 | return 0; |
230 | } |
231 | |