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 | |
---|
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_iterator_range() |
---|
31 | { |
---|
32 | |
---|
33 | typedef string::iterator iterator; |
---|
34 | typedef string::const_iterator const_iterator; |
---|
35 | typedef iterator_range<iterator> irange; |
---|
36 | typedef iterator_range<const_iterator> cirange; |
---|
37 | string str = "hello world"; |
---|
38 | const string cstr = "const world"; |
---|
39 | irange r = make_iterator_range( str ); |
---|
40 | r = make_iterator_range( str.begin(), str.end() ); |
---|
41 | cirange r2 = make_iterator_range( cstr ); |
---|
42 | r2 = make_iterator_range( cstr.begin(), cstr.end() ); |
---|
43 | r2 = make_iterator_range( str ); |
---|
44 | |
---|
45 | BOOST_CHECK( !r.empty() ); |
---|
46 | BOOST_CHECK( !r2.empty() ); |
---|
47 | |
---|
48 | //#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) |
---|
49 | // if( !(bool)r ) |
---|
50 | // BOOST_CHECK( false ); |
---|
51 | // if( !(bool)r2 ) |
---|
52 | // BOOST_CHECK( false ); |
---|
53 | //#else |
---|
54 | if( !r ) |
---|
55 | BOOST_CHECK( false ); |
---|
56 | if( !r2 ) |
---|
57 | BOOST_CHECK( false ); |
---|
58 | //#endif |
---|
59 | |
---|
60 | BOOST_CHECK_EQUAL( r.size(), size( r ) ); |
---|
61 | BOOST_CHECK_EQUAL( r2.size(), size( r2 ) ); |
---|
62 | |
---|
63 | BOOST_CHECK_EQUAL( distance( r.begin(), r.end() ), |
---|
64 | distance( begin( r2 ), end( r2 ) ) ); |
---|
65 | cout << r << r2; |
---|
66 | |
---|
67 | string res = copy_range<string>( r ); |
---|
68 | BOOST_CHECK( equal( res.begin(), res.end(), r.begin() ) ); |
---|
69 | |
---|
70 | irange rr = make_iterator_range( str ); |
---|
71 | BOOST_CHECK( rr.equal( r ) ); |
---|
72 | |
---|
73 | rr = make_iterator_range( str.begin(), str.begin() + 5 ); |
---|
74 | BOOST_CHECK( rr == "hello" ); |
---|
75 | BOOST_CHECK( rr != "hell" ); |
---|
76 | BOOST_CHECK( rr < "hello dude" ); |
---|
77 | BOOST_CHECK( "hello" == rr ); |
---|
78 | BOOST_CHECK( "hell" != rr ); |
---|
79 | BOOST_CHECK( ! ("hello dude" < rr ) ); |
---|
80 | irange rrr = rr; |
---|
81 | BOOST_CHECK( rrr == rr ); |
---|
82 | BOOST_CHECK( !( rrr != rr ) ); |
---|
83 | BOOST_CHECK( !( rrr < rr ) ); |
---|
84 | |
---|
85 | const irange cr = make_iterator_range( str ); |
---|
86 | BOOST_CHECK_EQUAL( cr.front(), 'h' ); |
---|
87 | BOOST_CHECK_EQUAL( cr.back(), 'd' ); |
---|
88 | BOOST_CHECK_EQUAL( cr[1], 'e' ); |
---|
89 | |
---|
90 | rrr = make_iterator_range( str, 1, -1 ); |
---|
91 | BOOST_CHECK( rrr == "ello worl" ); |
---|
92 | rrr = make_iterator_range( rrr, -1, 1 ); |
---|
93 | BOOST_CHECK( rrr == str ); |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | using boost::unit_test::test_suite; |
---|
98 | |
---|
99 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
---|
100 | { |
---|
101 | test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" ); |
---|
102 | |
---|
103 | test->add( BOOST_TEST_CASE( &check_iterator_range ) ); |
---|
104 | |
---|
105 | return test; |
---|
106 | } |
---|
107 | |
---|