]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/range/test/adaptor_test/transformed.cpp
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)
9 // For more information, see http://www.boost.org/libs/range/
11 #include <boost/range/adaptor/transformed.hpp>
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
16 #include <boost/assign.hpp>
17 #include <boost/bind.hpp>
18 #include <boost/range/algorithm_ext.hpp>
31 typedef int result_type
;
32 int operator()(int x
) const { return x
* 2; }
37 typedef int result_type
;
38 int operator()(int x
) const { return x
/ 2; }
47 typedef int result_type
;
49 lambda(const lambda_init
& init
) {}
50 lambda(const lambda
& rhs
) {}
52 int operator()(int x
) const { return x
+ 1; }
56 lambda
& operator=(const lambda
& rhs
) { return *this; }
59 template< class Container
, class TransformFn
>
60 void transformed_test_impl_core( Container
& c
, TransformFn fn
)
62 using namespace boost::adaptors
;
64 std::vector
< int > test_result1
;
65 boost::push_back(test_result1
, c
| transformed(fn
));
67 std::vector
< int > test_result2
;
68 boost::push_back(test_result2
, adaptors::transform(c
, fn
));
70 std::vector
< int > reference
;
71 std::transform(c
.begin(), c
.end(), std::back_inserter(reference
), fn
);
73 BOOST_CHECK_EQUAL_COLLECTIONS( reference
.begin(), reference
.end(),
74 test_result1
.begin(), test_result1
.end() );
76 BOOST_CHECK_EQUAL_COLLECTIONS( reference
.begin(), reference
.end(),
77 test_result2
.begin(), test_result2
.end() );
81 void check_copy_assign(Rng r
)
87 template< class Container
, class TransformFn
>
88 void transformed_range_copy_assign(Container
& c
, TransformFn fn
)
90 using namespace boost::adaptors
;
91 check_copy_assign(c
| transformed(fn
));
92 check_copy_assign(adaptors::transform(c
, fn
));
95 template< class Container
, class TransformFn
, class TransformFnInit
>
96 void transformed_test_fn_impl()
98 using namespace boost::assign
;
101 TransformFnInit init
;
102 TransformFn
fn( init
);
105 transformed_test_impl_core(c
, fn
);
109 transformed_test_impl_core(c
, fn
);
111 // Test many elements
112 c
+= 1,1,1,2,2,2,2,2,3,4,5,6,7,8,9;
113 transformed_test_impl_core(c
, fn
);
115 // test the range and iterator are copy assignable
116 transformed_range_copy_assign(c
, fn
);
119 template< class Container
>
120 void transformed_test_impl()
122 transformed_test_fn_impl
< Container
, double_x
, double_x
>();
123 transformed_test_fn_impl
< Container
, halve_x
, halve_x
>();
124 transformed_test_fn_impl
< Container
, lambda
, lambda_init
>();
127 void transformed_test()
129 transformed_test_impl
< std::vector
< int > >();
130 transformed_test_impl
< std::list
< int > >();
131 transformed_test_impl
< std::set
< int > >();
132 transformed_test_impl
< std::multiset
< int > >();
137 int foo() const { return 7; }
140 void transformed_bind()
142 using namespace boost::adaptors
;
144 std::vector
<foo_bind
> input(5);
145 std::vector
<int> output
;
146 boost::range::push_back(
148 input
| transformed(boost::bind(&foo_bind::foo
, _1
)));
150 BOOST_CHECK_EQUAL(output
.size(), input
.size());
152 std::vector
<int> reference_output(5, 7);
153 BOOST_CHECK_EQUAL_COLLECTIONS(
154 output
.begin(), output
.end(),
155 reference_output
.begin(), reference_output
.end());
160 boost::unit_test::test_suite
*
161 init_unit_test_suite(int argc
, char* argv
[])
163 boost::unit_test::test_suite
* test
164 = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.transformed" );
166 test
->add(BOOST_TEST_CASE(&boost::transformed_test
));
167 test
->add(BOOST_TEST_CASE(&boost::transformed_bind
));