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