• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost.MultiIndex basic test.
2  *
3  * Copyright 2003-2017 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_basic.hpp"
12 
13 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
14 #include <algorithm>
15 #include <vector>
16 #include "pre_multi_index.hpp"
17 #include "employee.hpp"
18 #include <boost/detail/lightweight_test.hpp>
19 
20 using namespace boost::multi_index;
21 
22 struct less_by_employee_age
23 {
operator ()less_by_employee_age24   bool operator()(const employee& e1,const employee& e2)const
25   {
26     return e1.age<e2.age;
27   }
28 };
29 
30 struct no_addressof_type
31 {
no_addressof_typeno_addressof_type32   no_addressof_type(int n):n(n){}
33 
operator &no_addressof_type34   void operator&()const{}
35 
36   int n;
37 };
38 
operator ==(const no_addressof_type & x,const no_addressof_type & y)39 bool operator==(const no_addressof_type& x,const no_addressof_type& y)
40 {
41   return x.n==y.n;
42 }
43 
operator <(const no_addressof_type & x,const no_addressof_type & y)44 bool operator<(const no_addressof_type& x,const no_addressof_type& y)
45 {
46   return x.n<y.n;
47 }
48 
49 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
50 namespace boost{
51 #endif
52 
hash_value(const no_addressof_type & x)53 inline std::size_t hash_value(const no_addressof_type& x)
54 {
55   boost::hash<int> h;
56   return h(x.n);
57 }
58 
59 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
60 } /* namespace boost */
61 #endif
62 
test_basic()63 void test_basic()
64 {
65   employee_set          es;
66   std::vector<employee> v;
67 
68 #if defined(BOOST_NO_MEMBER_TEMPLATES)
69   employee_set_by_name& i1=get<by_name>(es);
70 #else
71   employee_set_by_name& i1=es.get<by_name>();
72 #endif
73 
74   const employee_set_by_age& i2=get<2>(es);
75   employee_set_as_inserted&  i3=get<3>(es);
76   employee_set_by_ssn&       i4=get<ssn>(es);
77   employee_set_randomly&     i5=get<randomly>(es);
78 
79   es.insert(employee(0,"Joe",31,1123));
80   es.insert(employee(5,"Anna",41,1123));      /* clash*/
81   i1.insert(employee(1,"Robert",27,5601));
82   es.insert(employee(2,"John",40,7889));
83   i3.push_back(employee(3,"Albert",20,9012));
84   i4.insert(employee(4,"John",57,1002));
85   i5.push_back(employee(0,"Andrew",60,2302)); /* clash */
86 
87   v.push_back(employee(0,"Joe",31,1123));
88   v.push_back(employee(1,"Robert",27,5601));
89   v.push_back(employee(2,"John",40,7889));
90   v.push_back(employee(3,"Albert",20,9012));
91   v.push_back(employee(4,"John",57,1002));
92 
93   {
94     /* by insertion order */
95 
96     BOOST_TEST(std::equal(i3.begin(),i3.end(),v.begin()));
97     BOOST_TEST(std::equal(i5.begin(),i5.end(),v.begin()));
98   }
99 
100   {
101     /* by id */
102 
103     std::sort(v.begin(),v.end());
104     BOOST_TEST(std::equal(es.begin(),es.end(),v.begin()));
105   }
106 
107   {
108     /* by age */
109 
110     std::sort(v.begin(),v.end(),less_by_employee_age());
111     BOOST_TEST(std::equal(i2.begin(),i2.end(),v.begin()));
112   }
113 
114   {
115     /* testcase for https://svn.boost.org/trac10/ticket/13307 */
116 
117     typedef multi_index_container<
118       no_addressof_type,
119       indexed_by<
120         random_access<>,
121         ordered_non_unique<identity<no_addressof_type> >,
122         sequenced<>,
123         hashed_non_unique<identity<no_addressof_type> >
124       >
125     > multi_index_t;
126 
127     multi_index_t        c;
128     const multi_index_t& cc=c;
129     no_addressof_type    x(0);
130     int                  a[]={1,2};
131     int                  b[]={6,7};
132     c.push_back(x);
133     c.insert(c.end(),a,a+2);
134     c.push_back(no_addressof_type(3));
135     c.emplace_back(4);
136     c.get<1>().emplace_hint(c.get<1>().begin(),5);
137     c.get<1>().insert(b,b+2);
138     (void)c.begin()->n;
139     (void)c.get<1>().begin()->n;
140     (void)c.get<2>().begin()->n;
141     (void)c.get<3>().begin()->n;
142     (void)c.get<3>().begin(0)->n;
143     (void)c.iterator_to(c.front());
144     (void)cc.iterator_to(c.front());
145     (void)c.get<1>().iterator_to(c.front());
146     (void)cc.get<1>().iterator_to(c.front());
147     (void)c.get<2>().iterator_to(c.front());
148     (void)cc.get<2>().iterator_to(c.front());
149     (void)c.get<3>().iterator_to(c.front());
150     (void)cc.get<3>().iterator_to(c.front());
151     (void)c.get<3>().local_iterator_to(c.front());
152     (void)cc.get<3>().local_iterator_to(c.front());
153     multi_index_t c2=c;(void)c2;
154     c.erase(c.begin());
155     c.erase(c.begin(),c.end());
156   }
157 
158 }
159