1// Boost.Assign library
2//
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)
7//
8// For more information, see http://www.boost.org/libs/assign/
9//
10
11
12#include <boost/detail/workaround.hpp>
13
14#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
15# pragma warn -8091 // supress warning in Boost.Test
16# pragma warn -8057 // unused argument argc/argv in Boost.Test
17#endif
18
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>
28#include <cstddef>
29#include <ostream>
30#include <string>
31
32using namespace boost;
33using namespace boost::multi_index;
34namespace ba = boost::assign;
35
36//
37// Define a classical multi_index_container for employees
38//
39struct employee
40{
41 int id;
42 std::string name;
43 int age;
44
45 employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){}
46
47 bool operator==(const employee& x)const
48 {
49 return id==x.id&&name==x.name&&age==x.age;
50 }
51
52 bool operator<(const employee& x)const
53 {
54 return id<x.id;
55 }
56
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);}
61
62 struct comp_id
63 {
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;}
66 };
67
68 friend std::ostream& operator<<(std::ostream& os,const employee& e)
69 {
70 os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
71 return os;
72 }
73};
74
75struct name{};
76struct by_name{};
77struct age{};
78struct as_inserted{};
79
80typedef
81 multi_index_container<
82 employee,
83 indexed_by<
84 ordered_unique<
85 identity<employee> >,
86 ordered_non_unique<
87 tag<name,by_name>,
88 BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
89 ordered_non_unique<
90 tag<age>,
91 BOOST_MULTI_INDEX_MEMBER(employee,int,age)>,
92 sequenced<
93 tag<as_inserted> > > >
94 employee_set;
95
96#if defined(BOOST_NO_MEMBER_TEMPLATES)
97typedef nth_index<
98 employee_set,1>::type employee_set_by_name;
99#else
100typedef employee_set::nth_index<1>::type employee_set_by_name;
101#endif
102
103typedef boost::multi_index::index<
104 employee_set,age>::type employee_set_by_age;
105typedef boost::multi_index::index<
106 employee_set,as_inserted>::type employee_set_as_inserted;
107
108//
109// Define a multi_index_container with a list-like index and an ordered index
110//
111typedef multi_index_container<
112 std::string,
113 indexed_by<
114 sequenced<>, // list-like index
115 ordered_non_unique<identity<std::string> > // words by alphabetical order
116 >
117> text_container;
118
119
120
121void test_multi_index_container()
122{
123 employee_set eset = ba::list_of< employee >(u: 1,u0: "Franz",u1: 30)(2,"Hanz",40)(3,"Ilse",50);
124 BOOST_CHECK( eset.size() == 3u );
125
126 //
127 // This container is associative, hence we can use 'insert()'
128 //
129
130 ba::insert( c&: eset )(4,"Kurt",55)(5,"Bjarne",77)(7,"Thorsten",24);
131 BOOST_CHECK( eset.size() == 6u );
132
133 employee_set_by_name& name_index = boost::multi_index::get<name>(m&: eset);
134 employee_set_by_name::iterator i = name_index.find(x: "Ilse");
135 BOOST_CHECK( i->id == 3 );
136 BOOST_CHECK( i->age == 50 );
137
138 text_container text = ba::list_of< std::string >(t: "Have")("you")("ever")("wondered")("how")("much")("Boost")("rocks?!");
139 BOOST_CHECK_EQUAL( text.size(), 8u );
140 BOOST_CHECK_EQUAL( *text.begin(), "Have" );
141
142 //
143 // This container is a sequence, hence we can use 'push_back()' and 'push_font()'
144 //
145
146 ba::push_back( c&: text )("Well")(",")("A")("LOT")(",")("obviously!");
147 BOOST_CHECK_EQUAL( text.size(), 14u );
148 BOOST_CHECK_EQUAL( *--text.end(), "obviously!" );
149
150 ba::push_front( c&: text ) = "question:", "simple", "A";
151 BOOST_CHECK_EQUAL( text.size(), 17u );
152 BOOST_CHECK_EQUAL( text.front(), "A" );
153}
154
155
156
157using boost::unit_test::test_suite;
158
159test_suite* init_unit_test_suite( int argc, char* argv[] )
160{
161 test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
162
163 test->add( BOOST_TEST_CASE( &test_multi_index_container ) );
164
165 return test;
166}
167
168
169

source code of boost/libs/assign/test/multi_index_container.cpp