3 // Copyright Thorsten Ottosen 2003-2004. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org/libs/range/
11 #include <boost/detail/workaround.hpp>
13 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
14 # pragma warn -8091 // supress warning in Boost.Test
15 # pragma warn -8057 // unused argument argc/argv in Boost.Test
18 #include <boost/range/functions.hpp>
19 #include <boost/range/metafunctions.hpp>
20 #include <boost/range/as_literal.hpp>
21 #include <boost/test/test_tools.hpp>
30 // example: extrating bounds in a generic algorithm
32 template< typename Range, typename T >
33 inline typename boost::range_iterator<Range>::type
34 find( Range& c, const T& value )
36 return std::find( boost::begin( c ), boost::end( c ), value );
39 template< typename Range, typename T >
40 inline typename boost::range_iterator<Range>::type
41 find( const Range& c, const T& value )
43 return std::find( boost::begin( c ), boost::end( c ), value );
47 // replace first value and return its index
49 template< class Range, class T >
50 inline typename boost::range_difference<Range>::type
51 my_generic_replace( Range& c, const T& value, const T& replacement )
53 typename boost::range_iterator<Range>::type found = find( c, value );
55 if( found != boost::end( c ) )
57 return std::distance( boost::begin( c ), found );
62 void check_algorithm()
68 std::vector<int> my_vector;
69 int values[] = { 1,2,3,4,5,6,7,8,9 };
70 my_vector.assign( values, values + 9 );
71 typedef std::vector<int>::iterator iterator;
72 std::pair<iterator,iterator> my_view( boost::begin( my_vector ),
73 boost::begin( my_vector ) + N );
74 BOOST_CHECK_EQUAL( my_generic_replace( my_vector, 4, 2 ), 3 );
75 BOOST_CHECK_EQUAL( my_generic_replace( my_view, 4, 2 ), N );
79 #include <boost/test/unit_test.hpp>
80 using boost::unit_test::test_suite;
82 test_suite* init_unit_test_suite( int argc, char* argv[] )
84 test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" );
86 test->add( BOOST_TEST_CASE( &check_algorithm ) );