remove.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.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 Value >
  27. void test_remove_impl( const Container& c, Value to_remove )
  28. {
  29. Container reference(c);
  30. typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
  31. iterator_t reference_it
  32. = std::remove(reference.begin(), reference.end(), to_remove);
  33. Container test(c);
  34. iterator_t test_it = boost::remove(test, to_remove);
  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(test2, to_remove);
  41. BOOST_CHECK_EQUAL( std::distance(test2.begin(), test_it2),
  42. std::distance(reference.begin(), reference_it) );
  43. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  44. test2.begin(), test2.end() );
  45. }
  46. template< class Container >
  47. void test_remove_impl()
  48. {
  49. using namespace boost::assign;
  50. Container cont;
  51. test_remove_impl(cont, 0);
  52. cont.clear();
  53. cont += 1;
  54. test_remove_impl(cont, 0);
  55. test_remove_impl(cont, 1);
  56. cont.clear();
  57. cont += 1,1,1,1,1;
  58. test_remove_impl(cont, 0);
  59. test_remove_impl(cont, 1);
  60. cont.clear();
  61. cont += 1,2,3,4,5,6,7,8,9;
  62. test_remove_impl(cont, 1);
  63. test_remove_impl(cont, 9);
  64. test_remove_impl(cont, 4);
  65. }
  66. void test_remove()
  67. {
  68. test_remove_impl< std::vector<int> >();
  69. test_remove_impl< std::list<int> >();
  70. test_remove_impl< std::deque<int> >();
  71. }
  72. }
  73. }
  74. boost::unit_test::test_suite*
  75. init_unit_test_suite(int argc, char* argv[])
  76. {
  77. boost::unit_test::test_suite* test
  78. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.remove" );
  79. test->add( BOOST_TEST_CASE( &boost::test_remove ) );
  80. return test;
  81. }