1 /* Boost.MultiIndex test for special set operations.
2 *
3 * Copyright 2003-2015 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_special_set_ops.hpp"
12
13 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
14 #include <algorithm>
15 #include <sstream>
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
string_to_int(const std::string & str)22 static int string_to_int(const std::string& str)
23 {
24 std::istringstream iss(str);
25 int res;
26 iss>>res;
27 return res;
28 }
29
30 struct comp_int_string
31 {
operator ()comp_int_string32 bool operator()(int x,const std::string& y)const
33 {
34 return x<string_to_int(y);
35 }
36
operator ()comp_int_string37 bool operator()(const std::string& x,int y)const
38 {
39 return string_to_int(x)<y;
40 }
41 };
42
43 struct hash_string_as_int
44 {
operator ()hash_string_as_int45 int operator()(const std::string& x)const
46 {
47 return static_cast<int>(boost::hash<int>()(string_to_int(x)));
48 }
49 };
50
51 struct eq_string_int
52 {
operator ()eq_string_int53 bool operator()(int x,const std::string& y)const
54 {
55 return x==string_to_int(y);
56 }
57
operator ()eq_string_int58 bool operator()(const std::string& x,int y)const
59 {
60 return operator()(y,x);
61 }
62 };
63
test_special_set_ops()64 void test_special_set_ops()
65 {
66 employee_set es;
67
68 es.insert(employee(0,"Joe",31,1123));
69 es.insert(employee(1,"Robert",27,5601));
70 es.insert(employee(2,"John",40,7889));
71 es.insert(employee(3,"Albert",20,9012));
72 es.insert(employee(4,"John",57,1002));
73
74 std::pair<employee_set_by_ssn::iterator,employee_set_by_ssn::iterator> p=
75 get<ssn>(es).equal_range(
76 "7889",hash_string_as_int(),eq_string_int());
77
78 BOOST_TEST(std::distance(p.first,p.second)==1&&(p.first)->id==2);
79
80 BOOST_TEST(
81 get<ssn>(es).count(
82 "5601",hash_string_as_int(),eq_string_int())==1);
83
84 BOOST_TEST(
85 get<ssn>(es).find(
86 "1123",hash_string_as_int(),eq_string_int())->name=="Joe");
87
88 BOOST_TEST(
89 std::distance(
90 get<age>(es).lower_bound("27",comp_int_string()),
91 get<age>(es).upper_bound("40",comp_int_string()))==3);
92
93 BOOST_TEST(es.count(2,employee::comp_id())==1);
94 BOOST_TEST(es.count(5,employee::comp_id())==0);
95 }
96