1 // Copyright Neil Groves 2009. Use, modification and
2 // distribution is subject to the Boost Software License, Version
3 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
7 // For more information, see http://www.boost.org/libs/range/
9 #include <boost/range/algorithm/partition.hpp>
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
14 #include <boost/assign.hpp>
15 #include <boost/bind.hpp>
16 #include "../test_driver/range_return_test_driver.hpp"
24 namespace boost_range_test_algorithm_partition
28 typedef bool result_type
;
29 typedef int argument_type
;
30 bool operator()(int x
) const { return x
== 5; }
33 // test the 'partition' algorithm
34 template<class UnaryPredicate
>
35 class partition_test_policy
38 template< class Container
>
39 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
40 test_iter(Container
& cont
)
42 typedef BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type iter_t
;
44 const Container
old_cont(cont
);
45 Container
cont2(old_cont
);
46 iter_t result
= boost::partition(cont
, UnaryPredicate());
48 boost::partition(cont2
, UnaryPredicate());
51 boost::make_iterator_range(cont2
), UnaryPredicate());
53 BOOST_CHECK_EQUAL_COLLECTIONS( cont
.begin(), cont
.end(),
54 cont2
.begin(), cont2
.end() );
59 UnaryPredicate
pred() const { return UnaryPredicate(); }
61 template< boost::range_return_value return_type
>
64 template< class Container
, class Policy
>
65 BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,return_type
>::type
66 operator()(Policy
& policy
, Container
& cont
)
68 typedef BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,return_type
>::type result_t
;
70 const Container
old_cont(cont
);
71 Container
cont2(old_cont
);
72 result_t result
= boost::partition
<return_type
>(cont
, policy
.pred());
74 // Test that operation a temporary created by using
75 // make_iterator_range.
76 boost::partition
<return_type
>(
77 boost::make_iterator_range(cont2
), policy
.pred());
79 BOOST_CHECK_EQUAL_COLLECTIONS( cont
.begin(), cont
.end(),
80 cont2
.begin(), cont2
.end() );
86 template< class Container
>
87 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
88 reference(Container
& cont
)
90 return std::partition(cont
.begin(), cont
.end(), UnaryPredicate());
94 template<class Container
>
95 void test_partition_impl()
97 using namespace boost::assign
;
99 boost::range_test::range_return_test_driver test_driver
;
101 partition_test_policy
< equal_to_5
> policy
;
104 test_driver(cont
, policy
);
108 test_driver(cont
, policy
);
111 cont
+= 1,2,2,2,2,2,3,4,5,6,7,8,9;
112 test_driver(cont
, policy
);
115 cont
+= 1,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,5,6,7,8,9;
116 test_driver(cont
, policy
);
119 void test_partition()
121 test_partition_impl
< std::vector
<int> >();
122 test_partition_impl
< std::list
<int> >();
123 test_partition_impl
< std::deque
<int> >();
127 boost::unit_test::test_suite
*
128 init_unit_test_suite(int argc
, char* argv
[])
130 boost::unit_test::test_suite
* test
131 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.partition" );
133 test
->add( BOOST_TEST_CASE( &boost_range_test_algorithm_partition::test_partition
) );