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 | //[transformed_example |
12 | #include <boost/range/adaptor/transformed.hpp> |
13 | #include <boost/range/algorithm/copy.hpp> |
14 | #include <boost/assign.hpp> |
15 | #include <iterator> |
16 | #include <iostream> |
17 | #include <vector> |
18 | |
19 | //<- |
20 | #include <boost/test/test_tools.hpp> |
21 | #include <boost/test/unit_test.hpp> |
22 | |
23 | #include <boost/range/algorithm_ext/push_back.hpp> |
24 | |
25 | namespace |
26 | { |
27 | //-> |
28 | struct double_int |
29 | { |
30 | typedef int result_type; |
31 | int operator()(int x) const { return x * 2; } |
32 | }; |
33 | |
34 | //<- |
35 | void transformed_example_test() |
36 | //-> |
37 | //=int main(int argc, const char* argv[]) |
38 | { |
39 | using namespace boost::adaptors; |
40 | using namespace boost::assign; |
41 | |
42 | std::vector<int> input; |
43 | input += 1,2,3,4,5,6,7,8,9,10; |
44 | |
45 | boost::copy( |
46 | rng: input | transformed(double_int()), |
47 | out: std::ostream_iterator<int>(std::cout, "," )); |
48 | |
49 | //= return 0; |
50 | //=} |
51 | //] |
52 | std::vector<int> reference; |
53 | reference += 2,4,6,8,10,12,14,16,18,20; |
54 | |
55 | std::vector<int> test; |
56 | boost::push_back(on&: test, from: input | transformed(double_int())); |
57 | |
58 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
59 | test.begin(), test.end() ); |
60 | } |
61 | } |
62 | |
63 | boost::unit_test::test_suite* |
64 | init_unit_test_suite(int argc, char* argv[]) |
65 | { |
66 | boost::unit_test::test_suite* test |
67 | = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.transformed_example" ); |
68 | |
69 | test->add( BOOST_TEST_CASE( &transformed_example_test ) ); |
70 | |
71 | return test; |
72 | } |
73 | |