strided2.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // This test was added due to a report that the Range Adaptors:
  12. // 1. Caused havoc when using namespace boost::adaptors was used
  13. // 2. Did not work with non-member functions
  14. // 3. The strided adaptor could not be composed with sliced
  15. //
  16. // None of these issues could be reproduced on GCC 4.4, but this
  17. // work makes for useful additional test coverage since this
  18. // uses chaining of adaptors and non-member functions whereas
  19. // most of the tests avoid this use case.
  20. #include <boost/range/adaptor/strided.hpp>
  21. #include <boost/range/adaptor/sliced.hpp>
  22. #include <boost/range/adaptor/transformed.hpp>
  23. #include <boost/range/irange.hpp>
  24. #include <boost/test/test_tools.hpp>
  25. #include <boost/test/unit_test.hpp>
  26. #include <boost/assign.hpp>
  27. #include <boost/range/algorithm_ext.hpp>
  28. #include <algorithm>
  29. #include <vector>
  30. namespace boost
  31. {
  32. namespace
  33. {
  34. int times_two(int x) { return x * 2; }
  35. void strided_test2()
  36. {
  37. using namespace boost::adaptors;
  38. using namespace boost::assign;
  39. std::vector<int> v;
  40. boost::push_back(v, boost::irange(0,10));
  41. std::vector<int> z;
  42. boost::push_back(z, v | sliced(2,6) | strided(2) | transformed(&times_two));
  43. std::vector<int> reference;
  44. reference += 4,8;
  45. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  46. z.begin(), z.end() );
  47. }
  48. }
  49. }
  50. boost::unit_test::test_suite*
  51. init_unit_test_suite(int argc, char* argv[])
  52. {
  53. boost::unit_test::test_suite* test
  54. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.strided2" );
  55. test->add( BOOST_TEST_CASE( &boost::strided_test2 ) );
  56. return test;
  57. }