1 /* Boost.MultiIndex test for iterators.
3 * Copyright 2003-2013 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org/libs/multi_index for library home page.
11 #include "test_iterators.hpp"
13 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
14 #include "pre_multi_index.hpp"
15 #include "employee.hpp"
16 #include <boost/detail/lightweight_test.hpp>
17 #include <boost/next_prior.hpp>
19 using namespace boost::multi_index
;
21 template<typename Index
>
22 void test_non_const_iterators(Index
& i
,int target
)
24 typedef typename
Index::iterator iterator
;
25 typedef typename
Index::reverse_iterator reverse_iterator
;
28 for(iterator it
=i
.begin();it
!=i
.end();++it
){
29 BOOST_TEST(i
.iterator_to(*it
)==it
);
33 for(reverse_iterator rit
=i
.rbegin();rit
!=i
.rend();++rit
){
37 for(iterator it2
=i
.end();it2
!=i
.begin();){
42 for(reverse_iterator rit2
=i
.rend();rit2
!=i
.rbegin();){
47 BOOST_TEST(n
==target
&&n
==m
&&n
==p
&&n
==q
);
50 template<typename Index
>
51 void test_const_iterators(const Index
& i
,int target
)
53 typedef typename
Index::const_iterator const_iterator
;
54 typedef typename
Index::const_reverse_iterator const_reverse_iterator
;
56 BOOST_TEST(i
.cbegin()==i
.begin());
57 BOOST_TEST(i
.cend()==i
.end());
58 BOOST_TEST(i
.crbegin()==i
.rbegin());
59 BOOST_TEST(i
.crend()==i
.rend());
62 for(const_iterator it
=i
.begin();it
!=i
.end();++it
){
63 BOOST_TEST(i
.iterator_to(*it
)==it
);
67 for(const_reverse_iterator rit
=i
.rbegin();rit
!=i
.rend();++rit
){
71 for(const_iterator it2
=i
.end();it2
!=i
.begin();){
76 for(const_reverse_iterator rit2
=i
.rend();rit2
!=i
.rbegin();){
81 BOOST_TEST(n
==target
&&n
==m
&&n
==p
&&n
==q
);
84 template<typename Index
>
85 void test_non_const_hashed_iterators(Index
& i
,int target
)
87 typedef typename
Index::iterator iterator
;
88 typedef typename
Index::local_iterator local_iterator
;
89 typedef typename
Index::size_type size_type
;
92 for(iterator it
=i
.begin();it
!=i
.end();++it
){
93 BOOST_TEST(i
.iterator_to(*it
)==it
);
97 for(size_type buc
=0;buc
<i
.bucket_count();++buc
){
98 for(local_iterator it
=i
.begin(buc
);it
!=i
.end(buc
);++it
){
99 BOOST_TEST(i
.local_iterator_to(*it
)==it
);
104 BOOST_TEST(n
==target
&&n
==m
);
107 template<typename Index
>
108 void test_const_hashed_iterators(const Index
& i
,int target
)
110 typedef typename
Index::const_iterator const_iterator
;
111 typedef typename
Index::const_local_iterator const_local_iterator
;
112 typedef typename
Index::size_type size_type
;
114 BOOST_TEST(i
.cbegin()==i
.begin());
115 BOOST_TEST(i
.cend()==i
.end());
118 for(const_iterator it
=i
.begin();it
!=i
.end();++it
){
119 BOOST_TEST(i
.iterator_to(*it
)==it
);
123 for(size_type buc
=0;buc
<i
.bucket_count();++buc
){
124 BOOST_TEST(i
.cbegin(buc
)==i
.begin(buc
));
125 BOOST_TEST(i
.cend(buc
)==i
.end(buc
));
126 for(const_local_iterator it
=i
.begin(buc
);it
!=i
.end(buc
);++it
){
127 BOOST_TEST(i
.local_iterator_to(*it
)==it
);
132 BOOST_TEST(n
==target
&&n
==m
);
135 template<typename Index
>
136 void test_non_const_rnd_iterators(Index
& i
,int target
)
138 typedef typename
Index::iterator iterator
;
139 typedef typename
Index::reverse_iterator reverse_iterator
;
140 typedef typename
Index::difference_type difference_type
;
142 iterator middle
=i
.begin()+(i
.end()-i
.begin())/2;
143 difference_type off
=middle
-i
.begin();
144 reverse_iterator rmiddle
=i
.rbegin()+off
;
145 bool odd
=((i
.end()-i
.begin())%2)!=0;
148 for(iterator it
=i
.begin();it
!=middle
;++it
){
149 BOOST_TEST(i
.iterator_to(*it
)==it
);
153 if(odd
)n
+=(boost::prior(i
.end()))->id
;
155 for(reverse_iterator rit
=i
.rbegin();rit
!=rmiddle
;++rit
){
159 if(odd
)m
+=(boost::prior(i
.rend()))->id
;
161 for(iterator it2
=i
.end();it2
!=middle
;){
166 if(odd
)p
-=middle
->id
;
168 for(reverse_iterator rit2
=i
.rend();rit2
!=rmiddle
;){
173 if(odd
)q
-=rmiddle
->id
;
175 BOOST_TEST(n
==target
&&n
==m
&&n
==p
&&n
==q
);
178 template<typename Index
>
179 void test_const_rnd_iterators(const Index
& i
,int target
)
181 typedef typename
Index::const_iterator const_iterator
;
182 typedef typename
Index::const_reverse_iterator const_reverse_iterator
;
183 typedef typename
Index::difference_type difference_type
;
185 BOOST_TEST(i
.cbegin()==i
.begin());
186 BOOST_TEST(i
.cend()==i
.end());
187 BOOST_TEST(i
.crbegin()==i
.rbegin());
188 BOOST_TEST(i
.crend()==i
.rend());
190 const_iterator middle
=i
.begin()+(i
.end()-i
.begin())/2;
191 difference_type off
=middle
-i
.begin();
192 const_reverse_iterator rmiddle
=i
.rbegin()+off
;
193 bool odd
=((i
.end()-i
.begin())%2)!=0;
196 for(const_iterator it
=i
.begin();it
!=middle
;++it
){
197 BOOST_TEST(i
.iterator_to(*it
)==it
);
201 if(odd
)n
+=(boost::prior(i
.end()))->id
;
203 for(const_reverse_iterator rit
=i
.rbegin();rit
!=rmiddle
;++rit
){
207 if(odd
)m
+=(boost::prior(i
.rend()))->id
;
209 for(const_iterator it2
=i
.end();it2
!=middle
;){
214 if(odd
)p
-=middle
->id
;
216 for(const_reverse_iterator rit2
=i
.rend();rit2
!=rmiddle
;){
221 if(odd
)q
-=rmiddle
->id
;
223 BOOST_TEST(n
==target
&&n
==m
&&n
==p
&&n
==q
);
226 void test_iterators()
230 es
.insert(employee(0,"Joe",31,1123));
231 es
.insert(employee(1,"Robert",27,5601));
232 es
.insert(employee(2,"John",40,7889));
233 es
.insert(employee(3,"Albert",20,9012));
234 es
.insert(employee(4,"John",57,1002));
236 int target
=0+1+2+3+4;
238 test_non_const_iterators (es
,target
);
239 test_const_iterators (es
,target
);
240 test_non_const_hashed_iterators(get
<1>(es
),target
);
241 test_const_hashed_iterators (get
<1>(es
),target
);
242 test_non_const_iterators (get
<2>(es
),target
);
243 test_const_iterators (get
<2>(es
),target
);
244 test_non_const_iterators (get
<3>(es
),target
);
245 test_const_iterators (get
<3>(es
),target
);
246 test_non_const_hashed_iterators(get
<4>(es
),target
);
247 test_const_hashed_iterators (get
<4>(es
),target
);
248 test_non_const_rnd_iterators (get
<5>(es
),target
);
249 test_const_rnd_iterators (get
<5>(es
),target
);