| 1 |
|
|---|
| 2 |
|
|---|
| 3 | #include <string>
|
|---|
| 4 | #include <boost/multi_index_container.hpp>
|
|---|
| 5 | #include <boost/multi_index/hashed_index.hpp>
|
|---|
| 6 | #include <boost/multi_index/ordered_index.hpp>
|
|---|
| 7 | #include <boost/multi_index/identity.hpp>
|
|---|
| 8 | #include <boost/multi_index/member.hpp>
|
|---|
| 9 |
|
|---|
| 10 | using boost::multi_index_container;
|
|---|
| 11 | using namespace boost::multi_index;
|
|---|
| 12 |
|
|---|
| 13 | struct employee
|
|---|
| 14 | {
|
|---|
| 15 | int id;
|
|---|
| 16 | std::string name;
|
|---|
| 17 | int ssnumber;
|
|---|
| 18 |
|
|---|
| 19 | employee(int id,const std::string& name,int ssnumber):
|
|---|
| 20 | id(id),name(name),ssnumber(ssnumber){}
|
|---|
| 21 |
|
|---|
| 22 | bool operator<(const employee& e)const{return id<e.id;}
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | struct ustring_hasher {
|
|---|
| 26 |
|
|---|
| 27 | std::size_t operator ()(std::string const& wstr) const {
|
|---|
| 28 | boost::hash<std::string> hasher;
|
|---|
| 29 | return hasher(wstr);
|
|---|
| 30 | }
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | struct name{} ; // just for boost tags
|
|---|
| 34 |
|
|---|
| 35 | typedef multi_index_container<
|
|---|
| 36 | employee,
|
|---|
| 37 | indexed_by<
|
|---|
| 38 | // hashed on name
|
|---|
| 39 | hashed_unique<tag<name>, member<employee,std::string,&employee::name>, ustring_hasher >
|
|---|
| 40 | >
|
|---|
| 41 | > employee_set;
|
|---|
| 42 |
|
|---|
| 43 | typedef employee_set::index<name>::type employee_set_by_name;
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | #define StrConst1 "Foobar!@##$%%^&Yavapai@QuuxQUUX@@27"
|
|---|
| 47 | #define StrConst2 "CabbagesAndTurnips$haveDrivenMeAway"
|
|---|
| 48 |
|
|---|
| 49 | class UT_Boost {
|
|---|
| 50 | public:
|
|---|
| 51 | virtual void Run() { testBasic(); }
|
|---|
| 52 |
|
|---|
| 53 | protected:
|
|---|
| 54 | void testBasic() {
|
|---|
| 55 | employee_set testSet;
|
|---|
| 56 | std::string s(StrConst1);
|
|---|
| 57 | employee e1(1, s, 100);
|
|---|
| 58 | testSet.insert(e1);
|
|---|
| 59 |
|
|---|
| 60 | s = std::string(StrConst2);
|
|---|
| 61 | employee e2(2, s, 101);
|
|---|
| 62 | testSet.insert(e2);
|
|---|
| 63 |
|
|---|
| 64 | employee_set_by_name& name_index = testSet.get<name>();
|
|---|
| 65 |
|
|---|
| 66 | s = std::string(StrConst1);
|
|---|
| 67 |
|
|---|
| 68 | employee_set::index_iterator<name>::type it = name_index.find(s);
|
|---|
| 69 | BT_TEST_CONDITION(it != testSet.end());
|
|---|
| 70 | BT_TEST_CONDITION(it->id == 1);
|
|---|
| 71 |
|
|---|
| 72 | s = std::string("Hello There");
|
|---|
| 73 | it = name_index.find(s);
|
|---|
| 74 | BT_TEST_CONDITION(it == testSet.end());
|
|---|
| 75 |
|
|---|
| 76 | }
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|