xref: /llvm-project/libcxx/test/std/containers/associative/multiset/multiset.cons/deduct.pass.cpp (revision ef70fe4d264d20abdd8476605650479a96a62071)
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 // multiset(InputIterator, InputIterator,
17 //          Compare = Compare(), Allocator = Allocator())
18 //   -> multiset<iter-value-type<InputIterator>, Compare, Allocator>;
19 // template<class Key, class Compare = less<Key>,
20 //          class Allocator = allocator<Key>>
21 // multiset(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
22 //   -> multiset<Key, Compare, Allocator>;
23 // template<class InputIterator, class Allocator>
24 // multiset(InputIterator, InputIterator, Allocator)
25 //   -> multiset<iter-value-type<InputIterator>,
26 //               less<iter-value-type<InputIterator>>, Allocator>;
27 // template<class Key, class Allocator>
28 // multiset(initializer_list<Key>, Allocator)
29 //   -> multiset<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 //   multiset(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
34 //     -> multiset<ranges::range_value_t<R>, Compare, Allocator>;
35 //
36 // template<ranges::input_range R, class Allocator>
37 //   multiset(from_range_t, R&&, Allocator)
38 //     -> multiset<ranges::range_value_t<R>, less<ranges::range_value_t<R>>, Allocator>;
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 {
operator <(NotAnAllocator,NotAnAllocator)52   friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
53 };
54 
main(int,char **)55 int main(int, char **) {
56   {
57     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
58     std::multiset s(std::begin(arr), std::end(arr));
59 
60     ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
61     const int expected_s[] = { 1, 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::multiset s(std::begin(arr), std::end(arr), std::greater<int>());
69 
70     ASSERT_SAME_TYPE(decltype(s), std::multiset<int, std::greater<int> >);
71     const int expected_s[] = { INT_MAX, 3, 2, 1, 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::multiset s(std::begin(arr), std::end(arr), std::greater<int>(),
79                     test_allocator<int>(0, 42));
80 
81     ASSERT_SAME_TYPE(
82         decltype(s),
83         std::multiset<int, std::greater<int>, test_allocator<int> >);
84     const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
85     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
86                       std::end(expected_s)));
87     assert(s.get_allocator().get_id() == 42);
88   }
89 
90   {
91     std::multiset<long> source;
92     std::multiset s(source);
93     ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
94     assert(s.size() == 0);
95   }
96 
97   {
98     std::multiset<long> source;
99     std::multiset s{ source };  // braces instead of parens
100     ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
101     assert(s.size() == 0);
102   }
103 
104   {
105     std::multiset<long> source;
106     std::multiset s(source, std::multiset<long>::allocator_type());
107     ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
108     assert(s.size() == 0);
109   }
110 
111   {
112     std::multiset s{ 1, 2, 1, INT_MAX, 3 };
113 
114     ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
115     const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
116     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
117                       std::end(expected_s)));
118   }
119 
120   {
121     std::multiset s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>());
122 
123     ASSERT_SAME_TYPE(decltype(s), std::multiset<int, std::greater<int> >);
124     const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
125     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
126                       std::end(expected_s)));
127   }
128 
129   {
130     std::multiset s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>(),
131                     test_allocator<int>(0, 43));
132 
133     ASSERT_SAME_TYPE(
134         decltype(s),
135         std::multiset<int, std::greater<int>, test_allocator<int> >);
136     const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
137     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
138                       std::end(expected_s)));
139     assert(s.get_allocator().get_id() == 43);
140   }
141 
142   {
143     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
144     std::multiset s(std::begin(arr), std::end(arr), test_allocator<int>(0, 44));
145 
146     ASSERT_SAME_TYPE(decltype(s),
147                      std::multiset<int, std::less<int>, test_allocator<int> >);
148     const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
149     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
150                       std::end(expected_s)));
151     assert(s.get_allocator().get_id() == 44);
152   }
153 
154   {
155     std::multiset s({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 45));
156 
157     ASSERT_SAME_TYPE(decltype(s),
158                      std::multiset<int, std::less<int>, test_allocator<int> >);
159     const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
160     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
161                       std::end(expected_s)));
162     assert(s.get_allocator().get_id() == 45);
163   }
164 
165   {
166     NotAnAllocator a;
167     std::multiset s{ a }; // multiset(initializer_list<NotAnAllocator>)
168     ASSERT_SAME_TYPE(decltype(s), std::multiset<NotAnAllocator>);
169     assert(s.size() == 1);
170   }
171 
172   {
173     std::multiset<long> source;
174     std::multiset s{ source, source }; // multiset(initializer_list<multiset<long>>)
175     ASSERT_SAME_TYPE(decltype(s), std::multiset<std::multiset<long> >);
176     assert(s.size() == 2);
177   }
178 
179   {
180     NotAnAllocator a;
181     std::multiset s{ a, a }; // multiset(initializer_list<NotAnAllocator>)
182     ASSERT_SAME_TYPE(decltype(s), std::multiset<NotAnAllocator>);
183     assert(s.size() == 2);
184   }
185 
186   {
187     int source[3] = { 3, 4, 5 };
188     std::multiset s(source, source + 3); // multiset(InputIterator, InputIterator)
189     ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
190     assert(s.size() == 3);
191   }
192 
193   {
194     int source[3] = { 3, 4, 5 };
195     std::multiset s{ source, source + 3 }; // multiset(initializer_list<int*>)
196     ASSERT_SAME_TYPE(decltype(s), std::multiset<int *>);
197     assert(s.size() == 2);
198   }
199 
200 #if TEST_STD_VER >= 23
201     {
202       using Range = std::array<int, 0>;
203       using Comp = std::greater<int>;
204       using DefaultComp = std::less<int>;
205       using Alloc = test_allocator<int>;
206 
207       { // (from_range, range)
208         std::multiset c(std::from_range, Range());
209         static_assert(std::is_same_v<decltype(c), std::multiset<int>>);
210       }
211 
212       { // (from_range, range, comp)
213         std::multiset c(std::from_range, Range(), Comp());
214         static_assert(std::is_same_v<decltype(c), std::multiset<int, Comp>>);
215       }
216 
217       { // (from_range, range, comp, alloc)
218         std::multiset c(std::from_range, Range(), Comp(), Alloc());
219         static_assert(std::is_same_v<decltype(c), std::multiset<int, Comp, Alloc>>);
220       }
221 
222       { // (from_range, range, alloc)
223         std::multiset c(std::from_range, Range(), Alloc());
224         static_assert(std::is_same_v<decltype(c), std::multiset<int, DefaultComp, Alloc>>);
225       }
226     }
227 #endif
228 
229   AssociativeContainerDeductionGuidesSfinaeAway<std::multiset, std::multiset<int>>();
230 
231   return 0;
232 }
233