| 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/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_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 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 | typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t; |
| 42 | |
| 43 | const Container old_cont(cont); |
| 44 | Container cont2(old_cont); |
| 45 | iter_t result = boost::partition(cont, UnaryPredicate()); |
| 46 | |
| 47 | boost::partition(cont2, UnaryPredicate()); |
| 48 | cont2 = old_cont; |
| 49 | boost::partition( |
| 50 | boost::make_iterator_range(cont2), UnaryPredicate()); |
| 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 | |
| 69 | const Container old_cont(cont); |
| 70 | Container cont2(old_cont); |
| 71 | result_t result = boost::partition<return_type>(cont, policy.pred()); |
| 72 | |
| 73 | // Test that operation a temporary created by using |
| 74 | // make_iterator_range. |
| 75 | boost::partition<return_type>( |
| 76 | boost::make_iterator_range(cont2), policy.pred()); |
| 77 | |
| 78 | BOOST_CHECK_EQUAL_COLLECTIONS( cont.begin(), cont.end(), |
| 79 | cont2.begin(), cont2.end() ); |
| 80 | |
| 81 | return result; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | template< class Container > |
| 86 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
| 87 | reference(Container& cont) |
| 88 | { |
| 89 | return std::partition(cont.begin(), cont.end(), UnaryPredicate()); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | template<class Container> |
| 94 | void test_partition_impl() |
| 95 | { |
| 96 | using namespace boost::assign; |
| 97 | |
| 98 | boost::range_test::range_return_test_driver test_driver; |
| 99 | |
| 100 | partition_test_policy< equal_to_5 > policy; |
| 101 | |
| 102 | Container cont; |
| 103 | test_driver(cont, policy); |
| 104 | |
| 105 | cont.clear(); |
| 106 | cont += 1; |
| 107 | test_driver(cont, policy); |
| 108 | |
| 109 | cont.clear(); |
| 110 | cont += 1,2,2,2,2,2,3,4,5,6,7,8,9; |
| 111 | test_driver(cont, policy); |
| 112 | |
| 113 | cont.clear(); |
| 114 | cont += 1,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,5,6,7,8,9; |
| 115 | test_driver(cont, policy); |
| 116 | } |
| 117 | |
| 118 | void test_partition() |
| 119 | { |
| 120 | test_partition_impl< std::vector<int> >(); |
| 121 | test_partition_impl< std::list<int> >(); |
| 122 | test_partition_impl< std::deque<int> >(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | boost::unit_test::test_suite* |
| 127 | init_unit_test_suite(int argc, char* argv[]) |
| 128 | { |
| 129 | boost::unit_test::test_suite* test |
| 130 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.partition" ); |
| 131 | |
| 132 | test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_partition::test_partition ) ); |
| 133 | |
| 134 | return test; |
| 135 | } |
| 136 | |