| 1 | /* Boost.MultiIndex test for replace(), modify() and modify_key(). |
|---|
| 2 | * |
|---|
| 3 | * Copyright 2003-2006 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_update.hpp" |
|---|
| 12 | |
|---|
| 13 | #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ |
|---|
| 14 | #include <algorithm> |
|---|
| 15 | #include "pre_multi_index.hpp" |
|---|
| 16 | #include "employee.hpp" |
|---|
| 17 | #include "pair_of_ints.hpp" |
|---|
| 18 | #include <boost/test/test_tools.hpp> |
|---|
| 19 | |
|---|
| 20 | using namespace boost::multi_index; |
|---|
| 21 | |
|---|
| 22 | void test_update() |
|---|
| 23 | { |
|---|
| 24 | employee_set es; |
|---|
| 25 | employee_set_as_inserted& i=get<as_inserted>(es); |
|---|
| 26 | employee_set_randomly& r=get<randomly>(es); |
|---|
| 27 | |
|---|
| 28 | es.insert(employee(0,"Joe",31,1123)); |
|---|
| 29 | es.insert(employee(1,"Robert",27,5601)); |
|---|
| 30 | es.insert(employee(2,"John",40,7889)); |
|---|
| 31 | es.insert(employee(3,"Olbert",20,9012)); |
|---|
| 32 | es.insert(employee(4,"John",57,1002)); |
|---|
| 33 | |
|---|
| 34 | employee_set::iterator it=es.find(employee(0,"Joe",31,1123)); |
|---|
| 35 | employee_set_as_inserted::iterator it1= |
|---|
| 36 | project<as_inserted>(es,get<name>(es).find("Olbert")); |
|---|
| 37 | employee_set_randomly::iterator it2= |
|---|
| 38 | project<randomly>(es,get<age>(es).find(57)); |
|---|
| 39 | |
|---|
| 40 | BOOST_CHECK(es.replace(it,*it)); |
|---|
| 41 | BOOST_CHECK(!es.replace(it,employee(3,"Joe",31,1123))&&it->id==0); |
|---|
| 42 | BOOST_CHECK(es.replace(it,employee(0,"Joe",32,1123))&&it->age==32); |
|---|
| 43 | BOOST_CHECK(i.replace(it1,employee(3,"Albert",20,9012))&&it1->name== |
|---|
| 44 | "Albert"); |
|---|
| 45 | BOOST_CHECK(!r.replace(it2,employee(4,"John",57,5601))); |
|---|
| 46 | |
|---|
| 47 | { |
|---|
| 48 | typedef multi_index_container< |
|---|
| 49 | pair_of_ints, |
|---|
| 50 | indexed_by< |
|---|
| 51 | ordered_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first)>, |
|---|
| 52 | hashed_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,second)>, |
|---|
| 53 | sequenced<> > > |
|---|
| 54 | int_int_set; |
|---|
| 55 | |
|---|
| 56 | int_int_set iis; |
|---|
| 57 | nth_index<int_int_set,1>::type& ii1=get<1>(iis); |
|---|
| 58 | nth_index<int_int_set,2>::type& ii2=get<2>(iis); |
|---|
| 59 | iis.insert(pair_of_ints(0,0)); |
|---|
| 60 | iis.insert(pair_of_ints(5,5)); |
|---|
| 61 | iis.insert(pair_of_ints(10,10)); |
|---|
| 62 | |
|---|
| 63 | BOOST_CHECK(!iis.replace(iis.begin(),pair_of_ints(5,0))); |
|---|
| 64 | BOOST_CHECK(!ii2.replace(ii2.begin(),pair_of_ints(0,5))); |
|---|
| 65 | BOOST_CHECK(!ii1.replace(project<1>(iis,iis.begin()),pair_of_ints(5,11))); |
|---|
| 66 | BOOST_CHECK(!iis.replace(iis.begin(),pair_of_ints(11,5))); |
|---|
| 67 | BOOST_CHECK(!iis.replace(++iis.begin(),pair_of_ints(10,5))); |
|---|
| 68 | BOOST_CHECK(!ii1.replace( |
|---|
| 69 | project<1>(iis,++iis.begin()),pair_of_ints(5,10))); |
|---|
| 70 | BOOST_CHECK(!iis.replace(--iis.end(),pair_of_ints(5,10))); |
|---|
| 71 | BOOST_CHECK(!ii2.replace(--ii2.end(),pair_of_ints(10,5))); |
|---|
| 72 | |
|---|
| 73 | BOOST_CHECK(iis.modify(iis.begin(),increment_first)); |
|---|
| 74 | BOOST_CHECK(ii2.modify(ii2.begin(),increment_first)); |
|---|
| 75 | BOOST_CHECK(ii1.modify(project<1>(iis,iis.begin()),increment_first)); |
|---|
| 76 | BOOST_CHECK(ii2.modify(ii2.begin(),increment_first)); |
|---|
| 77 | |
|---|
| 78 | BOOST_CHECK(!iis.modify(iis.begin(),increment_first)); |
|---|
| 79 | BOOST_CHECK(iis.size()==2); |
|---|
| 80 | |
|---|
| 81 | iis.insert(pair_of_ints(0,0)); |
|---|
| 82 | BOOST_CHECK(ii2.modify(--ii2.end(),increment_second)); |
|---|
| 83 | BOOST_CHECK(iis.modify(iis.begin(),increment_second)); |
|---|
| 84 | BOOST_CHECK(ii2.modify(--ii2.end(),increment_second)); |
|---|
| 85 | BOOST_CHECK(iis.modify(iis.begin(),increment_second)); |
|---|
| 86 | |
|---|
| 87 | BOOST_CHECK(!ii2.modify(--ii2.end(),increment_second)); |
|---|
| 88 | BOOST_CHECK(ii2.size()==2); |
|---|
| 89 | |
|---|
| 90 | iis.insert(pair_of_ints(0,0)); |
|---|
| 91 | BOOST_CHECK(iis.modify_key(iis.begin(),increment_int)); |
|---|
| 92 | BOOST_CHECK(iis.modify_key(iis.begin(),increment_int)); |
|---|
| 93 | BOOST_CHECK(iis.modify_key(iis.begin(),increment_int)); |
|---|
| 94 | BOOST_CHECK(iis.modify_key(iis.begin(),increment_int)); |
|---|
| 95 | |
|---|
| 96 | BOOST_CHECK(!iis.modify_key(iis.begin(),increment_int)); |
|---|
| 97 | BOOST_CHECK(iis.size()==2); |
|---|
| 98 | |
|---|
| 99 | nth_index_iterator<int_int_set,1>::type it=ii1.find(5); |
|---|
| 100 | BOOST_CHECK(ii1.modify_key(it,increment_int)); |
|---|
| 101 | BOOST_CHECK(ii1.modify_key(it,increment_int)); |
|---|
| 102 | BOOST_CHECK(ii1.modify_key(it,increment_int)); |
|---|
| 103 | BOOST_CHECK(ii1.modify_key(it,increment_int)); |
|---|
| 104 | |
|---|
| 105 | BOOST_CHECK(!ii1.modify_key(it,increment_int)); |
|---|
| 106 | BOOST_CHECK(ii1.size()==1); |
|---|
| 107 | } |
|---|
| 108 | { |
|---|
| 109 | typedef multi_index_container< |
|---|
| 110 | pair_of_ints, |
|---|
| 111 | indexed_by< |
|---|
| 112 | hashed_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first)>, |
|---|
| 113 | random_access<>, |
|---|
| 114 | ordered_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,second)> > > |
|---|
| 115 | int_int_set; |
|---|
| 116 | |
|---|
| 117 | int_int_set iis; |
|---|
| 118 | nth_index<int_int_set,1>::type& ii1=get<1>(iis); |
|---|
| 119 | int_int_set::iterator p1=iis.insert(pair_of_ints(0,0)).first; |
|---|
| 120 | int_int_set::iterator p2=iis.insert(pair_of_ints(5,5)).first; |
|---|
| 121 | int_int_set::iterator p3=iis.insert(pair_of_ints(10,10)).first; |
|---|
| 122 | |
|---|
| 123 | BOOST_CHECK(!iis.replace(p1,pair_of_ints(5,0))); |
|---|
| 124 | BOOST_CHECK(!ii1.replace(ii1.begin(),pair_of_ints(0,5))); |
|---|
| 125 | BOOST_CHECK(!iis.replace(p1,pair_of_ints(5,11))); |
|---|
| 126 | BOOST_CHECK(!iis.replace(p1,pair_of_ints(11,5))); |
|---|
| 127 | BOOST_CHECK(!iis.replace(p2,pair_of_ints(10,5))); |
|---|
| 128 | BOOST_CHECK(!iis.replace(p2,pair_of_ints(5,10))); |
|---|
| 129 | BOOST_CHECK(!iis.replace(p3,pair_of_ints(5,10))); |
|---|
| 130 | BOOST_CHECK(!ii1.replace(--ii1.end(),pair_of_ints(10,5))); |
|---|
| 131 | |
|---|
| 132 | BOOST_CHECK(iis.modify(p1,increment_first)); |
|---|
| 133 | BOOST_CHECK(ii1.modify(ii1.begin(),increment_first)); |
|---|
| 134 | BOOST_CHECK(iis.modify(p1,increment_first)); |
|---|
| 135 | BOOST_CHECK(ii1.modify(ii1.begin(),increment_first)); |
|---|
| 136 | |
|---|
| 137 | BOOST_CHECK(!iis.modify(p1,increment_first)); |
|---|
| 138 | BOOST_CHECK(iis.size()==2); |
|---|
| 139 | |
|---|
| 140 | p1=iis.insert(pair_of_ints(0,0)).first; |
|---|
| 141 | BOOST_CHECK(ii1.modify(--ii1.end(),increment_second)); |
|---|
| 142 | BOOST_CHECK(iis.modify(p1,increment_second)); |
|---|
| 143 | BOOST_CHECK(ii1.modify(--ii1.end(),increment_second)); |
|---|
| 144 | BOOST_CHECK(iis.modify(p1,increment_second)); |
|---|
| 145 | |
|---|
| 146 | BOOST_CHECK(!ii1.modify(--ii1.end(),increment_second)); |
|---|
| 147 | BOOST_CHECK(ii1.size()==2); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|