remove_copy_if.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_copy_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
  23. {
  24. template< class Iterator, class Value >
  25. void test_append(Iterator target, Value value)
  26. {
  27. *target++ = value;
  28. }
  29. template< class Container, class UnaryPredicate >
  30. void test_remove_copy_if_impl( const Container& c, UnaryPredicate pred )
  31. {
  32. typedef BOOST_DEDUCED_TYPENAME boost::range_value<const Container>::type value_type;
  33. std::vector<value_type> reference;
  34. test_append(
  35. std::remove_copy_if(c.begin(), c.end(), std::back_inserter(reference), pred),
  36. value_type()
  37. );
  38. std::vector<value_type> test;
  39. test_append(
  40. boost::remove_copy_if(c, std::back_inserter(test), pred),
  41. value_type()
  42. );
  43. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  44. test.begin(), test.end() );
  45. std::vector<value_type> test2;
  46. test_append(
  47. boost::remove_copy_if(boost::make_iterator_range(c),
  48. std::back_inserter(test2), pred),
  49. value_type()
  50. );
  51. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  52. test2.begin(), test2.end() );
  53. }
  54. template< class Container >
  55. void test_remove_copy_if_( const Container& c, int to_remove )
  56. {
  57. test_remove_copy_if_impl(c, boost::bind(std::equal_to<int>(), _1, to_remove));
  58. test_remove_copy_if_impl(c, boost::bind(std::not_equal_to<int>(), _1, to_remove));
  59. }
  60. template< class Container >
  61. void test_remove_copy_if_impl()
  62. {
  63. using namespace boost::assign;
  64. Container cont;
  65. test_remove_copy_if_(cont, 0);
  66. cont.clear();
  67. cont += 1;
  68. test_remove_copy_if_(cont, 0);
  69. test_remove_copy_if_(cont, 1);
  70. cont.clear();
  71. cont += 1,1,1,1,1;
  72. test_remove_copy_if_(cont, 0);
  73. test_remove_copy_if_(cont, 1);
  74. cont.clear();
  75. cont += 1,2,3,4,5,6,7,8,9;
  76. test_remove_copy_if_(cont, 1);
  77. test_remove_copy_if_(cont, 9);
  78. test_remove_copy_if_(cont, 4);
  79. }
  80. inline void test_remove_copy_if()
  81. {
  82. test_remove_copy_if_impl< std::vector<int> >();
  83. test_remove_copy_if_impl< std::list<int> >();
  84. test_remove_copy_if_impl< std::deque<int> >();
  85. }
  86. }
  87. boost::unit_test::test_suite*
  88. init_unit_test_suite(int argc, char* argv[])
  89. {
  90. boost::unit_test::test_suite* test
  91. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.remove_copy_if" );
  92. test->add( BOOST_TEST_CASE( &test_remove_copy_if ) );
  93. return test;
  94. }