stable_partition.cpp 4.2 KB

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