multi_index/test/test_iterators.cpp
Joaquín M López Muñoz 47b172ab68 Boost 1.35 version of Boost.MultiIndex
allocator_utilities.hpp: added partial_std_allocator_wrapper::value_type
composite_key.hpp: used hash_fwd.hpp
auto_space.hpp: added support for non-standard allocators
bidir_node_iterator.hpp: moved friend-injected operators out of class
copy_map.hpp: added support for non-standard allocators
hash_index_args.hpp: removed deprecated use of <boost/functional/hash/hash.hpp>
hash_index_iterator.hpp: moved friend-injected operators our of class
hash_index_node.hpp: added support for non-standard allocators
header_holder.hpp:added support for non-standard allocators
index_base.hpp: added support for non-standard allocators, added modify_rollback, added small improvement to modify
index_loader.hpp: added support for non-standard allocators
index_matcher.hpp: added support for non-standard allocators
index_node_base.hpp: added support for non-standard allocators
iter_adaptor.hpp: added some out-of-class operators to alleviate a MSVC++ 6.0 problem
modify_key_adaptor.hpp: renamed some vars to accomudate broader usage scope
node_type.hpp: added support for non-standard allocators
ord_index_node.hpp: added support for non-standard allocators
ord_index_ops.hpp: implemented a more efficient equal_range
rnd_index_loader.hpp: added support for non-standard allocators
rnd_index_node.hpp: added support for non-standard allocators
rnd_index_ops.hpp: added support for non-standard allocators
rnd_index_ptr_array.hpp: added support for non-standard allocators
rnd_node_iterator.hpp: moved friend-injected operators out of class
seq_index_node.hpp: added support for non-standard allocators
seq_index_ops.hpp: added support for non-standard allocators
uintptr_type.hpp: added support for __int64
unbounded.hpp: fixed ODR problem
value_compare.hpp: fixed a small unefficiency
global_fun: initial commit
hashed_index.hpp: added support for non-standard allocators, added c[r]{begin|end}, [local_]iterator_to, rollback modify
identity_fwd.hpp: fixed wrong include guard name
key_extractors.hpp: added global_fun
mem_fun.hpp: removed superfluous =0's
ordered_index.hpp: added support for non-standard allocators, added c[r]{begin|end}, iterator_to, rollback modify, improved equal_range and range, added conformance to DR 233
random_access_index.hpp: added support for non-standard allocators, added c[r]{begin|end}, iterator_to, rollback modify, added conformance to 23.1.1/9
sequenced_index.hpp: added support for non-standard allocators, added c[r]{begin|end}, iterator_to, rollback modify, added conformance to 23.1.1/9, improved resize
multi_index_container.hpp: added support for non-standard allocators, improved ctor_args_list, rollback modify
acknowledgements.html: added entry for Boost 1.35
examples.html: renamed example 2, added B.IP example/composite_keys.cpp
future_work.html: removed entry on bimap
hash_indices.html: added c[r]{begin|end}, [local_]iterator_to, rollback modify
reference/index.html: added global_fun
reference/key_extraction.html: added global_fun, added technical correction
multi_index_container.html: added support for non-standard allocators
ord_indices.html: added c[r]{begin|end}, iterator_to, rollback modify
rnd_indices.html: added c[r]{begin|end}, iterator_to, rollback modify
seq_indices.html: added c[r]{begin|end}, iterator_to, rollback modify
release_notes.html: added entry for Boost 1.35
tests.html: added new serialization test file
basics.html: added rollback modify
creation.html: added support for non-standard allocators
tutorial/indices.html: added iterator_to
tutorial/key_extraction.html: added global_fun
composite_keys.cpp: fixed technicality
fun_key.cpp: was memfun_key.cpp, added global_fun
ip_allocator.cpp: initial commit
example/Jamfile.v2: renamed memfun_key, added ip_allocator
test_perf.cpp: fixed technicality
employee.hpp: used a non-standard allocator
test/Jamfile.v2: added new test file
non_std_allocator.hpp: initial commit
pair_of_ints.hpp: added decrement facilities
test_capacity.cpp: added extra check on resize
test_copy_assignment.cpp: added test for 23.1.1/9
test_iterators.cpp: added tests for c[r]{begin|end} and [local_]iterator_to, fixed technicality
test_key_extractors.cpp: added tests for global_fun
test_modifiers.cpp: added tests dor DR 233, fixed technicality
test_range.cpp: added extra checks to secure range refactoring
test_rearrange.cpp: fixed technicality
test_serialization.cpp: added new test file
test_serialization1.cpp: corrected include, used a non-standard allocator
test_serialization2.cpp: corrected include, used a non-standard allocator, split some stuff ro test_serialization3.cpp
test_serialization3.cpp: initial commit
test_serialization3.hpp: initial commit
test_serialization_template.hpp: removed some reliance on ADL
test_update.cpp: addes tests for rollback modify, fixed technicality

