• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost.MultiIndex test for iterators.
2  *
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)
7  *
8  * See http://www.boost.org/libs/multi_index for library home page.
9  */
10 
11 #include "test_iterators.hpp"
12 
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>
18 
19 using namespace boost::multi_index;
20 
21 template<typename Index>
test_non_const_iterators(Index & i,int target)22 void test_non_const_iterators(Index& i,int target)
23 {
24   typedef typename Index::iterator         iterator;
25   typedef typename Index::reverse_iterator reverse_iterator;
26 
27   int n=0;
28   for(iterator it=i.begin();it!=i.end();++it){
29     BOOST_TEST(i.iterator_to(*it)==it);
30     n+=it->id;
31   }
32   int m=0;
33   for(reverse_iterator rit=i.rbegin();rit!=i.rend();++rit){
34     m+=rit->id;
35   }
36   int p=0;
37   for(iterator it2=i.end();it2!=i.begin();){
38     --it2;
39     p+=it2->id;
40   }
41   int q=0;
42   for(reverse_iterator rit2=i.rend();rit2!=i.rbegin();){
43     --rit2;
44     q+=rit2->id;
45   }
46 
47   BOOST_TEST(n==target&&n==m&&n==p&&n==q);
48 }
49 
50 template<typename Index>
test_const_iterators(const Index & i,int target)51 void test_const_iterators(const Index& i,int target)
52 {
53   typedef typename Index::const_iterator         const_iterator;
54   typedef typename Index::const_reverse_iterator const_reverse_iterator;
55 
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());
60 
61   int n=0;
62   for(const_iterator it=i.begin();it!=i.end();++it){
63     BOOST_TEST(i.iterator_to(*it)==it);
64     n+=it->id;
65   }
66   int m=0;
67   for(const_reverse_iterator rit=i.rbegin();rit!=i.rend();++rit){
68     m+=rit->id;
69   }
70   int p=0;
71   for(const_iterator it2=i.end();it2!=i.begin();){
72     --it2;
73     p+=it2->id;
74   }
75   int q=0;
76   for(const_reverse_iterator rit2=i.rend();rit2!=i.rbegin();){
77     --rit2;
78     q+=rit2->id;
79   }
80 
81   BOOST_TEST(n==target&&n==m&&n==p&&n==q);
82 }
83 
84 template<typename Index>
test_non_const_hashed_iterators(Index & i,int target)85 void test_non_const_hashed_iterators(Index& i,int target)
86 {
87   typedef typename Index::iterator       iterator;
88   typedef typename Index::local_iterator local_iterator;
89   typedef typename Index::size_type      size_type;
90 
91   int n=0;
92   for(iterator it=i.begin();it!=i.end();++it){
93     BOOST_TEST(i.iterator_to(*it)==it);
94     n+=it->id;
95   }
96   int m=0;
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);
100       m+=it->id;
101     }
102   }
103 
104   BOOST_TEST(n==target&&n==m);
105 }
106 
107 template<typename Index>
test_const_hashed_iterators(const Index & i,int target)108 void test_const_hashed_iterators(const Index& i,int target)
109 {
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;
113 
114   BOOST_TEST(i.cbegin()==i.begin());
115   BOOST_TEST(i.cend()==i.end());
116 
117   int n=0;
118   for(const_iterator it=i.begin();it!=i.end();++it){
119     BOOST_TEST(i.iterator_to(*it)==it);
120     n+=it->id;
121   }
122   int m=0;
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);
128       m+=it->id;
129     }
130   }
131 
132   BOOST_TEST(n==target&&n==m);
133 }
134 
135 template<typename Index>
test_non_const_rnd_iterators(Index & i,int target)136 void test_non_const_rnd_iterators(Index& i,int target)
137 {
138   typedef typename Index::iterator         iterator;
139   typedef typename Index::reverse_iterator reverse_iterator;
140   typedef typename Index::difference_type  difference_type;
141 
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;
146 
147   int n=0;
148   for(iterator it=i.begin();it!=middle;++it){
149     BOOST_TEST(i.iterator_to(*it)==it);
150     n+=it->id;
151     n+=it[off].id;
152   }
153   if(odd)n+=(boost::prior(i.end()))->id;
154   int m=0;
155   for(reverse_iterator rit=i.rbegin();rit!=rmiddle;++rit){
156     m+=rit->id;
157     m+=(rit+off)->id;
158   }
159   if(odd)m+=(boost::prior(i.rend()))->id;
160   int p=0;
161   for(iterator it2=i.end();it2!=middle;){
162     --it2;
163     p+=it2->id;
164     p+=(it2-off)->id;
165   }
166   if(odd)p-=middle->id;
167   int q=0;
168   for(reverse_iterator rit2=i.rend();rit2!=rmiddle;){
169     --rit2;
170     q+=rit2->id;
171     q+=(rit2-off)->id;
172   }
173   if(odd)q-=rmiddle->id;
174 
175   BOOST_TEST(n==target&&n==m&&n==p&&n==q);
176 }
177 
178 template<typename Index>
test_const_rnd_iterators(const Index & i,int target)179 void test_const_rnd_iterators(const Index& i,int target)
180 {
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;
184 
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());
189 
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;
194 
195   int n=0;
196   for(const_iterator it=i.begin();it!=middle;++it){
197     BOOST_TEST(i.iterator_to(*it)==it);
198     n+=it->id;
199     n+=it[off].id;
200   }
201   if(odd)n+=(boost::prior(i.end()))->id;
202   int m=0;
203   for(const_reverse_iterator rit=i.rbegin();rit!=rmiddle;++rit){
204     m+=rit->id;
205     m+=(rit+off)->id;
206   }
207   if(odd)m+=(boost::prior(i.rend()))->id;
208   int p=0;
209   for(const_iterator it2=i.end();it2!=middle;){
210     --it2;
211     p+=it2->id;
212     p+=(it2-off)->id;
213   }
214   if(odd)p-=middle->id;
215   int q=0;
216   for(const_reverse_iterator rit2=i.rend();rit2!=rmiddle;){
217     --rit2;
218     q+=rit2->id;
219     q+=(rit2-off)->id;
220   }
221   if(odd)q-=rmiddle->id;
222 
223   BOOST_TEST(n==target&&n==m&&n==p&&n==q);
224 }
225 
test_iterators()226 void test_iterators()
227 {
228   employee_set es;
229 
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));
235 
236   int target=0+1+2+3+4;
237 
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);
250 }
251