• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost.MultiIndex test for standard set operations.
2  *
3  * Copyright 2003-2014 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_set_ops.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 type1{};
23 
24 struct type2
25 {
26 private:
operator type1type227   operator type1()const{return type1();}
28 };
29 
30 struct type3
31 {
operator type1type332   operator type1()const{return type1();}
33 };
34 
35 struct less_type12
36 {
operator ()less_type1237   bool operator()(type1,type1)const{return false;}
operator ()less_type1238   bool operator()(type1,type2)const{return false;}
operator ()less_type1239   bool operator()(type2,type1)const{return false;}
40 };
41 
less_type1_f(type1,type1)42 bool less_type1_f(type1,type1){return false;}
43 
44 struct hash_type12
45 {
operator ()hash_type1246   std::size_t operator()(type1)const{return 0;}
operator ()hash_type1247   std::size_t operator()(type2)const{return 0;}
48 };
49 
50 struct eq_type12
51 {
operator ()eq_type1252   bool operator()(type1,type1)const{return true;}
operator ()eq_type1253   bool operator()(type1,type2)const{return true;}
operator ()eq_type1254   bool operator()(type2,type1)const{return true;}
55 };
56 
test_set_ops()57 void test_set_ops()
58 {
59   employee_set               es;
60   employee_set_by_name&      i1=get<by_name>(es);
61   const employee_set_by_age& i2=get<age>(es);
62   employee_set_by_ssn&       i4=get<ssn>(es);
63 
64   es.insert(employee(0,"Joe",31,1123));
65   es.insert(employee(1,"Robert",27,5601));
66   es.insert(employee(2,"John",40,7889));
67   es.insert(employee(3,"Albert",20,9012));
68   es.insert(employee(4,"John",57,1002));
69 
70   BOOST_TEST(i1.find("John")->name=="John");
71   BOOST_TEST(i2.find(41)==i2.end());
72   BOOST_TEST(i4.find(5601)->name=="Robert");
73 
74   BOOST_TEST(i1.count("John")==2);
75   BOOST_TEST(es.count(employee(10,"",-1,0))==0);
76   BOOST_TEST(i4.count(7881)==0);
77 
78   BOOST_TEST(
79     std::distance(
80       i2.lower_bound(31),
81       i2.upper_bound(60))==3);
82 
83   std::pair<employee_set_by_name::iterator,employee_set_by_name::iterator> p=
84     i1.equal_range("John");
85   BOOST_TEST(std::distance(p.first,p.second)==2);
86 
87   p=i1.equal_range("Serena");
88   BOOST_TEST(p.first==i1.end()&&p.second==i1.end());
89 
90   std::pair<employee_set_by_age::iterator,employee_set_by_age::iterator> p2=
91     i2.equal_range(30);
92   BOOST_TEST(p2.first==p2.second&&p2.first->age==31);
93 
94   /* check promotion detection plays nice with private conversion */
95 
96   multi_index_container<
97     type1,
98     indexed_by<
99       ordered_unique<identity<type1>,less_type12>,
100       hashed_unique<identity<type1>,hash_type12,eq_type12>
101     >
102   > c;
103   c.insert(type1());
104 
105   BOOST_TEST(c.find(type2())==c.begin());
106   BOOST_TEST(c.count(type2())==1);
107   BOOST_TEST(c.lower_bound(type2())==c.begin());
108   BOOST_TEST(c.upper_bound(type2())==c.end());
109   BOOST_TEST(c.equal_range(type2())==std::make_pair(c.begin(),c.end()));
110 
111   BOOST_TEST(c.get<1>().find(type2())==c.get<1>().begin());
112   BOOST_TEST(c.get<1>().count(type2())==1);
113   BOOST_TEST(c.get<1>().equal_range(type2())==
114              std::make_pair(c.get<1>().begin(),c.get<1>().end()));
115 
116   /* check promotion detection does not break with functions */
117 
118   multi_index_container<
119     type1,
120     indexed_by<
121       ordered_unique<identity<type1>,bool(*)(type1,type1)>
122     >
123   > c2(boost::make_tuple(boost::make_tuple(identity<type1>(),&less_type1_f)));
124   c2.insert(type1());
125 
126   BOOST_TEST(c2.find(type3())==c2.begin());
127 }
128