1 // Boost.Range library
2 //
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)
7 //
8 // For more information, see http://www.boost.org/libs/range/
9 //
10
11 #include <boost/detail/workaround.hpp>
12
13 #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
14 # pragma warn -8091 // suppress warning in Boost.Test
15 # pragma warn -8057 // unused argument argc/argv in Boost.Test
16 #endif
17
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>
22 #include <iostream>
23 #include <algorithm>
24 #include <vector>
25 #include <utility>
26
27 namespace
28 {
29 //
30 // example: extracting bounds in a generic algorithm
31 //
32 template< typename Range, typename T >
33 inline typename boost::range_iterator<Range>::type
find(Range & c,const T & value)34 find( Range& c, const T& value )
35 {
36 return std::find( boost::begin( c ), boost::end( c ), value );
37 }
38
39 template< typename Range, typename T >
40 inline typename boost::range_iterator<Range>::type
find(const Range & c,const T & value)41 find( const Range& c, const T& value )
42 {
43 return std::find( boost::begin( c ), boost::end( c ), value );
44 }
45
46 //
47 // replace first value and return its index
48 //
49 template< class Range, class T >
50 inline typename boost::range_difference<Range>::type
my_generic_replace(Range & c,const T & value,const T & replacement)51 my_generic_replace( Range& c, const T& value, const T& replacement )
52 {
53 typename boost::range_iterator<Range>::type found = find( c, value );
54
55 if( found != boost::end( c ) )
56 *found = replacement;
57 return std::distance( boost::begin( c ), found );
58 }
59 }
60
61
check_algorithm()62 void check_algorithm()
63 {
64 //
65 // usage
66 //
67 const int N = 5;
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 );
76
77 }
78
79 #include <boost/test/unit_test.hpp>
80 using boost::unit_test::test_suite;
81
init_unit_test_suite(int argc,char * argv[])82 test_suite* init_unit_test_suite( int argc, char* argv[] )
83 {
84 test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" );
85
86 test->add( BOOST_TEST_CASE( &check_algorithm ) );
87
88 return test;
89 }
90
91
92
93