1 // Copyright (C) 2017 Michel Morin.
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <vector>
8 #include <list>
9 #include <boost/container/slist.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <boost/iterator/advance.hpp>
12 #include <boost/iterator/transform_iterator.hpp>
13
twice(int x)14 int twice(int x) { return x + x; }
15
16 template <typename Iterator>
test_advance(Iterator it_from,Iterator it_to,int n)17 void test_advance(Iterator it_from, Iterator it_to, int n)
18 {
19 boost::advance(it_from, n);
20 BOOST_TEST(it_from == it_to);
21 }
22
main()23 int main()
24 {
25 int array[3] = {1, 2, 3};
26 int* ptr1 = array;
27 int* ptr2 = array + 3;
28
29 {
30 test_advance(ptr1, ptr2, 3);
31 test_advance(ptr2, ptr1, -3);
32
33 test_advance(
34 boost::make_transform_iterator(ptr1, twice)
35 , boost::make_transform_iterator(ptr2, twice)
36 , 3
37 );
38 test_advance(
39 boost::make_transform_iterator(ptr2, twice)
40 , boost::make_transform_iterator(ptr1, twice)
41 , -3
42 );
43 }
44
45 {
46 std::vector<int> ints(ptr1, ptr2);
47 test_advance(ints.begin(), ints.end(), 3);
48 test_advance(ints.end(), ints.begin(), -3);
49
50 test_advance(
51 boost::make_transform_iterator(ints.begin(), twice)
52 , boost::make_transform_iterator(ints.end(), twice)
53 , 3
54 );
55 test_advance(
56 boost::make_transform_iterator(ints.end(), twice)
57 , boost::make_transform_iterator(ints.begin(), twice)
58 , -3
59 );
60 }
61
62 {
63 std::list<int> ints(ptr1, ptr2);
64 test_advance(ints.begin(), ints.end(), 3);
65 test_advance(ints.end(), ints.begin(), -3);
66
67 test_advance(
68 boost::make_transform_iterator(ints.begin(), twice)
69 , boost::make_transform_iterator(ints.end(), twice)
70 , 3
71 );
72 test_advance(
73 boost::make_transform_iterator(ints.end(), twice)
74 , boost::make_transform_iterator(ints.begin(), twice)
75 , -3
76 );
77 }
78
79 {
80 boost::container::slist<int> ints(ptr1, ptr2);
81 test_advance(ints.begin(), ints.end(), 3);
82
83 test_advance(
84 boost::make_transform_iterator(ints.begin(), twice)
85 , boost::make_transform_iterator(ints.end(), twice)
86 , 3
87 );
88 }
89
90 return boost::report_errors();
91 }
92