2006-05-17 17:19:16 +00:00
|
|
|
|
|
|
|
// Copyright Daniel James 2006. Use, modification, and distribution are
|
|
|
|
// subject to 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)
|
|
|
|
|
|
|
|
#include <boost/unordered_set.hpp>
|
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
#include <boost/detail/lightweight_test.hpp>
|
|
|
|
#include <boost/next_prior.hpp>
|
|
|
|
#include "../objects/test.hpp"
|
|
|
|
#include "../helpers/random_values.hpp"
|
|
|
|
#include "../helpers/tracker.hpp"
|
|
|
|
#include "../helpers/equivalent.hpp"
|
|
|
|
#include "../helpers/invariants.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
template <class X>
|
|
|
|
void unique_insert_tests1(X* = 0)
|
2006-05-17 17:19:16 +00:00
|
|
|
{
|
2006-06-12 23:30:46 +00:00
|
|
|
typedef typename X::iterator iterator;
|
|
|
|
typedef test::ordered<X> ordered;
|
|
|
|
typedef typename test::ordered<X>::iterator ordered_iterator;
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
std::cerr<<"insert(value) tests for containers with unique keys.\n";
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
X x;
|
|
|
|
test::ordered<X> tracker = test::create_ordered(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
test::random_values<X> v(1000);
|
|
|
|
for(typename test::random_values<X>::iterator it = v.begin();
|
2006-05-17 17:19:16 +00:00
|
|
|
it != v.end(); ++it)
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
typename X::size_type old_bucket_count = x.bucket_count();
|
|
|
|
float b = x.max_load_factor();
|
|
|
|
|
2006-06-12 23:30:46 +00:00
|
|
|
std::pair<iterator, bool> r1 = x.insert(*it);
|
|
|
|
std::pair<ordered_iterator, bool> r2 = tracker.insert(*it);
|
2006-05-17 17:19:16 +00:00
|
|
|
|
|
|
|
BOOST_TEST(r1.second == r2.second);
|
|
|
|
BOOST_TEST(*r1.first == *r2.first);
|
|
|
|
|
|
|
|
tracker.compare_key(x, *it);
|
2006-05-21 17:14:11 +00:00
|
|
|
|
|
|
|
if(x.size() < b * old_bucket_count)
|
|
|
|
BOOST_TEST(x.bucket_count() == old_bucket_count);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
template <class X>
|
|
|
|
void equivalent_insert_tests1(X* = 0)
|
2006-05-17 17:19:16 +00:00
|
|
|
{
|
|
|
|
std::cerr<<"insert(value) tests for containers with equivalent keys.\n";
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
X x;
|
|
|
|
test::ordered<X> tracker = test::create_ordered(x);
|
2006-05-17 17:19:16 +00:00
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
test::random_values<X> v(1000);
|
|
|
|
for(typename test::random_values<X>::iterator it = v.begin();
|
2006-05-17 17:19:16 +00:00
|
|
|
it != v.end(); ++it)
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
typename X::size_type old_bucket_count = x.bucket_count();
|
|
|
|
float b = x.max_load_factor();
|
|
|
|
|
|
|
|
typename X::iterator r1 = x.insert(*it);
|
|
|
|
typename test::ordered<X>::iterator r2 = tracker.insert(*it);
|
2006-05-17 17:19:16 +00:00
|
|
|
|
|
|
|
BOOST_TEST(*r1 == *r2);
|
|
|
|
|
|
|
|
tracker.compare_key(x, *it);
|
2006-05-21 17:14:11 +00:00
|
|
|
|
|
|
|
if(x.size() < b * old_bucket_count)
|
|
|
|
BOOST_TEST(x.bucket_count() == old_bucket_count);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
template <class X>
|
|
|
|
void insert_tests2(X* = 0)
|
2006-05-17 17:19:16 +00:00
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
typedef typename test::ordered<X> tracker_type;
|
|
|
|
typedef typename X::iterator iterator;
|
|
|
|
typedef typename X::const_iterator const_iterator;
|
2006-05-17 17:19:16 +00:00
|
|
|
typedef typename tracker_type::iterator tracker_iterator;
|
|
|
|
|
|
|
|
std::cerr<<"insert(begin(), value) tests.\n";
|
|
|
|
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
X x;
|
2006-05-17 17:19:16 +00:00
|
|
|
tracker_type tracker = test::create_ordered(x);
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
test::random_values<X> v(1000);
|
|
|
|
for(typename test::random_values<X>::iterator it = v.begin();
|
2006-05-17 17:19:16 +00:00
|
|
|
it != v.end(); ++it)
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
typename X::size_type old_bucket_count = x.bucket_count();
|
|
|
|
float b = x.max_load_factor();
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
iterator r1 = x.insert(x.begin(), *it);
|
|
|
|
tracker_iterator r2 = tracker.insert(tracker.begin(), *it);
|
|
|
|
BOOST_TEST(*r1 == *r2);
|
|
|
|
tracker.compare_key(x, *it);
|
2006-05-21 17:14:11 +00:00
|
|
|
|
|
|
|
if(x.size() < b * old_bucket_count)
|
|
|
|
BOOST_TEST(x.bucket_count() == old_bucket_count);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr<<"insert(end(), value) tests.\n";
|
|
|
|
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
X x;
|
|
|
|
X const& x_const = x;
|
2006-05-17 17:19:16 +00:00
|
|
|
tracker_type tracker = test::create_ordered(x);
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
test::random_values<X> v(100);
|
|
|
|
for(typename test::random_values<X>::iterator it = v.begin();
|
2006-05-17 17:19:16 +00:00
|
|
|
it != v.end(); ++it)
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
typename X::size_type old_bucket_count = x.bucket_count();
|
|
|
|
float b = x.max_load_factor();
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
const_iterator r1 = x.insert(x_const.end(), *it);
|
|
|
|
tracker_iterator r2 = tracker.insert(tracker.end(), *it);
|
|
|
|
BOOST_TEST(*r1 == *r2);
|
|
|
|
tracker.compare_key(x, *it);
|
2006-05-21 17:14:11 +00:00
|
|
|
|
|
|
|
if(x.size() < b * old_bucket_count)
|
|
|
|
BOOST_TEST(x.bucket_count() == old_bucket_count);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr<<"insert(pos, value) tests.\n";
|
|
|
|
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
X x;
|
2006-05-17 17:19:16 +00:00
|
|
|
const_iterator pos = x.begin();
|
|
|
|
tracker_type tracker = test::create_ordered(x);
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
test::random_values<X> v(1000);
|
|
|
|
for(typename test::random_values<X>::iterator it = v.begin();
|
2006-05-17 17:19:16 +00:00
|
|
|
it != v.end(); ++it)
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
typename X::size_type old_bucket_count = x.bucket_count();
|
|
|
|
float b = x.max_load_factor();
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
pos = x.insert(pos, *it);
|
|
|
|
tracker_iterator r2 = tracker.insert(tracker.begin(), *it);
|
|
|
|
BOOST_TEST(*pos == *r2);
|
|
|
|
tracker.compare_key(x, *it);
|
2006-05-21 17:14:11 +00:00
|
|
|
|
|
|
|
if(x.size() < b * old_bucket_count)
|
|
|
|
BOOST_TEST(x.bucket_count() == old_bucket_count);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr<<"insert single item range tests.\n";
|
|
|
|
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
X x;
|
2006-05-17 17:19:16 +00:00
|
|
|
tracker_type tracker = test::create_ordered(x);
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
test::random_values<X> v(1000);
|
|
|
|
for(typename test::random_values<X>::iterator it = v.begin();
|
2006-05-17 17:19:16 +00:00
|
|
|
it != v.end(); ++it)
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
typename X::size_type old_bucket_count = x.bucket_count();
|
|
|
|
float b = x.max_load_factor();
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
x.insert(it, boost::next(it));
|
|
|
|
tracker.insert(*it);
|
|
|
|
tracker.compare_key(x, *it);
|
2006-05-21 17:14:11 +00:00
|
|
|
|
|
|
|
if(x.size() < b * old_bucket_count)
|
|
|
|
BOOST_TEST(x.bucket_count() == old_bucket_count);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr<<"insert range tests.\n";
|
|
|
|
|
|
|
|
{
|
2006-05-21 17:14:11 +00:00
|
|
|
X x;
|
2006-05-17 17:19:16 +00:00
|
|
|
const_iterator pos = x.begin();
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
test::random_values<X> v(1000);
|
2006-05-17 17:19:16 +00:00
|
|
|
x.insert(v.begin(), v.end());
|
2006-06-12 23:30:46 +00:00
|
|
|
test::check_container(x, v);
|
2006-05-17 17:19:16 +00:00
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
template <class X>
|
|
|
|
void map_tests(X* = 0)
|
|
|
|
{
|
|
|
|
X x;
|
|
|
|
test::ordered<X> tracker = test::create_ordered(x);
|
|
|
|
|
|
|
|
test::random_values<X> v(1000);
|
|
|
|
for(typename test::random_values<X>::iterator it = v.begin();
|
|
|
|
it != v.end(); ++it)
|
|
|
|
{
|
|
|
|
typename X::size_type old_bucket_count = x.bucket_count();
|
|
|
|
float b = x.max_load_factor();
|
|
|
|
|
|
|
|
x[it->first] = it->second;
|
|
|
|
tracker[it->first] = it->second;
|
|
|
|
|
|
|
|
tracker.compare_key(x, *it);
|
|
|
|
|
|
|
|
if(x.size() < b * old_bucket_count)
|
|
|
|
BOOST_TEST(x.bucket_count() == old_bucket_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
test::check_equivalent_keys(x);
|
|
|
|
}
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
unique_insert_tests1((boost::unordered_set<int>*) 0);
|
|
|
|
equivalent_insert_tests1((boost::unordered_multiset<int>*) 0);
|
|
|
|
unique_insert_tests1((boost::unordered_map<int, int>*) 0);
|
|
|
|
equivalent_insert_tests1((boost::unordered_multimap<int, int>*) 0);
|
|
|
|
|
|
|
|
unique_insert_tests1((boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
equivalent_insert_tests1((boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
unique_insert_tests1((boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
equivalent_insert_tests1((boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
|
|
|
|
insert_tests2((boost::unordered_set<int>*) 0);
|
|
|
|
insert_tests2((boost::unordered_multiset<int>*) 0);
|
|
|
|
insert_tests2((boost::unordered_map<int, int>*) 0);
|
|
|
|
insert_tests2((boost::unordered_multimap<int, int>*) 0);
|
|
|
|
|
|
|
|
insert_tests2((boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
insert_tests2((boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
insert_tests2((boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
insert_tests2((boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
|
2006-05-21 17:14:11 +00:00
|
|
|
map_tests((boost::unordered_map<int, int>*) 0);
|
|
|
|
map_tests((boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
return boost::report_errors();
|
|
|
|
}
|