• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/distance.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_distance(Iterator it_from,Iterator it_to,int n)17 void test_distance(Iterator it_from, Iterator it_to, int n)
18 {
19     BOOST_TEST(boost::distance(it_from, it_to) == n);
20 }
21 
main()22 int main()
23 {
24     int array[3] = {1, 2, 3};
25     int* ptr1 = array;
26     int* ptr2 = array + 3;
27 
28     {
29         test_distance(ptr1, ptr2,  3);
30         test_distance(ptr2, ptr1, -3);
31 
32         test_distance(
33             boost::make_transform_iterator(ptr1, twice)
34           , boost::make_transform_iterator(ptr2, twice)
35           , 3
36         );
37         test_distance(
38             boost::make_transform_iterator(ptr2, twice)
39           , boost::make_transform_iterator(ptr1, twice)
40           , -3
41         );
42     }
43 
44     {
45         std::vector<int> ints(ptr1, ptr2);
46         test_distance(ints.begin(), ints.end(),  3);
47         test_distance(ints.end(), ints.begin(), -3);
48 
49         test_distance(
50             boost::make_transform_iterator(ints.begin(), twice)
51           , boost::make_transform_iterator(ints.end(), twice)
52           , 3
53         );
54         test_distance(
55             boost::make_transform_iterator(ints.end(), twice)
56           , boost::make_transform_iterator(ints.begin(), twice)
57           , -3
58         );
59     }
60 
61     {
62         std::list<int> ints(ptr1, ptr2);
63         test_distance(ints.begin(), ints.end(),  3);
64 
65         test_distance(
66             boost::make_transform_iterator(ints.begin(), twice)
67           , boost::make_transform_iterator(ints.end(), twice)
68           , 3
69         );
70     }
71 
72     {
73         boost::container::slist<int> ints(ptr1, ptr2);
74         test_distance(ints.begin(), ints.end(),  3);
75 
76         test_distance(
77             boost::make_transform_iterator(ints.begin(), twice)
78           , boost::make_transform_iterator(ints.end(), twice)
79           , 3
80         );
81     }
82 
83     return boost::report_errors();
84 }
85