replace_copy.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.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_replace_copy_impl( const Container& c, Value to_replace )
  31. {
  32. const Value replace_with = to_replace * 2;
  33. typedef typename boost::range_value<Container>::type value_type;
  34. std::vector<value_type> reference;
  35. typedef BOOST_DEDUCED_TYPENAME std::vector<value_type>::iterator
  36. iterator_t BOOST_RANGE_UNUSED;
  37. test_append(
  38. std::replace_copy(c.begin(), c.end(),
  39. std::back_inserter(reference), to_replace, replace_with),
  40. to_replace);
  41. std::vector<value_type> test;
  42. test_append(
  43. boost::replace_copy(c, std::back_inserter(test), to_replace, replace_with),
  44. to_replace);
  45. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  46. test.begin(), test.end() );
  47. std::vector<value_type> test2;
  48. test_append(
  49. boost::replace_copy(boost::make_iterator_range(c),
  50. std::back_inserter(test2), to_replace,
  51. replace_with),
  52. to_replace);
  53. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  54. test2.begin(), test2.end() );
  55. }
  56. template< class Container >
  57. void test_replace_copy_impl()
  58. {
  59. using namespace boost::assign;
  60. Container cont;
  61. test_replace_copy_impl(cont, 0);
  62. cont.clear();
  63. cont += 1;
  64. test_replace_copy_impl(cont, 0);
  65. test_replace_copy_impl(cont, 1);
  66. cont.clear();
  67. cont += 1,1,1,1,1;
  68. test_replace_copy_impl(cont, 0);
  69. test_replace_copy_impl(cont, 1);
  70. cont.clear();
  71. cont += 1,2,3,4,5,6,7,8,9;
  72. test_replace_copy_impl(cont, 1);
  73. test_replace_copy_impl(cont, 9);
  74. test_replace_copy_impl(cont, 4);
  75. }
  76. void test_replace_copy()
  77. {
  78. test_replace_copy_impl< std::vector<int> >();
  79. test_replace_copy_impl< std::list<int> >();
  80. test_replace_copy_impl< std::deque<int> >();
  81. }
  82. }
  83. boost::unit_test::test_suite*
  84. init_unit_test_suite(int argc, char* argv[])
  85. {
  86. boost::unit_test::test_suite* test
  87. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.replace_copy" );
  88. test->add( BOOST_TEST_CASE( &test_replace_copy ) );
  89. return test;
  90. }