xref: /llvm-project/libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/deduct_const.pass.cpp (revision 3cd4531b9ba421d1d096e746d787fe3039a546bb)
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 
67 using P = std::pair<int, long>;
68 using PC = std::pair<const int, long>;
69 
main(int,char **)70 int main(int, char**)
71 {
72     const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
73 
74     {
75     const PC arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
76     std::unordered_map m(std::begin(arr), std::end(arr));
77     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long>);
78     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
79     }
80 
81     {
82     const PC arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
83     std::unordered_map m(std::begin(arr), std::end(arr), 42);
84     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long>);
85     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
86     }
87 
88     {
89     const PC arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
90     std::unordered_map m(std::begin(arr), std::end(arr), 42, std::hash<short>());
91     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<int>>);
92     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
93     }
94 
95     {
96     const PC arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
97     std::unordered_map m(std::begin(arr), std::end(arr), 42, std::hash<short>(), std::equal_to<>());
98     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<>>);
99     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
100     }
101 
102     {
103     const PC arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
104     std::unordered_map m(std::begin(arr), std::end(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({ PC{1,1L}, PC{2,2L}, PC{1,1L}, PC{INT_MAX,1L}, PC{3,1L} }, 42, std::hash<short>(), std::equal_to<>());
130     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<>>);
131     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
132     }
133 
134     {
135     std::unordered_map m({ PC{1,1L}, PC{2,2L}, PC{1,1L}, PC{INT_MAX,1L}, PC{3,1L} }, 42, std::hash<short>(), std::equal_to<>(), test_allocator<PC>(0, 44));
136     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<>, test_allocator<PC>>);
137     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
138     assert(m.get_allocator().get_id() == 44);
139     }
140 
141     {
142     const PC arr[] = { {1,1}, {2,2}, {1,1}, {INT_MAX,1}, {3,1} };
143     std::unordered_map m(std::begin(arr), std::end(arr), 42, test_allocator<PC>(0, 45));
144     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<int>, std::equal_to<int>, test_allocator<PC>>);
145     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
146     assert(m.get_allocator().get_id() == 45);
147     }
148 
149     {
150     const PC arr[] = { {1,1}, {2,2}, {1,1}, {INT_MAX,1}, {3,1} };
151     std::unordered_map m(std::begin(arr), std::end(arr), 42, std::hash<short>(), test_allocator<PC>(0, 46));
152     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<int>, test_allocator<PC>>);
153     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
154     assert(m.get_allocator().get_id() == 46);
155     }
156 
157     {
158     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));
159     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<int>, std::equal_to<int>, test_allocator<PC>>);
160     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
161     assert(m.get_allocator().get_id() == 47);
162     }
163 
164     {
165     std::unordered_map m({ PC{1,1L}, PC{2,2L}, PC{1,1L}, PC{INT_MAX,1L}, PC{3,1L} }, 42, std::hash<short>(), test_allocator<PC>(0, 48));
166     ASSERT_SAME_TYPE(decltype(m), std::unordered_map<int, long, std::hash<short>, std::equal_to<int>, test_allocator<PC>>);
167     assert(std::is_permutation(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
168     assert(m.get_allocator().get_id() == 48);
169     }
170 
171     return 0;
172 }
173