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#include <algorithm> // std::is_permutation
59#include <cassert>
60#include <climits> // INT_MAX
61#include <iterator>
62#include <type_traits>
63#include <unordered_map>
64
65#include "test_allocator.h"
66
67using P = std::pair<int, long>;
68using PC = std::pair<const int, long>;
69
70int main(int, char**) {
71 const PC expected_m[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};
72
73 {
74 const PC arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
75 std::unordered_map m(std::begin(arr: arr), std::end(arr: arr));
76 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long>);
77 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
78 }
79
80 {
81 const PC arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
82 std::unordered_map m(std::begin(arr: arr), std::end(arr: arr), 42);
83 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long>);
84 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
85 }
86
87 {
88 const PC arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
89 std::unordered_map m(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>());
90 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<int>>);
91 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
92 }
93
94 {
95 const PC arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
96 std::unordered_map m(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>(), std::equal_to<>());
97 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<>>);
98 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
99 }
100
101 {
102 const PC arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
103 std::unordered_map m(
104 std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>(), std::equal_to<>(), test_allocator<PC>(0, 41));
105 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<>, test_allocator<PC>>);
106 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
107 assert(m.get_allocator().get_id() == 41);
108 }
109
110 {
111 std::unordered_map m{PC{1, 1L}, PC{2, 2L}, PC{1, 1L}, PC{INT_MAX, 1L}, PC{3, 1L}};
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 std::unordered_map m({PC{1, 1L}, PC{2, 2L}, PC{1, 1L}, PC{INT_MAX, 1L}, PC{3, 1L}}, 42);
118 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long>);
119 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
120 }
121
122 {
123 std::unordered_map m({PC{1, 1L}, PC{2, 2L}, PC{1, 1L}, PC{INT_MAX, 1L}, PC{3, 1L}}, 42, std::hash<short>());
124 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>>);
125 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
126 }
127
128 {
129 std::unordered_map m(
130 {PC{1, 1L}, PC{2, 2L}, PC{1, 1L}, PC{INT_MAX, 1L}, PC{3, 1L}}, 42, std::hash<short>(), std::equal_to<>());
131 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<>>);
132 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
133 }
134
135 {
136 std::unordered_map m(
137 {PC{1, 1L}, PC{2, 2L}, PC{1, 1L}, PC{INT_MAX, 1L}, PC{3, 1L}},
138 42,
139 std::hash<short>(),
140 std::equal_to<>(),
141 test_allocator<PC>(0, 44));
142 ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<>, test_allocator<PC>>);
143 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
144 assert(m.get_allocator().get_id() == 44);
145 }
146
147 {
148 const PC arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
149 std::unordered_map m(std::begin(arr: arr), std::end(arr: arr), 42, test_allocator<PC>(0, 45));
150 ASSERT_SAME_TYPE(
151 decltype(m), std::unordered_map<int, long, std::hash<int>, std::equal_to<int>, test_allocator<PC>>);
152 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
153 assert(m.get_allocator().get_id() == 45);
154 }
155
156 {
157 const PC arr[] = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 1}, {3, 1}};
158 std::unordered_map m(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<short>(), test_allocator<PC>(0, 46));
159 ASSERT_SAME_TYPE(
160 decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<int>, test_allocator<PC>>);
161 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
162 assert(m.get_allocator().get_id() == 46);
163 }
164
165 {
166 std::unordered_map m({PC{1, 1L}, PC{2, 2L}, PC{1, 1L}, PC{INT_MAX, 1L}, PC{3, 1L}}, 42, test_allocator<PC>(0, 47));
167 ASSERT_SAME_TYPE(
168 decltype(m), std::unordered_map<int, long, std::hash<int>, std::equal_to<int>, test_allocator<PC>>);
169 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
170 assert(m.get_allocator().get_id() == 47);
171 }
172
173 {
174 std::unordered_map m(
175 {PC{1, 1L}, PC{2, 2L}, PC{1, 1L}, PC{INT_MAX, 1L}, PC{3, 1L}},
176 42,
177 std::hash<short>(),
178 test_allocator<PC>(0, 48));
179 ASSERT_SAME_TYPE(
180 decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<int>, test_allocator<PC>>);
181 assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
182 assert(m.get_allocator().get_id() == 48);
183 }
184
185 return 0;
186}
187

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