remove_if.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Boost.Range library
  2. //
  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)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/algorithm/remove_if.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/bind.hpp>
  16. #include <algorithm>
  17. #include <functional>
  18. #include <list>
  19. #include <numeric>
  20. #include <deque>
  21. #include <vector>
  22. namespace boost
  23. {
  24. namespace
  25. {
  26. template< class Container, class UnaryPredicate >
  27. void test_remove_if_impl( const Container& c, UnaryPredicate pred )
  28. {
  29. Container reference(c);
  30. typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
  31. iterator_t reference_it
  32. = std::remove_if(reference.begin(), reference.end(), pred);
  33. Container test(c);
  34. iterator_t test_it = boost::remove_if(test, pred);
  35. BOOST_CHECK_EQUAL( std::distance(test.begin(), test_it),
  36. std::distance(reference.begin(), reference_it) );
  37. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  38. test.begin(), test.end() );
  39. Container test2(c);
  40. iterator_t test_it2 = boost::remove_if(
  41. boost::make_iterator_range(test2), pred);
  42. BOOST_CHECK_EQUAL( std::distance(test2.begin(), test_it2),
  43. std::distance(reference.begin(), reference_it) );
  44. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  45. test2.begin(), test2.end() );
  46. }
  47. template< class Container >
  48. void test_remove_if_( const Container& c, int to_remove )
  49. {
  50. test_remove_if_impl(c, boost::bind(std::equal_to<int>(), _1, to_remove));
  51. test_remove_if_impl(c, boost::bind(std::not_equal_to<int>(), _1, to_remove));
  52. }
  53. template< class Container >
  54. void test_remove_if_impl()
  55. {
  56. using namespace boost::assign;
  57. Container cont;
  58. test_remove_if_(cont, 0);
  59. cont.clear();
  60. cont += 1;
  61. test_remove_if_(cont, 0);
  62. test_remove_if_(cont, 1);
  63. cont.clear();
  64. cont += 1,1,1,1,1;
  65. test_remove_if_(cont, 0);
  66. test_remove_if_(cont, 1);
  67. cont.clear();
  68. cont += 1,2,3,4,5,6,7,8,9;
  69. test_remove_if_(cont, 1);
  70. test_remove_if_(cont, 9);
  71. test_remove_if_(cont, 4);
  72. }
  73. inline void test_remove_if()
  74. {
  75. test_remove_if_impl< std::vector<int> >();
  76. test_remove_if_impl< std::list<int> >();
  77. test_remove_if_impl< std::deque<int> >();
  78. }
  79. }
  80. }
  81. boost::unit_test::test_suite*
  82. init_unit_test_suite(int argc, char* argv[])
  83. {
  84. boost::unit_test::test_suite* test
  85. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.remove_if" );
  86. test->add( BOOST_TEST_CASE( &boost::test_remove_if ) );
  87. return test;
  88. }