1 2 // Copyright 2016 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 #include "../helpers/invariants.hpp" 15 16 #include <map> 17 #include <set> 18 19 namespace insert_hint { UNORDERED_AUTO_TEST(insert_hint_empty)20 UNORDERED_AUTO_TEST (insert_hint_empty) { 21 typedef boost::unordered_multiset<int> container; 22 container x; 23 x.insert(x.cbegin(), 10); 24 BOOST_TEST_EQ(x.size(), 1u); 25 BOOST_TEST_EQ(x.count(10), 1u); 26 test::check_equivalent_keys(x); 27 } 28 UNORDERED_AUTO_TEST(insert_hint_empty2)29 UNORDERED_AUTO_TEST (insert_hint_empty2) { 30 typedef boost::unordered_multimap<std::string, int> container; 31 container x; 32 x.emplace_hint(x.cbegin(), "hello", 50); 33 BOOST_TEST_EQ(x.size(), 1u); 34 BOOST_TEST_EQ(x.count("hello"), 1u); 35 BOOST_TEST_EQ(x.find("hello")->second, 50); 36 test::check_equivalent_keys(x); 37 } 38 UNORDERED_AUTO_TEST(insert_hint_single)39 UNORDERED_AUTO_TEST (insert_hint_single) { 40 typedef boost::unordered_multiset<std::string> container; 41 container x; 42 x.insert("equal"); 43 x.insert(x.cbegin(), "equal"); 44 BOOST_TEST_EQ(x.size(), 2u); 45 BOOST_TEST_EQ(x.count("equal"), 2u); 46 test::check_equivalent_keys(x); 47 } 48 UNORDERED_AUTO_TEST(insert_hint_single2)49 UNORDERED_AUTO_TEST (insert_hint_single2) { 50 typedef boost::unordered_multimap<int, std::string> container; 51 container x; 52 x.emplace(10, "one"); 53 x.emplace_hint(x.cbegin(), 10, "two"); 54 BOOST_TEST_EQ(x.size(), 2u); 55 BOOST_TEST_EQ(x.count(10), 2u); 56 57 container::iterator it = x.find(10); 58 std::string v0 = (it++)->second; 59 std::string v1 = (it++)->second; 60 61 BOOST_TEST(v0 == "one" || v0 == "two"); 62 BOOST_TEST(v1 == "one" || v1 == "two"); 63 BOOST_TEST(v0 != v1); 64 65 test::check_equivalent_keys(x); 66 } 67 UNORDERED_AUTO_TEST(insert_hint_multiple)68 UNORDERED_AUTO_TEST (insert_hint_multiple) { 69 for (unsigned int size = 0; size < 10; ++size) { 70 for (unsigned int offset = 0; offset <= size; ++offset) { 71 typedef boost::unordered_multiset<std::string> container; 72 container x; 73 74 for (unsigned int i = 0; i < size; ++i) { 75 x.insert("multiple"); 76 } 77 78 BOOST_TEST_EQ(x.size(), size); 79 80 container::const_iterator position = x.cbegin(); 81 for (unsigned int i = 0; i < offset; ++i) { 82 ++position; 83 } 84 85 x.insert(position, "multiple"); 86 87 BOOST_TEST_EQ(x.size(), size + 1u); 88 BOOST_TEST_EQ(x.count("multiple"), size + 1u); 89 test::check_equivalent_keys(x); 90 } 91 } 92 } 93 UNORDERED_AUTO_TEST(insert_hint_unique)94 UNORDERED_AUTO_TEST (insert_hint_unique) { 95 typedef boost::unordered_set<int> container; 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 UNORDERED_AUTO_TEST(insert_hint_unique_single)103 UNORDERED_AUTO_TEST (insert_hint_unique_single) { 104 typedef boost::unordered_set<int> container; 105 container x; 106 x.insert(10); 107 108 x.insert(x.cbegin(), 10); 109 BOOST_TEST_EQ(x.size(), 1u); 110 BOOST_TEST_EQ(x.count(10), 1u); 111 test::check_equivalent_keys(x); 112 113 x.insert(x.cbegin(), 20); 114 BOOST_TEST_EQ(x.size(), 2u); 115 BOOST_TEST_EQ(x.count(10), 1u); 116 BOOST_TEST_EQ(x.count(20), 1u); 117 test::check_equivalent_keys(x); 118 } 119 } 120 121 RUN_TESTS() 122