[SVN r39922]
2007-10-11 10:57:30 +00:00

251 lines
6.7 KiB
C++

/* Boost.MultiIndex test for iterators.
*
* Copyright 2003-2007 Joaquín M López Muñoz.
* 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)
*
* See http://www.boost.org/libs/multi_index for library home page.
*/
#include "test_iterators.hpp"
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include "pre_multi_index.hpp"
#include "employee.hpp"
#include <boost/next_prior.hpp>
#include <boost/test/test_tools.hpp>
using namespace boost::multi_index;
template<typename Index>
void test_non_const_iterators(Index& i,int target)
{
typedef typename Index::iterator iterator;
typedef typename Index::reverse_iterator reverse_iterator;
int n=0;
for(iterator it=i.begin();it!=i.end();++it){
BOOST_CHECK(i.iterator_to(*it)==it);
n+=it->id;
}
int m=0;
for(reverse_iterator rit=i.rbegin();rit!=i.rend();++rit){
m+=rit->id;
}
int p=0;
for(iterator it2=i.end();it2!=i.begin();){
--it2;
p+=it2->id;
}
int q=0;
for(reverse_iterator rit2=i.rend();rit2!=i.rbegin();){
--rit2;
q+=rit2->id;
}
BOOST_CHECK(n==target&&n==m&&n==p&&n==q);
}
template<typename Index>
void test_const_iterators(const Index& i,int target)
{
typedef typename Index::const_iterator const_iterator;
typedef typename Index::const_reverse_iterator const_reverse_iterator;
BOOST_CHECK(i.cbegin()==i.begin());
BOOST_CHECK(i.cend()==i.end());
BOOST_CHECK(i.crbegin()==i.rbegin());
BOOST_CHECK(i.crend()==i.rend());
int n=0;
for(const_iterator it=i.begin();it!=i.end();++it){
BOOST_CHECK(i.iterator_to(*it)==it);
n+=it->id;
}
int m=0;
for(const_reverse_iterator rit=i.rbegin();rit!=i.rend();++rit){
m+=rit->id;
}
int p=0;
for(const_iterator it2=i.end();it2!=i.begin();){
--it2;
p+=it2->id;
}
int q=0;
for(const_reverse_iterator rit2=i.rend();rit2!=i.rbegin();){
--rit2;
q+=rit2->id;
}
BOOST_CHECK(n==target&&n==m&&n==p&&n==q);
}
template<typename Index>
void test_non_const_hashed_iterators(Index& i,int target)
{
typedef typename Index::iterator iterator;
typedef typename Index::local_iterator local_iterator;
typedef typename Index::size_type size_type;
int n=0;
for(iterator it=i.begin();it!=i.end();++it){
BOOST_CHECK(i.iterator_to(*it)==it);
n+=it->id;
}
int m=0;
for(size_type buc=0;buc<i.bucket_count();++buc){
for(local_iterator it=i.begin(buc);it!=i.end(buc);++it){
BOOST_CHECK(i.local_iterator_to(*it)==it);
m+=it->id;
}
}
BOOST_CHECK(n==target&&n==m);
}
template<typename Index>
void test_const_hashed_iterators(const Index& i,int target)
{
typedef typename Index::const_iterator const_iterator;
typedef typename Index::const_local_iterator const_local_iterator;
typedef typename Index::size_type size_type;
BOOST_CHECK(i.cbegin()==i.begin());
BOOST_CHECK(i.cend()==i.end());
int n=0;
for(const_iterator it=i.begin();it!=i.end();++it){
BOOST_CHECK(i.iterator_to(*it)==it);
n+=it->id;
}
int m=0;
for(size_type buc=0;buc<i.bucket_count();++buc){
BOOST_CHECK(i.cbegin(buc)==i.begin(buc));
BOOST_CHECK(i.cend(buc)==i.end(buc));
for(const_local_iterator it=i.begin(buc);it!=i.end(buc);++it){
BOOST_CHECK(i.local_iterator_to(*it)==it);
m+=it->id;
}
}
BOOST_CHECK(n==target&&n==m);
}
template<typename Index>
void test_non_const_rnd_iterators(Index& i,int target)
{
typedef typename Index::iterator iterator;
typedef typename Index::reverse_iterator reverse_iterator;
typedef typename Index::difference_type difference_type;
iterator middle=i.begin()+(i.end()-i.begin())/2;
difference_type off=middle-i.begin();
reverse_iterator rmiddle=i.rbegin()+off;
bool odd=((i.end()-i.begin())%2)!=0;
int n=0;
for(iterator it=i.begin();it!=middle;++it){
BOOST_CHECK(i.iterator_to(*it)==it);
n+=it->id;
n+=it[off].id;
}
if(odd)n+=(boost::prior(i.end()))->id;
int m=0;
for(reverse_iterator rit=i.rbegin();rit!=rmiddle;++rit){
m+=rit->id;
m+=(rit+off)->id;
}
if(odd)m+=(boost::prior(i.rend()))->id;
int p=0;
for(iterator it2=i.end();it2!=middle;){
--it2;
p+=it2->id;
p+=(it2-off)->id;
}
if(odd)p-=middle->id;
int q=0;
for(reverse_iterator rit2=i.rend();rit2!=rmiddle;){
--rit2;
q+=rit2->id;
q+=(rit2-off)->id;
}
if(odd)q-=rmiddle->id;
BOOST_CHECK(n==target&&n==m&&n==p&&n==q);
}
template<typename Index>
void test_const_rnd_iterators(const Index& i,int target)
{
typedef typename Index::const_iterator const_iterator;
typedef typename Index::const_reverse_iterator const_reverse_iterator;
typedef typename Index::difference_type difference_type;
BOOST_CHECK(i.cbegin()==i.begin());
BOOST_CHECK(i.cend()==i.end());
BOOST_CHECK(i.crbegin()==i.rbegin());
BOOST_CHECK(i.crend()==i.rend());
const_iterator middle=i.begin()+(i.end()-i.begin())/2;
difference_type off=middle-i.begin();
const_reverse_iterator rmiddle=i.rbegin()+off;
bool odd=((i.end()-i.begin())%2)!=0;
int n=0;
for(const_iterator it=i.begin();it!=middle;++it){
BOOST_CHECK(i.iterator_to(*it)==it);
n+=it->id;
n+=it[off].id;
}
if(odd)n+=(boost::prior(i.end()))->id;
int m=0;
for(const_reverse_iterator rit=i.rbegin();rit!=rmiddle;++rit){
m+=rit->id;
m+=(rit+off)->id;
}
if(odd)m+=(boost::prior(i.rend()))->id;
int p=0;
for(const_iterator it2=i.end();it2!=middle;){
--it2;
p+=it2->id;
p+=(it2-off)->id;
}
if(odd)p-=middle->id;
int q=0;
for(const_reverse_iterator rit2=i.rend();rit2!=rmiddle;){
--rit2;
q+=rit2->id;
q+=(rit2-off)->id;
}
if(odd)q-=rmiddle->id;
BOOST_CHECK(n==target&&n==m&&n==p&&n==q);
}
void test_iterators()
{
employee_set es;
es.insert(employee(0,"Joe",31,1123));
es.insert(employee(1,"Robert",27,5601));
es.insert(employee(2,"John",40,7889));
es.insert(employee(3,"Albert",20,9012));
es.insert(employee(4,"John",57,1002));
int target=0+1+2+3+4;
test_non_const_iterators (es,target);
test_const_iterators (es,target);
test_non_const_hashed_iterators(get<1>(es),target);
test_const_hashed_iterators (get<1>(es),target);
test_non_const_iterators (get<2>(es),target);
test_const_iterators (get<2>(es),target);
test_non_const_iterators (get<3>(es),target);
test_const_iterators (get<3>(es),target);
test_non_const_hashed_iterators(get<4>(es),target);
test_const_hashed_iterators (get<4>(es),target);
test_non_const_rnd_iterators (get<5>(es),target);
test_const_rnd_iterators (get<5>(es),target);
}