| 1 | |
| 2 | // Copyright 2016 Daniel James. |
| 3 | // Copyright 2022-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 "../helpers/unordered.hpp" |
| 8 | |
| 9 | #include "../helpers/test.hpp" |
| 10 | #include "../helpers/invariants.hpp" |
| 11 | |
| 12 | #include <map> |
| 13 | #include <set> |
| 14 | |
| 15 | namespace insert_hint { |
| 16 | #ifndef BOOST_UNORDERED_FOA_TESTS |
| 17 | UNORDERED_AUTO_TEST (insert_hint_empty) { |
| 18 | typedef boost::unordered_multiset<int> container; |
| 19 | container x; |
| 20 | x.insert(hint: x.cbegin(), x: 10); |
| 21 | BOOST_TEST_EQ(x.size(), 1u); |
| 22 | BOOST_TEST_EQ(x.count(10), 1u); |
| 23 | test::check_equivalent_keys(x1: x); |
| 24 | } |
| 25 | |
| 26 | UNORDERED_AUTO_TEST (insert_hint_empty2) { |
| 27 | typedef boost::unordered_multimap<std::string, int> container; |
| 28 | container x; |
| 29 | x.emplace_hint(hint: x.cbegin(), args: "hello" , args: 50); |
| 30 | BOOST_TEST_EQ(x.size(), 1u); |
| 31 | BOOST_TEST_EQ(x.count("hello" ), 1u); |
| 32 | BOOST_TEST_EQ(x.find("hello" )->second, 50); |
| 33 | test::check_equivalent_keys(x1: x); |
| 34 | } |
| 35 | |
| 36 | UNORDERED_AUTO_TEST (insert_hint_single) { |
| 37 | typedef boost::unordered_multiset<std::string> container; |
| 38 | container x; |
| 39 | x.insert(x: "equal" ); |
| 40 | x.insert(hint: x.cbegin(), x: "equal" ); |
| 41 | BOOST_TEST_EQ(x.size(), 2u); |
| 42 | BOOST_TEST_EQ(x.count("equal" ), 2u); |
| 43 | test::check_equivalent_keys(x1: x); |
| 44 | } |
| 45 | |
| 46 | UNORDERED_AUTO_TEST (insert_hint_single2) { |
| 47 | typedef boost::unordered_multimap<int, std::string> container; |
| 48 | container x; |
| 49 | x.emplace(args: 10, args: "one" ); |
| 50 | x.emplace_hint(hint: x.cbegin(), args: 10, args: "two" ); |
| 51 | BOOST_TEST_EQ(x.size(), 2u); |
| 52 | BOOST_TEST_EQ(x.count(10), 2u); |
| 53 | |
| 54 | container::iterator it = x.find(k: 10); |
| 55 | std::string v0 = (it++)->second; |
| 56 | std::string v1 = (it++)->second; |
| 57 | |
| 58 | BOOST_TEST(v0 == "one" || v0 == "two" ); |
| 59 | BOOST_TEST(v1 == "one" || v1 == "two" ); |
| 60 | BOOST_TEST(v0 != v1); |
| 61 | |
| 62 | test::check_equivalent_keys(x1: x); |
| 63 | } |
| 64 | |
| 65 | UNORDERED_AUTO_TEST (insert_hint_multiple) { |
| 66 | for (unsigned int size = 0; size < 10; ++size) { |
| 67 | for (unsigned int offset = 0; offset <= size; ++offset) { |
| 68 | typedef boost::unordered_multiset<std::string> container; |
| 69 | container x; |
| 70 | |
| 71 | for (unsigned int i = 0; i < size; ++i) { |
| 72 | x.insert(x: "multiple" ); |
| 73 | } |
| 74 | |
| 75 | BOOST_TEST_EQ(x.size(), size); |
| 76 | |
| 77 | container::const_iterator position = x.cbegin(); |
| 78 | for (unsigned int i = 0; i < offset; ++i) { |
| 79 | ++position; |
| 80 | } |
| 81 | |
| 82 | x.insert(hint: position, x: "multiple" ); |
| 83 | |
| 84 | BOOST_TEST_EQ(x.size(), size + 1u); |
| 85 | BOOST_TEST_EQ(x.count("multiple" ), size + 1u); |
| 86 | test::check_equivalent_keys(x1: x); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | template <class X> static void insert_hint_unique(X*) |
| 93 | { |
| 94 | typedef X container; |
| 95 | |
| 96 | container x; |
| 97 | x.insert(x.cbegin(), 10); |
| 98 | BOOST_TEST_EQ(x.size(), 1u); |
| 99 | BOOST_TEST_EQ(x.count(10), 1u); |
| 100 | test::check_equivalent_keys(x); |
| 101 | } |
| 102 | |
| 103 | template <class X> static void insert_hint_unique_single(X*) |
| 104 | { |
| 105 | typedef X container; |
| 106 | |
| 107 | container x; |
| 108 | x.insert(10); |
| 109 | |
| 110 | x.insert(x.cbegin(), 10); |
| 111 | BOOST_TEST_EQ(x.size(), 1u); |
| 112 | BOOST_TEST_EQ(x.count(10), 1u); |
| 113 | test::check_equivalent_keys(x); |
| 114 | |
| 115 | x.insert(x.cbegin(), 20); |
| 116 | BOOST_TEST_EQ(x.size(), 2u); |
| 117 | BOOST_TEST_EQ(x.count(10), 1u); |
| 118 | BOOST_TEST_EQ(x.count(20), 1u); |
| 119 | test::check_equivalent_keys(x); |
| 120 | } |
| 121 | |
| 122 | #ifdef BOOST_UNORDERED_FOA_TESTS |
| 123 | static boost::unordered_flat_set<int>* test_set; |
| 124 | static boost::unordered_node_set<int>* test_node_set; |
| 125 | |
| 126 | UNORDERED_TEST(insert_hint_unique, ((test_set)(test_node_set))) |
| 127 | UNORDERED_TEST(insert_hint_unique_single, ((test_set)(test_node_set))) |
| 128 | #else |
| 129 | static boost::unordered_set<int>* test_set; |
| 130 | UNORDERED_TEST(insert_hint_unique, ((test_set))) |
| 131 | UNORDERED_TEST(insert_hint_unique_single, ((test_set))) |
| 132 | #endif |
| 133 | } // namespace insert_hint |
| 134 | |
| 135 | RUN_TESTS() |
| 136 | |