| 1 | /* Boost.MultiIndex basic test. |
|---|
| 2 | * |
|---|
| 3 | * Copyright 2003-2005 Joaquín M López Muñoz. |
|---|
| 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_basic.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/test/test_tools.hpp> |
|---|
| 19 | |
|---|
| 20 | using namespace boost::multi_index; |
|---|
| 21 | |
|---|
| 22 | struct less_by_employee_age |
|---|
| 23 | { |
|---|
| 24 | bool operator()(const employee& e1,const employee& e2)const |
|---|
| 25 | { |
|---|
| 26 | return e1.age<e2.age; |
|---|
| 27 | } |
|---|
| 28 | }; |
|---|
| 29 | |
|---|
| 30 | void test_basic() |
|---|
| 31 | { |
|---|
| 32 | employee_set es; |
|---|
| 33 | std::vector<employee> v; |
|---|
| 34 | |
|---|
| 35 | #if defined(BOOST_NO_MEMBER_TEMPLATES) |
|---|
| 36 | employee_set_by_name& i1=get<by_name>(es); |
|---|
| 37 | #else |
|---|
| 38 | employee_set_by_name& i1=es.get<by_name>(); |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | const employee_set_by_age& i2=get<2>(es); |
|---|
| 42 | employee_set_as_inserted& i3=get<3>(es); |
|---|
| 43 | employee_set_by_ssn& i4=get<ssn>(es); |
|---|
| 44 | |
|---|
| 45 | es.insert(employee(0,"Joe",31,1123)); |
|---|
| 46 | es.insert(employee(5,"Anna",41,1123)); /* clash*/ |
|---|
| 47 | i1.insert(employee(1,"Robert",27,5601)); |
|---|
| 48 | es.insert(employee(2,"John",40,7889)); |
|---|
| 49 | i3.push_back(employee(3,"Albert",20,9012)); |
|---|
| 50 | i4.insert(employee(4,"John",57,1002)); |
|---|
| 51 | i4.insert(employee(0,"Andrew",60,2302)); /* clash */ |
|---|
| 52 | |
|---|
| 53 | v.push_back(employee(0,"Joe",31,1123)); |
|---|
| 54 | v.push_back(employee(1,"Robert",27,5601)); |
|---|
| 55 | v.push_back(employee(2,"John",40,7889)); |
|---|
| 56 | v.push_back(employee(3,"Albert",20,9012)); |
|---|
| 57 | v.push_back(employee(4,"John",57,1002)); |
|---|
| 58 | |
|---|
| 59 | { |
|---|
| 60 | /* by insertion order */ |
|---|
| 61 | |
|---|
| 62 | BOOST_CHECK(std::equal(i3.begin(),i3.end(),v.begin())); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | { |
|---|
| 66 | /* by id */ |
|---|
| 67 | |
|---|
| 68 | std::sort(v.begin(),v.end()); |
|---|
| 69 | BOOST_CHECK(std::equal(es.begin(),es.end(),v.begin())); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | { |
|---|
| 73 | /* by age */ |
|---|
| 74 | |
|---|
| 75 | std::sort(v.begin(),v.end(),less_by_employee_age()); |
|---|
| 76 | BOOST_CHECK(std::equal(i2.begin(),i2.end(),v.begin())); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | } |
|---|