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(__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 |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <boost/range/functions.hpp> |
---|
19 | #include <boost/range/metafunctions.hpp> |
---|
20 | #include <boost/test/test_tools.hpp> |
---|
21 | #include <iostream> |
---|
22 | #include <algorithm> |
---|
23 | #include <vector> |
---|
24 | #include <utility> |
---|
25 | |
---|
26 | namespace |
---|
27 | { |
---|
28 | // |
---|
29 | // example: extrating bounds in a generic algorithm |
---|
30 | // |
---|
31 | template< typename Range, typename T > |
---|
32 | inline typename boost::range_iterator<Range>::type |
---|
33 | find( Range& c, const T& value ) |
---|
34 | { |
---|
35 | return std::find( boost::begin( c ), boost::end( c ), value ); |
---|
36 | } |
---|
37 | |
---|
38 | template< typename Range, typename T > |
---|
39 | inline typename boost::range_const_iterator<Range>::type |
---|
40 | find( const Range& c, const T& value ) |
---|
41 | { |
---|
42 | return std::find( boost::begin( c ), boost::end( c ), value ); |
---|
43 | } |
---|
44 | |
---|
45 | // |
---|
46 | // replace first value and return its index |
---|
47 | // |
---|
48 | template< class Range, class T > |
---|
49 | inline typename boost::range_size<Range>::type |
---|
50 | my_generic_replace( Range& c, const T& value, const T& replacement ) |
---|
51 | { |
---|
52 | typename boost::range_iterator<Range>::type found = find( c, value ); |
---|
53 | |
---|
54 | if( found != boost::end( c ) ) |
---|
55 | *found = replacement; |
---|
56 | return std::distance( boost::begin( c ), found ); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | void check_algorithm() |
---|
62 | { |
---|
63 | // |
---|
64 | // usage |
---|
65 | // |
---|
66 | const unsigned N = 5; |
---|
67 | std::vector<int> my_vector; |
---|
68 | int values[] = { 1,2,3,4,5,6,7,8,9 }; |
---|
69 | my_vector.assign( values, values + 9 ); |
---|
70 | typedef std::vector<int>::iterator iterator; |
---|
71 | std::pair<iterator,iterator> my_view( boost::begin( my_vector ), |
---|
72 | boost::begin( my_vector ) + N ); |
---|
73 | char str_val[] = "a string"; |
---|
74 | char* str = str_val; |
---|
75 | |
---|
76 | BOOST_CHECK_EQUAL( my_generic_replace( my_vector, 4, 2 ), 3u ); |
---|
77 | BOOST_CHECK_EQUAL( my_generic_replace( my_view, 4, 2 ), N ); |
---|
78 | BOOST_CHECK_EQUAL( my_generic_replace( str, 'a', 'b' ), 0u ); |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | #include <boost/test/unit_test.hpp> |
---|
83 | using boost::unit_test::test_suite; |
---|
84 | |
---|
85 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
---|
86 | { |
---|
87 | test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" ); |
---|
88 | |
---|
89 | test->add( BOOST_TEST_CASE( &check_algorithm ) ); |
---|
90 | |
---|
91 | return test; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|