| 1 | |
| 2 | // Copyright 2016 Daniel James. |
| 3 | // Copyright 2023 Christian Mazakas |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #include <boost/config.hpp> |
| 8 | |
| 9 | #include "../helpers/postfix.hpp" |
| 10 | #include "../helpers/prefix.hpp" |
| 11 | #include "../helpers/unordered.hpp" |
| 12 | |
| 13 | #include "../helpers/helpers.hpp" |
| 14 | #include "../helpers/metafunctions.hpp" |
| 15 | #include "../helpers/test.hpp" |
| 16 | #include <boost/core/pointer_traits.hpp> |
| 17 | #include <boost/static_assert.hpp> |
| 18 | #include <boost/type_traits/is_same.hpp> |
| 19 | #include <set> |
| 20 | #include <string> |
| 21 | |
| 22 | #if defined(BOOST_GCC) && BOOST_GCC >= 130000 |
| 23 | #pragma GCC diagnostic push |
| 24 | #pragma GCC diagnostic ignored "-Wself-move" |
| 25 | #endif |
| 26 | |
| 27 | template <template <class Key, class T, class Hash = boost::hash<Key>, |
| 28 | class Pred = std::equal_to<Key>, |
| 29 | class Allocator = std::allocator<std::pair<Key const, T> > > |
| 30 | class Map> |
| 31 | static void example1() |
| 32 | { |
| 33 | typedef typename Map<int, std::string>::insert_return_type insert_return_type; |
| 34 | |
| 35 | Map<int, std::string> src; |
| 36 | src.emplace(1, "one" ); |
| 37 | src.emplace(2, "two" ); |
| 38 | src.emplace(3, "buckle my shoe" ); |
| 39 | Map<int, std::string> dst; |
| 40 | dst.emplace(3, "three" ); |
| 41 | |
| 42 | dst.insert(src.extract(src.find(1))); |
| 43 | dst.insert(src.extract(2)); |
| 44 | insert_return_type r = dst.insert(src.extract(3)); |
| 45 | |
| 46 | BOOST_TEST(src.empty()); |
| 47 | BOOST_TEST(dst.size() == 3); |
| 48 | BOOST_TEST(dst[1] == "one" ); |
| 49 | BOOST_TEST(dst[2] == "two" ); |
| 50 | BOOST_TEST(dst[3] == "three" ); |
| 51 | BOOST_TEST(!r.inserted); |
| 52 | BOOST_TEST(r.position == dst.find(3)); |
| 53 | BOOST_TEST(r.node.mapped() == "buckle my shoe" ); |
| 54 | } |
| 55 | |
| 56 | template <template <class Key, class Hash = boost::hash<Key>, |
| 57 | class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> > |
| 58 | class Set> |
| 59 | static void example2() |
| 60 | { |
| 61 | Set<int> src; |
| 62 | src.insert(1); |
| 63 | src.insert(3); |
| 64 | src.insert(5); |
| 65 | Set<int> dst; |
| 66 | dst.insert(2); |
| 67 | dst.insert(4); |
| 68 | dst.insert(5); |
| 69 | // dst.merge(src); |
| 70 | // Merge src into dst. |
| 71 | // src == {5} |
| 72 | // dst == {1, 2, 3, 4, 5} |
| 73 | } |
| 74 | |
| 75 | template <template <class Key, class Hash = boost::hash<Key>, |
| 76 | class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> > |
| 77 | class Set> |
| 78 | static void example3() |
| 79 | { |
| 80 | typedef typename Set<int>::iterator iterator; |
| 81 | |
| 82 | Set<int> src; |
| 83 | src.insert(1); |
| 84 | src.insert(3); |
| 85 | src.insert(5); |
| 86 | Set<int> dst; |
| 87 | dst.insert(2); |
| 88 | dst.insert(4); |
| 89 | dst.insert(5); |
| 90 | for (iterator i = src.begin(); i != src.end();) { |
| 91 | std::pair<iterator, iterator> p = dst.equal_range(*i); |
| 92 | if (p.first == p.second) |
| 93 | dst.insert(p.first, src.extract(i++)); |
| 94 | else |
| 95 | ++i; |
| 96 | } |
| 97 | BOOST_TEST(src.size() == 1); |
| 98 | BOOST_TEST(*src.begin() == 5); |
| 99 | |
| 100 | std::set<int> dst2(dst.begin(), dst.end()); |
| 101 | std::set<int>::iterator it = dst2.begin(); |
| 102 | BOOST_TEST(*it++ == 1); |
| 103 | BOOST_TEST(*it++ == 2); |
| 104 | BOOST_TEST(*it++ == 3); |
| 105 | BOOST_TEST(*it++ == 4); |
| 106 | BOOST_TEST(*it++ == 5); |
| 107 | BOOST_TEST(it == dst2.end()); |
| 108 | } |
| 109 | |
| 110 | template <template <class Key, class T, class Hash = boost::hash<Key>, |
| 111 | class Pred = std::equal_to<Key>, |
| 112 | class Allocator = std::allocator<std::pair<Key const, T> > > |
| 113 | class Map, |
| 114 | template <class Key, class Hash = boost::hash<Key>, |
| 115 | class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> > |
| 116 | class Set> |
| 117 | static void failed_insertion_with_hint() |
| 118 | { |
| 119 | { |
| 120 | Set<int> src; |
| 121 | Set<int> dst; |
| 122 | src.emplace(10); |
| 123 | src.emplace(20); |
| 124 | dst.emplace(10); |
| 125 | dst.emplace(20); |
| 126 | |
| 127 | typename Set<int>::node_type nh = src.extract(10); |
| 128 | |
| 129 | BOOST_TEST(dst.insert(dst.find(10), std::move(nh)) == dst.find(10)); |
| 130 | BOOST_TEST(nh); |
| 131 | BOOST_TEST(!nh.empty()); |
| 132 | BOOST_TEST(nh.value() == 10); |
| 133 | |
| 134 | BOOST_TEST(dst.insert(dst.find(20), std::move(nh)) == dst.find(10)); |
| 135 | BOOST_TEST(nh); |
| 136 | BOOST_TEST(!nh.empty()); |
| 137 | BOOST_TEST(nh.value() == 10); |
| 138 | |
| 139 | BOOST_TEST(src.count(10) == 0); |
| 140 | BOOST_TEST(src.count(20) == 1); |
| 141 | BOOST_TEST(dst.count(10) == 1); |
| 142 | BOOST_TEST(dst.count(20) == 1); |
| 143 | } |
| 144 | |
| 145 | { |
| 146 | Map<int, int> src; |
| 147 | Map<int, int> dst; |
| 148 | src.emplace(10, 30); |
| 149 | src.emplace(20, 5); |
| 150 | dst.emplace(10, 20); |
| 151 | dst.emplace(20, 2); |
| 152 | |
| 153 | typename Map<int, int>::node_type nh = src.extract(10); |
| 154 | BOOST_TEST(dst.insert(dst.find(10), std::move(nh)) == dst.find(10)); |
| 155 | BOOST_TEST(nh); |
| 156 | BOOST_TEST(!nh.empty()); |
| 157 | BOOST_TEST(nh.key() == 10); |
| 158 | BOOST_TEST(nh.mapped() == 30); |
| 159 | BOOST_TEST(dst[10] == 20); |
| 160 | |
| 161 | BOOST_TEST(dst.insert(dst.find(20), std::move(nh)) == dst.find(10)); |
| 162 | BOOST_TEST(nh); |
| 163 | BOOST_TEST(!nh.empty()); |
| 164 | BOOST_TEST(nh.key() == 10); |
| 165 | BOOST_TEST(nh.mapped() == 30); |
| 166 | BOOST_TEST(dst[10] == 20); |
| 167 | |
| 168 | BOOST_TEST(src.count(10) == 0); |
| 169 | BOOST_TEST(src.count(20) == 1); |
| 170 | BOOST_TEST(dst.count(10) == 1); |
| 171 | BOOST_TEST(dst.count(20) == 1); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | template <typename NodeHandle> |
| 176 | bool node_handle_compare( |
| 177 | NodeHandle const& nh, typename NodeHandle::value_type const& x) |
| 178 | { |
| 179 | return x == nh.value(); |
| 180 | } |
| 181 | |
| 182 | template <typename NodeHandle> |
| 183 | bool node_handle_compare( |
| 184 | NodeHandle const& nh, std::pair<typename NodeHandle::key_type const, |
| 185 | typename NodeHandle::mapped_type> const& x) |
| 186 | { |
| 187 | return x.first == nh.key() && x.second == nh.mapped(); |
| 188 | } |
| 189 | |
| 190 | template <typename Container> void node_handle_tests_impl(Container& c) |
| 191 | { |
| 192 | typedef typename Container::node_type node_type; |
| 193 | |
| 194 | typename Container::value_type value = *c.begin(); |
| 195 | |
| 196 | node_type n1; |
| 197 | BOOST_TEST(!n1); |
| 198 | BOOST_TEST(n1.empty()); |
| 199 | |
| 200 | node_type n2 = c.extract(c.begin()); |
| 201 | BOOST_TEST(n2); |
| 202 | BOOST_TEST(!n2.empty()); |
| 203 | node_handle_compare(n2, value); |
| 204 | |
| 205 | node_type n3 = std::move(n2); |
| 206 | BOOST_TEST(n3); |
| 207 | BOOST_TEST(!n2); |
| 208 | node_handle_compare(n3, value); |
| 209 | // TODO: Check that n2 doesn't have an allocator? |
| 210 | // Maybe by swapping and observing that the allocator is |
| 211 | // swapped rather than moved? |
| 212 | |
| 213 | n1 = std::move(n3); |
| 214 | BOOST_TEST(n1); |
| 215 | BOOST_TEST(!n3); |
| 216 | node_handle_compare(n1, value); |
| 217 | |
| 218 | // Self move-assignment empties the node_handle. |
| 219 | n1 = std::move(n1); |
| 220 | BOOST_TEST(!n1); |
| 221 | |
| 222 | n3 = std::move(n3); |
| 223 | BOOST_TEST(!n3); |
| 224 | |
| 225 | typename Container::value_type value1 = *c.begin(); |
| 226 | n1 = c.extract(c.begin()); |
| 227 | typename Container::value_type value2 = *c.begin(); |
| 228 | n2 = c.extract(c.begin()); |
| 229 | n3 = node_type(); |
| 230 | |
| 231 | node_handle_compare(n1, value1); |
| 232 | node_handle_compare(n2, value2); |
| 233 | n1.swap(n2); |
| 234 | BOOST_TEST(n1); |
| 235 | BOOST_TEST(n2); |
| 236 | node_handle_compare(n1, value2); |
| 237 | node_handle_compare(n2, value1); |
| 238 | |
| 239 | BOOST_TEST(n1); |
| 240 | BOOST_TEST(!n3); |
| 241 | n1.swap(n3); |
| 242 | BOOST_TEST(!n1); |
| 243 | BOOST_TEST(n3); |
| 244 | node_handle_compare(n3, value2); |
| 245 | |
| 246 | BOOST_TEST(!n1); |
| 247 | BOOST_TEST(n2); |
| 248 | n1.swap(n2); |
| 249 | BOOST_TEST(n1); |
| 250 | BOOST_TEST(!n2); |
| 251 | node_handle_compare(n1, value1); |
| 252 | |
| 253 | node_type n4; |
| 254 | BOOST_TEST(!n2); |
| 255 | BOOST_TEST(!n4); |
| 256 | n2.swap(n4); |
| 257 | BOOST_TEST(!n2); |
| 258 | BOOST_TEST(!n4); |
| 259 | } |
| 260 | |
| 261 | template <template <class Key, class T, class Hash = boost::hash<Key>, |
| 262 | class Pred = std::equal_to<Key>, |
| 263 | class Allocator = std::allocator<std::pair<Key const, T> > > |
| 264 | class Map, |
| 265 | template <class Key, class Hash = boost::hash<Key>, |
| 266 | class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> > |
| 267 | class Set> |
| 268 | static void node_handle_tests() |
| 269 | { |
| 270 | Set<int> x1; |
| 271 | x1.emplace(100); |
| 272 | x1.emplace(140); |
| 273 | x1.emplace(-55); |
| 274 | node_handle_tests_impl(x1); |
| 275 | |
| 276 | Map<int, std::string> x2; |
| 277 | x2.emplace(10, "ten" ); |
| 278 | x2.emplace(-23, "twenty" ); |
| 279 | x2.emplace(-76, "thirty" ); |
| 280 | node_handle_tests_impl(x2); |
| 281 | } |
| 282 | |
| 283 | #ifdef BOOST_UNORDERED_FOA_TESTS |
| 284 | |
| 285 | template <class X> typename X::iterator insert_empty_node(X& x) |
| 286 | { |
| 287 | typedef typename X::node_type node_type; |
| 288 | return x.insert(node_type()).position; |
| 289 | } |
| 290 | |
| 291 | #else |
| 292 | |
| 293 | template <class Key, class T, class Hash, class KeyEqual, class Allocator> |
| 294 | typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator |
| 295 | insert_empty_node(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& c) |
| 296 | { |
| 297 | typedef |
| 298 | typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::node_type |
| 299 | node_type; |
| 300 | |
| 301 | return c.insert(node_type()).position; |
| 302 | } |
| 303 | |
| 304 | template <class T, class Hash, class KeyEqual, class Allocator> |
| 305 | typename boost::unordered_set<T, Hash, KeyEqual, Allocator>::iterator |
| 306 | insert_empty_node(boost::unordered_set<T, Hash, KeyEqual, Allocator>& c) |
| 307 | { |
| 308 | typedef typename boost::unordered_set<T, Hash, KeyEqual, Allocator>::node_type |
| 309 | node_type; |
| 310 | |
| 311 | return c.insert(node_type()).position; |
| 312 | } |
| 313 | |
| 314 | template <class Key, class T, class Hash, class KeyEqual, class Allocator> |
| 315 | typename boost::unordered_multimap<Key, T, Hash, KeyEqual, Allocator>::iterator |
| 316 | insert_empty_node( |
| 317 | boost::unordered_multimap<Key, T, Hash, KeyEqual, Allocator>& c) |
| 318 | { |
| 319 | typedef typename boost::unordered_multimap<Key, T, Hash, KeyEqual, |
| 320 | Allocator>::node_type node_type; |
| 321 | |
| 322 | return c.insert(node_type()); |
| 323 | } |
| 324 | |
| 325 | template <class T, class Hash, class KeyEqual, class Allocator> |
| 326 | typename boost::unordered_multiset<T, Hash, KeyEqual, Allocator>::iterator |
| 327 | insert_empty_node(boost::unordered_multiset<T, Hash, KeyEqual, Allocator>& c) |
| 328 | { |
| 329 | typedef |
| 330 | typename boost::unordered_multiset<T, Hash, KeyEqual, Allocator>::node_type |
| 331 | node_type; |
| 332 | |
| 333 | return c.insert(node_type()); |
| 334 | } |
| 335 | |
| 336 | #endif |
| 337 | |
| 338 | template <typename Container1, typename Container2> |
| 339 | static void insert_node_handle_unique(Container1& c1, Container2& c2) |
| 340 | { |
| 341 | typedef typename Container1::node_type node_type; |
| 342 | typedef typename Container1::value_type value_type; |
| 343 | BOOST_STATIC_ASSERT( |
| 344 | (boost::is_same<node_type, typename Container2::node_type>::value)); |
| 345 | |
| 346 | typedef typename Container1::iterator iterator1; |
| 347 | typedef typename Container2::iterator iterator2; |
| 348 | typedef typename Container2::insert_return_type insert_return_type2; |
| 349 | |
| 350 | Container1 c1_copy(c1); |
| 351 | Container2 c2_copy; |
| 352 | |
| 353 | iterator1 r1 = insert_empty_node(c1); |
| 354 | insert_return_type2 r2 = c2.insert(node_type()); |
| 355 | BOOST_TEST(r1 == c1.end()); |
| 356 | BOOST_TEST(!r2.inserted); |
| 357 | BOOST_TEST(!r2.node); |
| 358 | BOOST_TEST(r2.position == c2.end()); |
| 359 | |
| 360 | while (!c1.empty()) { |
| 361 | value_type v = *c1.begin(); |
| 362 | value_type const* v_ptr = boost::to_address(c1.begin()); |
| 363 | std::size_t count = c2.count(test::get_key<Container1>(v)); |
| 364 | insert_return_type2 r = c2.insert(c1.extract(c1.begin())); |
| 365 | if (!count) { |
| 366 | BOOST_TEST(r.inserted); |
| 367 | BOOST_TEST_EQ(c2.count(test::get_key<Container1>(v)), count + 1); |
| 368 | BOOST_TEST(r.position != c2.end()); |
| 369 | BOOST_TEST(boost::to_address(r.position) == v_ptr); |
| 370 | BOOST_TEST(!r.node); |
| 371 | } else { |
| 372 | BOOST_TEST(!r.inserted); |
| 373 | BOOST_TEST_EQ(c2.count(test::get_key<Container1>(v)), count); |
| 374 | BOOST_TEST(r.position != c2.end()); |
| 375 | BOOST_TEST( |
| 376 | test::get_key<Container2>(*r.position) == test::get_key<Container2>(v)); |
| 377 | BOOST_TEST(r.node); |
| 378 | node_handle_compare(r.node, v); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | while (!c1_copy.empty()) { |
| 383 | value_type v = *c1_copy.begin(); |
| 384 | value_type const* v_ptr = boost::to_address(c1_copy.begin()); |
| 385 | std::size_t count = c2_copy.count(test::get_key<Container1>(v)); |
| 386 | iterator2 pos = |
| 387 | c2_copy.insert(c2_copy.begin(), c1_copy.extract(c1_copy.begin())); |
| 388 | if (!count) { |
| 389 | BOOST_TEST_EQ(c2_copy.count(test::get_key<Container1>(v)), count + 1); |
| 390 | BOOST_TEST(pos != c2.end()); |
| 391 | BOOST_TEST(boost::to_address(pos) == v_ptr); |
| 392 | } else { |
| 393 | BOOST_TEST_EQ(c2_copy.count(test::get_key<Container1>(v)), count); |
| 394 | BOOST_TEST(pos != c2_copy.end()); |
| 395 | BOOST_TEST( |
| 396 | test::get_key<Container2>(*pos) == test::get_key<Container2>(v)); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | template <typename Container1, typename Container2> |
| 402 | static void insert_node_handle_unique2(Container1& c1, Container2& c2) |
| 403 | { |
| 404 | typedef typename Container1::node_type node_type; |
| 405 | typedef typename Container1::value_type value_type; |
| 406 | BOOST_STATIC_ASSERT( |
| 407 | (boost::is_same<node_type, typename Container2::node_type>::value)); |
| 408 | |
| 409 | // typedef typename Container1::insert_return_type |
| 410 | // insert_return_type1; |
| 411 | typedef typename Container2::insert_return_type insert_return_type2; |
| 412 | |
| 413 | while (!c1.empty()) { |
| 414 | value_type v = *c1.begin(); |
| 415 | value_type const* v_ptr = boost::to_address(c1.begin()); |
| 416 | std::size_t count = c2.count(test::get_key<Container1>(v)); |
| 417 | insert_return_type2 r = c2.insert(c1.extract(test::get_key<Container1>(v))); |
| 418 | if (r.inserted) { |
| 419 | BOOST_TEST_EQ(c2.count(test::get_key<Container1>(v)), count + 1); |
| 420 | BOOST_TEST(r.position != c2.end()); |
| 421 | BOOST_TEST(boost::to_address(r.position) == v_ptr); |
| 422 | BOOST_TEST(!r.node); |
| 423 | } else { |
| 424 | BOOST_TEST_EQ(c2.count(test::get_key<Container1>(v)), count); |
| 425 | BOOST_TEST(r.position != c2.end()); |
| 426 | BOOST_TEST( |
| 427 | test::get_key<Container2>(*r.position) == test::get_key<Container2>(v)); |
| 428 | BOOST_TEST(r.node); |
| 429 | node_handle_compare(r.node, v); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | struct hash_thing |
| 435 | { |
| 436 | std::size_t operator()(int x) const |
| 437 | { |
| 438 | return static_cast<std::size_t>(x * 13 + 5); |
| 439 | } |
| 440 | }; |
| 441 | |
| 442 | #ifdef BOOST_UNORDERED_FOA_TESTS |
| 443 | |
| 444 | UNORDERED_AUTO_TEST (examples) { |
| 445 | example1<boost::unordered_node_map>(); |
| 446 | example2<boost::unordered_node_set>(); |
| 447 | example3<boost::unordered_node_set>(); |
| 448 | failed_insertion_with_hint<boost::unordered_node_map, |
| 449 | boost::unordered_node_set>(); |
| 450 | node_handle_tests<boost::unordered_node_map, boost::unordered_node_set>(); |
| 451 | } |
| 452 | |
| 453 | UNORDERED_AUTO_TEST (insert_node_handle_unique_tests) { |
| 454 | { |
| 455 | boost::unordered_node_set<int> x1; |
| 456 | boost::unordered_node_set<int> x2; |
| 457 | x1.emplace(100); |
| 458 | x1.emplace(140); |
| 459 | x1.emplace(-55); |
| 460 | x2.emplace(140); |
| 461 | insert_node_handle_unique(x1, x2); |
| 462 | BOOST_TEST(x2.size() == 3); |
| 463 | } |
| 464 | |
| 465 | { |
| 466 | boost::unordered_node_set<int> x1; |
| 467 | boost::unordered_node_set<int> x2; |
| 468 | x1.emplace(100); |
| 469 | x1.emplace(140); |
| 470 | x1.emplace(-55); |
| 471 | x2.emplace(140); |
| 472 | insert_node_handle_unique(x1, x2); |
| 473 | BOOST_TEST(x2.size() == 3); |
| 474 | } |
| 475 | |
| 476 | { |
| 477 | boost::unordered_node_map<int, int, hash_thing> x1; |
| 478 | boost::unordered_node_map<int, int> x2; |
| 479 | x1.emplace(67, 50); |
| 480 | x1.emplace(23, 45); |
| 481 | x1.emplace(18, 19); |
| 482 | x2.emplace(23, 50); |
| 483 | x2.emplace(12, 49); |
| 484 | insert_node_handle_unique(x1, x2); |
| 485 | BOOST_TEST(x2.size() == 4); |
| 486 | } |
| 487 | |
| 488 | { |
| 489 | boost::unordered_node_map<int, int, hash_thing> x1; |
| 490 | boost::unordered_node_map<int, int> x2; |
| 491 | x1.emplace(67, 50); |
| 492 | x1.emplace(23, 45); |
| 493 | x1.emplace(18, 19); |
| 494 | x2.emplace(23, 50); |
| 495 | x2.emplace(12, 49); |
| 496 | insert_node_handle_unique(x1, x2); |
| 497 | BOOST_TEST(x2.size() == 4); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | UNORDERED_AUTO_TEST (insert_node_handle_unique_tests2) { |
| 502 | { |
| 503 | boost::unordered_node_set<int> x1; |
| 504 | boost::unordered_node_set<int> x2; |
| 505 | x1.emplace(100); |
| 506 | x1.emplace(140); |
| 507 | x1.emplace(-55); |
| 508 | x2.emplace(140); |
| 509 | insert_node_handle_unique2(x1, x2); |
| 510 | BOOST_TEST(x2.size() == 3); |
| 511 | } |
| 512 | |
| 513 | { |
| 514 | boost::unordered_node_set<int> x1; |
| 515 | boost::unordered_node_set<int> x2; |
| 516 | x1.emplace(100); |
| 517 | x1.emplace(140); |
| 518 | x1.emplace(-55); |
| 519 | x2.emplace(140); |
| 520 | insert_node_handle_unique2(x1, x2); |
| 521 | BOOST_TEST(x2.size() == 3); |
| 522 | } |
| 523 | |
| 524 | { |
| 525 | boost::unordered_node_map<int, int, hash_thing> x1; |
| 526 | boost::unordered_node_map<int, int> x2; |
| 527 | x1.emplace(67, 50); |
| 528 | x1.emplace(23, 45); |
| 529 | x1.emplace(18, 19); |
| 530 | x2.emplace(23, 50); |
| 531 | x2.emplace(12, 49); |
| 532 | insert_node_handle_unique2(x1, x2); |
| 533 | BOOST_TEST(x2.size() == 4); |
| 534 | } |
| 535 | |
| 536 | { |
| 537 | boost::unordered_node_map<int, int, hash_thing> x1; |
| 538 | boost::unordered_node_map<int, int> x2; |
| 539 | x1.emplace(67, 50); |
| 540 | x1.emplace(23, 45); |
| 541 | x1.emplace(18, 19); |
| 542 | x2.emplace(23, 50); |
| 543 | x2.emplace(12, 49); |
| 544 | insert_node_handle_unique2(x1, x2); |
| 545 | BOOST_TEST(x2.size() == 4); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | #else |
| 550 | UNORDERED_AUTO_TEST (examples) { |
| 551 | example1<boost::unordered_map>(); |
| 552 | example2<boost::unordered_set>(); |
| 553 | example3<boost::unordered_set>(); |
| 554 | failed_insertion_with_hint<boost::unordered_map, boost::unordered_set>(); |
| 555 | node_handle_tests<boost::unordered_map, boost::unordered_set>(); |
| 556 | } |
| 557 | |
| 558 | template <typename Container1, typename Container2> |
| 559 | void insert_node_handle_equiv(Container1& c1, Container2& c2) |
| 560 | { |
| 561 | typedef typename Container1::node_type node_type; |
| 562 | typedef typename Container1::value_type value_type; |
| 563 | BOOST_STATIC_ASSERT( |
| 564 | (boost::is_same<node_type, typename Container2::node_type>::value)); |
| 565 | |
| 566 | typedef typename Container1::iterator iterator1; |
| 567 | typedef typename Container2::iterator iterator2; |
| 568 | |
| 569 | iterator1 r1 = insert_empty_node(c1); |
| 570 | iterator2 r2 = c2.insert(node_type()); |
| 571 | BOOST_TEST(r1 == c1.end()); |
| 572 | BOOST_TEST(r2 == c2.end()); |
| 573 | |
| 574 | while (!c1.empty()) { |
| 575 | value_type v = *c1.begin(); |
| 576 | value_type const* v_ptr = boost::to_address(c1.begin()); |
| 577 | std::size_t count = c2.count(test::get_key<Container1>(v)); |
| 578 | iterator2 r = c2.insert(c1.extract(c1.begin())); |
| 579 | BOOST_TEST_EQ(c2.count(test::get_key<Container1>(v)), count + 1); |
| 580 | BOOST_TEST(r != c2.end()); |
| 581 | BOOST_TEST(boost::to_address(r) == v_ptr); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | UNORDERED_AUTO_TEST (insert_node_handle_unique_tests) { |
| 586 | { |
| 587 | boost::unordered_set<int> x1; |
| 588 | boost::unordered_set<int> x2; |
| 589 | x1.emplace(args: 100); |
| 590 | x1.emplace(args: 140); |
| 591 | x1.emplace(args: -55); |
| 592 | x2.emplace(args: 140); |
| 593 | insert_node_handle_unique(c1&: x1, c2&: x2); |
| 594 | BOOST_TEST(x2.size() == 3); |
| 595 | } |
| 596 | |
| 597 | { |
| 598 | boost::unordered_multiset<int> x1; |
| 599 | boost::unordered_set<int> x2; |
| 600 | x1.emplace(args: 100); |
| 601 | x1.emplace(args: 140); |
| 602 | x1.emplace(args: -55); |
| 603 | x2.emplace(args: 140); |
| 604 | insert_node_handle_unique(c1&: x1, c2&: x2); |
| 605 | BOOST_TEST(x2.size() == 3); |
| 606 | } |
| 607 | |
| 608 | { |
| 609 | boost::unordered_map<int, int, hash_thing> x1; |
| 610 | boost::unordered_map<int, int> x2; |
| 611 | x1.emplace(args: 67, args: 50); |
| 612 | x1.emplace(args: 23, args: 45); |
| 613 | x1.emplace(args: 18, args: 19); |
| 614 | x2.emplace(args: 23, args: 50); |
| 615 | x2.emplace(args: 12, args: 49); |
| 616 | insert_node_handle_unique(c1&: x1, c2&: x2); |
| 617 | BOOST_TEST(x2.size() == 4); |
| 618 | } |
| 619 | |
| 620 | { |
| 621 | boost::unordered_multimap<int, int, hash_thing> x1; |
| 622 | boost::unordered_map<int, int> x2; |
| 623 | x1.emplace(args: 67, args: 50); |
| 624 | x1.emplace(args: 23, args: 45); |
| 625 | x1.emplace(args: 18, args: 19); |
| 626 | x2.emplace(args: 23, args: 50); |
| 627 | x2.emplace(args: 12, args: 49); |
| 628 | insert_node_handle_unique(c1&: x1, c2&: x2); |
| 629 | BOOST_TEST(x2.size() == 4); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | UNORDERED_AUTO_TEST (insert_node_handle_equiv_tests) { |
| 634 | { |
| 635 | boost::unordered_multimap<int, int, hash_thing> x1; |
| 636 | boost::unordered_multimap<int, int> x2; |
| 637 | x1.emplace(args: 67, args: 50); |
| 638 | x1.emplace(args: 67, args: 100); |
| 639 | x1.emplace(args: 23, args: 45); |
| 640 | x1.emplace(args: 18, args: 19); |
| 641 | x2.emplace(args: 23, args: 50); |
| 642 | x2.emplace(args: 12, args: 49); |
| 643 | insert_node_handle_equiv(c1&: x1, c2&: x2); |
| 644 | BOOST_TEST(x2.size() == 6); |
| 645 | } |
| 646 | |
| 647 | { |
| 648 | boost::unordered_map<int, int, hash_thing> x1; |
| 649 | boost::unordered_multimap<int, int> x2; |
| 650 | x1.emplace(args: 67, args: 50); |
| 651 | x1.emplace(args: 67, args: 100); |
| 652 | x1.emplace(args: 23, args: 45); |
| 653 | x1.emplace(args: 18, args: 19); |
| 654 | x2.emplace(args: 23, args: 50); |
| 655 | x2.emplace(args: 12, args: 49); |
| 656 | insert_node_handle_equiv(c1&: x1, c2&: x2); |
| 657 | BOOST_TEST(x2.size() == 5); |
| 658 | } |
| 659 | |
| 660 | { |
| 661 | boost::unordered_multiset<int, hash_thing> x1; |
| 662 | boost::unordered_multiset<int> x2; |
| 663 | x1.emplace(args: 67); |
| 664 | x1.emplace(args: 67); |
| 665 | x1.emplace(args: 23); |
| 666 | x1.emplace(args: 18); |
| 667 | x2.emplace(args: 23); |
| 668 | x2.emplace(args: 12); |
| 669 | insert_node_handle_equiv(c1&: x1, c2&: x2); |
| 670 | BOOST_TEST(x2.size() == 6); |
| 671 | } |
| 672 | |
| 673 | { |
| 674 | boost::unordered_set<int, hash_thing> x1; |
| 675 | boost::unordered_multiset<int> x2; |
| 676 | x1.emplace(args: 67); |
| 677 | x1.emplace(args: 67); |
| 678 | x1.emplace(args: 23); |
| 679 | x1.emplace(args: 18); |
| 680 | x2.emplace(args: 23); |
| 681 | x2.emplace(args: 12); |
| 682 | insert_node_handle_equiv(c1&: x1, c2&: x2); |
| 683 | BOOST_TEST(x2.size() == 5); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | UNORDERED_AUTO_TEST (insert_node_handle_unique_tests2) { |
| 688 | { |
| 689 | boost::unordered_set<int> x1; |
| 690 | boost::unordered_set<int> x2; |
| 691 | x1.emplace(args: 100); |
| 692 | x1.emplace(args: 140); |
| 693 | x1.emplace(args: -55); |
| 694 | x2.emplace(args: 140); |
| 695 | insert_node_handle_unique2(c1&: x1, c2&: x2); |
| 696 | BOOST_TEST(x2.size() == 3); |
| 697 | } |
| 698 | |
| 699 | { |
| 700 | boost::unordered_multiset<int> x1; |
| 701 | boost::unordered_set<int> x2; |
| 702 | x1.emplace(args: 100); |
| 703 | x1.emplace(args: 140); |
| 704 | x1.emplace(args: -55); |
| 705 | x2.emplace(args: 140); |
| 706 | insert_node_handle_unique2(c1&: x1, c2&: x2); |
| 707 | BOOST_TEST(x2.size() == 3); |
| 708 | } |
| 709 | |
| 710 | { |
| 711 | boost::unordered_map<int, int, hash_thing> x1; |
| 712 | boost::unordered_map<int, int> x2; |
| 713 | x1.emplace(args: 67, args: 50); |
| 714 | x1.emplace(args: 23, args: 45); |
| 715 | x1.emplace(args: 18, args: 19); |
| 716 | x2.emplace(args: 23, args: 50); |
| 717 | x2.emplace(args: 12, args: 49); |
| 718 | insert_node_handle_unique2(c1&: x1, c2&: x2); |
| 719 | BOOST_TEST(x2.size() == 4); |
| 720 | } |
| 721 | |
| 722 | { |
| 723 | boost::unordered_multimap<int, int, hash_thing> x1; |
| 724 | boost::unordered_map<int, int> x2; |
| 725 | x1.emplace(args: 67, args: 50); |
| 726 | x1.emplace(args: 23, args: 45); |
| 727 | x1.emplace(args: 18, args: 19); |
| 728 | x2.emplace(args: 23, args: 50); |
| 729 | x2.emplace(args: 12, args: 49); |
| 730 | insert_node_handle_unique2(c1&: x1, c2&: x2); |
| 731 | BOOST_TEST(x2.size() == 4); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | #endif |
| 736 | |
| 737 | RUN_TESTS() |
| 738 | |