2016-08-17 12:08:15 +01:00
|
|
|
|
|
|
|
|
// Copyright 2016 Daniel James.
|
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
// clang-format off
|
2016-08-17 12:08:15 +01:00
|
|
|
#include "../helpers/prefix.hpp"
|
|
|
|
|
#include <boost/unordered_set.hpp>
|
|
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
|
#include "../helpers/postfix.hpp"
|
2017-02-19 13:05:17 +00:00
|
|
|
// clang-format on
|
2016-08-17 12:08:15 +01:00
|
|
|
|
|
|
|
|
#include "../helpers/test.hpp"
|
|
|
|
|
#include "../helpers/invariants.hpp"
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
namespace insert_hint {
|
|
|
|
|
UNORDERED_AUTO_TEST(insert_hint_empty)
|
2016-08-17 12:08:15 +01:00
|
|
|
{
|
|
|
|
|
typedef boost::unordered_multiset<int> container;
|
|
|
|
|
container x;
|
|
|
|
|
x.insert(x.cbegin(), 10);
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), 1u);
|
|
|
|
|
BOOST_TEST_EQ(x.count(10), 1u);
|
2016-08-17 12:08:15 +01:00
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
UNORDERED_AUTO_TEST(insert_hint_empty2)
|
|
|
|
|
{
|
2016-08-17 12:08:15 +01:00
|
|
|
typedef boost::unordered_multimap<std::string, int> container;
|
|
|
|
|
container x;
|
|
|
|
|
x.emplace_hint(x.cbegin(), "hello", 50);
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), 1u);
|
|
|
|
|
BOOST_TEST_EQ(x.count("hello"), 1u);
|
2016-08-17 12:08:15 +01:00
|
|
|
BOOST_TEST_EQ(x.find("hello")->second, 50);
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
UNORDERED_AUTO_TEST(insert_hint_single)
|
|
|
|
|
{
|
2016-08-17 12:08:15 +01:00
|
|
|
typedef boost::unordered_multiset<std::string> container;
|
|
|
|
|
container x;
|
|
|
|
|
x.insert("equal");
|
|
|
|
|
x.insert(x.cbegin(), "equal");
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), 2u);
|
|
|
|
|
BOOST_TEST_EQ(x.count("equal"), 2u);
|
2016-08-17 12:08:15 +01:00
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
UNORDERED_AUTO_TEST(insert_hint_single2)
|
|
|
|
|
{
|
2016-08-17 12:08:15 +01:00
|
|
|
typedef boost::unordered_multimap<int, std::string> container;
|
|
|
|
|
container x;
|
|
|
|
|
x.emplace(10, "one");
|
|
|
|
|
x.emplace_hint(x.cbegin(), 10, "two");
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), 2u);
|
|
|
|
|
BOOST_TEST_EQ(x.count(10), 2u);
|
2016-08-17 12:08:15 +01:00
|
|
|
|
|
|
|
|
container::iterator it = x.find(10);
|
|
|
|
|
std::string v0 = (it++)->second;
|
|
|
|
|
std::string v1 = (it++)->second;
|
|
|
|
|
|
|
|
|
|
BOOST_TEST(v0 == "one" || v0 == "two");
|
|
|
|
|
BOOST_TEST(v1 == "one" || v1 == "two");
|
|
|
|
|
BOOST_TEST(v0 != v1);
|
|
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
UNORDERED_AUTO_TEST(insert_hint_multiple)
|
|
|
|
|
{
|
2016-08-17 12:08:15 +01:00
|
|
|
for (unsigned int size = 0; size < 10; ++size) {
|
2017-02-19 13:05:17 +00:00
|
|
|
for (unsigned int offset = 0; offset <= size; ++offset) {
|
2016-08-17 12:08:15 +01:00
|
|
|
typedef boost::unordered_multiset<std::string> container;
|
|
|
|
|
container x;
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
for (unsigned int i = 0; i < size; ++i) {
|
|
|
|
|
x.insert("multiple");
|
|
|
|
|
}
|
2016-08-17 12:08:15 +01:00
|
|
|
|
|
|
|
|
BOOST_TEST_EQ(x.size(), size);
|
|
|
|
|
|
|
|
|
|
container::const_iterator position = x.cbegin();
|
2017-02-19 13:05:17 +00:00
|
|
|
for (unsigned int i = 0; i < offset; ++i) {
|
|
|
|
|
++position;
|
|
|
|
|
}
|
2016-08-17 12:08:15 +01:00
|
|
|
|
|
|
|
|
x.insert(position, "multiple");
|
|
|
|
|
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), size + 1u);
|
|
|
|
|
BOOST_TEST_EQ(x.count("multiple"), size + 1u);
|
2016-08-17 12:08:15 +01:00
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
UNORDERED_AUTO_TEST(insert_hint_unique)
|
|
|
|
|
{
|
2016-08-17 12:08:15 +01:00
|
|
|
typedef boost::unordered_set<int> container;
|
|
|
|
|
container x;
|
|
|
|
|
x.insert(x.cbegin(), 10);
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), 1u);
|
|
|
|
|
BOOST_TEST_EQ(x.count(10), 1u);
|
2016-08-17 12:08:15 +01:00
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 13:05:17 +00:00
|
|
|
UNORDERED_AUTO_TEST(insert_hint_unique_single)
|
|
|
|
|
{
|
2016-08-17 12:08:15 +01:00
|
|
|
typedef boost::unordered_set<int> container;
|
|
|
|
|
container x;
|
|
|
|
|
x.insert(10);
|
|
|
|
|
|
|
|
|
|
x.insert(x.cbegin(), 10);
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), 1u);
|
|
|
|
|
BOOST_TEST_EQ(x.count(10), 1u);
|
2016-08-17 12:08:15 +01:00
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
|
|
|
|
|
x.insert(x.cbegin(), 20);
|
2016-08-18 12:33:31 +01:00
|
|
|
BOOST_TEST_EQ(x.size(), 2u);
|
|
|
|
|
BOOST_TEST_EQ(x.count(10), 1u);
|
|
|
|
|
BOOST_TEST_EQ(x.count(20), 1u);
|
2016-08-17 12:08:15 +01:00
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RUN_TESTS()
|