1 | // Boost.Range library |
2 | // |
3 | // Copyright Neil Groves 2009. 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 | // |
9 | // For more information, see http://www.boost.org/libs/range/ |
10 | // |
11 | #include <boost/range/adaptor/indirected.hpp> |
12 | |
13 | #include <boost/test/test_tools.hpp> |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <boost/assign.hpp> |
17 | #include <boost/shared_ptr.hpp> |
18 | #include <boost/range/algorithm_ext.hpp> |
19 | |
20 | #include <algorithm> |
21 | #include <list> |
22 | #include <set> |
23 | #include <vector> |
24 | |
25 | namespace boost |
26 | { |
27 | namespace |
28 | { |
29 | |
30 | template< class Container > |
31 | void indirected_test_impl( Container& c ) |
32 | { |
33 | using namespace boost::adaptors; |
34 | |
35 | // This is my preferred syntax using the | operator. |
36 | std::vector< int > test_result1; |
37 | boost::push_back(test_result1, c | indirected); |
38 | |
39 | // This is an alternative syntax preferred by some. |
40 | std::vector< int > test_result2; |
41 | boost::push_back(test_result2, adaptors::indirect(c)); |
42 | |
43 | // Calculate the reference result. |
44 | std::vector< int > reference_result; |
45 | typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t; |
46 | for (iter_t it = c.begin(); it != c.end(); ++it) |
47 | { |
48 | reference_result.push_back(**it); |
49 | } |
50 | |
51 | BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(), |
52 | reference_result.end(), |
53 | test_result1.begin(), |
54 | test_result1.end() ); |
55 | |
56 | BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(), |
57 | reference_result.end(), |
58 | test_result2.begin(), |
59 | test_result2.end() ); |
60 | } |
61 | |
62 | template< class Container > |
63 | void indirected_test_impl() |
64 | { |
65 | using namespace boost::assign; |
66 | |
67 | Container c; |
68 | |
69 | indirected_test_impl(c); |
70 | |
71 | c += boost::shared_ptr<int>(new int(1)); |
72 | indirected_test_impl(c); |
73 | |
74 | std::vector<int> v; |
75 | v += 1,1,2,2,2,3,4,4,4,4,5,6,7,8,9,9; |
76 | for (std::vector<int>::const_iterator it = v.begin(); it != v.end(); ++it) |
77 | { |
78 | c += boost::shared_ptr<int>(new int(*it)); |
79 | } |
80 | indirected_test_impl(c); |
81 | } |
82 | |
83 | void indirected_test() |
84 | { |
85 | indirected_test_impl< std::vector< boost::shared_ptr< int > > >(); |
86 | } |
87 | } |
88 | } |
89 | |
90 | boost::unit_test::test_suite* |
91 | init_unit_test_suite(int argc, char* argv[]) |
92 | { |
93 | boost::unit_test::test_suite* test |
94 | = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indirected" ); |
95 | |
96 | test->add( BOOST_TEST_CASE( &boost::indirected_test ) ); |
97 | |
98 | return test; |
99 | } |
100 | |