More tests for unordered associative containers.

[SVN r2959]
This commit is contained in:
Daniel James
2006-05-21 17:14:11 +00:00
parent 822b0c7ffd
commit fbd37a946b
28 changed files with 1975 additions and 86 deletions
+6
View File
@@ -20,4 +20,10 @@ test-suite unordered-tests
[ run insert_tests.cpp ]
[ run erase_tests.cpp ]
[ run find_tests.cpp ]
[ run bucket_tests.cpp ]
[ run load_factor_tests.cpp ]
[ run rehash_tests.cpp ]
[ run swap_tests.cpp : : : <define>BOOST_UNORDERED_SWAP_METHOD=1 : swap_tests1 ]
[ run swap_tests.cpp : : : <define>BOOST_UNORDERED_SWAP_METHOD=2 : swap_tests2 ]
[ run swap_tests.cpp : : : <define>BOOST_UNORDERED_SWAP_METHOD=3 : swap_tests3 ]
;
+16 -1
View File
@@ -53,9 +53,11 @@ void assign_tests2(T* = 0)
typename T::hasher hf;
typename T::key_equal eq;
typename T::hasher hf1(1);
typename T::key_equal eq1(1);
typename T::hasher hf2(2);
typename T::key_equal eq1(1);
typename T::key_equal eq2(2);
typename T::allocator_type al1(1);
typename T::allocator_type al2(2);
std::cerr<<"assign_tests2.1\n";
{
@@ -68,6 +70,19 @@ void assign_tests2(T* = 0)
BOOST_TEST(test::equivalent(x2.key_eq(), eq1));
check_container(x2, v);
}
std::cerr<<"assign_tests2.2\n";
{
// TODO: Need to generate duplicates...
test::random_values<T> v1(100), v2(100);
T x1(v1.begin(), v1.end(), 0, hf1, eq1, al1);
T x2(v2.begin(), v2.end(), 0, hf2, eq2, al2);
x2 = x1;
BOOST_TEST(test::equivalent(x2.hash_function(), hf1));
BOOST_TEST(test::equivalent(x2.key_eq(), eq1));
BOOST_TEST(test::equivalent(x2.get_allocator(), al2));
check_container(x2, v1);
}
}
int main()
+60
View File
@@ -0,0 +1,60 @@
// 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 <algorithm>
#include "../objects/test.hpp"
#include "../helpers/random_values.hpp"
#include "../helpers/helpers.hpp"
template <class X>
void bucket_tests(X* = 0)
{
typedef typename X::size_type size_type;
typedef typename X::const_local_iterator const_local_iterator;
test::random_values<X> v(1000);
X x(v.begin(), v.end());
BOOST_TEST(x.bucket_count() < x.max_bucket_count());
std::cerr<<x.bucket_count()<<"<"<<x.max_bucket_count()<<"\n";
for(typename test::random_values<X>::const_iterator
it = v.begin(), end = v.end(); it != end; ++it)
{
size_type bucket = x.bucket(test::get_key<X>(*it));
BOOST_TEST(bucket < x.bucket_count());
if(bucket < x.max_bucket_count()) {
// lit? lend?? I need a new naming scheme.
const_local_iterator lit = x.begin(bucket), lend = x.end(bucket);
while(lit != lend && test::get_key<X>(*it) != test::get_key<X>(*lit)) ++lit;
BOOST_TEST(lit != lend);
}
}
for(size_type i = 0; i < x.bucket_count(); ++i) {
BOOST_TEST(x.bucket_size(i) == (size_type) std::distance(x.begin(i), x.end(i)));
X const& x_ref(x);
BOOST_TEST(x.bucket_size(i) == (size_type) std::distance(x_ref.begin(i), x_ref.end(i)));
}
}
int main()
{
bucket_tests((boost::unordered_set<int>*) 0);
bucket_tests((boost::unordered_multiset<int>*) 0);
bucket_tests((boost::unordered_map<int, int>*) 0);
bucket_tests((boost::unordered_multimap<int, int>*) 0);
bucket_tests((boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
bucket_tests((boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
bucket_tests((boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
bucket_tests((boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
return boost::report_errors();
}
+39 -2
View File
@@ -18,6 +18,7 @@ void constructor_tests1(T* = 0)
{
typename T::hasher hf;
typename T::key_equal eq;
typename T::allocator_type al;
std::cerr<<"Construct 1\n";
{
@@ -26,6 +27,7 @@ void constructor_tests1(T* = 0)
BOOST_TEST(x.bucket_count() >= 0);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
}
std::cerr<<"Construct 2\n";
@@ -35,6 +37,7 @@ void constructor_tests1(T* = 0)
BOOST_TEST(x.bucket_count() >= 100);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
}
std::cerr<<"Construct 3\n";
@@ -44,6 +47,7 @@ void constructor_tests1(T* = 0)
BOOST_TEST(x.bucket_count() >= 2000);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
}
std::cerr<<"Construct 4\n";
@@ -52,6 +56,7 @@ void constructor_tests1(T* = 0)
BOOST_TEST(x.empty());
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
}
std::cerr<<"Construct 5\n";
@@ -61,6 +66,7 @@ void constructor_tests1(T* = 0)
BOOST_TEST(x.bucket_count() >= 10000);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
check_container(x, v);
}
@@ -71,6 +77,7 @@ void constructor_tests1(T* = 0)
BOOST_TEST(x.bucket_count() >= 10000);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
check_container(x, v);
}
@@ -81,6 +88,7 @@ void constructor_tests1(T* = 0)
BOOST_TEST(x.bucket_count() >= 100);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
check_container(x, v);
}
@@ -90,6 +98,28 @@ void constructor_tests1(T* = 0)
T x(v.begin(), v.end());
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
check_container(x, v);
}
std::cerr<<"Construct 9\n";
{
T x(0, hf, eq, al);
BOOST_TEST(x.empty());
BOOST_TEST(x.bucket_count() >= 0);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
}
std::cerr<<"Construct 10\n";
{
test::random_values<T> v(1000);
T x(v.begin(), v.end(), 10000, hf, eq, al);
BOOST_TEST(x.bucket_count() >= 10000);
BOOST_TEST(test::equivalent(x.hash_function(), hf));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
check_container(x, v);
}
}
@@ -103,6 +133,9 @@ void constructor_tests2(T* = 0)
typename T::key_equal eq;
typename T::key_equal eq1(1);
typename T::key_equal eq2(2);
typename T::allocator_type al;
typename T::allocator_type al1(1);
typename T::allocator_type al2(2);
std::cerr<<"Construct 1\n";
{
@@ -110,6 +143,7 @@ void constructor_tests2(T* = 0)
BOOST_TEST(x.bucket_count() >= 10000);
BOOST_TEST(test::equivalent(x.hash_function(), hf1));
BOOST_TEST(test::equivalent(x.key_eq(), eq1));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
}
std::cerr<<"Construct 2\n";
@@ -119,6 +153,7 @@ void constructor_tests2(T* = 0)
BOOST_TEST(x.bucket_count() >= 100);
BOOST_TEST(test::equivalent(x.hash_function(), hf1));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
}
std::cerr<<"Construct 3\n";
@@ -127,6 +162,7 @@ void constructor_tests2(T* = 0)
T x(v.begin(), v.end(), 0, hf1, eq1);
BOOST_TEST(test::equivalent(x.hash_function(), hf1));
BOOST_TEST(test::equivalent(x.key_eq(), eq1));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
check_container(x, v);
}
@@ -137,6 +173,7 @@ void constructor_tests2(T* = 0)
BOOST_TEST(x.bucket_count() >= 1000);
BOOST_TEST(test::equivalent(x.hash_function(), hf1));
BOOST_TEST(test::equivalent(x.key_eq(), eq));
BOOST_TEST(test::equivalent(x.get_allocator(), al));
check_container(x, v);
}
@@ -144,8 +181,8 @@ void constructor_tests2(T* = 0)
std::cerr<<"Construct 5\n";
{
test::random_values<T> v(100);
T x(v.begin(), v.end(), 0, hf, eq);
T y(x.begin(), x.end(), 0, hf1, eq1);
T x(v.begin(), v.end(), 0, hf, eq, al1);
T y(x.begin(), x.end(), 0, hf1, eq1, al2);
check_container(x, v);
check_container(y, x);
}
+8 -5
View File
@@ -17,6 +17,7 @@ void copy_construct_tests1(T* = 0)
{
typename T::hasher hf;
typename T::key_equal eq;
typename T::allocator_type al;
{
T x;
@@ -24,6 +25,7 @@ void copy_construct_tests1(T* = 0)
BOOST_TEST(y.empty());
BOOST_TEST(test::equivalent(y.hash_function(), hf));
BOOST_TEST(test::equivalent(y.key_eq(), eq));
BOOST_TEST(test::equivalent(y.get_allocator(), al));
BOOST_TEST(x.max_load_factor() == y.max_load_factor());
test::check_equivalent_keys(y);
}
@@ -42,15 +44,14 @@ void copy_construct_tests1(T* = 0)
// In this test I drop the original containers max load factor, so it
// is much lower than the load factor. The hash table is not allowed
// to rehash, but the destination container should probably allocate
// enough buckets to decrease the load factor appropriately. Although,
// I don't think it has to.
// enough buckets to decrease the load factor appropriately.
test::random_values<T> v(1000);
T x(v.begin(), v.end());
x.max_load_factor(x.load_factor() / 4);
T y(x);
test::unordered_equivalence_tester<T> equivalent(x);
equivalent(y);
// I don't think this is guaranteed:
// This isn't guaranteed:
BOOST_TEST(y.load_factor() < y.max_load_factor());
test::check_equivalent_keys(y);
}
@@ -63,14 +64,16 @@ void copy_construct_tests2(T* ptr = 0)
typename T::hasher hf(1);
typename T::key_equal eq(1);
typename T::allocator_type al(1);
{
// TODO: I could check how many buckets y has, it should be lower (QOI issue).
T x(10000, hf, eq);
T x(10000, hf, eq, al);
T y(x);
BOOST_TEST(y.empty());
BOOST_TEST(test::equivalent(y.hash_function(), hf));
BOOST_TEST(test::equivalent(y.key_eq(), eq));
BOOST_TEST(test::equivalent(y.get_allocator(), al));
BOOST_TEST(x.max_load_factor() == y.max_load_factor());
test::check_equivalent_keys(y);
}
@@ -79,7 +82,7 @@ void copy_construct_tests2(T* ptr = 0)
// TODO: Invariant checks are especially important here.
test::random_values<T> v(1000);
T x(v.begin(), v.end(), 0, hf, eq);
T x(v.begin(), v.end(), 0, hf, eq, al);
T y(x);
test::unordered_equivalence_tester<T> equivalent(x);
equivalent(y);
+3 -1
View File
@@ -70,7 +70,7 @@ void erase_tests1(Container* = 0)
pos = boost::next(prev);
}
next = boost::next(pos);
typename Container::key_type key = test::get_key<Container>(*x.begin());
typename Container::key_type key = test::get_key<Container>(*pos);
std::size_t count = x.count(key);
BOOST_TEST(next == x.erase(pos));
--size;
@@ -137,4 +137,6 @@ int main()
erase_tests1((boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
std::cerr<<"\nErase unordered_multimap<test::object,..>.\n";
erase_tests1((boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
return boost::report_errors();
}
+100 -36
View File
@@ -15,76 +15,94 @@
#include <iostream>
template <class Container>
void unique_insert_tests1(Container* = 0)
template <class X>
void unique_insert_tests1(X* = 0)
{
std::cerr<<"insert(value) tests for containers with unique keys.\n";
Container x;
test::ordered<Container> tracker = test::create_ordered(x);
X x;
test::ordered<X> tracker = test::create_ordered(x);
test::random_values<Container> v(1000);
for(typename test::random_values<Container>::iterator it = v.begin();
test::random_values<X> v(1000);
for(typename test::random_values<X>::iterator it = v.begin();
it != v.end(); ++it)
{
std::pair<typename Container::iterator, bool> r1 = x.insert(*it);
std::pair<typename test::ordered<Container>::iterator, bool> r2
typename X::size_type old_bucket_count = x.bucket_count();
float b = x.max_load_factor();
std::pair<typename X::iterator, bool> r1 = x.insert(*it);
std::pair<typename test::ordered<X>::iterator, bool> r2
= tracker.insert(*it);
BOOST_TEST(r1.second == r2.second);
BOOST_TEST(*r1.first == *r2.first);
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);
}
template <class Container>
void equivalent_insert_tests1(Container* = 0)
template <class X>
void equivalent_insert_tests1(X* = 0)
{
std::cerr<<"insert(value) tests for containers with equivalent keys.\n";
Container x;
test::ordered<Container> tracker = test::create_ordered(x);
X x;
test::ordered<X> tracker = test::create_ordered(x);
test::random_values<Container> v(1000);
for(typename test::random_values<Container>::iterator it = v.begin();
test::random_values<X> v(1000);
for(typename test::random_values<X>::iterator it = v.begin();
it != v.end(); ++it)
{
typename Container::iterator r1 = x.insert(*it);
typename test::ordered<Container>::iterator r2 = tracker.insert(*it);
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);
BOOST_TEST(*r1 == *r2);
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);
}
template <class Container>
void insert_tests2(Container* = 0)
template <class X>
void insert_tests2(X* = 0)
{
typedef typename test::ordered<Container> tracker_type;
typedef typename Container::iterator iterator;
typedef typename Container::const_iterator const_iterator;
typedef typename test::ordered<X> tracker_type;
typedef typename X::iterator iterator;
typedef typename X::const_iterator const_iterator;
typedef typename tracker_type::iterator tracker_iterator;
std::cerr<<"insert(begin(), value) tests.\n";
{
Container x;
X x;
tracker_type tracker = test::create_ordered(x);
test::random_values<Container> v(1000);
for(typename test::random_values<Container>::iterator it = v.begin();
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();
iterator r1 = x.insert(x.begin(), *it);
tracker_iterator r2 = tracker.insert(tracker.begin(), *it);
BOOST_TEST(*r1 == *r2);
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);
@@ -93,18 +111,24 @@ void insert_tests2(Container* = 0)
std::cerr<<"insert(end(), value) tests.\n";
{
Container x;
Container const& x_const = x;
X x;
X const& x_const = x;
tracker_type tracker = test::create_ordered(x);
test::random_values<Container> v(100);
for(typename test::random_values<Container>::iterator it = v.begin();
test::random_values<X> v(100);
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();
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);
if(x.size() < b * old_bucket_count)
BOOST_TEST(x.bucket_count() == old_bucket_count);
}
test::check_equivalent_keys(x);
@@ -113,18 +137,24 @@ void insert_tests2(Container* = 0)
std::cerr<<"insert(pos, value) tests.\n";
{
Container x;
X x;
const_iterator pos = x.begin();
tracker_type tracker = test::create_ordered(x);
test::random_values<Container> v(1000);
for(typename test::random_values<Container>::iterator it = v.begin();
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();
pos = x.insert(pos, *it);
tracker_iterator r2 = tracker.insert(tracker.begin(), *it);
BOOST_TEST(*pos == *r2);
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);
@@ -133,16 +163,22 @@ void insert_tests2(Container* = 0)
std::cerr<<"insert single item range tests.\n";
{
Container x;
X x;
tracker_type tracker = test::create_ordered(x);
test::random_values<Container> v(1000);
for(typename test::random_values<Container>::iterator it = v.begin();
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.insert(it, boost::next(it));
tracker.insert(*it);
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);
@@ -151,10 +187,10 @@ void insert_tests2(Container* = 0)
std::cerr<<"insert range tests.\n";
{
Container x;
X x;
const_iterator pos = x.begin();
test::random_values<Container> v(1000);
test::random_values<X> v(1000);
x.insert(v.begin(), v.end());
check_container(x, v);
@@ -162,6 +198,31 @@ void insert_tests2(Container* = 0)
}
}
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);
}
int main()
{
unique_insert_tests1((boost::unordered_set<int>*) 0);
@@ -184,5 +245,8 @@ int main()
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);
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);
return boost::report_errors();
}
+72
View File
@@ -0,0 +1,72 @@
// 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/limits.hpp>
#include "../helpers/random_values.hpp"
template <class X>
void load_factor_tests(X* = 0)
{
X x;
BOOST_TEST(x.max_load_factor() == 1.0);
BOOST_TEST(x.load_factor() == 0);
// A valid implementation could fail these tests, but I think they're
// reasonable.
x.max_load_factor(2.0); BOOST_TEST(x.max_load_factor() == 2.0);
x.max_load_factor(0.5); BOOST_TEST(x.max_load_factor() == 0.5);
}
template <class X>
void insert_test(X*, float mlf)
{
X x;
x.max_load_factor(mlf);
float b = x.max_load_factor();
test::random_values<X> values(1000);
for(typename test::random_values<X>::const_iterator
it = values.begin(), end = values.end(); it != end; ++it)
{
typename X::size_type old_size = x.size(),
old_bucket_count = x.bucket_count();
x.insert(*it);
if(old_size + 1 < b * old_bucket_count)
BOOST_TEST(x.bucket_count() == old_bucket_count);
}
}
template <class X>
void load_factor_insert_tests(X* ptr = 0)
{
insert_test(ptr, 1.0);
insert_test(ptr, 0.1);
insert_test(ptr, 100);
insert_test(ptr, (std::numeric_limits<float>::min)());
if(std::numeric_limits<float>::has_infinity)
insert_test(ptr, std::numeric_limits<float>::infinity());
}
int main()
{
load_factor_tests((boost::unordered_set<int>*) 0);
load_factor_tests((boost::unordered_multiset<int>*) 0);
load_factor_tests((boost::unordered_map<int, int>*) 0);
load_factor_tests((boost::unordered_multimap<int, int>*) 0);
load_factor_insert_tests((boost::unordered_set<int>*) 0);
load_factor_insert_tests((boost::unordered_multiset<int>*) 0);
load_factor_insert_tests((boost::unordered_map<int, int>*) 0);
load_factor_insert_tests((boost::unordered_multimap<int, int>*) 0);
return boost::report_errors();
}
+67
View File
@@ -0,0 +1,67 @@
// 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 "../helpers/random_values.hpp"
#include "../helpers/tracker.hpp"
template <class X>
bool postcondition(X const& x, typename X::size_type n)
{
return x.bucket_count() > x.size() / x.max_load_factor() && x.bucket_count() >= n;
}
template <class X>
void rehash_empty_test1(X* = 0)
{
X x;
x.rehash(10000);
BOOST_TEST(postcondition(x, 10000));
x.rehash(0);
BOOST_TEST(postcondition(x, 0));
}
template <class X>
void rehash_test1(X* = 0)
{
test::random_values<X> v(1000);
test::ordered<X> tracker;
tracker.insert(v.begin(), v.end());
X x(v.begin(), v.end());
x.rehash(0); BOOST_TEST(postcondition(x, 0));
tracker.compare(x);
x.max_load_factor(0.25);
x.rehash(0); BOOST_TEST(postcondition(x, 0));
tracker.compare(x);
x.max_load_factor(50.0);
x.rehash(0); BOOST_TEST(postcondition(x, 0));
tracker.compare(x);
x.rehash(1000); BOOST_TEST(postcondition(x, 1000));
tracker.compare(x);
}
template <class X>
void rehash_tests(X* ptr = 0)
{
rehash_empty_test1(ptr);
rehash_test1(ptr);
}
int main() {
rehash_tests((boost::unordered_set<int>*) 0);
rehash_tests((boost::unordered_multiset<int>*) 0);
rehash_tests((boost::unordered_map<int, int>*) 0);
rehash_tests((boost::unordered_multimap<int, int>*) 0);
return boost::report_errors();
}
+135
View File
@@ -0,0 +1,135 @@
// 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 "../objects/test.hpp"
#include "../helpers/random_values.hpp"
#include "../helpers/tracker.hpp"
#include "../helpers/invariants.hpp"
template <class X>
void swap_test_impl(X& x1, X& x2)
{
test::ordered<X> tracker1 = test::create_ordered(x1);
test::ordered<X> tracker2 = test::create_ordered(x2);
tracker1.insert(x1.begin(), x1.end());
tracker2.insert(x2.begin(), x2.end());
x1.swap(x2);
tracker1.compare(x2);
tracker2.compare(x1);
}
template <class X>
void swap_tests1(X* = 0)
{
{
X x;
swap_test_impl(x, x);
}
{
X x,y;
swap_test_impl(x, y);
}
{
test::random_values<X> v(1000);
X x, y(v.begin(), v.end());
swap_test_impl(x, y);
swap_test_impl(x, y);
}
{
test::random_values<X> vx(1000), vy(1000);
X x(vx.begin(), vx.end()), y(vy.begin(), vy.end());
swap_test_impl(x, y);
swap_test_impl(x, y);
}
}
template <class X>
void swap_tests2(X* ptr = 0)
{
swap_tests1(ptr);
typedef typename X::hasher hasher;
typedef typename X::key_equal key_equal;
typedef typename X::allocator_type allocator_type;
{
X x(0, hasher(1), key_equal(1));
X y(0, hasher(2), key_equal(2));
swap_test_impl(x, y);
}
{
test::random_values<X> v(1000);
X x(v.begin(), v.end(), 0, hasher(1), key_equal(1));
X y(0, hasher(2), key_equal(2));
swap_test_impl(x, y);
swap_test_impl(x, y);
}
{
test::random_values<X> vx(1000), vy(1000);
X x(vx.begin(), vx.end(), 0, hasher(1), key_equal(1));
X y(vy.begin(), vy.end(), 0, hasher(2), key_equal(2));
swap_test_impl(x, y);
swap_test_impl(x, y);
}
#if BOOST_UNORDERED_SWAP_METHOD == 1
{
test::random_values<X> vx(1000), vy(1000);
X x(vx.begin(), vx.end(), 0, hasher(), key_equal(), allocator_type(1));
X y(vy.begin(), vy.end(), 0, hasher(), key_equal(), allocator_type(2));
try {
swap_test_impl(x, y);
BOOST_ERROR("Using swap method 1, swapping with unequal allocators didn't throw.");
} catch (std::runtime_error) {}
}
#else
{
test::random_values<X> vx(1000), vy(1000);
X x(vx.begin(), vx.end(), 0, hasher(), key_equal(), allocator_type(1));
X y(vy.begin(), vy.end(), 0, hasher(), key_equal(), allocator_type(2));
swap_test_impl(x, y);
swap_test_impl(x, y);
}
{
test::random_values<X> vx(1000), vy(1000);
X x(vx.begin(), vx.end(), 0, hasher(1), key_equal(1), allocator_type(1));
X y(vy.begin(), vy.end(), 0, hasher(2), key_equal(2), allocator_type(2));
swap_test_impl(x, y);
swap_test_impl(x, y);
}
#endif
}
int main()
{
std::cerr<<"Erase unordered_set<int>.\n";
swap_tests1((boost::unordered_set<int>*) 0);
std::cerr<<"\nErase unordered_multiset<int>.\n";
swap_tests1((boost::unordered_multiset<int>*) 0);
std::cerr<<"\nErase unordered_map<int>.\n";
swap_tests1((boost::unordered_map<int, int>*) 0);
std::cerr<<"\nErase unordered_multimap<int>.\n";
swap_tests1((boost::unordered_multimap<int, int>*) 0);
std::cerr<<"\nErase unordered_set<test::object,..>.\n";
swap_tests2((boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
std::cerr<<"\nErase unordered_multiset<test::object,..>.\n";
swap_tests2((boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
std::cerr<<"\nErase unordered_map<test::object,..>.\n";
swap_tests2((boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
std::cerr<<"\nErase unordered_multimap<test::object,..>.\n";
swap_tests2((boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >*) 0);
return boost::report_errors();
}