1 // Boost.Assign library
3 // Copyright Thorsten Ottosen 2003-2004. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org/libs/assign/
12 #include <boost/detail/workaround.hpp>
14 #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
15 # pragma warn -8091 // suppress warning in Boost.Test
16 # pragma warn -8057 // unused argument argc/argv in Boost.Test
19 #include <boost/assign/list_of.hpp>
20 #include <boost/assign/list_inserter.hpp>
21 #include <boost/multi_index_container.hpp>
22 #include <boost/multi_index/identity.hpp>
23 #include <boost/multi_index/member.hpp>
24 #include <boost/multi_index/ordered_index.hpp>
25 #include <boost/multi_index/sequenced_index.hpp>
26 #include <boost/test/test_tools.hpp>
27 #include <boost/test/unit_test.hpp>
32 using namespace boost
;
33 using namespace boost::multi_index
;
34 namespace ba
= boost::assign
;
37 // Define a classical multi_index_container for employees
45 employee(int id_
,std::string name_
,int age_
):id(id_
),name(name_
),age(age_
){}
47 bool operator==(const employee
& x
)const
49 return id
==x
.id
&&name
==x
.name
&&age
==x
.age
;
52 bool operator<(const employee
& x
)const
57 bool operator!=(const employee
& x
)const{return !(*this==x
);}
58 bool operator> (const employee
& x
)const{return x
<*this;}
59 bool operator>=(const employee
& x
)const{return !(*this<x
);}
60 bool operator<=(const employee
& x
)const{return !(x
<*this);}
64 bool operator()(int x
,const employee
& e2
)const{return x
<e2
.id
;}
65 bool operator()(const employee
& e1
,int x
)const{return e1
.id
<x
;}
68 friend std::ostream
& operator<<(std::ostream
& os
,const employee
& e
)
70 os
<<e
.id
<<" "<<e
.name
<<" "<<e
.age
<<std::endl
;
81 multi_index_container
<
88 BOOST_MULTI_INDEX_MEMBER(employee
,std::string
,name
)>,
91 BOOST_MULTI_INDEX_MEMBER(employee
,int,age
)>,
93 tag
<as_inserted
> > > >
96 #if defined(BOOST_NO_MEMBER_TEMPLATES)
98 employee_set
,1>::type employee_set_by_name
;
100 typedef employee_set::nth_index
<1>::type employee_set_by_name
;
103 typedef boost::multi_index::index
<
104 employee_set
,age
>::type employee_set_by_age
;
105 typedef boost::multi_index::index
<
106 employee_set
,as_inserted
>::type employee_set_as_inserted
;
109 // Define a multi_index_container with a list-like index and an ordered index
111 typedef multi_index_container
<
114 sequenced
<>, // list-like index
115 ordered_non_unique
<identity
<std::string
> > // words by alphabetical order
121 void test_multi_index_container()
123 employee_set eset
= ba::list_of
< employee
>(1,"Franz",30)(2,"Hanz",40)(3,"Ilse",50);
124 BOOST_CHECK( eset
.size() == 3u );
127 // This container is associative, hence we can use 'insert()'
130 ba::insert( eset
)(4,"Kurt",55)(5,"Bjarne",77)(7,"Thorsten",24);
131 BOOST_CHECK( eset
.size() == 6u );
133 employee_set_by_name
& name_index
= boost::multi_index::get
<name
>(eset
);
134 employee_set_by_name::iterator i
= name_index
.find("Ilse");
135 BOOST_CHECK( i
->id
== 3 );
136 BOOST_CHECK( i
->age
== 50 );
138 text_container text
= ba::list_of
< std::string
>("Have")("you")("ever")("wondered")("how")("much")("Boost")("rocks?!");
139 BOOST_CHECK_EQUAL( text
.size(), 8u );
140 BOOST_CHECK_EQUAL( *text
.begin(), "Have" );
143 // This container is a sequence, hence we can use 'push_back()' and 'push_font()'
146 ba::push_back( text
)("Well")(",")("A")("LOT")(",")("obviously!");
147 BOOST_CHECK_EQUAL( text
.size(), 14u );
148 BOOST_CHECK_EQUAL( *--text
.end(), "obviously!" );
150 ba::push_front( text
) = "question:", "simple", "A";
151 BOOST_CHECK_EQUAL( text
.size(), 17u );
152 BOOST_CHECK_EQUAL( text
.front(), "A" );
157 using boost::unit_test::test_suite
;
159 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
161 test_suite
* test
= BOOST_TEST_SUITE( "List Test Suite" );
163 test
->add( BOOST_TEST_CASE( &test_multi_index_container
) );