erase.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2010. 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_ext/erase.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/range/iterator.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <vector>
  18. namespace
  19. {
  20. template< class Container >
  21. void test_erase_impl()
  22. {
  23. Container source;
  24. for (int i = 0; i < 10; ++i)
  25. source.push_back(i);
  26. Container reference(source);
  27. Container test(source);
  28. typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
  29. iterator_t first_ref = reference.begin();
  30. iterator_t last_ref = reference.end();
  31. boost::iterator_range< iterator_t > test_range(test.begin(), test.end());
  32. reference.erase(first_ref, last_ref);
  33. boost::erase(test, test_range);
  34. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  35. test.begin(), test.end() );
  36. }
  37. void test_erase()
  38. {
  39. test_erase_impl<std::vector<int> >();
  40. test_erase_impl<std::list<int> >();
  41. }
  42. template< class Container >
  43. void test_remove_erase_impl()
  44. {
  45. Container source;
  46. for (int i = 0; i < 10; ++i)
  47. source.push_back(i);
  48. Container reference(source);
  49. Container test(source);
  50. boost::remove_erase(test, 5);
  51. reference.erase( std::find(reference.begin(), reference.end(), 5) );
  52. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  53. test.begin(), test.end() );
  54. }
  55. void test_remove_erase()
  56. {
  57. test_remove_erase_impl<std::vector<int> >();
  58. test_remove_erase_impl<std::list<int> >();
  59. }
  60. struct is_even
  61. {
  62. typedef bool result_type;
  63. typedef int argument_type;
  64. bool operator()(int x) const { return x % 2 == 0; }
  65. };
  66. template< class Container >
  67. void test_remove_erase_if_impl()
  68. {
  69. Container source;
  70. for (int i = 0; i < 10; ++i)
  71. source.push_back(i);
  72. Container reference;
  73. typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iterator_t;
  74. iterator_t last_source = source.end();
  75. is_even pred;
  76. for (iterator_t it_source = source.begin(); it_source != last_source; ++it_source)
  77. {
  78. if (!pred(*it_source))
  79. reference.push_back(*it_source);
  80. }
  81. Container test(source);
  82. boost::remove_erase_if(test, is_even());
  83. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  84. test.begin(), test.end() );
  85. }
  86. void test_remove_erase_if()
  87. {
  88. test_remove_erase_if_impl<std::vector<int> >();
  89. test_remove_erase_if_impl<std::list<int> >();
  90. }
  91. }
  92. boost::unit_test::test_suite*
  93. init_unit_test_suite(int argc, char* argv[])
  94. {
  95. boost::unit_test::test_suite* test
  96. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.erase" );
  97. test->add( BOOST_TEST_CASE( &test_erase ) );
  98. test->add( BOOST_TEST_CASE( &test_remove_erase ) );
  99. test->add( BOOST_TEST_CASE( &test_remove_erase_if ) );
  100. return test;
  101. }