• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // (C) Copyright Toon Knapen    2001.
2 // (C) Copyright Roland Richter 2003.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <boost/config.hpp>
8 #include <boost/test/minimal.hpp>
9 
10 #include <boost/iterator/permutation_iterator.hpp>
11 #include <boost/static_assert.hpp>
12 #include <boost/iterator/iterator_concepts.hpp>
13 #include <boost/concept/assert.hpp>
14 
15 #include <vector>
16 #include <list>
17 
18 #include <algorithm>
19 
20 
21 // This test checks for convertibility/interoperability among similar
22 // permutation iterators.  We're not using container iterators
23 // underneath, as in permutation_test, because of bugs in GCC-3.3's
24 // __normal_iterator that make is_convertible choke when testing
25 // convertibility.
iterop_test()26 void iterop_test()
27 {
28     typedef boost::permutation_iterator< double*, int const* > permutation_type;
29     typedef boost::permutation_iterator< double const*, int const* > permutation_const_type;
30 
31     BOOST_CONCEPT_ASSERT((
32       boost_concepts::InteroperableIteratorConcept<
33            permutation_type
34          , permutation_const_type
35       >));
36 }
37 
permutation_test()38 void permutation_test()
39 {
40   // Example taken from documentation of old permutation_iterator.
41   typedef std::vector< double > element_range_type;
42   typedef std::list< int > index_type;
43 
44   const int element_range_size = 10;
45   const int index_size = 7;
46 
47   BOOST_STATIC_ASSERT(index_size <= element_range_size);
48   element_range_type elements( element_range_size );
49   for( element_range_type::iterator el_it = elements.begin(); el_it != elements.end(); ++el_it )
50     { *el_it = std::distance(elements.begin(), el_it); }
51 
52   index_type indices( index_size );
53   for( index_type::iterator i_it = indices.begin(); i_it != indices.end(); ++i_it )
54     { *i_it = element_range_size - index_size + std::distance(indices.begin(), i_it); }
55   std::reverse( indices.begin(), indices.end() );
56 
57   typedef boost::permutation_iterator< element_range_type::iterator, index_type::iterator > permutation_type;
58   permutation_type begin = boost::make_permutation_iterator( elements.begin(), indices.begin() );
59   permutation_type it = begin;
60   permutation_type end = boost::make_permutation_iterator( elements.begin(), indices.end() );
61 
62   BOOST_CHECK( it == begin );
63   BOOST_CHECK( it != end );
64 
65   BOOST_CHECK( std::distance( begin, end ) == index_size );
66 
67   for( index_type::iterator i_it1 = indices.begin(); it != end; ++i_it1, ++it )
68   {
69     BOOST_CHECK( *it == elements[ *i_it1 ] );
70   }
71 
72   it = begin;
73   for( int i1 = 0; i1 < index_size - 1 ; ++++i1, ++++it )
74   {
75     index_type::iterator i_it2 = indices.begin();
76     std::advance( i_it2, i1 );
77     BOOST_CHECK( *it == elements[ *i_it2 ] );
78   }
79 
80   it = begin;
81   std::advance(it, index_size);
82   for( index_type::iterator i_it3 = indices.end(); it != begin; )
83   {
84     BOOST_CHECK( *--it == elements[ *--i_it3 ] );
85   }
86 
87   it = begin;
88   std::advance(it, index_size);
89   for( int i2 = 0; i2 < index_size - 1; i2+=2, --it )
90   {
91     index_type::iterator i_it4 = --indices.end();
92     std::advance( i_it4, -i2 );
93     BOOST_CHECK( *--it == elements[ *i_it4 ] );
94   }
95 
96 }
97 
98 
test_main(int,char * [])99 int test_main(int, char *[])
100 {
101   permutation_test();
102   return 0;
103 }
104