• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // multiset(InputIterator, InputIterator,
18 //          Compare = Compare(), Allocator = Allocator())
19 //   -> multiset<iter-value-type<InputIterator>, Compare, Allocator>;
20 // template<class Key, class Compare = less<Key>,
21 //          class Allocator = allocator<Key>>
22 // multiset(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
23 //   -> multiset<Key, Compare, Allocator>;
24 // template<class InputIterator, class Allocator>
25 // multiset(InputIterator, InputIterator, Allocator)
26 //   -> multiset<iter-value-type<InputIterator>,
27 //               less<iter-value-type<InputIterator>>, Allocator>;
28 // template<class Key, class Allocator>
29 // multiset(initializer_list<Key>, Allocator)
30 //   -> multiset<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::multiset s(std::begin(arr), std::end(arr));
49 
50     ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
51     const int expected_s[] = { 1, 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::multiset s(std::begin(arr), std::end(arr), std::greater<int>());
59 
60     ASSERT_SAME_TYPE(decltype(s), std::multiset<int, std::greater<int> >);
61     const int expected_s[] = { INT_MAX, 3, 2, 1, 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::multiset s(std::begin(arr), std::end(arr), std::greater<int>(),
69                     test_allocator<int>(0, 42));
70 
71     ASSERT_SAME_TYPE(
72         decltype(s),
73         std::multiset<int, std::greater<int>, test_allocator<int> >);
74     const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
75     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
76                       std::end(expected_s)));
77     assert(s.get_allocator().get_id() == 42);
78   }
79 
80   {
81     std::multiset<long> source;
82     std::multiset s(source);
83     ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
84     assert(s.size() == 0);
85   }
86 
87   {
88     std::multiset<long> source;
89     std::multiset s{ source };  // braces instead of parens
90     ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
91     assert(s.size() == 0);
92   }
93 
94   {
95     std::multiset<long> source;
96     std::multiset s(source, std::multiset<long>::allocator_type());
97     ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
98     assert(s.size() == 0);
99   }
100 
101   {
102     std::multiset s{ 1, 2, 1, INT_MAX, 3 };
103 
104     ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
105     const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
106     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
107                       std::end(expected_s)));
108   }
109 
110   {
111     std::multiset s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>());
112 
113     ASSERT_SAME_TYPE(decltype(s), std::multiset<int, std::greater<int> >);
114     const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
115     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
116                       std::end(expected_s)));
117   }
118 
119   {
120     std::multiset s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>(),
121                     test_allocator<int>(0, 43));
122 
123     ASSERT_SAME_TYPE(
124         decltype(s),
125         std::multiset<int, std::greater<int>, test_allocator<int> >);
126     const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
127     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
128                       std::end(expected_s)));
129     assert(s.get_allocator().get_id() == 43);
130   }
131 
132   {
133     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
134     std::multiset s(std::begin(arr), std::end(arr), test_allocator<int>(0, 44));
135 
136     ASSERT_SAME_TYPE(decltype(s),
137                      std::multiset<int, std::less<int>, test_allocator<int> >);
138     const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
139     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
140                       std::end(expected_s)));
141     assert(s.get_allocator().get_id() == 44);
142   }
143 
144   {
145     std::multiset s({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 45));
146 
147     ASSERT_SAME_TYPE(decltype(s),
148                      std::multiset<int, std::less<int>, test_allocator<int> >);
149     const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
150     assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
151                       std::end(expected_s)));
152     assert(s.get_allocator().get_id() == 45);
153   }
154 
155   {
156     NotAnAllocator a;
157     std::multiset s{ a }; // multiset(initializer_list<NotAnAllocator>)
158     ASSERT_SAME_TYPE(decltype(s), std::multiset<NotAnAllocator>);
159     assert(s.size() == 1);
160   }
161 
162   {
163     std::multiset<long> source;
164     std::multiset s{ source, source }; // multiset(initializer_list<multiset<long>>)
165     ASSERT_SAME_TYPE(decltype(s), std::multiset<std::multiset<long> >);
166     assert(s.size() == 2);
167   }
168 
169   {
170     NotAnAllocator a;
171     std::multiset s{ a, a }; // multiset(initializer_list<NotAnAllocator>)
172     ASSERT_SAME_TYPE(decltype(s), std::multiset<NotAnAllocator>);
173     assert(s.size() == 2);
174   }
175 
176   {
177     int source[3] = { 3, 4, 5 };
178     std::multiset s(source, source + 3); // multiset(InputIterator, InputIterator)
179     ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
180     assert(s.size() == 3);
181   }
182 
183   {
184     int source[3] = { 3, 4, 5 };
185     std::multiset s{ source, source + 3 }; // multiset(initializer_list<int*>)
186     ASSERT_SAME_TYPE(decltype(s), std::multiset<int *>);
187     assert(s.size() == 2);
188   }
189 
190   return 0;
191 }
192