replace_copy_if.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/replace_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_replace_copy_if_impl( const Container& c, UnaryPredicate pred )
  31. {
  32. typedef BOOST_DEDUCED_TYPENAME boost::range_value<const Container>::type value_type;
  33. const value_type replace_with = value_type();
  34. std::vector<value_type> reference;
  35. test_append(
  36. std::replace_copy_if(c.begin(), c.end(), std::back_inserter(reference), pred, replace_with),
  37. value_type()
  38. );
  39. std::vector<value_type> test;
  40. test_append(
  41. boost::replace_copy_if(c, std::back_inserter(test), pred, replace_with),
  42. value_type()
  43. );
  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::replace_copy_if(boost::make_iterator_range(c),
  49. std::back_inserter(test2), pred,
  50. replace_with),
  51. value_type()
  52. );
  53. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  54. test2.begin(), test2.end() );
  55. }
  56. template< class Container >
  57. void test_replace_copy_if_( const Container& c, int to_replace )
  58. {
  59. test_replace_copy_if_impl(c, boost::bind(std::equal_to<int>(), _1, to_replace));
  60. test_replace_copy_if_impl(c, boost::bind(std::not_equal_to<int>(), _1, to_replace));
  61. }
  62. template< class Container >
  63. void test_replace_copy_if_impl()
  64. {
  65. using namespace boost::assign;
  66. Container cont;
  67. test_replace_copy_if_(cont, 0);
  68. cont.clear();
  69. cont += 1;
  70. test_replace_copy_if_(cont, 0);
  71. test_replace_copy_if_(cont, 1);
  72. cont.clear();
  73. cont += 1,1,1,1,1;
  74. test_replace_copy_if_(cont, 0);
  75. test_replace_copy_if_(cont, 1);
  76. cont.clear();
  77. cont += 1,2,3,4,5,6,7,8,9;
  78. test_replace_copy_if_(cont, 1);
  79. test_replace_copy_if_(cont, 9);
  80. test_replace_copy_if_(cont, 4);
  81. }
  82. inline void test_replace_copy_if()
  83. {
  84. test_replace_copy_if_impl< std::vector<int> >();
  85. test_replace_copy_if_impl< std::list<int> >();
  86. test_replace_copy_if_impl< std::deque<int> >();
  87. }
  88. }
  89. boost::unit_test::test_suite*
  90. init_unit_test_suite(int argc, char* argv[])
  91. {
  92. boost::unit_test::test_suite* test
  93. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.replace_copy_if" );
  94. test->add( BOOST_TEST_CASE( &test_replace_copy_if ) );
  95. return test;
  96. }