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