| 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 | |
| 11 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 12 | // class Alloc = allocator<pair<const Key, T>>> |
| 13 | // class unordered_map |
| 14 | |
| 15 | // void swap(unordered_map& __u); |
| 16 | |
| 17 | #include <unordered_map> |
| 18 | #include <string> |
| 19 | #include <cassert> |
| 20 | #include <cstddef> |
| 21 | |
| 22 | #include "../../../test_compare.h" |
| 23 | #include "../../../test_hash.h" |
| 24 | #include "test_macros.h" |
| 25 | #include "test_allocator.h" |
| 26 | #include "min_allocator.h" |
| 27 | |
| 28 | int main(int, char**) { |
| 29 | { |
| 30 | typedef test_hash<int> Hash; |
| 31 | typedef test_equal_to<int> Compare; |
| 32 | typedef test_allocator<std::pair<const int, std::string> > Alloc; |
| 33 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 34 | C c1(0, Hash(1), Compare(1), Alloc(1, 1)); |
| 35 | C c2(0, Hash(2), Compare(2), Alloc(1, 2)); |
| 36 | c2.max_load_factor(z: 2); |
| 37 | swap(c1, c2); |
| 38 | |
| 39 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
| 40 | assert(c1.size() == 0); |
| 41 | assert(c1.hash_function() == Hash(2)); |
| 42 | assert(c1.key_eq() == Compare(2)); |
| 43 | assert(c1.get_allocator().get_id() == 1); |
| 44 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 45 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 46 | assert(c1.max_load_factor() == 2); |
| 47 | |
| 48 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
| 49 | assert(c2.size() == 0); |
| 50 | assert(c2.hash_function() == Hash(1)); |
| 51 | assert(c2.key_eq() == Compare(1)); |
| 52 | assert(c2.get_allocator().get_id() == 2); |
| 53 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 54 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 55 | assert(c2.max_load_factor() == 1); |
| 56 | } |
| 57 | { |
| 58 | typedef test_hash<int> Hash; |
| 59 | typedef test_equal_to<int> Compare; |
| 60 | typedef test_allocator<std::pair<const int, std::string> > Alloc; |
| 61 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 62 | typedef std::pair<int, std::string> P; |
| 63 | P a2[] = { |
| 64 | P(10, "ten" ), |
| 65 | P(20, "twenty" ), |
| 66 | P(30, "thirty" ), |
| 67 | P(40, "forty" ), |
| 68 | P(50, "fifty" ), |
| 69 | P(60, "sixty" ), |
| 70 | P(70, "seventy" ), |
| 71 | P(80, "eighty" ), |
| 72 | }; |
| 73 | C c1(0, Hash(1), Compare(1), Alloc(1, 1)); |
| 74 | C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(1, 2)); |
| 75 | c2.max_load_factor(z: 2); |
| 76 | C::iterator it2 = c2.begin(); |
| 77 | swap(x&: c1, y&: c2); |
| 78 | |
| 79 | assert(c1.bucket_count() >= 8); |
| 80 | assert(c1.size() == 8); |
| 81 | assert(c1.at(10) == "ten" ); |
| 82 | assert(c1.at(20) == "twenty" ); |
| 83 | assert(c1.at(30) == "thirty" ); |
| 84 | assert(c1.at(40) == "forty" ); |
| 85 | assert(c1.at(50) == "fifty" ); |
| 86 | assert(c1.at(60) == "sixty" ); |
| 87 | assert(c1.at(70) == "seventy" ); |
| 88 | assert(c1.at(80) == "eighty" ); |
| 89 | assert(c1.hash_function() == Hash(2)); |
| 90 | assert(c1.key_eq() == Compare(2)); |
| 91 | assert(c1.get_allocator().get_id() == 1); |
| 92 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 93 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 94 | assert(c1.max_load_factor() == 2); |
| 95 | assert(it2 == c1.begin()); // Iterators are not invalidated |
| 96 | |
| 97 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
| 98 | assert(c2.size() == 0); |
| 99 | assert(c2.hash_function() == Hash(1)); |
| 100 | assert(c2.key_eq() == Compare(1)); |
| 101 | assert(c2.get_allocator().get_id() == 2); |
| 102 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 103 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 104 | assert(c2.max_load_factor() == 1); |
| 105 | } |
| 106 | { |
| 107 | typedef test_hash<int> Hash; |
| 108 | typedef test_equal_to<int> Compare; |
| 109 | typedef test_allocator<std::pair<const int, std::string> > Alloc; |
| 110 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 111 | typedef std::pair<int, std::string> P; |
| 112 | P a1[] = { |
| 113 | P(1, "one" ), |
| 114 | P(2, "two" ), |
| 115 | P(3, "three" ), |
| 116 | P(4, "four" ), |
| 117 | P(1, "four" ), |
| 118 | P(2, "four" ), |
| 119 | }; |
| 120 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1, 1)); |
| 121 | C c2(0, Hash(2), Compare(2), Alloc(1, 2)); |
| 122 | c2.max_load_factor(z: 2); |
| 123 | C::iterator it1 = c1.begin(); |
| 124 | swap(x&: c1, y&: c2); |
| 125 | |
| 126 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
| 127 | assert(c1.size() == 0); |
| 128 | assert(c1.hash_function() == Hash(2)); |
| 129 | assert(c1.key_eq() == Compare(2)); |
| 130 | assert(c1.get_allocator().get_id() == 1); |
| 131 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 132 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 133 | assert(c1.max_load_factor() == 2); |
| 134 | |
| 135 | assert(c2.bucket_count() >= 4); |
| 136 | assert(c2.size() == 4); |
| 137 | assert(c2.at(1) == "one" ); |
| 138 | assert(c2.at(2) == "two" ); |
| 139 | assert(c2.at(3) == "three" ); |
| 140 | assert(c2.at(4) == "four" ); |
| 141 | assert(c2.hash_function() == Hash(1)); |
| 142 | assert(c2.key_eq() == Compare(1)); |
| 143 | assert(c2.get_allocator().get_id() == 2); |
| 144 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 145 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 146 | assert(c2.max_load_factor() == 1); |
| 147 | assert(it1 == c2.begin()); // Iterators are not invalidated |
| 148 | } |
| 149 | { |
| 150 | typedef test_hash<int> Hash; |
| 151 | typedef test_equal_to<int> Compare; |
| 152 | typedef test_allocator<std::pair<const int, std::string> > Alloc; |
| 153 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 154 | typedef std::pair<int, std::string> P; |
| 155 | P a1[] = { |
| 156 | P(1, "one" ), |
| 157 | P(2, "two" ), |
| 158 | P(3, "three" ), |
| 159 | P(4, "four" ), |
| 160 | P(1, "four" ), |
| 161 | P(2, "four" ), |
| 162 | }; |
| 163 | P a2[] = { |
| 164 | P(10, "ten" ), |
| 165 | P(20, "twenty" ), |
| 166 | P(30, "thirty" ), |
| 167 | P(40, "forty" ), |
| 168 | P(50, "fifty" ), |
| 169 | P(60, "sixty" ), |
| 170 | P(70, "seventy" ), |
| 171 | P(80, "eighty" ), |
| 172 | }; |
| 173 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1, 1)); |
| 174 | C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(1, 2)); |
| 175 | c2.max_load_factor(z: 2); |
| 176 | C::iterator it1 = c1.begin(); |
| 177 | C::iterator it2 = c2.begin(); |
| 178 | swap(x&: c1, y&: c2); |
| 179 | |
| 180 | assert(c1.bucket_count() >= 8); |
| 181 | assert(c1.size() == 8); |
| 182 | assert(c1.at(10) == "ten" ); |
| 183 | assert(c1.at(20) == "twenty" ); |
| 184 | assert(c1.at(30) == "thirty" ); |
| 185 | assert(c1.at(40) == "forty" ); |
| 186 | assert(c1.at(50) == "fifty" ); |
| 187 | assert(c1.at(60) == "sixty" ); |
| 188 | assert(c1.at(70) == "seventy" ); |
| 189 | assert(c1.at(80) == "eighty" ); |
| 190 | assert(c1.hash_function() == Hash(2)); |
| 191 | assert(c1.key_eq() == Compare(2)); |
| 192 | assert(c1.get_allocator().get_id() == 1); |
| 193 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 194 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 195 | assert(c1.max_load_factor() == 2); |
| 196 | assert(it2 == c1.begin()); // Iterators are not invalidated |
| 197 | |
| 198 | assert(c2.bucket_count() >= 4); |
| 199 | assert(c2.size() == 4); |
| 200 | assert(c2.at(1) == "one" ); |
| 201 | assert(c2.at(2) == "two" ); |
| 202 | assert(c2.at(3) == "three" ); |
| 203 | assert(c2.at(4) == "four" ); |
| 204 | assert(c2.hash_function() == Hash(1)); |
| 205 | assert(c2.key_eq() == Compare(1)); |
| 206 | assert(c2.get_allocator().get_id() == 2); |
| 207 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 208 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 209 | assert(c2.max_load_factor() == 1); |
| 210 | assert(it1 == c2.begin()); // Iterators are not invalidated |
| 211 | } |
| 212 | |
| 213 | { |
| 214 | typedef test_hash<int> Hash; |
| 215 | typedef test_equal_to<int> Compare; |
| 216 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
| 217 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 218 | C c1(0, Hash(1), Compare(1), Alloc(1)); |
| 219 | C c2(0, Hash(2), Compare(2), Alloc(2)); |
| 220 | c2.max_load_factor(z: 2); |
| 221 | swap(x&: c1, y&: c2); |
| 222 | |
| 223 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
| 224 | assert(c1.size() == 0); |
| 225 | assert(c1.hash_function() == Hash(2)); |
| 226 | assert(c1.key_eq() == Compare(2)); |
| 227 | assert(c1.get_allocator() == Alloc(2)); |
| 228 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 229 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 230 | assert(c1.max_load_factor() == 2); |
| 231 | |
| 232 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
| 233 | assert(c2.size() == 0); |
| 234 | assert(c2.hash_function() == Hash(1)); |
| 235 | assert(c2.key_eq() == Compare(1)); |
| 236 | assert(c2.get_allocator() == Alloc(1)); |
| 237 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 238 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 239 | assert(c2.max_load_factor() == 1); |
| 240 | } |
| 241 | { |
| 242 | typedef test_hash<int> Hash; |
| 243 | typedef test_equal_to<int> Compare; |
| 244 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
| 245 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 246 | typedef std::pair<int, std::string> P; |
| 247 | P a2[] = { |
| 248 | P(10, "ten" ), |
| 249 | P(20, "twenty" ), |
| 250 | P(30, "thirty" ), |
| 251 | P(40, "forty" ), |
| 252 | P(50, "fifty" ), |
| 253 | P(60, "sixty" ), |
| 254 | P(70, "seventy" ), |
| 255 | P(80, "eighty" ), |
| 256 | }; |
| 257 | C c1(0, Hash(1), Compare(1), Alloc(1)); |
| 258 | C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(2)); |
| 259 | c2.max_load_factor(z: 2); |
| 260 | swap(x&: c1, y&: c2); |
| 261 | |
| 262 | assert(c1.bucket_count() >= 8); |
| 263 | assert(c1.size() == 8); |
| 264 | assert(c1.at(10) == "ten" ); |
| 265 | assert(c1.at(20) == "twenty" ); |
| 266 | assert(c1.at(30) == "thirty" ); |
| 267 | assert(c1.at(40) == "forty" ); |
| 268 | assert(c1.at(50) == "fifty" ); |
| 269 | assert(c1.at(60) == "sixty" ); |
| 270 | assert(c1.at(70) == "seventy" ); |
| 271 | assert(c1.at(80) == "eighty" ); |
| 272 | assert(c1.hash_function() == Hash(2)); |
| 273 | assert(c1.key_eq() == Compare(2)); |
| 274 | assert(c1.get_allocator() == Alloc(2)); |
| 275 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 276 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 277 | assert(c1.max_load_factor() == 2); |
| 278 | |
| 279 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
| 280 | assert(c2.size() == 0); |
| 281 | assert(c2.hash_function() == Hash(1)); |
| 282 | assert(c2.key_eq() == Compare(1)); |
| 283 | assert(c2.get_allocator() == Alloc(1)); |
| 284 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 285 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 286 | assert(c2.max_load_factor() == 1); |
| 287 | } |
| 288 | { |
| 289 | typedef test_hash<int> Hash; |
| 290 | typedef test_equal_to<int> Compare; |
| 291 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
| 292 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 293 | typedef std::pair<int, std::string> P; |
| 294 | P a1[] = { |
| 295 | P(1, "one" ), |
| 296 | P(2, "two" ), |
| 297 | P(3, "three" ), |
| 298 | P(4, "four" ), |
| 299 | P(1, "four" ), |
| 300 | P(2, "four" ), |
| 301 | }; |
| 302 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1)); |
| 303 | C c2(0, Hash(2), Compare(2), Alloc(2)); |
| 304 | c2.max_load_factor(z: 2); |
| 305 | swap(x&: c1, y&: c2); |
| 306 | |
| 307 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
| 308 | assert(c1.size() == 0); |
| 309 | assert(c1.hash_function() == Hash(2)); |
| 310 | assert(c1.key_eq() == Compare(2)); |
| 311 | assert(c1.get_allocator() == Alloc(2)); |
| 312 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 313 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 314 | assert(c1.max_load_factor() == 2); |
| 315 | |
| 316 | assert(c2.bucket_count() >= 4); |
| 317 | assert(c2.size() == 4); |
| 318 | assert(c2.at(1) == "one" ); |
| 319 | assert(c2.at(2) == "two" ); |
| 320 | assert(c2.at(3) == "three" ); |
| 321 | assert(c2.at(4) == "four" ); |
| 322 | assert(c2.hash_function() == Hash(1)); |
| 323 | assert(c2.key_eq() == Compare(1)); |
| 324 | assert(c2.get_allocator() == Alloc(1)); |
| 325 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 326 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 327 | assert(c2.max_load_factor() == 1); |
| 328 | } |
| 329 | { |
| 330 | typedef test_hash<int> Hash; |
| 331 | typedef test_equal_to<int> Compare; |
| 332 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
| 333 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 334 | typedef std::pair<int, std::string> P; |
| 335 | P a1[] = { |
| 336 | P(1, "one" ), |
| 337 | P(2, "two" ), |
| 338 | P(3, "three" ), |
| 339 | P(4, "four" ), |
| 340 | P(1, "four" ), |
| 341 | P(2, "four" ), |
| 342 | }; |
| 343 | P a2[] = { |
| 344 | P(10, "ten" ), |
| 345 | P(20, "twenty" ), |
| 346 | P(30, "thirty" ), |
| 347 | P(40, "forty" ), |
| 348 | P(50, "fifty" ), |
| 349 | P(60, "sixty" ), |
| 350 | P(70, "seventy" ), |
| 351 | P(80, "eighty" ), |
| 352 | }; |
| 353 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1)); |
| 354 | C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(2)); |
| 355 | c2.max_load_factor(z: 2); |
| 356 | swap(x&: c1, y&: c2); |
| 357 | |
| 358 | assert(c1.bucket_count() >= 8); |
| 359 | assert(c1.size() == 8); |
| 360 | assert(c1.at(10) == "ten" ); |
| 361 | assert(c1.at(20) == "twenty" ); |
| 362 | assert(c1.at(30) == "thirty" ); |
| 363 | assert(c1.at(40) == "forty" ); |
| 364 | assert(c1.at(50) == "fifty" ); |
| 365 | assert(c1.at(60) == "sixty" ); |
| 366 | assert(c1.at(70) == "seventy" ); |
| 367 | assert(c1.at(80) == "eighty" ); |
| 368 | assert(c1.hash_function() == Hash(2)); |
| 369 | assert(c1.key_eq() == Compare(2)); |
| 370 | assert(c1.get_allocator() == Alloc(2)); |
| 371 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 372 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 373 | assert(c1.max_load_factor() == 2); |
| 374 | |
| 375 | assert(c2.bucket_count() >= 4); |
| 376 | assert(c2.size() == 4); |
| 377 | assert(c2.at(1) == "one" ); |
| 378 | assert(c2.at(2) == "two" ); |
| 379 | assert(c2.at(3) == "three" ); |
| 380 | assert(c2.at(4) == "four" ); |
| 381 | assert(c2.hash_function() == Hash(1)); |
| 382 | assert(c2.key_eq() == Compare(1)); |
| 383 | assert(c2.get_allocator() == Alloc(1)); |
| 384 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 385 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 386 | assert(c2.max_load_factor() == 1); |
| 387 | } |
| 388 | #if TEST_STD_VER >= 11 |
| 389 | { |
| 390 | typedef test_hash<int> Hash; |
| 391 | typedef test_equal_to<int> Compare; |
| 392 | typedef min_allocator<std::pair<const int, std::string> > Alloc; |
| 393 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 394 | C c1(0, Hash(1), Compare(1), Alloc()); |
| 395 | C c2(0, Hash(2), Compare(2), Alloc()); |
| 396 | c2.max_load_factor(2); |
| 397 | swap(c1, c2); |
| 398 | |
| 399 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
| 400 | assert(c1.size() == 0); |
| 401 | assert(c1.hash_function() == Hash(2)); |
| 402 | assert(c1.key_eq() == Compare(2)); |
| 403 | assert(c1.get_allocator() == Alloc()); |
| 404 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 405 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 406 | assert(c1.max_load_factor() == 2); |
| 407 | |
| 408 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
| 409 | assert(c2.size() == 0); |
| 410 | assert(c2.hash_function() == Hash(1)); |
| 411 | assert(c2.key_eq() == Compare(1)); |
| 412 | assert(c2.get_allocator() == Alloc()); |
| 413 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 414 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 415 | assert(c2.max_load_factor() == 1); |
| 416 | } |
| 417 | { |
| 418 | typedef test_hash<int> Hash; |
| 419 | typedef test_equal_to<int> Compare; |
| 420 | typedef min_allocator<std::pair<const int, std::string> > Alloc; |
| 421 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 422 | typedef std::pair<int, std::string> P; |
| 423 | P a2[] = { |
| 424 | P(10, "ten" ), |
| 425 | P(20, "twenty" ), |
| 426 | P(30, "thirty" ), |
| 427 | P(40, "forty" ), |
| 428 | P(50, "fifty" ), |
| 429 | P(60, "sixty" ), |
| 430 | P(70, "seventy" ), |
| 431 | P(80, "eighty" ), |
| 432 | }; |
| 433 | C c1(0, Hash(1), Compare(1), Alloc()); |
| 434 | C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc()); |
| 435 | c2.max_load_factor(2); |
| 436 | swap(c1, c2); |
| 437 | |
| 438 | assert(c1.bucket_count() >= 8); |
| 439 | assert(c1.size() == 8); |
| 440 | assert(c1.at(10) == "ten" ); |
| 441 | assert(c1.at(20) == "twenty" ); |
| 442 | assert(c1.at(30) == "thirty" ); |
| 443 | assert(c1.at(40) == "forty" ); |
| 444 | assert(c1.at(50) == "fifty" ); |
| 445 | assert(c1.at(60) == "sixty" ); |
| 446 | assert(c1.at(70) == "seventy" ); |
| 447 | assert(c1.at(80) == "eighty" ); |
| 448 | assert(c1.hash_function() == Hash(2)); |
| 449 | assert(c1.key_eq() == Compare(2)); |
| 450 | assert(c1.get_allocator() == Alloc()); |
| 451 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 452 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 453 | assert(c1.max_load_factor() == 2); |
| 454 | |
| 455 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
| 456 | assert(c2.size() == 0); |
| 457 | assert(c2.hash_function() == Hash(1)); |
| 458 | assert(c2.key_eq() == Compare(1)); |
| 459 | assert(c2.get_allocator() == Alloc()); |
| 460 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 461 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 462 | assert(c2.max_load_factor() == 1); |
| 463 | } |
| 464 | { |
| 465 | typedef test_hash<int> Hash; |
| 466 | typedef test_equal_to<int> Compare; |
| 467 | typedef min_allocator<std::pair<const int, std::string> > Alloc; |
| 468 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 469 | typedef std::pair<int, std::string> P; |
| 470 | P a1[] = { |
| 471 | P(1, "one" ), |
| 472 | P(2, "two" ), |
| 473 | P(3, "three" ), |
| 474 | P(4, "four" ), |
| 475 | P(1, "four" ), |
| 476 | P(2, "four" ), |
| 477 | }; |
| 478 | C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc()); |
| 479 | C c2(0, Hash(2), Compare(2), Alloc()); |
| 480 | c2.max_load_factor(2); |
| 481 | swap(c1, c2); |
| 482 | |
| 483 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
| 484 | assert(c1.size() == 0); |
| 485 | assert(c1.hash_function() == Hash(2)); |
| 486 | assert(c1.key_eq() == Compare(2)); |
| 487 | assert(c1.get_allocator() == Alloc()); |
| 488 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 489 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 490 | assert(c1.max_load_factor() == 2); |
| 491 | |
| 492 | assert(c2.bucket_count() >= 4); |
| 493 | assert(c2.size() == 4); |
| 494 | assert(c2.at(1) == "one" ); |
| 495 | assert(c2.at(2) == "two" ); |
| 496 | assert(c2.at(3) == "three" ); |
| 497 | assert(c2.at(4) == "four" ); |
| 498 | assert(c2.hash_function() == Hash(1)); |
| 499 | assert(c2.key_eq() == Compare(1)); |
| 500 | assert(c2.get_allocator() == Alloc()); |
| 501 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 502 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 503 | assert(c2.max_load_factor() == 1); |
| 504 | } |
| 505 | { |
| 506 | typedef test_hash<int> Hash; |
| 507 | typedef test_equal_to<int> Compare; |
| 508 | typedef min_allocator<std::pair<const int, std::string> > Alloc; |
| 509 | typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C; |
| 510 | typedef std::pair<int, std::string> P; |
| 511 | P a1[] = { |
| 512 | P(1, "one" ), |
| 513 | P(2, "two" ), |
| 514 | P(3, "three" ), |
| 515 | P(4, "four" ), |
| 516 | P(1, "four" ), |
| 517 | P(2, "four" ), |
| 518 | }; |
| 519 | P a2[] = { |
| 520 | P(10, "ten" ), |
| 521 | P(20, "twenty" ), |
| 522 | P(30, "thirty" ), |
| 523 | P(40, "forty" ), |
| 524 | P(50, "fifty" ), |
| 525 | P(60, "sixty" ), |
| 526 | P(70, "seventy" ), |
| 527 | P(80, "eighty" ), |
| 528 | }; |
| 529 | C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc()); |
| 530 | C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc()); |
| 531 | c2.max_load_factor(2); |
| 532 | swap(c1, c2); |
| 533 | |
| 534 | assert(c1.bucket_count() >= 8); |
| 535 | assert(c1.size() == 8); |
| 536 | assert(c1.at(10) == "ten" ); |
| 537 | assert(c1.at(20) == "twenty" ); |
| 538 | assert(c1.at(30) == "thirty" ); |
| 539 | assert(c1.at(40) == "forty" ); |
| 540 | assert(c1.at(50) == "fifty" ); |
| 541 | assert(c1.at(60) == "sixty" ); |
| 542 | assert(c1.at(70) == "seventy" ); |
| 543 | assert(c1.at(80) == "eighty" ); |
| 544 | assert(c1.hash_function() == Hash(2)); |
| 545 | assert(c1.key_eq() == Compare(2)); |
| 546 | assert(c1.get_allocator() == Alloc()); |
| 547 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 548 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
| 549 | assert(c1.max_load_factor() == 2); |
| 550 | |
| 551 | assert(c2.bucket_count() >= 4); |
| 552 | assert(c2.size() == 4); |
| 553 | assert(c2.at(1) == "one" ); |
| 554 | assert(c2.at(2) == "two" ); |
| 555 | assert(c2.at(3) == "three" ); |
| 556 | assert(c2.at(4) == "four" ); |
| 557 | assert(c2.hash_function() == Hash(1)); |
| 558 | assert(c2.key_eq() == Compare(1)); |
| 559 | assert(c2.get_allocator() == Alloc()); |
| 560 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
| 561 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
| 562 | assert(c2.max_load_factor() == 1); |
| 563 | } |
| 564 | #endif |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |