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)
7 #include <boost/config.hpp>
8 #include <boost/test/minimal.hpp>
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>
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
28 typedef boost::permutation_iterator
< double*, int const* > permutation_type
;
29 typedef boost::permutation_iterator
< double const*, int const* > permutation_const_type
;
31 BOOST_CONCEPT_ASSERT((
32 boost_concepts::InteroperableIteratorConcept
<
34 , permutation_const_type
38 void permutation_test()
40 // Example taken from documentation of old permutation_iterator.
41 typedef std::vector
< double > element_range_type
;
42 typedef std::list
< int > index_type
;
44 const int element_range_size
= 10;
45 const int index_size
= 7;
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
); }
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() );
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() );
62 BOOST_CHECK( it
== begin
);
63 BOOST_CHECK( it
!= end
);
65 BOOST_CHECK( std::distance( begin
, end
) == index_size
);
67 for( index_type::iterator i_it1
= indices
.begin(); it
!= end
; ++i_it1
, ++it
)
69 BOOST_CHECK( *it
== elements
[ *i_it1
] );
73 for( int i1
= 0; i1
< index_size
- 1 ; ++++i1
, ++++it
)
75 index_type::iterator i_it2
= indices
.begin();
76 std::advance( i_it2
, i1
);
77 BOOST_CHECK( *it
== elements
[ *i_it2
] );
81 std::advance(it
, index_size
);
82 for( index_type::iterator i_it3
= indices
.end(); it
!= begin
; )
84 BOOST_CHECK( *--it
== elements
[ *--i_it3
] );
88 std::advance(it
, index_size
);
89 for( int i2
= 0; i2
< index_size
- 1; i2
+=2, --it
)
91 index_type::iterator i_it4
= --indices
.end();
92 std::advance( i_it4
, -i2
);
93 BOOST_CHECK( *--it
== elements
[ *i_it4
] );
99 int test_main(int, char *[])