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 |
10 | |
11 | // <unordered_map> |
12 | |
13 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
14 | // class Alloc = allocator<pair<const Key, T>>> |
15 | // class unordered_map |
16 | |
17 | // unordered_map& operator=(unordered_map&& u); |
18 | |
19 | #include <unordered_map> |
20 | #include <string> |
21 | #include <cassert> |
22 | #include <cfloat> |
23 | #include <cmath> |
24 | #include <cstddef> |
25 | |
26 | #include "test_macros.h" |
27 | #include "../../../test_compare.h" |
28 | #include "../../../test_hash.h" |
29 | #include "test_allocator.h" |
30 | #include "min_allocator.h" |
31 | |
32 | int main(int, char**) |
33 | { |
34 | { |
35 | typedef test_allocator<std::pair<const int, std::string> > A; |
36 | typedef std::unordered_map<int, std::string, |
37 | test_hash<int>, |
38 | test_equal_to<int>, |
39 | A |
40 | > C; |
41 | typedef std::pair<int, std::string> P; |
42 | P a[] = |
43 | { |
44 | P(1, "one" ), |
45 | P(2, "two" ), |
46 | P(3, "three" ), |
47 | P(4, "four" ), |
48 | P(1, "four" ), |
49 | P(2, "four" ), |
50 | }; |
51 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
52 | 7, |
53 | test_hash<int>(8), |
54 | test_equal_to<int>(9), |
55 | A(10) |
56 | ); |
57 | C c(a, a + 2, |
58 | 7, |
59 | test_hash<int>(2), |
60 | test_equal_to<int>(3), |
61 | A(4) |
62 | ); |
63 | c = std::move(c0); |
64 | LIBCPP_ASSERT(c.bucket_count() == 7); |
65 | assert(c.size() == 4); |
66 | assert(c.at(1) == "one" ); |
67 | assert(c.at(2) == "two" ); |
68 | assert(c.at(3) == "three" ); |
69 | assert(c.at(4) == "four" ); |
70 | assert(c.hash_function() == test_hash<int>(8)); |
71 | assert(c.key_eq() == test_equal_to<int>(9)); |
72 | assert(c.get_allocator() == A(4)); |
73 | assert(!c.empty()); |
74 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
75 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
76 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
77 | assert(c.max_load_factor() == 1); |
78 | } |
79 | { |
80 | typedef test_allocator<std::pair<const int, std::string> > A; |
81 | typedef std::unordered_map<int, std::string, |
82 | test_hash<int>, |
83 | test_equal_to<int>, |
84 | A |
85 | > C; |
86 | typedef std::pair<int, std::string> P; |
87 | P a[] = |
88 | { |
89 | P(1, "one" ), |
90 | P(2, "two" ), |
91 | P(3, "three" ), |
92 | P(4, "four" ), |
93 | P(1, "four" ), |
94 | P(2, "four" ), |
95 | }; |
96 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
97 | 7, |
98 | test_hash<int>(8), |
99 | test_equal_to<int>(9), |
100 | A(10) |
101 | ); |
102 | C c(a, a + 2, |
103 | 7, |
104 | test_hash<int>(2), |
105 | test_equal_to<int>(3), |
106 | A(10) |
107 | ); |
108 | C::iterator it0 = c0.begin(); |
109 | c = std::move(c0); |
110 | LIBCPP_ASSERT(c.bucket_count() == 7); |
111 | assert(c.size() == 4); |
112 | assert(c.at(1) == "one" ); |
113 | assert(c.at(2) == "two" ); |
114 | assert(c.at(3) == "three" ); |
115 | assert(c.at(4) == "four" ); |
116 | assert(c.hash_function() == test_hash<int>(8)); |
117 | assert(c.key_eq() == test_equal_to<int>(9)); |
118 | assert(c.get_allocator() == A(10)); |
119 | assert(!c.empty()); |
120 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
121 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
122 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
123 | assert(c.max_load_factor() == 1); |
124 | assert(c0.size() == 0); |
125 | assert(it0 == c.begin()); // Iterators remain valid |
126 | } |
127 | { |
128 | typedef other_allocator<std::pair<const int, std::string> > A; |
129 | typedef std::unordered_map<int, std::string, |
130 | test_hash<int>, |
131 | test_equal_to<int>, |
132 | A |
133 | > C; |
134 | typedef std::pair<int, std::string> P; |
135 | P a[] = |
136 | { |
137 | P(1, "one" ), |
138 | P(2, "two" ), |
139 | P(3, "three" ), |
140 | P(4, "four" ), |
141 | P(1, "four" ), |
142 | P(2, "four" ), |
143 | }; |
144 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
145 | 7, |
146 | test_hash<int>(8), |
147 | test_equal_to<int>(9), |
148 | A(10) |
149 | ); |
150 | C c(a, a + 2, |
151 | 7, |
152 | test_hash<int>(2), |
153 | test_equal_to<int>(3), |
154 | A(4) |
155 | ); |
156 | C::iterator it0 = c0.begin(); |
157 | c = std::move(c0); |
158 | LIBCPP_ASSERT(c.bucket_count() == 7); |
159 | assert(c.size() == 4); |
160 | assert(c.at(1) == "one" ); |
161 | assert(c.at(2) == "two" ); |
162 | assert(c.at(3) == "three" ); |
163 | assert(c.at(4) == "four" ); |
164 | assert(c.hash_function() == test_hash<int>(8)); |
165 | assert(c.key_eq() == test_equal_to<int>(9)); |
166 | assert(c.get_allocator() == A(10)); |
167 | assert(!c.empty()); |
168 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
169 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
170 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
171 | assert(c.max_load_factor() == 1); |
172 | assert(c0.size() == 0); |
173 | assert(it0 == c.begin()); // Iterators remain valid |
174 | } |
175 | { |
176 | typedef min_allocator<std::pair<const int, std::string> > A; |
177 | typedef std::unordered_map<int, std::string, |
178 | test_hash<int>, |
179 | test_equal_to<int>, |
180 | A |
181 | > C; |
182 | typedef std::pair<int, std::string> P; |
183 | P a[] = |
184 | { |
185 | P(1, "one" ), |
186 | P(2, "two" ), |
187 | P(3, "three" ), |
188 | P(4, "four" ), |
189 | P(1, "four" ), |
190 | P(2, "four" ), |
191 | }; |
192 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
193 | 7, |
194 | test_hash<int>(8), |
195 | test_equal_to<int>(9), |
196 | A() |
197 | ); |
198 | C c(a, a + 2, |
199 | 7, |
200 | test_hash<int>(2), |
201 | test_equal_to<int>(3), |
202 | A() |
203 | ); |
204 | C::iterator it0 = c0.begin(); |
205 | c = std::move(c0); |
206 | LIBCPP_ASSERT(c.bucket_count() == 7); |
207 | assert(c.size() == 4); |
208 | assert(c.at(1) == "one" ); |
209 | assert(c.at(2) == "two" ); |
210 | assert(c.at(3) == "three" ); |
211 | assert(c.at(4) == "four" ); |
212 | assert(c.hash_function() == test_hash<int>(8)); |
213 | assert(c.key_eq() == test_equal_to<int>(9)); |
214 | assert(c.get_allocator() == A()); |
215 | assert(!c.empty()); |
216 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
217 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
218 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
219 | assert(c.max_load_factor() == 1); |
220 | assert(c0.size() == 0); |
221 | assert(it0 == c.begin()); // Iterators remain valid |
222 | } |
223 | |
224 | return 0; |
225 | } |
226 | |