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 // <set>
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: libcpp-no-deduction-guides
12 // UNSUPPORTED: apple-clang-9.1
13
14 // template<class InputIterator,
15 // class Compare = less<iter-value-type<InputIterator>>,
16 // class Allocator = allocator<iter-value-type<InputIterator>>>
17 // set(InputIterator, InputIterator,
18 // Compare = Compare(), Allocator = Allocator())
19 // -> set<iter-value-type<InputIterator>, Compare, Allocator>;
20 // template<class Key, class Compare = less<Key>,
21 // class Allocator = allocator<Key>>
22 // set(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
23 // -> set<Key, Compare, Allocator>;
24 // template<class InputIterator, class Allocator>
25 // set(InputIterator, InputIterator, Allocator)
26 // -> set<iter-value-type<InputIterator>,
27 // less<iter-value-type<InputIterator>>, Allocator>;
28 // template<class Key, class Allocator>
29 // set(initializer_list<Key>, Allocator)
30 // -> set<Key, less<Key>, Allocator>;
31
32 #include <algorithm> // std::equal
33 #include <cassert>
34 #include <climits> // INT_MAX
35 #include <functional>
36 #include <set>
37 #include <type_traits>
38
39 #include "test_allocator.h"
40
41 struct NotAnAllocator {
operator <(NotAnAllocator,NotAnAllocator)42 friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
43 };
44
main(int,char **)45 int main(int, char **) {
46 {
47 const int arr[] = { 1, 2, 1, INT_MAX, 3 };
48 std::set s(std::begin(arr), std::end(arr));
49
50 ASSERT_SAME_TYPE(decltype(s), std::set<int>);
51 const int expected_s[] = { 1, 2, 3, INT_MAX };
52 assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
53 std::end(expected_s)));
54 }
55
56 {
57 const int arr[] = { 1, 2, 1, INT_MAX, 3 };
58 std::set s(std::begin(arr), std::end(arr), std::greater<int>());
59
60 ASSERT_SAME_TYPE(decltype(s), std::set<int, std::greater<int> >);
61 const int expected_s[] = { INT_MAX, 3, 2, 1 };
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), std::end(arr), std::greater<int>(),
69 test_allocator<int>(0, 42));
70
71 ASSERT_SAME_TYPE(decltype(s),
72 std::set<int, std::greater<int>, test_allocator<int> >);
73 const int expected_s[] = { INT_MAX, 3, 2, 1 };
74 assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
75 std::end(expected_s)));
76 assert(s.get_allocator().get_id() == 42);
77 }
78
79 {
80 std::set<long> source;
81 std::set s(source);
82 ASSERT_SAME_TYPE(decltype(s), std::set<long>);
83 assert(s.size() == 0);
84 }
85
86 {
87 std::set<long> source;
88 std::set s{ source }; // braces instead of parens
89 ASSERT_SAME_TYPE(decltype(s), std::set<long>);
90 assert(s.size() == 0);
91 }
92
93 {
94 std::set<long> source;
95 std::set s(source, std::set<long>::allocator_type());
96 ASSERT_SAME_TYPE(decltype(s), std::set<long>);
97 assert(s.size() == 0);
98 }
99
100 {
101 std::set s{ 1, 2, 1, INT_MAX, 3 };
102
103 ASSERT_SAME_TYPE(decltype(s), std::set<int>);
104 const int expected_s[] = { 1, 2, 3, INT_MAX };
105 assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
106 std::end(expected_s)));
107 }
108
109 {
110 std::set s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>());
111
112 ASSERT_SAME_TYPE(decltype(s), std::set<int, std::greater<int> >);
113 const int expected_s[] = { INT_MAX, 3, 2, 1 };
114 assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
115 std::end(expected_s)));
116 }
117
118 {
119 std::set s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>(),
120 test_allocator<int>(0, 43));
121
122 ASSERT_SAME_TYPE(decltype(s),
123 std::set<int, std::greater<int>, test_allocator<int> >);
124 const int expected_s[] = { INT_MAX, 3, 2, 1 };
125 assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
126 std::end(expected_s)));
127 assert(s.get_allocator().get_id() == 43);
128 }
129
130 {
131 const int arr[] = { 1, 2, 1, INT_MAX, 3 };
132 std::set s(std::begin(arr), std::end(arr), test_allocator<int>(0, 44));
133
134 ASSERT_SAME_TYPE(decltype(s),
135 std::set<int, std::less<int>, test_allocator<int> >);
136 const int expected_s[] = { 1, 2, 3, INT_MAX };
137 assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
138 std::end(expected_s)));
139 assert(s.get_allocator().get_id() == 44);
140 }
141
142 {
143 std::set s({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 45));
144
145 ASSERT_SAME_TYPE(decltype(s),
146 std::set<int, std::less<int>, test_allocator<int> >);
147 const int expected_s[] = { 1, 2, 3, INT_MAX };
148 assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
149 std::end(expected_s)));
150 assert(s.get_allocator().get_id() == 45);
151 }
152
153 {
154 NotAnAllocator a;
155 std::set s{ a }; // set(initializer_list<NotAnAllocator>)
156 ASSERT_SAME_TYPE(decltype(s), std::set<NotAnAllocator>);
157 assert(s.size() == 1);
158 }
159
160 {
161 std::set<long> source;
162 std::set s{ source, source }; // set(initializer_list<set<long>>)
163 ASSERT_SAME_TYPE(decltype(s), std::set<std::set<long> >);
164 assert(s.size() == 1);
165 }
166
167 {
168 NotAnAllocator a;
169 std::set s{ a, a }; // set(initializer_list<NotAnAllocator>)
170 ASSERT_SAME_TYPE(decltype(s), std::set<NotAnAllocator>);
171 assert(s.size() == 1);
172 }
173
174 {
175 int source[3] = { 3, 4, 5 };
176 std::set s(source, source + 3); // set(InputIterator, InputIterator)
177 ASSERT_SAME_TYPE(decltype(s), std::set<int>);
178 assert(s.size() == 3);
179 }
180
181 {
182 int source[3] = { 3, 4, 5 };
183 std::set s{ source, source + 3 }; // set(initializer_list<int*>)
184 ASSERT_SAME_TYPE(decltype(s), std::set<int *>);
185 assert(s.size() == 2);
186 }
187
188 return 0;
189 }
190