remove_copy.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.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<typename Iterator, typename Value>
  25. void test_append(Iterator target, Value value)
  26. {
  27. *target++ = value;
  28. }
  29. template< class Container, class Value >
  30. void test_remove_copy_impl( const Container& c, Value to_remove )
  31. {
  32. typedef typename boost::range_value<Container>::type value_type;
  33. std::vector<value_type> reference;
  34. typedef BOOST_DEDUCED_TYPENAME std::vector<value_type>::iterator
  35. iterator_t BOOST_RANGE_UNUSED;
  36. test_append(
  37. std::remove_copy(c.begin(), c.end(),
  38. std::back_inserter(reference), to_remove),
  39. to_remove);
  40. std::vector<value_type> test;
  41. test_append(
  42. boost::remove_copy(c, std::back_inserter(test), to_remove),
  43. to_remove);
  44. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  45. test.begin(), test.end() );
  46. std::vector<value_type> test2;
  47. test_append(
  48. boost::remove_copy(boost::make_iterator_range(c),
  49. std::back_inserter(test2), to_remove),
  50. to_remove);
  51. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  52. test2.begin(), test2.end() );
  53. }
  54. template< class Container >
  55. void test_remove_copy_impl()
  56. {
  57. using namespace boost::assign;
  58. Container cont;
  59. test_remove_copy_impl(cont, 0);
  60. cont.clear();
  61. cont += 1;
  62. test_remove_copy_impl(cont, 0);
  63. test_remove_copy_impl(cont, 1);
  64. cont.clear();
  65. cont += 1,1,1,1,1;
  66. test_remove_copy_impl(cont, 0);
  67. test_remove_copy_impl(cont, 1);
  68. cont.clear();
  69. cont += 1,2,3,4,5,6,7,8,9;
  70. test_remove_copy_impl(cont, 1);
  71. test_remove_copy_impl(cont, 9);
  72. test_remove_copy_impl(cont, 4);
  73. }
  74. void test_remove_copy()
  75. {
  76. test_remove_copy_impl< std::vector<int> >();
  77. test_remove_copy_impl< std::list<int> >();
  78. test_remove_copy_impl< std::deque<int> >();
  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_copy" );
  86. test->add( BOOST_TEST_CASE( &test_remove_copy ) );
  87. return test;
  88. }