| 1 | |
| 2 | // Copyright 2007-2009 Daniel James. |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | // clang-format off |
| 7 | #include "../helpers/prefix.hpp" |
| 8 | #include <boost/unordered_set.hpp> |
| 9 | #include <boost/unordered_map.hpp> |
| 10 | #include "../helpers/postfix.hpp" |
| 11 | // clang-format on |
| 12 | |
| 13 | #include "../helpers/test.hpp" |
| 14 | |
| 15 | namespace insert_stable { |
| 16 | struct member |
| 17 | { |
| 18 | int tag1_; |
| 19 | int tag2_; |
| 20 | |
| 21 | member() : tag1_(0), tag2_(0) {} |
| 22 | member(int t1, int t2) : tag1_(t1), tag2_(t2) {} |
| 23 | |
| 24 | friend bool operator==(member const& x, member const& y) |
| 25 | { |
| 26 | return x.tag1_ == y.tag1_; |
| 27 | } |
| 28 | |
| 29 | friend bool operator!=(member const& x, member const& y) |
| 30 | { |
| 31 | return x.tag1_ != y.tag1_; |
| 32 | } |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP |
| 37 | namespace boost |
| 38 | #else |
| 39 | namespace insert_stable |
| 40 | #endif |
| 41 | { |
| 42 | std::size_t hash_value(insert_stable::member const& x) |
| 43 | { |
| 44 | return static_cast<std::size_t>(x.tag1_); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // This is no longer supported, as there's no longer an efficient way to get to |
| 49 | // the end of a group of equivalent nodes. |
| 50 | #if 0 |
| 51 | |
| 52 | UNORDERED_AUTO_TEST(stable_insert_test1) |
| 53 | { |
| 54 | boost::unordered_multiset<insert_stable::member> x; |
| 55 | |
| 56 | x.insert(insert_stable::member(1, 1)); |
| 57 | x.insert(insert_stable::member(1, 2)); |
| 58 | x.insert(insert_stable::member(1, 3)); |
| 59 | |
| 60 | BOOST_TEST(x.count(insert_stable::member(1, 4)) == 3); |
| 61 | |
| 62 | boost::unordered_multiset<insert_stable::member>::const_iterator |
| 63 | it = x.begin(), |
| 64 | end = x.end(); |
| 65 | BOOST_TEST(it != end); |
| 66 | if (it != end) { |
| 67 | BOOST_TEST(it->tag2_ == 1); |
| 68 | ++it; |
| 69 | } |
| 70 | BOOST_TEST(it != end); |
| 71 | if (it != end) { |
| 72 | BOOST_TEST(it->tag2_ == 2); |
| 73 | ++it; |
| 74 | } |
| 75 | BOOST_TEST(it != end); |
| 76 | if (it != end) { |
| 77 | BOOST_TEST(it->tag2_ == 3); |
| 78 | ++it; |
| 79 | } |
| 80 | BOOST_TEST(it == end); |
| 81 | } |
| 82 | |
| 83 | UNORDERED_AUTO_TEST(stable_insert_test2) |
| 84 | { |
| 85 | boost::unordered_multimap<insert_stable::member, int> x; |
| 86 | typedef boost::unordered_multimap<insert_stable::member, |
| 87 | int>::const_iterator iterator; |
| 88 | |
| 89 | iterator it = x.emplace(insert_stable::member(1, 1), 1); |
| 90 | it = x.emplace(insert_stable::member(1, 2), 2); |
| 91 | it = x.emplace(insert_stable::member(1, 3), 3); |
| 92 | |
| 93 | BOOST_TEST(x.count(insert_stable::member(1, 4)) == 3); |
| 94 | |
| 95 | it = x.begin(); |
| 96 | iterator end = x.end(); |
| 97 | BOOST_TEST(it != end); |
| 98 | if (it != end) { |
| 99 | BOOST_TEST(it->first.tag2_ == 1 && it->second == 1); |
| 100 | ++it; |
| 101 | } |
| 102 | BOOST_TEST(it != end); |
| 103 | if (it != end) { |
| 104 | BOOST_TEST(it->first.tag2_ == 2 && it->second == 2); |
| 105 | ++it; |
| 106 | } |
| 107 | BOOST_TEST(it != end); |
| 108 | if (it != end) { |
| 109 | BOOST_TEST(it->first.tag2_ == 3 && it->second == 3); |
| 110 | ++it; |
| 111 | } |
| 112 | BOOST_TEST(it == end); |
| 113 | } |
| 114 | |
| 115 | #endif |
| 116 | |
| 117 | RUN_TESTS() |
| 118 | |