1 | // Boost.Range library |
---|
2 | // |
---|
3 | // Copyright Thorsten Ottosen & Larry Evans 2003-2005. 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 | |
---|
12 | #include <boost/detail/workaround.hpp> |
---|
13 | |
---|
14 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) |
---|
15 | # pragma warn -8091 // supress warning in Boost.Test |
---|
16 | # pragma warn -8057 // unused argument argc/argv in Boost.Test |
---|
17 | #endif |
---|
18 | |
---|
19 | |
---|
20 | #include <boost/range/iterator_range.hpp> |
---|
21 | #include <boost/range/functions.hpp> |
---|
22 | #include <boost/test/test_tools.hpp> |
---|
23 | #include <boost/test/unit_test.hpp> |
---|
24 | #include <iostream> |
---|
25 | #include <string> |
---|
26 | |
---|
27 | using namespace boost; |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | void check_reference_type(); |
---|
31 | |
---|
32 | void check_iterator_range() |
---|
33 | { |
---|
34 | |
---|
35 | typedef string::iterator iterator; |
---|
36 | typedef string::const_iterator const_iterator; |
---|
37 | typedef iterator_range<iterator> irange; |
---|
38 | typedef iterator_range<const_iterator> cirange; |
---|
39 | string str = "hello world"; |
---|
40 | const string cstr = "const world"; |
---|
41 | irange r = make_iterator_range( str ); |
---|
42 | r = make_iterator_range( str.begin(), str.end() ); |
---|
43 | cirange r2 = make_iterator_range( cstr ); |
---|
44 | r2 = make_iterator_range( cstr.begin(), cstr.end() ); |
---|
45 | r2 = make_iterator_range( str ); |
---|
46 | |
---|
47 | BOOST_CHECK( !r.empty() ); |
---|
48 | BOOST_CHECK( !r2.empty() ); |
---|
49 | |
---|
50 | //#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) |
---|
51 | // if( !(bool)r ) |
---|
52 | // BOOST_CHECK( false ); |
---|
53 | // if( !(bool)r2 ) |
---|
54 | // BOOST_CHECK( false ); |
---|
55 | //#else |
---|
56 | if( !r ) |
---|
57 | BOOST_CHECK( false ); |
---|
58 | if( !r2 ) |
---|
59 | BOOST_CHECK( false ); |
---|
60 | //#endif |
---|
61 | |
---|
62 | BOOST_CHECK_EQUAL( r.size(), size( r ) ); |
---|
63 | BOOST_CHECK_EQUAL( r2.size(), size( r2 ) ); |
---|
64 | |
---|
65 | BOOST_CHECK_EQUAL( distance( r.begin(), r.end() ), |
---|
66 | distance( begin( r2 ), end( r2 ) ) ); |
---|
67 | cout << r << r2; |
---|
68 | |
---|
69 | string res = copy_range<string>( r ); |
---|
70 | BOOST_CHECK( equal( res.begin(), res.end(), r.begin() ) ); |
---|
71 | |
---|
72 | irange rr = make_iterator_range( str ); |
---|
73 | BOOST_CHECK( rr.equal( r ) ); |
---|
74 | |
---|
75 | rr = make_iterator_range( str.begin(), str.begin() + 5 ); |
---|
76 | BOOST_CHECK( rr == "hello" ); |
---|
77 | BOOST_CHECK( rr != "hell" ); |
---|
78 | BOOST_CHECK( rr < "hello dude" ); |
---|
79 | BOOST_CHECK( "hello" == rr ); |
---|
80 | BOOST_CHECK( "hell" != rr ); |
---|
81 | BOOST_CHECK( ! ("hello dude" < rr ) ); |
---|
82 | irange rrr = rr; |
---|
83 | BOOST_CHECK( rrr == rr ); |
---|
84 | BOOST_CHECK( !( rrr != rr ) ); |
---|
85 | BOOST_CHECK( !( rrr < rr ) ); |
---|
86 | |
---|
87 | const irange cr = make_iterator_range( str ); |
---|
88 | BOOST_CHECK_EQUAL( cr.front(), 'h' ); |
---|
89 | BOOST_CHECK_EQUAL( cr.back(), 'd' ); |
---|
90 | BOOST_CHECK_EQUAL( cr[1], 'e' ); |
---|
91 | |
---|
92 | rrr = make_iterator_range( str, 1, -1 ); |
---|
93 | BOOST_CHECK( rrr == "ello worl" ); |
---|
94 | rrr = make_iterator_range( rrr, -1, 1 ); |
---|
95 | BOOST_CHECK( rrr == str ); |
---|
96 | |
---|
97 | check_reference_type(); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | using boost::unit_test::test_suite; |
---|
102 | |
---|
103 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
---|
104 | { |
---|
105 | test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" ); |
---|
106 | |
---|
107 | test->add( BOOST_TEST_CASE( &check_iterator_range ) ); |
---|
108 | |
---|
109 | return test; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | // |
---|
114 | // |
---|
115 | // Check that constness is propgated correct from |
---|
116 | // the iterator types. |
---|
117 | // |
---|
118 | // Test contributed by Larry Evans. |
---|
119 | // |
---|
120 | |
---|
121 | template< class Container > |
---|
122 | int test_iter_range( Container& a_cont ) |
---|
123 | { |
---|
124 | typedef BOOST_DEDUCED_TYPENAME range_result_iterator<Container>::type citer_type; |
---|
125 | typedef iterator_range<citer_type> riter_type; |
---|
126 | riter_type a_riter( make_iterator_range( a_cont ) ); |
---|
127 | a_riter.front(); |
---|
128 | a_riter.back(); |
---|
129 | int i = a_riter[0]; |
---|
130 | return i; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | void check_reference_type() |
---|
136 | { |
---|
137 | typedef vector<int> veci_type; |
---|
138 | veci_type a_vec; |
---|
139 | a_vec.push_back( 999 ); |
---|
140 | test_iter_range<veci_type>(a_vec); |
---|
141 | test_iter_range<veci_type const>(a_vec); |
---|
142 | } |
---|