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

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