| 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) |
| 5 | // |
| 6 | // |
| 7 | // For more information, see http://www.boost.org/libs/range/ |
| 8 | // |
| 9 | #include <boost/range/algorithm/stable_partition.hpp> |
| 10 | |
| 11 | #include <boost/test/test_tools.hpp> |
| 12 | #include <boost/test/unit_test.hpp> |
| 13 | |
| 14 | #include <boost/assign.hpp> |
| 15 | #include "../test_driver/range_return_test_driver.hpp" |
| 16 | #include <algorithm> |
| 17 | #include <functional> |
| 18 | #include <list> |
| 19 | #include <numeric> |
| 20 | #include <deque> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace boost_range_test_algorithm_stable_partition |
| 24 | { |
| 25 | struct equal_to_5 |
| 26 | { |
| 27 | typedef bool result_type; |
| 28 | typedef int argument_type; |
| 29 | bool operator()(int x) const { return x == 5; } |
| 30 | }; |
| 31 | |
| 32 | // test the 'partition' algorithm |
| 33 | template<class UnaryPredicate> |
| 34 | class stable_partition_test_policy |
| 35 | { |
| 36 | public: |
| 37 | template< class Container > |
| 38 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
| 39 | test_iter(Container& cont) |
| 40 | { |
| 41 | Container cont2(cont); |
| 42 | |
| 43 | typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t; |
| 44 | iter_t result = boost::stable_partition(cont, UnaryPredicate()); |
| 45 | |
| 46 | iter_t temp_result = boost::stable_partition( |
| 47 | boost::make_iterator_range(cont2), UnaryPredicate()); |
| 48 | |
| 49 | BOOST_CHECK_EQUAL( std::distance(cont.begin(), result), |
| 50 | std::distance(cont2.begin(), temp_result) ); |
| 51 | |
| 52 | BOOST_CHECK_EQUAL_COLLECTIONS( cont.begin(), cont.end(), |
| 53 | cont2.begin(), cont2.end() ); |
| 54 | |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | UnaryPredicate pred() const { return UnaryPredicate(); } |
| 59 | |
| 60 | template< boost::range_return_value return_type > |
| 61 | struct test_range |
| 62 | { |
| 63 | template< class Container, class Policy > |
| 64 | BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type |
| 65 | operator()(Policy& policy, Container& cont) |
| 66 | { |
| 67 | typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t; |
| 68 | Container cont2(cont); |
| 69 | result_t result = boost::stable_partition<return_type>(cont, policy.pred()); |
| 70 | |
| 71 | result_t result2 = boost::stable_partition<return_type>( |
| 72 | boost::make_iterator_range(cont2), policy.pred()); |
| 73 | |
| 74 | boost::ignore_unused_variable_warning(result2); |
| 75 | |
| 76 | BOOST_CHECK_EQUAL_COLLECTIONS( cont2.begin(), cont2.end(), |
| 77 | cont.begin(), cont.end() ); |
| 78 | |
| 79 | return result; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | template< class Container > |
| 84 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
| 85 | reference(Container& cont) |
| 86 | { |
| 87 | return std::stable_partition(cont.begin(), cont.end(), UnaryPredicate()); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | template<class Container> |
| 92 | void test_stable_partition_impl() |
| 93 | { |
| 94 | using namespace boost::assign; |
| 95 | |
| 96 | boost::range_test::range_return_test_driver test_driver; |
| 97 | |
| 98 | stable_partition_test_policy< equal_to_5 > policy; |
| 99 | |
| 100 | Container cont; |
| 101 | test_driver(cont, policy); |
| 102 | |
| 103 | cont.clear(); |
| 104 | cont += 1; |
| 105 | test_driver(cont, policy); |
| 106 | |
| 107 | cont.clear(); |
| 108 | cont += 1,2,2,2,2,2,3,4,5,6,7,8,9; |
| 109 | test_driver(cont, policy); |
| 110 | |
| 111 | cont.clear(); |
| 112 | cont += 1,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,5,6,7,8,9; |
| 113 | test_driver(cont, policy); |
| 114 | } |
| 115 | |
| 116 | void test_stable_partition() |
| 117 | { |
| 118 | test_stable_partition_impl< std::vector<int> >(); |
| 119 | test_stable_partition_impl< std::list<int> >(); |
| 120 | test_stable_partition_impl< std::deque<int> >(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | boost::unit_test::test_suite* |
| 125 | init_unit_test_suite(int argc, char* argv[]) |
| 126 | { |
| 127 | boost::unit_test::test_suite* test |
| 128 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.stable_partition" ); |
| 129 | |
| 130 | test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_stable_partition::test_stable_partition ) ); |
| 131 | |
| 132 | return test; |
| 133 | } |
| 134 | |