replace.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <deque>
  18. #include <vector>
  19. namespace boost
  20. {
  21. namespace
  22. {
  23. template< class Container >
  24. void test_replace_impl(Container& cont)
  25. {
  26. const int what = 2;
  27. const int with_what = 5;
  28. std::vector<int> reference(cont.begin(), cont.end());
  29. std::replace(reference.begin(), reference.end(), what, with_what);
  30. std::vector<int> target(cont.begin(), cont.end());
  31. boost::replace(target, what, with_what);
  32. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  33. target.begin(), target.end() );
  34. std::vector<int> target2(cont.begin(), cont.end());
  35. boost::replace(boost::make_iterator_range(target2), what,
  36. with_what);
  37. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  38. target2.begin(), target2.end() );
  39. }
  40. template< class Container >
  41. void test_replace_impl()
  42. {
  43. using namespace boost::assign;
  44. Container cont;
  45. test_replace_impl(cont);
  46. cont.clear();
  47. cont += 1;
  48. test_replace_impl(cont);
  49. cont.clear();
  50. cont += 1,2,3,4,5,6,7,8,9;
  51. test_replace_impl(cont);
  52. }
  53. void test_replace()
  54. {
  55. test_replace_impl< std::vector<int> >();
  56. test_replace_impl< std::list<int> >();
  57. test_replace_impl< std::deque<int> >();
  58. }
  59. }
  60. }
  61. boost::unit_test::test_suite*
  62. init_unit_test_suite(int argc, char* argv[])
  63. {
  64. boost::unit_test::test_suite* test
  65. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.replace" );
  66. test->add( BOOST_TEST_CASE( &boost::test_replace ) );
  67. return test;
  68. }