| 1 | /* Boost.MultiIndex test for rearrange operations. | 
|---|
| 2 |  * | 
|---|
| 3 |  * Copyright 2003-2007 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_rearrange.hpp" | 
|---|
| 12 |  | 
|---|
| 13 | #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ | 
|---|
| 14 | #include <algorithm> | 
|---|
| 15 | #include <iterator> | 
|---|
| 16 | #include "pre_multi_index.hpp" | 
|---|
| 17 | #include <boost/multi_index_container.hpp> | 
|---|
| 18 | #include <boost/multi_index/sequenced_index.hpp> | 
|---|
| 19 | #include <boost/multi_index/random_access_index.hpp> | 
|---|
| 20 | #include <boost/ref.hpp> | 
|---|
| 21 | #include <boost/test/test_tools.hpp> | 
|---|
| 22 | #include <vector> | 
|---|
| 23 |  | 
|---|
| 24 | using namespace boost::multi_index; | 
|---|
| 25 |  | 
|---|
| 26 | #undef _ | 
|---|
| 27 | #define _ , | 
|---|
| 28 |  | 
|---|
| 29 | #undef CHECK_EQUAL | 
|---|
| 30 | #define CHECK_EQUAL(p,check_range) \ | 
|---|
| 31 | {\ | 
|---|
| 32 |   int v[]=check_range;\ | 
|---|
| 33 |   std::size_t size_v=sizeof(v)/sizeof(int);\ | 
|---|
| 34 |   BOOST_CHECK(std::size_t(std::distance((p).begin(),(p).end()))==size_v);\ | 
|---|
| 35 |   BOOST_CHECK(std::equal((p).begin(),(p).end(),&v[0]));\ | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | #undef CHECK_VOID_RANGE | 
|---|
| 39 | #define CHECK_VOID_RANGE(p) BOOST_CHECK((p).first==(p).second) | 
|---|
| 40 |  | 
|---|
| 41 | #if BOOST_WORKAROUND(__MWERKS__,<=0x3003) | 
|---|
| 42 | /* The "ISO C++ Template Parser" option makes CW8.3 incorrectly fail at | 
|---|
| 43 |  * expressions of the form sizeof(x) where x is an array local to a | 
|---|
| 44 |  * template function. | 
|---|
| 45 |  */ | 
|---|
| 46 |  | 
|---|
| 47 | #pragma parse_func_templ off | 
|---|
| 48 | #endif | 
|---|
| 49 |  | 
|---|
| 50 | template<typename Sequence> | 
|---|
| 51 | static void local_test_rearrange(BOOST_EXPLICIT_TEMPLATE_TYPE(Sequence)) | 
|---|
| 52 | { | 
|---|
| 53 |   typedef typename Sequence::iterator   iterator; | 
|---|
| 54 |   typedef typename Sequence::value_type value_type; | 
|---|
| 55 |  | 
|---|
| 56 |   Sequence sc; | 
|---|
| 57 |   sc.push_back(0); | 
|---|
| 58 |   sc.push_back(1); | 
|---|
| 59 |   sc.push_back(2); | 
|---|
| 60 |   sc.push_back(3); | 
|---|
| 61 |   sc.push_back(4); | 
|---|
| 62 |   sc.push_back(5); | 
|---|
| 63 |  | 
|---|
| 64 |   iterator it; | 
|---|
| 65 |  | 
|---|
| 66 |   it=sc.begin(); | 
|---|
| 67 |   std::advance(it,3); | 
|---|
| 68 |   sc.relocate(sc.begin(),it); | 
|---|
| 69 |   CHECK_EQUAL(sc,{3 _ 0 _ 1 _ 2 _ 4 _ 5}); | 
|---|
| 70 |   BOOST_CHECK(it==sc.begin()); | 
|---|
| 71 |  | 
|---|
| 72 |   sc.relocate(it,it); | 
|---|
| 73 |   CHECK_EQUAL(sc,{3 _ 0 _ 1 _ 2 _ 4 _ 5}); | 
|---|
| 74 |  | 
|---|
| 75 |   std::advance(it,3); | 
|---|
| 76 |   sc.relocate(sc.end(),it,sc.end()); | 
|---|
| 77 |   CHECK_EQUAL(sc,{3 _ 0 _ 1 _ 2 _ 4 _ 5}); | 
|---|
| 78 |  | 
|---|
| 79 |   sc.relocate(sc.begin(),it,it); | 
|---|
| 80 |   CHECK_EQUAL(sc,{3 _ 0 _ 1 _ 2 _ 4 _ 5}); | 
|---|
| 81 |  | 
|---|
| 82 |   iterator it2; | 
|---|
| 83 |  | 
|---|
| 84 |   it2=sc.begin(); | 
|---|
| 85 |   ++it2; | 
|---|
| 86 |   sc.relocate(it2,it,sc.end()); | 
|---|
| 87 |   CHECK_EQUAL(sc,{3 _ 2 _ 4 _ 5 _ 0 _ 1}); | 
|---|
| 88 |   BOOST_CHECK(std::distance(it,it2)==3); | 
|---|
| 89 |  | 
|---|
| 90 |   sc.relocate(--(sc.end()),it,it2); | 
|---|
| 91 |   CHECK_EQUAL(sc,{3 _ 0 _ 2 _ 4 _ 5 _ 1}); | 
|---|
| 92 |  | 
|---|
| 93 |   std::vector<boost::reference_wrapper<const value_type> > v; | 
|---|
| 94 |   for(iterator it3=sc.begin();it3!=sc.end();++it3){ | 
|---|
| 95 |     v.push_back(boost::cref(*it3)); | 
|---|
| 96 |   } | 
|---|
| 97 |  | 
|---|
| 98 |   sc.rearrange(v.begin()); | 
|---|
| 99 |   BOOST_CHECK(std::equal(sc.begin(),sc.end(),v.begin())); | 
|---|
| 100 |  | 
|---|
| 101 |   std::reverse(v.begin(),v.end()); | 
|---|
| 102 |   sc.rearrange(v.begin()); | 
|---|
| 103 |   BOOST_CHECK(std::equal(sc.begin(),sc.end(),v.begin())); | 
|---|
| 104 |  | 
|---|
| 105 |   std::sort(v.begin(),v.end()); | 
|---|
| 106 |   sc.rearrange(v.begin()); | 
|---|
| 107 |   BOOST_CHECK(std::equal(sc.begin(),sc.end(),v.begin())); | 
|---|
| 108 |  | 
|---|
| 109 |   std::reverse(v.begin(),v.begin()+v.size()/2); | 
|---|
| 110 |   sc.rearrange(v.begin()); | 
|---|
| 111 |   BOOST_CHECK(std::equal(sc.begin(),sc.end(),v.begin())); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | #if BOOST_WORKAROUND(__MWERKS__,<=0x3003) | 
|---|
| 115 | #pragma parse_func_templ reset | 
|---|
| 116 | #endif | 
|---|
| 117 |  | 
|---|
| 118 | void test_rearrange() | 
|---|
| 119 | { | 
|---|
| 120 |   typedef multi_index_container< | 
|---|
| 121 |     int, | 
|---|
| 122 |     indexed_by<sequenced<> > | 
|---|
| 123 |   > int_list; | 
|---|
| 124 |  | 
|---|
| 125 |   /* MSVC++ 6.0 chokes on local_test_rearrange without this | 
|---|
| 126 |    * explicit instantiation | 
|---|
| 127 |    */ | 
|---|
| 128 |   int_list il; | 
|---|
| 129 |   local_test_rearrange<int_list>(); | 
|---|
| 130 |  | 
|---|
| 131 |   typedef multi_index_container< | 
|---|
| 132 |     int, | 
|---|
| 133 |     indexed_by<random_access<> > | 
|---|
| 134 |   > int_vector; | 
|---|
| 135 |  | 
|---|
| 136 |   int_vector iv; | 
|---|
| 137 |   local_test_rearrange<int_vector>(); | 
|---|
| 138 | } | 
|---|