multi_index/test/test_modifiers.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

317 lines
8.6 KiB
C++

/* Boost.MultiIndex test for modifier memfuns.
*
* 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_modifiers.hpp"
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <boost/next_prior.hpp>
#include <iterator>
#include <vector>
#include "pre_multi_index.hpp"
#include "employee.hpp"
#include <boost/next_prior.hpp>
#include <boost/test/test_tools.hpp>
class always_one
{
public:
always_one():n(1){}
~always_one(){n=0;}
int get()const{return n;}
private:
int n;
};
inline bool operator==(const always_one& x,const always_one& y)
{
return x.get()==y.get();
}
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace boost{
#endif
inline std::size_t hash_value(const always_one& x)
{
return static_cast<std::size_t>(x.get());
}
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
} /* namespace boost */
#endif
using namespace boost::multi_index;
void test_modifiers()
{
employee_set es;
employee_set_by_name& i1=get<name>(es);
employee_set_by_age& i2=get<age>(es);
employee_set_as_inserted& i3=get<as_inserted>(es);
employee_set_by_ssn& i4=get<ssn>(es);
employee_set_randomly& i5=get<randomly>(es);
es.insert(employee(0,"Joe",31,1123));
BOOST_CHECK(es.insert(employee(0,"Joe",31,1123)).second==false);
BOOST_CHECK(i1.insert(employee(0,"Joe Jr.",5,2563)).second==false);
BOOST_CHECK(i2.insert(employee(1,"Victor",5,1123)).second==false);
BOOST_CHECK(i3.insert(i3.begin(),employee(1,"Victor",5,1123)).second
==false);
BOOST_CHECK(i3.push_front(employee(0,"Joe Jr.",5,2563)).second==false);
BOOST_CHECK(i3.push_back(employee(0,"Joe Jr.",5,2563)).second==false);
employee_set_by_name::iterator it1=i1.find("Joe");
i1.insert(it1,employee(1,"Joe Jr.",5,2563));
BOOST_CHECK(es.size()==2);
employee_set_by_age::iterator it2=i2.find(31);
i2.insert(it2,employee(2,"Grandda Joe",64,7881));
BOOST_CHECK(es.size()==3);
employee_set_as_inserted::iterator it3=i3.begin();
i3.insert(it3,100,employee(3,"Judy",39,6201));
BOOST_CHECK((--it3)->ssn==6201);
BOOST_CHECK(es.size()==4);
employee_set_randomly::iterator it5=i5.begin();
i5.insert(it5,100,employee(4,"Jill",52,3379));
BOOST_CHECK(i5.begin()->age==52);
BOOST_CHECK(es.size()==5);
es.erase(employee(1,"Joe Jr.",5,2563));
BOOST_CHECK(i3.size()==4&&i5.size()==4);
BOOST_CHECK(i1.erase("Judy")==1);
BOOST_CHECK(es.size()==3&&i2.size()==3);
BOOST_CHECK(i2.erase(it2)->age==52);
BOOST_CHECK(i3.size()==2&&i4.size()==2);
i3.pop_front();
BOOST_CHECK(i1.size()==1&&i2.size()==1);
i5.erase(i5.begin(),i5.end());
BOOST_CHECK(es.size()==0&&i3.size()==0);
es.insert(employee(0,"Joe",31,1123));
BOOST_CHECK(i1.erase(i1.begin())==i1.end());
BOOST_CHECK(i1.size()==0);
es.insert(employee(0,"Joe",31,1123));
es.insert(employee(1,"Jack",31,5032));
BOOST_CHECK(i2.erase(31)==2);
BOOST_CHECK(i2.size()==0);
i3.push_front(employee(1,"Jack",31,5032));
i3.push_back(employee(0,"Joe",31,1123));
BOOST_CHECK(i3.front()==employee(1,"Jack",31,5032));
BOOST_CHECK(i3.back()==employee(0,"Joe",31,1123));
i3.pop_back();
BOOST_CHECK(i3.back()==employee(1,"Jack",31,5032));
BOOST_CHECK(es.size()==1);
i3.pop_front();
BOOST_CHECK(es.size()==0);
i5.push_back(employee(1,"Jack",31,5032));
i5.push_front(employee(0,"Joe",31,1123));
i5.insert(i5.end()-1,employee(2,"Grandda Joe",64,7881));
BOOST_CHECK(i5.back()==employee(1,"Jack",31,5032));
BOOST_CHECK(i5.front()==employee(0,"Joe",31,1123));
BOOST_CHECK(i5[0]==i5.front()&&i5.at(0)==i5.front());
BOOST_CHECK(i5[i5.size()-1]==i5.back()&&i5.at(i5.size()-1)==i5.back());
i5.pop_front();
BOOST_CHECK(i5.back()==employee(1,"Jack",31,5032));
BOOST_CHECK(i5.front()==employee(2,"Grandda Joe",64,7881));
BOOST_CHECK(es.size()==2);
i5.pop_back();
BOOST_CHECK(i5.back()==employee(2,"Grandda Joe",64,7881));
BOOST_CHECK(i5.front()==i5.front());
BOOST_CHECK(es.size()==1);
i5.erase(i5.begin());
BOOST_CHECK(es.size()==0);
std::vector<employee> ve;
ve.push_back(employee(3,"Anna",31,5388));
ve.push_back(employee(1,"Rachel",27,9012));
ve.push_back(employee(2,"Agatha",40,1520));
i1.insert(ve.begin(),ve.end());
BOOST_CHECK(i2.size()==3);
BOOST_CHECK(i2.erase(i2.begin(),i2.end())==i2.end());
BOOST_CHECK(es.size()==0);
i2.insert(ve.begin(),ve.end());
BOOST_CHECK(i3.size()==3);
BOOST_CHECK(*(i3.erase(i3.begin()))==employee(1,"Rachel",27,9012));
BOOST_CHECK(i3.erase(i3.begin(),i3.end())==i3.end());
BOOST_CHECK(es.size()==0);
i3.insert(i3.end(),ve.begin(),ve.end());
BOOST_CHECK(es.size()==3);
BOOST_CHECK(i4.erase(9012)==1);
i4.erase(i4.begin());
BOOST_CHECK(i4.erase(i4.begin(),i4.end())==i4.end());
i4.insert(ve.begin(),ve.end());
BOOST_CHECK(i5.size()==3);
BOOST_CHECK(i5.erase(i5.begin(),i5.end())==i5.end());
BOOST_CHECK(es.size()==0);
i5.insert(i5.begin(),ve.begin(),ve.end());
BOOST_CHECK(i1.size()==3);
BOOST_CHECK(es.erase(es.begin(),es.end())==es.end());
BOOST_CHECK(i2.size()==0);
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));
employee_set es_backup(es);
employee_set es2;
es2.insert(employee(3,"Anna",31,5388));
es2.insert(employee(1,"Rachel",27,9012));
es2.insert(employee(2,"Agatha",40,1520));
employee_set es2_backup(es2);
i1.swap(get<1>(es2));
BOOST_CHECK(es==es2_backup&&es2==es_backup);
i2.swap(get<2>(es2));
BOOST_CHECK(es==es_backup&&es2==es2_backup);
i3.swap(get<3>(es2));
BOOST_CHECK(es==es2_backup&&es2==es_backup);
i4.swap(get<4>(es2));
BOOST_CHECK(es==es_backup&&es2==es2_backup);
i5.swap(get<5>(es2));
BOOST_CHECK(es==es2_backup&&es2==es_backup);
#if defined(BOOST_FUNCTION_SCOPE_USING_DECLARATION_BREAKS_ADL)
::boost::multi_index::detail::swap(i1,get<1>(es2));
#else
using std::swap;
swap(i1,get<1>(es2));
#endif
BOOST_CHECK(es==es_backup&&es2==es2_backup);
#if defined(BOOST_FUNCTION_SCOPE_USING_DECLARATION_BREAKS_ADL)
::boost::multi_index::detail::swap(i2,get<2>(es2));
#else
using std::swap;
swap(i2,get<2>(es2));
#endif
BOOST_CHECK(es==es2_backup&&es2==es_backup);
#if defined(BOOST_FUNCTION_SCOPE_USING_DECLARATION_BREAKS_ADL)
::boost::multi_index::detail::swap(i3,get<3>(es2));
#else
using std::swap;
swap(i3,get<3>(es2));
#endif
BOOST_CHECK(es==es_backup&&es2==es2_backup);
#if defined(BOOST_FUNCTION_SCOPE_USING_DECLARATION_BREAKS_ADL)
::boost::multi_index::detail::swap(i4,get<4>(es2));
#else
using std::swap;
swap(i4,get<4>(es2));
#endif
BOOST_CHECK(es==es2_backup&&es2==es_backup);
#if defined(BOOST_FUNCTION_SCOPE_USING_DECLARATION_BREAKS_ADL)
::boost::multi_index::detail::swap(i5,get<5>(es2));
#else
using std::swap;
swap(i5,get<5>(es2));
#endif
BOOST_CHECK(es==es_backup&&es2==es2_backup);
i3.clear();
BOOST_CHECK(i3.size()==0);
es=es2;
i4.clear();
BOOST_CHECK(i4.size()==0);
es=es2;
i5.clear();
BOOST_CHECK(i5.size()==0);
es2.clear();
BOOST_CHECK(es2.size()==0);
/* testcase for problem reported at
* http://lists.boost.org/boost-users/2006/12/24215.php
*/
multi_index_container<
always_one,
indexed_by<
hashed_non_unique<identity<always_one> >
>
> aoc;
aoc.insert(always_one());
aoc.insert(always_one());
aoc.erase(*(aoc.begin()));
BOOST_CHECK(aoc.empty());
/* Testcases for compliance with "as close to hint as possible"
* proposed behavior for associative containers:
* http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#233
* http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1780.html
*/
typedef multi_index_container<
int,
indexed_by<
ordered_non_unique<identity<int> >
>
> int_non_unique_container;
int_non_unique_container c;
c.insert(0);c.insert(0);
c.insert(1);c.insert(1);
c.insert(2);c.insert(2);
BOOST_CHECK(std::distance(c.begin(),c.insert(c.begin(),1))==2);
BOOST_CHECK(std::distance(c.begin(),c.insert(boost::next(c.begin()),1))==2);
BOOST_CHECK(std::distance(c.begin(),c.insert(c.lower_bound(1),1))==2);
BOOST_CHECK(
std::distance(c.begin(),c.insert(boost::next(c.lower_bound(1)),1))==3);
BOOST_CHECK(std::distance(c.begin(),c.insert(c.upper_bound(1),1))==8);
BOOST_CHECK(std::distance(c.begin(),c.insert(boost::prior(c.end()),1))==9);
BOOST_CHECK(std::distance(c.begin(),c.insert(c.end(),1))==10);
}