replaced_if.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/adaptor/replaced_if.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/range/algorithm_ext.hpp>
  16. #include <algorithm>
  17. #include <list>
  18. #include <set>
  19. #include <vector>
  20. namespace boost
  21. {
  22. namespace
  23. {
  24. struct if_value_is_one
  25. {
  26. bool operator()(int x) const { return x == 1; }
  27. };
  28. template< class Container >
  29. void replaced_if_test_impl( Container& c )
  30. {
  31. using namespace boost::adaptors;
  32. if_value_is_one pred;
  33. const int replacement_value = 0;
  34. std::vector< int > test_result1;
  35. boost::push_back(test_result1, c | replaced_if(pred, replacement_value));
  36. std::vector< int > test_result2;
  37. boost::push_back(test_result2, boost::adaptors::replace_if(c, pred, replacement_value));
  38. std::vector< int > reference( c.begin(), c.end() );
  39. std::replace_if(reference.begin(), reference.end(), pred, replacement_value);
  40. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  41. test_result1.begin(), test_result1.end() );
  42. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  43. test_result2.begin(), test_result2.end() );
  44. }
  45. template< class Container >
  46. void replaced_if_test_impl()
  47. {
  48. using namespace boost::assign;
  49. Container c;
  50. // Test empty
  51. replaced_if_test_impl(c);
  52. // Test one
  53. c += 1;
  54. replaced_if_test_impl(c);
  55. // Test many
  56. c += 1,1,1,2,2,2,3,3,3,3,3,4,5,6,6,6,7,8,9;
  57. replaced_if_test_impl(c);
  58. }
  59. void replaced_if_test()
  60. {
  61. replaced_if_test_impl< std::vector< int > >();
  62. replaced_if_test_impl< std::list< int > >();
  63. replaced_if_test_impl< std::set< int > >();
  64. replaced_if_test_impl< std::multiset< int > >();
  65. }
  66. }
  67. }
  68. boost::unit_test::test_suite*
  69. init_unit_test_suite(int argc, char* argv[])
  70. {
  71. boost::unit_test::test_suite* test
  72. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.replaced_if" );
  73. test->add( BOOST_TEST_CASE( &boost::replaced_if_test ) );
  74. return test;
  75. }