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// <unordered_map>
10// UNSUPPORTED: c++03, c++11, c++14
11
12// template<class InputIterator,
13// class Hash = hash<iter-key-type<InputIterator>>,
14// class Pred = equal_to<iter-key-type<InputIterator>>,
15// class Allocator = allocator<iter-to-alloc-type<InputIterator>>>
16// unordered_multimap(InputIterator, InputIterator, typename see below::size_type = see below,
17// Hash = Hash(), Pred = Pred(), Allocator = Allocator())
18// -> unordered_multimap<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>, Hash, Pred,
19// Allocator>;
20//
21// template<class Key, class T, class Hash = hash<Key>,
22// class Pred = equal_to<Key>, class Allocator = allocator<pair<const Key, T>>>
23// unordered_multimap(initializer_list<pair<Key, T>>,
24// typename see below::size_type = see below, Hash = Hash(),
25// Pred = Pred(), Allocator = Allocator())
26// -> unordered_multimap<Key, T, Hash, Pred, Allocator>;
27//
28// template<class InputIterator, class Allocator>
29// unordered_multimap(InputIterator, InputIterator, typename see below::size_type, Allocator)
30// -> unordered_multimap<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>,
31// hash<iter-key-type<InputIterator>>,
32// equal_to<iter-key-type<InputIterator>>, Allocator>;
33//
34// template<class InputIterator, class Allocator>
35// unordered_multimap(InputIterator, InputIterator, Allocator)
36// -> unordered_multimap<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>,
37// hash<iter-key-type<InputIterator>>,
38// equal_to<iter-key-type<InputIterator>>, Allocator>;
39//
40// template<class InputIterator, class Hash, class Allocator>
41// unordered_multimap(InputIterator, InputIterator, typename see below::size_type, Hash, Allocator)
42// -> unordered_multimap<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>, Hash,
43// equal_to<iter-key-type<InputIterator>>, Allocator>;
44//
45// template<class Key, class T, class Allocator>
46// unordered_multimap(initializer_list<pair<Key, T>>, typename see below::size_type, Allocator)
47// -> unordered_multimap<Key, T, hash<Key>, equal_to<Key>, Allocator>;
48//
49// template<class Key, class T, class Allocator>
50// unordered_multimap(initializer_list<pair<Key, T>>, Allocator)
51// -> unordered_multimap<Key, T, hash<Key>, equal_to<Key>, Allocator>;
52//
53// template<class Key, class T, class Hash, class Allocator>
54// unordered_multimap(initializer_list<pair<Key, T>>, typename see below::size_type, Hash,
55// Allocator)
56// -> unordered_multimap<Key, T, Hash, equal_to<Key>, Allocator>;
57//
58// template<ranges::input_range R, class Hash = hash<range-key-type<R>>,
59// class Pred = equal_to<range-key-type<R>>,
60// class Allocator = allocator<range-to-alloc-type<R>>>
61// unordered_multimap(from_range_t, R&&, typename see below::size_type = see below,
62// Hash = Hash(), Pred = Pred(), Allocator = Allocator())
63// -> unordered_multimap<range-key-type<R>, range-mapped-type<R>, Hash, Pred, Allocator>; // C++23
64//
65// template<ranges::input_range R, class Allocator>
66// unordered_multimap(from_range_t, R&&, typename see below::size_type, Allocator)
67// -> unordered_multimap<range-key-type<R>, range-mapped-type<R>, hash<range-key-type<R>>,
68// equal_to<range-key-type<R>>, Allocator>; // C++23
69//
70// template<ranges::input_range R, class Allocator>
71// unordered_multimap(from_range_t, R&&, Allocator)
72// -> unordered_multimap<range-key-type<R>, range-mapped-type<R>, hash<range-key-type<R>>,
73// equal_to<range-key-type<R>>, Allocator>; // C++23
74//
75// template<ranges::input_range R, class Hash, class Allocator>
76// unordered_multimap(from_range_t, R&&, typename see below::size_type, Hash, Allocator)
77// -> unordered_multimap<range-key-type<R>, range-mapped-type<R>, Hash,
78// equal_to<range-key-type<R>>, Allocator>; // C++23
79
80#include <algorithm> // is_permutation
81#include <array>
82#include <cassert>
83#include <climits> // INT_MAX
84#include <tuple>
85#include <type_traits>
86#include <unordered_map>
87#include <utility>
88#include <vector>
89
90#include "../../../test_compare.h"
91#include "../../../test_hash.h"
92#include "deduction_guides_sfinae_checks.h"
93#include "test_allocator.h"
94
95using P = std::pair<int, long>;
96using PC = std::pair<const int, long>;
97
98int main(int, char**) {
99 const PC expected_m[] = {{1, 1}, {1, 1}, {2, 2}, {3, 1}, {INT_MAX, 1}};
100
101 {
102 const P arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
103 std::unordered_multimap m(std::begin(arr: arr), std::end(arr: arr));
104 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long>);
105 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
106 }
107
108 {
109 const P arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
110 std::unordered_multimap m(std::begin(arr: arr), std::end(arr: arr), 42);
111 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long>);
112 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
113 }
114
115 {
116 const P arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
117 std::unordered_multimap m(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>());
118 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long, std::hash<short>, std::equal_to<int>>);
119 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
120 }
121
122 {
123 const P arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
124 std::unordered_multimap m(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>(), std::equal_to<>());
125 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long, std::hash<short>, std::equal_to<>>);
126 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
127 }
128
129 {
130 const P arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
131 std::unordered_multimap m(
132 std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>(), std::equal_to<>(), test_allocator<PC>(0, 41));
133 ASSERT_SAME_TYPE(
134 decltype(m), std::unordered_multimap<int, long, std::hash<short>, std::equal_to<>, test_allocator<PC>>);
135 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
136 assert(m.get_allocator().get_id() == 41);
137 }
138
139 {
140 std::unordered_multimap<int, long> source;
141 std::unordered_multimap m(source);
142 ASSERT_SAME_TYPE(decltype(m), decltype(source));
143 assert(m.size() == 0);
144 }
145
146 {
147 std::unordered_multimap<int, long> source;
148 std::unordered_multimap m{source}; // braces instead of parens
149 ASSERT_SAME_TYPE(decltype(m), decltype(source));
150 assert(m.size() == 0);
151 }
152
153 {
154 std::unordered_multimap<int, long, std::hash<short>, std::equal_to<>, test_allocator<PC>> source;
155 test_allocator<PC> a(0, 42);
156 std::unordered_multimap m(source, a);
157 ASSERT_SAME_TYPE(decltype(m), decltype(source));
158 assert(m.get_allocator().get_id() == 42);
159 assert(m.size() == 0);
160 }
161
162 {
163 std::unordered_multimap<int, long, std::hash<short>, std::equal_to<>, test_allocator<PC>> source;
164 test_allocator<PC> a(0, 43);
165 std::unordered_multimap m{source, a}; // braces instead of parens
166 ASSERT_SAME_TYPE(decltype(m), decltype(source));
167 assert(m.get_allocator().get_id() == 43);
168 assert(m.size() == 0);
169 }
170
171 {
172 std::unordered_multimap m{P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}};
173 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long>);
174 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
175 }
176
177 {
178 std::unordered_multimap m({P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, 42);
179 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long>);
180 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
181 }
182
183 {
184 std::unordered_multimap m({P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, 42, std::hash<short>());
185 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long, std::hash<short>>);
186 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
187 }
188
189 {
190 std::unordered_multimap m(
191 {P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, 42, std::hash<short>(), std::equal_to<>());
192 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, long, std::hash<short>, std::equal_to<>>);
193 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
194 }
195
196 {
197 std::unordered_multimap m(
198 {P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}},
199 42,
200 std::hash<short>(),
201 std::equal_to<>(),
202 test_allocator<PC>(0, 44));
203 ASSERT_SAME_TYPE(
204 decltype(m), std::unordered_multimap<int, long, std::hash<short>, std::equal_to<>, test_allocator<PC>>);
205 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
206 assert(m.get_allocator().get_id() == 44);
207 }
208
209 {
210 const P arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
211 std::unordered_multimap m(std::begin(arr: arr), std::end(arr: arr), 42, test_allocator<PC>(0, 45));
212 ASSERT_SAME_TYPE(
213 decltype(m), std::unordered_multimap<int, long, std::hash<int>, std::equal_to<int>, test_allocator<PC>>);
214 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
215 assert(m.get_allocator().get_id() == 45);
216 }
217
218 {
219 const P arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
220 std::unordered_multimap m(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>(), test_allocator<PC>(0, 46));
221 ASSERT_SAME_TYPE(
222 decltype(m), std::unordered_multimap<int, long, std::hash<short>, std::equal_to<int>, test_allocator<PC>>);
223 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
224 assert(m.get_allocator().get_id() == 46);
225 }
226
227 {
228 std::unordered_multimap m({P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, 42, test_allocator<PC>(0, 47));
229 ASSERT_SAME_TYPE(
230 decltype(m), std::unordered_multimap<int, long, std::hash<int>, std::equal_to<int>, test_allocator<PC>>);
231 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
232 assert(m.get_allocator().get_id() == 47);
233 }
234
235 {
236 std::unordered_multimap m(
237 {P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, 42, std::hash<short>(), test_allocator<PC>(0, 48));
238 ASSERT_SAME_TYPE(
239 decltype(m), std::unordered_multimap<int, long, std::hash<short>, std::equal_to<int>, test_allocator<PC>>);
240 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
241 assert(m.get_allocator().get_id() == 48);
242 }
243
244 {
245 // Examples from LWG3025
246 std::unordered_multimap m{std::pair{1, 1}, {2, 2}, {3, 3}};
247 ASSERT_SAME_TYPE(decltype(m), std::unordered_multimap<int, int>);
248
249 std::unordered_multimap m2{m.begin(), m.end()};
250 ASSERT_SAME_TYPE(decltype(m2), std::unordered_multimap<int, int>);
251 }
252
253 {
254 // Examples from LWG3531
255 std::unordered_multimap m1{{std::pair{1, 2}, {3, 4}}, 0};
256 ASSERT_SAME_TYPE(decltype(m1), std::unordered_multimap<int, int>);
257
258 using value_type = std::pair<const int, int>;
259 std::unordered_multimap m2{{value_type{1, 2}, {3, 4}}, 0};
260 ASSERT_SAME_TYPE(decltype(m2), std::unordered_multimap<int, int>);
261 }
262
263#if TEST_STD_VER >= 23
264 {
265 using Range = std::array<P, 0>;
266 using Pred = test_equal_to<int>;
267 using DefaultPred = std::equal_to<int>;
268 using Hash = test_hash<int>;
269 using DefaultHash = std::hash<int>;
270 using Alloc = test_allocator<PC>;
271
272 { // (from_range, range)
273 std::unordered_multimap c(std::from_range, Range());
274 static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long>>);
275 }
276
277 { // (from_range, range, n)
278 std::unordered_multimap c(std::from_range, Range(), std::size_t());
279 static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long>>);
280 }
281
282 { // (from_range, range, n, hash)
283 std::unordered_multimap c(std::from_range, Range(), std::size_t(), Hash());
284 static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long, Hash>>);
285 }
286
287 { // (from_range, range, n, hash, pred)
288 std::unordered_multimap c(std::from_range, Range(), std::size_t(), Hash(), Pred());
289 static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long, Hash, Pred>>);
290 }
291
292 { // (from_range, range, n, hash, pred, alloc)
293 std::unordered_multimap c(std::from_range, Range(), std::size_t(), Hash(), Pred(), Alloc());
294 static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long, Hash, Pred, Alloc>>);
295 }
296
297 { // (from_range, range, n, alloc)
298 std::unordered_multimap c(std::from_range, Range(), std::size_t(), Alloc());
299 static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long, DefaultHash, DefaultPred, Alloc>>);
300 }
301
302 // TODO(LWG 2713): uncomment this test once the constructor is added.
303 { // (from_range, range, alloc)
304 //std::unordered_multimap c(std::from_range, Range(), Alloc());
305 //static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long, DefaultHash, DefaultPred, Alloc>>);
306 }
307
308 { // (from_range, range, n, hash, alloc)
309 std::unordered_multimap c(std::from_range, Range(), std::size_t(), Hash(), Alloc());
310 static_assert(std::is_same_v<decltype(c), std::unordered_multimap<int, long, Hash, DefaultPred, Alloc>>);
311 }
312 }
313
314 {
315 std::vector<std::pair<const int, float>> pair_vec = {{1, 1.1f}, {2, 2.2f}, {3, 3.3f}};
316 std::unordered_multimap umm1(pair_vec.begin(), pair_vec.end());
317 ASSERT_SAME_TYPE(decltype(umm1), std::unordered_multimap<int, float>);
318
319 std::vector<std::tuple<int, double>> tuple_vec = {{10, 1.1}, {20, 2.2}, {30, 3.3}};
320 std::unordered_multimap umm2(tuple_vec.begin(), tuple_vec.end());
321 ASSERT_SAME_TYPE(decltype(umm2), std::unordered_multimap<int, double>);
322
323 std::vector<std::array<long, 2>> array_vec = {{100L, 101L}, {200L, 201L}, {300L, 301L}};
324 std::unordered_multimap umm3(array_vec.begin(), array_vec.end());
325 ASSERT_SAME_TYPE(decltype(umm3), std::unordered_multimap<long, long>);
326
327 std::vector<std::pair<int, char>> non_const_key_pair_vec = {{5, 'a'}, {6, 'b'}};
328 std::unordered_multimap umm4(non_const_key_pair_vec.begin(), non_const_key_pair_vec.end());
329 ASSERT_SAME_TYPE(decltype(umm4), std::unordered_multimap<int, char>);
330 }
331#endif
332
333 UnorderedContainerDeductionGuidesSfinaeAway<std::unordered_multimap, std::unordered_multimap<int, long>>();
334
335 return 0;
336}
337

source code of libcxx/test/std/containers/unord/unord.multimap/unord.multimap.cnstr/deduct.pass.cpp