test_rearrange.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Boost.MultiIndex test for rearrange operations.
  2. *
  3. * Copyright 2003-2013 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. #include "test_rearrange.hpp"
  11. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  12. #include <algorithm>
  13. #include <iterator>
  14. #include <boost/detail/lightweight_test.hpp>
  15. #include "pre_multi_index.hpp"
  16. #include <boost/multi_index_container.hpp>
  17. #include <boost/multi_index/sequenced_index.hpp>
  18. #include <boost/multi_index/random_access_index.hpp>
  19. #include <boost/next_prior.hpp>
  20. #include <boost/preprocessor/seq/enum.hpp>
  21. #include <boost/ref.hpp>
  22. #include <vector>
  23. using namespace boost::multi_index;
  24. #undef CHECK_EQUAL
  25. #define CHECK_EQUAL(p,check_seq) \
  26. {\
  27. int v[]={BOOST_PP_SEQ_ENUM(check_seq)};\
  28. std::size_t size_v=sizeof(v)/sizeof(int);\
  29. BOOST_TEST(std::size_t(std::distance((p).begin(),(p).end()))==size_v);\
  30. BOOST_TEST(std::equal((p).begin(),(p).end(),&v[0]));\
  31. }
  32. #undef CHECK_VOID_RANGE
  33. #define CHECK_VOID_RANGE(p) BOOST_TEST((p).first==(p).second)
  34. #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  35. /* The "ISO C++ Template Parser" option makes CW8.3 incorrectly fail at
  36. * expressions of the form sizeof(x) where x is an array local to a
  37. * template function.
  38. */
  39. #pragma parse_func_templ off
  40. #endif
  41. template<typename Sequence>
  42. static void local_test_rearrange()
  43. {
  44. typedef typename Sequence::iterator iterator;
  45. typedef typename Sequence::value_type value_type;
  46. Sequence sc;
  47. sc.push_back(0);
  48. sc.push_back(1);
  49. sc.push_back(2);
  50. sc.push_back(3);
  51. sc.push_back(4);
  52. sc.push_back(5);
  53. iterator it;
  54. it=sc.begin();
  55. std::advance(it,3);
  56. sc.relocate(sc.begin(),it);
  57. CHECK_EQUAL(sc,(3)(0)(1)(2)(4)(5));
  58. BOOST_TEST(it==sc.begin());
  59. sc.relocate(it,it);
  60. CHECK_EQUAL(sc,(3)(0)(1)(2)(4)(5));
  61. std::advance(it,3);
  62. sc.relocate(sc.end(),it,sc.end());
  63. CHECK_EQUAL(sc,(3)(0)(1)(2)(4)(5));
  64. sc.relocate(sc.begin(),it,it);
  65. CHECK_EQUAL(sc,(3)(0)(1)(2)(4)(5));
  66. iterator it2;
  67. it2=sc.begin();
  68. ++it2;
  69. sc.relocate(it2,it,sc.end());
  70. CHECK_EQUAL(sc,(3)(2)(4)(5)(0)(1));
  71. BOOST_TEST(std::distance(it,it2)==3);
  72. sc.relocate(boost::prior(sc.end()),it,it2);
  73. CHECK_EQUAL(sc,(3)(0)(2)(4)(5)(1));
  74. std::vector<boost::reference_wrapper<const value_type> > v;
  75. for(iterator it3=sc.begin();it3!=sc.end();++it3){
  76. v.push_back(boost::cref(*it3));
  77. }
  78. sc.rearrange(v.begin());
  79. BOOST_TEST(std::equal(sc.begin(),sc.end(),v.begin()));
  80. std::reverse(v.begin(),v.end());
  81. sc.rearrange(v.begin());
  82. BOOST_TEST(std::equal(sc.begin(),sc.end(),v.begin()));
  83. std::sort(v.begin(),v.end());
  84. sc.rearrange(v.begin());
  85. BOOST_TEST(std::equal(sc.begin(),sc.end(),v.begin()));
  86. std::reverse(v.begin(),v.begin()+v.size()/2);
  87. sc.rearrange(v.begin());
  88. BOOST_TEST(std::equal(sc.begin(),sc.end(),v.begin()));
  89. }
  90. #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  91. #pragma parse_func_templ reset
  92. #endif
  93. void test_rearrange()
  94. {
  95. typedef multi_index_container<
  96. int,
  97. indexed_by<sequenced<> >
  98. > int_list;
  99. local_test_rearrange<int_list>();
  100. typedef multi_index_container<
  101. int,
  102. indexed_by<random_access<> >
  103. > int_vector;
  104. local_test_rearrange<int_vector>();
  105. }