llvm-project
230 строк · 7.4 Кб
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
51struct NotAnAllocator {
52friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
53};
54
55int main(int, char **) {
56{
57const int arr[] = { 1, 2, 1, INT_MAX, 3 };
58std::set s(std::begin(arr), std::end(arr));
59
60ASSERT_SAME_TYPE(decltype(s), std::set<int>);
61const int expected_s[] = { 1, 2, 3, INT_MAX };
62assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
63std::end(expected_s)));
64}
65
66{
67const int arr[] = { 1, 2, 1, INT_MAX, 3 };
68std::set s(std::begin(arr), std::end(arr), std::greater<int>());
69
70ASSERT_SAME_TYPE(decltype(s), std::set<int, std::greater<int> >);
71const int expected_s[] = { INT_MAX, 3, 2, 1 };
72assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
73std::end(expected_s)));
74}
75
76{
77const int arr[] = { 1, 2, 1, INT_MAX, 3 };
78std::set s(std::begin(arr), std::end(arr), std::greater<int>(),
79test_allocator<int>(0, 42));
80
81ASSERT_SAME_TYPE(decltype(s),
82std::set<int, std::greater<int>, test_allocator<int> >);
83const int expected_s[] = { INT_MAX, 3, 2, 1 };
84assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
85std::end(expected_s)));
86assert(s.get_allocator().get_id() == 42);
87}
88
89{
90std::set<long> source;
91std::set s(source);
92ASSERT_SAME_TYPE(decltype(s), std::set<long>);
93assert(s.size() == 0);
94}
95
96{
97std::set<long> source;
98std::set s{ source }; // braces instead of parens
99ASSERT_SAME_TYPE(decltype(s), std::set<long>);
100assert(s.size() == 0);
101}
102
103{
104std::set<long> source;
105std::set s(source, std::set<long>::allocator_type());
106ASSERT_SAME_TYPE(decltype(s), std::set<long>);
107assert(s.size() == 0);
108}
109
110{
111std::set s{ 1, 2, 1, INT_MAX, 3 };
112
113ASSERT_SAME_TYPE(decltype(s), std::set<int>);
114const int expected_s[] = { 1, 2, 3, INT_MAX };
115assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
116std::end(expected_s)));
117}
118
119{
120std::set s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>());
121
122ASSERT_SAME_TYPE(decltype(s), std::set<int, std::greater<int> >);
123const int expected_s[] = { INT_MAX, 3, 2, 1 };
124assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
125std::end(expected_s)));
126}
127
128{
129std::set s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>(),
130test_allocator<int>(0, 43));
131
132ASSERT_SAME_TYPE(decltype(s),
133std::set<int, std::greater<int>, test_allocator<int> >);
134const int expected_s[] = { INT_MAX, 3, 2, 1 };
135assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
136std::end(expected_s)));
137assert(s.get_allocator().get_id() == 43);
138}
139
140{
141const int arr[] = { 1, 2, 1, INT_MAX, 3 };
142std::set s(std::begin(arr), std::end(arr), test_allocator<int>(0, 44));
143
144ASSERT_SAME_TYPE(decltype(s),
145std::set<int, std::less<int>, test_allocator<int> >);
146const int expected_s[] = { 1, 2, 3, INT_MAX };
147assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
148std::end(expected_s)));
149assert(s.get_allocator().get_id() == 44);
150}
151
152{
153std::set s({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 45));
154
155ASSERT_SAME_TYPE(decltype(s),
156std::set<int, std::less<int>, test_allocator<int> >);
157const int expected_s[] = { 1, 2, 3, INT_MAX };
158assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
159std::end(expected_s)));
160assert(s.get_allocator().get_id() == 45);
161}
162
163{
164NotAnAllocator a;
165std::set s{ a }; // set(initializer_list<NotAnAllocator>)
166ASSERT_SAME_TYPE(decltype(s), std::set<NotAnAllocator>);
167assert(s.size() == 1);
168}
169
170{
171std::set<long> source;
172std::set s{ source, source }; // set(initializer_list<set<long>>)
173ASSERT_SAME_TYPE(decltype(s), std::set<std::set<long> >);
174assert(s.size() == 1);
175}
176
177{
178NotAnAllocator a;
179std::set s{ a, a }; // set(initializer_list<NotAnAllocator>)
180ASSERT_SAME_TYPE(decltype(s), std::set<NotAnAllocator>);
181assert(s.size() == 1);
182}
183
184{
185int source[3] = { 3, 4, 5 };
186std::set s(source, source + 3); // set(InputIterator, InputIterator)
187ASSERT_SAME_TYPE(decltype(s), std::set<int>);
188assert(s.size() == 3);
189}
190
191{
192int source[3] = { 3, 4, 5 };
193std::set s{ source, source + 3 }; // set(initializer_list<int*>)
194ASSERT_SAME_TYPE(decltype(s), std::set<int *>);
195assert(s.size() == 2);
196}
197
198#if TEST_STD_VER >= 23
199{
200using Range = std::array<int, 0>;
201using Comp = std::greater<int>;
202using DefaultComp = std::less<int>;
203using Alloc = test_allocator<int>;
204
205{ // (from_range, range)
206std::set c(std::from_range, Range());
207static_assert(std::is_same_v<decltype(c), std::set<int>>);
208}
209
210{ // (from_range, range, comp)
211std::set c(std::from_range, Range(), Comp());
212static_assert(std::is_same_v<decltype(c), std::set<int, Comp>>);
213}
214
215{ // (from_range, range, comp, alloc)
216std::set c(std::from_range, Range(), Comp(), Alloc());
217static_assert(std::is_same_v<decltype(c), std::set<int, Comp, Alloc>>);
218}
219
220{ // (from_range, range, alloc)
221std::set c(std::from_range, Range(), Alloc());
222static_assert(std::is_same_v<decltype(c), std::set<int, DefaultComp, Alloc>>);
223}
224}
225#endif
226
227AssociativeContainerDeductionGuidesSfinaeAway<std::set, std::set<int>>();
228
229return 0;
230}
231