1 | /* Boost.MultiIndex test for special set operations. |
2 | * |
3 | * Copyright 2003-2021 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 | |
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 | { |
32 | bool operator()(int x,const std::string& y)const |
33 | { |
34 | return x<string_to_int(str: y); |
35 | } |
36 | |
37 | bool operator()(const std::string& x,int y)const |
38 | { |
39 | return string_to_int(str: x)<y; |
40 | } |
41 | }; |
42 | |
43 | struct hash_string_as_int |
44 | { |
45 | int operator()(const std::string& x)const |
46 | { |
47 | return static_cast<int>(boost::hash<int>()(string_to_int(str: x))); |
48 | } |
49 | }; |
50 | |
51 | struct eq_string_int |
52 | { |
53 | bool operator()(int x,const std::string& y)const |
54 | { |
55 | return x==string_to_int(str: y); |
56 | } |
57 | |
58 | bool operator()(const std::string& x,int y)const |
59 | { |
60 | return operator()(x: y,y: x); |
61 | } |
62 | }; |
63 | |
64 | void test_special_set_ops() |
65 | { |
66 | employee_set es; |
67 | |
68 | es.insert(x: employee(0,"Joe" ,31,1123)); |
69 | es.insert(x: employee(1,"Robert" ,27,5601)); |
70 | es.insert(x: employee(2,"John" ,40,7889)); |
71 | es.insert(x: employee(3,"Albert" ,20,9012)); |
72 | es.insert(x: employee(4,"John" ,57,1002)); |
73 | |
74 | std::pair<employee_set_by_ssn::iterator,employee_set_by_ssn::iterator> p= |
75 | get<ssn>(m&: es).equal_range( |
76 | k: "7889" ,hash: hash_string_as_int(),eq: 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).contains("5601" ,hash_string_as_int(),eq_string_int())); |
86 | |
87 | BOOST_TEST( |
88 | get<ssn>(es).find( |
89 | "1123" ,hash_string_as_int(),eq_string_int())->name=="Joe" ); |
90 | |
91 | BOOST_TEST( |
92 | std::distance( |
93 | get<age>(es).lower_bound("27" ,comp_int_string()), |
94 | get<age>(es).upper_bound("40" ,comp_int_string()))==3); |
95 | |
96 | BOOST_TEST(es.count(2,employee::comp_id())==1); |
97 | BOOST_TEST(es.count(5,employee::comp_id())==0); |
98 | |
99 | BOOST_TEST(es.contains(2,employee::comp_id())); |
100 | BOOST_TEST(!es.contains(5,employee::comp_id())); |
101 | } |
102 | |