• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost.MultiIndex test for comparison functions.
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_comparison.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 "pair_of_ints.hpp"
17 #include <boost/detail/lightweight_test.hpp>
18 
19 using namespace boost::multi_index;
20 
21 template<typename Value>
22 struct lookup_list{
23   typedef multi_index_container<
24     Value,
25     indexed_by<
26       sequenced<>,
27       ordered_non_unique<identity<Value> >
28     >
29   > type;
30 };
31 
32 template<typename Value>
33 struct lookup_vector{
34   typedef multi_index_container<
35     Value,
36     indexed_by<
37       random_access<>,
38       ordered_non_unique<identity<Value> >
39     >
40   > type;
41 };
42 
test_comparison()43 void test_comparison()
44 {
45   employee_set              es;
46   employee_set_by_name&     i1=get<1>(es);
47   employee_set_by_age&      i2=get<2>(es);
48   employee_set_as_inserted& i3=get<3>(es);
49   employee_set_by_ssn&      i4=get<4>(es);
50   employee_set_randomly&    i5=get<5>(es);
51   es.insert(employee(0,"Joe",31,1123));
52   es.insert(employee(1,"Robert",27,5601));
53   es.insert(employee(2,"John",40,7889));
54   es.insert(employee(3,"Albert",20,9012));
55   es.insert(employee(4,"John",57,1002));
56 
57   employee_set              es2;
58   employee_set_by_name&     i12=get<by_name>(es2);
59   employee_set_by_age&      i22=get<age>(es2);
60   employee_set_as_inserted& i32=get<3>(es2);
61   employee_set_by_ssn&      i42=get<4>(es2);
62   employee_set_randomly&    i52=get<5>(es2);
63   es2.insert(employee(0,"Joe",31,1123));
64   es2.insert(employee(1,"Robert",27,5601));
65   es2.insert(employee(2,"John",40,7889));
66   es2.insert(employee(3,"Albert",20,9012));
67 
68   BOOST_TEST(es==es&&es<=es&&es>=es&&
69              i12==i12&&
70              i22==i22&&i22<=i22&&i22>=i22&&
71              i32==i32&&i32<=i32&&i32>=i32&&
72              i42==i42&&
73              i52==i52&&i52<=i52&&i52>=i52);
74   BOOST_TEST(es!=es2&&es2<es&&es>es2&&!(es<=es2)&&!(es2>=es));
75   BOOST_TEST(i1!=i12);
76   BOOST_TEST(i2!=i22&&i22<i2&&i2>i22&&!(i2<=i22)&&!(i22>=i2));
77   BOOST_TEST(i3!=i32&&i32<i3&&i3>i32&&!(i3<=i32)&&!(i32>=i3));
78   BOOST_TEST(i4!=i42);
79   BOOST_TEST(i5!=i52&&i52<i5&&i5>i52&&!(i5<=i52)&&!(i52>=i5));
80 
81   multi_index_container<
82     pair_of_ints,
83     indexed_by<
84       hashed_non_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first)>
85     >
86   > hc1,hc2;
87   hc1.insert(pair_of_ints(0,0));
88   hc1.insert(pair_of_ints(0,1));
89   hc1.insert(pair_of_ints(0,2));
90   hc1.insert(pair_of_ints(0,3));
91   hc1.insert(pair_of_ints(1,0));
92   hc1.insert(pair_of_ints(1,1));
93 
94   hc2.insert(pair_of_ints(0,2));
95   hc2.insert(pair_of_ints(0,1));
96   hc2.insert(pair_of_ints(1,1));
97   hc2.insert(pair_of_ints(1,0));
98   hc2.insert(pair_of_ints(0,3));
99   hc2.insert(pair_of_ints(0,0));
100   BOOST_TEST(hc1==hc2);
101 
102   hc1.insert(pair_of_ints(0,4));
103   hc2.insert(pair_of_ints(0,5));
104   BOOST_TEST(hc1!=hc2);
105 
106   lookup_list<int>::type    l1;
107   lookup_list<char>::type   l2;
108   lookup_vector<char>::type l3;
109   lookup_list<long>::type   l4;
110   lookup_vector<long>::type l5;
111 
112   l1.push_back(3);
113   l1.push_back(4);
114   l1.push_back(5);
115   l1.push_back(1);
116   l1.push_back(2);
117 
118   l2.push_back(char(3));
119   l2.push_back(char(4));
120   l2.push_back(char(5));
121   l2.push_back(char(1));
122   l2.push_back(char(2));
123 
124   l3.push_back(char(3));
125   l3.push_back(char(4));
126   l3.push_back(char(5));
127   l3.push_back(char(1));
128   l3.push_back(char(2));
129 
130   l4.push_back(long(3));
131   l4.push_back(long(4));
132   l4.push_back(long(5));
133   l4.push_back(long(1));
134 
135   l5.push_back(long(3));
136   l5.push_back(long(4));
137   l5.push_back(long(5));
138   l5.push_back(long(1));
139 
140   BOOST_TEST(l1==l2&&l1<=l2&&l1>=l2);
141   BOOST_TEST(
142     get<1>(l1)==get<1>(l2)&&get<1>(l1)<=get<1>(l2)&&get<1>(l1)>=get<1>(l2));
143   BOOST_TEST(
144     get<1>(l1)==get<1>(l3)&&get<1>(l1)<=get<1>(l3)&&get<1>(l1)>=get<1>(l3));
145   BOOST_TEST(l1!=l4&&l4<l1&&l1>l4);
146   BOOST_TEST(
147     get<1>(l1)!=get<1>(l4)&&get<1>(l1)<get<1>(l4)&&get<1>(l4)>get<1>(l1));
148   BOOST_TEST(l3!=l5&&l5<l3&&l3>l5);
149   BOOST_TEST(
150     get<1>(l3)!=get<1>(l5)&&get<1>(l3)<get<1>(l5)&&get<1>(l5)>get<1>(l3));
151 }
152