2006-05-17 17:19:16 +00:00
|
|
|
|
2009-03-09 20:56:23 +00:00
|
|
|
// Copyright 2006-2009 Daniel James.
|
2006-07-01 22:31:26 +00:00
|
|
|
// 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)
|
2006-05-17 17:19:16 +00:00
|
|
|
|
2009-11-26 23:15:30 +00:00
|
|
|
#include "../helpers/prefix.hpp"
|
2006-05-17 17:19:16 +00:00
|
|
|
#include <boost/unordered_set.hpp>
|
|
|
|
|
#include <boost/unordered_map.hpp>
|
2012-04-08 15:29:15 +00:00
|
|
|
#include "../helpers/postfix.hpp"
|
|
|
|
|
|
2008-03-24 17:03:15 +00:00
|
|
|
#include "../helpers/test.hpp"
|
2006-05-17 17:19:16 +00:00
|
|
|
#include <boost/next_prior.hpp>
|
|
|
|
|
#include "../objects/test.hpp"
|
|
|
|
|
#include "../helpers/random_values.hpp"
|
|
|
|
|
#include "../helpers/tracker.hpp"
|
|
|
|
|
#include "../helpers/equivalent.hpp"
|
|
|
|
|
#include "../helpers/helpers.hpp"
|
2012-10-07 08:19:01 +00:00
|
|
|
#include "../helpers/invariants.hpp"
|
2016-09-18 12:22:48 +01:00
|
|
|
#include <vector>
|
2006-05-17 17:19:16 +00:00
|
|
|
#include <iostream>
|
2016-10-04 15:51:07 +01:00
|
|
|
#include <cstdlib>
|
2006-05-17 17:19:16 +00:00
|
|
|
|
2008-03-24 17:03:15 +00:00
|
|
|
namespace erase_tests
|
|
|
|
|
{
|
|
|
|
|
|
2012-05-07 10:57:35 +00:00
|
|
|
test::seed_t initialize_seed(85638);
|
2007-12-06 11:42:28 +00:00
|
|
|
|
2016-10-05 09:45:53 +01:00
|
|
|
std::size_t random_value(std::size_t max) {
|
2016-10-04 15:51:07 +01:00
|
|
|
using namespace std;
|
2016-10-05 09:45:53 +01:00
|
|
|
return static_cast<std::size_t>(rand()) % max;
|
2016-10-04 15:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
template <class Container>
|
2012-09-05 23:33:22 +00:00
|
|
|
void erase_tests1(Container*, test::random_generator generator)
|
2006-05-17 17:19:16 +00:00
|
|
|
{
|
2016-09-18 12:22:48 +01:00
|
|
|
typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator;
|
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator c_iterator;
|
2016-10-05 09:45:53 +01:00
|
|
|
typedef BOOST_DEDUCED_TYPENAME Container::difference_type difference_type;
|
2016-09-18 12:22:48 +01:00
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
std::cerr<<"Erase by key.\n";
|
|
|
|
|
{
|
2011-08-14 18:52:43 +00:00
|
|
|
test::check_instances check_;
|
|
|
|
|
|
2008-01-20 18:55:57 +00:00
|
|
|
test::random_values<Container> v(1000, generator);
|
2006-05-17 17:19:16 +00:00
|
|
|
Container x(v.begin(), v.end());
|
2012-10-07 08:19:01 +00:00
|
|
|
int iterations = 0;
|
2010-01-04 22:49:39 +00:00
|
|
|
for(BOOST_DEDUCED_TYPENAME test::random_values<Container>::iterator
|
|
|
|
|
it = v.begin(); it != v.end(); ++it)
|
2006-05-17 17:19:16 +00:00
|
|
|
{
|
|
|
|
|
std::size_t count = x.count(test::get_key<Container>(*it));
|
|
|
|
|
std::size_t old_size = x.size();
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(count == x.erase(test::get_key<Container>(*it)));
|
|
|
|
|
BOOST_TEST(x.size() == old_size - count);
|
|
|
|
|
BOOST_TEST(x.count(test::get_key<Container>(*it)) == 0);
|
|
|
|
|
BOOST_TEST(x.find(test::get_key<Container>(*it)) == x.end());
|
2012-10-07 08:19:01 +00:00
|
|
|
if (++iterations % 20 == 0) test::check_equivalent_keys(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cerr<<"erase(begin()).\n";
|
|
|
|
|
{
|
2011-08-14 18:52:43 +00:00
|
|
|
test::check_instances check_;
|
|
|
|
|
|
2008-01-20 18:55:57 +00:00
|
|
|
test::random_values<Container> v(1000, generator);
|
2006-05-17 17:19:16 +00:00
|
|
|
Container x(v.begin(), v.end());
|
|
|
|
|
std::size_t size = x.size();
|
2012-10-07 08:19:01 +00:00
|
|
|
int iterations = 0;
|
2006-05-17 17:19:16 +00:00
|
|
|
while(size > 0 && !x.empty())
|
|
|
|
|
{
|
2010-01-04 22:49:39 +00:00
|
|
|
BOOST_DEDUCED_TYPENAME Container::key_type
|
|
|
|
|
key = test::get_key<Container>(*x.begin());
|
2006-05-17 17:19:16 +00:00
|
|
|
std::size_t count = x.count(key);
|
2016-09-18 12:22:48 +01:00
|
|
|
iterator pos = x.erase(x.begin());
|
2006-05-17 17:19:16 +00:00
|
|
|
--size;
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(pos == x.begin());
|
|
|
|
|
BOOST_TEST(x.count(key) == count - 1);
|
|
|
|
|
BOOST_TEST(x.size() == size);
|
2012-10-07 08:19:01 +00:00
|
|
|
if (++iterations % 20 == 0) test::check_equivalent_keys(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.empty());
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cerr<<"erase(random position).\n";
|
|
|
|
|
{
|
2011-08-14 18:52:43 +00:00
|
|
|
test::check_instances check_;
|
|
|
|
|
|
2008-01-20 18:55:57 +00:00
|
|
|
test::random_values<Container> v(1000, generator);
|
2006-05-17 17:19:16 +00:00
|
|
|
Container x(v.begin(), v.end());
|
|
|
|
|
std::size_t size = x.size();
|
2012-10-07 08:19:01 +00:00
|
|
|
int iterations = 0;
|
2006-05-17 17:19:16 +00:00
|
|
|
while(size > 0 && !x.empty())
|
|
|
|
|
{
|
2016-10-05 09:45:53 +01:00
|
|
|
std::size_t index = random_value(x.size());
|
2016-09-18 12:22:48 +01:00
|
|
|
c_iterator prev, pos, next;
|
2006-05-17 17:19:16 +00:00
|
|
|
if(index == 0) {
|
|
|
|
|
prev = pos = x.begin();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
prev = boost::next(x.begin(), index - 1);
|
|
|
|
|
pos = boost::next(prev);
|
|
|
|
|
}
|
|
|
|
|
next = boost::next(pos);
|
2010-01-04 22:49:39 +00:00
|
|
|
BOOST_DEDUCED_TYPENAME Container::key_type
|
|
|
|
|
key = test::get_key<Container>(*pos);
|
2006-05-17 17:19:16 +00:00
|
|
|
std::size_t count = x.count(key);
|
2016-07-25 14:18:39 +01:00
|
|
|
BOOST_TEST(count > 0);
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(next == x.erase(pos));
|
2006-05-17 17:19:16 +00:00
|
|
|
--size;
|
|
|
|
|
if(size > 0)
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(index == 0 ? next == x.begin() :
|
2008-01-13 16:19:26 +00:00
|
|
|
next == boost::next(prev));
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.count(key) == count - 1);
|
2016-07-25 14:18:39 +01:00
|
|
|
if (x.count(key) != count - 1) {
|
|
|
|
|
std::cerr << count << " => " << x.count(key) << std::endl;
|
|
|
|
|
}
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.size() == size);
|
2012-10-07 08:19:01 +00:00
|
|
|
if (++iterations % 20 == 0) test::check_equivalent_keys(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.empty());
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cerr<<"erase(ranges).\n";
|
|
|
|
|
{
|
2011-08-14 18:52:43 +00:00
|
|
|
test::check_instances check_;
|
|
|
|
|
|
2008-01-20 18:55:57 +00:00
|
|
|
test::random_values<Container> v(500, generator);
|
2006-05-17 17:19:16 +00:00
|
|
|
Container x(v.begin(), v.end());
|
|
|
|
|
|
|
|
|
|
std::size_t size = x.size();
|
|
|
|
|
|
|
|
|
|
// I'm actually stretching it a little here, as the standard says it
|
|
|
|
|
// returns 'the iterator immediately following the erase elements'
|
|
|
|
|
// and if nothing is erased, then there's nothing to follow. But I
|
|
|
|
|
// think this is the only sensible option...
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.erase(x.end(), x.end()) == x.end());
|
|
|
|
|
BOOST_TEST(x.erase(x.begin(), x.begin()) == x.begin());
|
|
|
|
|
BOOST_TEST(x.size() == size);
|
2012-10-07 08:19:01 +00:00
|
|
|
test::check_equivalent_keys(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.erase(x.begin(), x.end()) == x.end());
|
|
|
|
|
BOOST_TEST(x.empty());
|
|
|
|
|
BOOST_TEST(x.begin() == x.end());
|
2012-10-07 08:19:01 +00:00
|
|
|
test::check_equivalent_keys(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.erase(x.begin(), x.end()) == x.begin());
|
2012-10-07 08:19:01 +00:00
|
|
|
test::check_equivalent_keys(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-18 12:22:48 +01:00
|
|
|
std::cerr<<"erase(random ranges).\n";
|
|
|
|
|
{
|
|
|
|
|
test::check_instances check_;
|
|
|
|
|
Container x;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
|
test::random_values<Container> v(1000, generator);
|
|
|
|
|
x.insert(v.begin(), v.end());
|
|
|
|
|
|
|
|
|
|
// Note that erase only invalidates the erased iterators.
|
|
|
|
|
std::vector<c_iterator> iterators;
|
|
|
|
|
for(c_iterator it = x.cbegin(); it != x.cend(); ++it) {
|
|
|
|
|
iterators.push_back(it);
|
|
|
|
|
}
|
|
|
|
|
iterators.push_back(x.cend());
|
|
|
|
|
|
|
|
|
|
while(iterators.size() > 1) {
|
2016-10-05 09:45:53 +01:00
|
|
|
std::size_t start = random_value(iterators.size());
|
|
|
|
|
std::size_t length = random_value(iterators.size() - start);
|
2016-09-18 12:22:48 +01:00
|
|
|
x.erase(iterators[start], iterators[start + length]);
|
|
|
|
|
iterators.erase(
|
2016-10-05 09:45:53 +01:00
|
|
|
boost::next(iterators.begin(),
|
|
|
|
|
static_cast<difference_type>(start)),
|
|
|
|
|
boost::next(iterators.begin(),
|
|
|
|
|
static_cast<difference_type>(start + length)));
|
2016-09-18 12:22:48 +01:00
|
|
|
|
|
|
|
|
BOOST_TEST(x.size() == iterators.size() - 1);
|
|
|
|
|
BOOST_DEDUCED_TYPENAME std::vector<c_iterator>::const_iterator
|
|
|
|
|
i2 = iterators.begin();
|
|
|
|
|
for(c_iterator i1 = x.cbegin(); i1 != x.cend(); ++i1) {
|
|
|
|
|
BOOST_TEST(i1 == *i2);
|
|
|
|
|
++i2;
|
|
|
|
|
}
|
|
|
|
|
BOOST_TEST(x.cend() == *i2);
|
|
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
|
}
|
|
|
|
|
BOOST_TEST(x.empty());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-22 00:42:07 +00:00
|
|
|
std::cerr<<"quick_erase(begin()).\n";
|
2009-12-15 22:52:52 +00:00
|
|
|
{
|
2011-08-14 18:52:43 +00:00
|
|
|
test::check_instances check_;
|
|
|
|
|
|
2009-12-15 22:52:52 +00:00
|
|
|
test::random_values<Container> v(1000, generator);
|
|
|
|
|
Container x(v.begin(), v.end());
|
|
|
|
|
std::size_t size = x.size();
|
2012-10-07 08:19:01 +00:00
|
|
|
int iterations = 0;
|
2009-12-15 22:52:52 +00:00
|
|
|
while(size > 0 && !x.empty())
|
|
|
|
|
{
|
2010-01-04 22:49:39 +00:00
|
|
|
BOOST_DEDUCED_TYPENAME Container::key_type
|
|
|
|
|
key = test::get_key<Container>(*x.begin());
|
2009-12-15 22:52:52 +00:00
|
|
|
std::size_t count = x.count(key);
|
2010-03-22 00:42:07 +00:00
|
|
|
x.quick_erase(x.begin());
|
2009-12-15 22:52:52 +00:00
|
|
|
--size;
|
|
|
|
|
BOOST_TEST(x.count(key) == count - 1);
|
|
|
|
|
BOOST_TEST(x.size() == size);
|
2012-10-07 08:19:01 +00:00
|
|
|
if (++iterations % 20 == 0) test::check_equivalent_keys(x);
|
2009-12-15 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
BOOST_TEST(x.empty());
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-22 00:42:07 +00:00
|
|
|
std::cerr<<"quick_erase(random position).\n";
|
2009-12-15 22:52:52 +00:00
|
|
|
{
|
2011-08-14 18:52:43 +00:00
|
|
|
test::check_instances check_;
|
|
|
|
|
|
2009-12-15 22:52:52 +00:00
|
|
|
test::random_values<Container> v(1000, generator);
|
|
|
|
|
Container x(v.begin(), v.end());
|
|
|
|
|
std::size_t size = x.size();
|
2012-10-07 08:19:01 +00:00
|
|
|
int iterations = 0;
|
2009-12-15 22:52:52 +00:00
|
|
|
while(size > 0 && !x.empty())
|
|
|
|
|
{
|
2016-10-05 09:45:53 +01:00
|
|
|
std::size_t index = random_value(x.size());
|
2009-12-15 22:52:52 +00:00
|
|
|
BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next;
|
|
|
|
|
if(index == 0) {
|
|
|
|
|
prev = pos = x.begin();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
prev = boost::next(x.begin(), index - 1);
|
|
|
|
|
pos = boost::next(prev);
|
|
|
|
|
}
|
|
|
|
|
next = boost::next(pos);
|
2010-01-04 22:49:39 +00:00
|
|
|
BOOST_DEDUCED_TYPENAME Container::key_type
|
|
|
|
|
key = test::get_key<Container>(*pos);
|
2009-12-15 22:52:52 +00:00
|
|
|
std::size_t count = x.count(key);
|
2016-07-25 14:18:39 +01:00
|
|
|
BOOST_TEST(count > 0);
|
2010-03-22 00:42:07 +00:00
|
|
|
x.quick_erase(pos);
|
2009-12-15 22:52:52 +00:00
|
|
|
--size;
|
|
|
|
|
if(size > 0)
|
|
|
|
|
BOOST_TEST(index == 0 ? next == x.begin() :
|
|
|
|
|
next == boost::next(prev));
|
|
|
|
|
BOOST_TEST(x.count(key) == count - 1);
|
2016-07-25 14:18:39 +01:00
|
|
|
if (x.count(key) != count - 1) {
|
|
|
|
|
std::cerr << count << " => " << x.count(key) << std::endl;
|
|
|
|
|
}
|
2009-12-15 22:52:52 +00:00
|
|
|
BOOST_TEST(x.size() == size);
|
2012-10-07 08:19:01 +00:00
|
|
|
if (++iterations % 20 == 0) test::check_equivalent_keys(x);
|
2009-12-15 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
BOOST_TEST(x.empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
std::cerr<<"clear().\n";
|
|
|
|
|
{
|
2011-08-14 18:52:43 +00:00
|
|
|
test::check_instances check_;
|
|
|
|
|
|
2008-01-20 18:55:57 +00:00
|
|
|
test::random_values<Container> v(500, generator);
|
2006-05-17 17:19:16 +00:00
|
|
|
Container x(v.begin(), v.end());
|
|
|
|
|
x.clear();
|
2009-05-27 17:44:09 +00:00
|
|
|
BOOST_TEST(x.empty());
|
|
|
|
|
BOOST_TEST(x.begin() == x.end());
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
2008-01-20 18:55:57 +00:00
|
|
|
|
|
|
|
|
std::cerr<<"\n";
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-04 22:49:39 +00:00
|
|
|
boost::unordered_set<test::object,
|
|
|
|
|
test::hash, test::equal_to,
|
2012-07-08 11:55:35 +00:00
|
|
|
test::allocator1<test::object> >* test_set;
|
2010-01-04 22:49:39 +00:00
|
|
|
boost::unordered_multiset<test::object,
|
|
|
|
|
test::hash, test::equal_to,
|
2012-07-08 11:55:35 +00:00
|
|
|
test::allocator2<test::object> >* test_multiset;
|
2010-01-04 22:49:39 +00:00
|
|
|
boost::unordered_map<test::object, test::object,
|
|
|
|
|
test::hash, test::equal_to,
|
2012-07-08 11:55:35 +00:00
|
|
|
test::allocator1<test::object> >* test_map;
|
2010-01-04 22:49:39 +00:00
|
|
|
boost::unordered_multimap<test::object, test::object,
|
|
|
|
|
test::hash, test::equal_to,
|
2012-07-08 11:55:35 +00:00
|
|
|
test::allocator2<test::object> >* test_multimap;
|
2008-03-24 17:03:15 +00:00
|
|
|
|
|
|
|
|
using test::default_generator;
|
|
|
|
|
using test::generate_collisions;
|
|
|
|
|
|
|
|
|
|
UNORDERED_TEST(erase_tests1,
|
|
|
|
|
((test_set)(test_multiset)(test_map)(test_multimap))
|
|
|
|
|
((default_generator)(generate_collisions))
|
|
|
|
|
)
|
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
2008-03-24 17:03:15 +00:00
|
|
|
|
|
|
|
|
RUN_TESTS()
|