push_front.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include <boost/range/algorithm_ext/push_front.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/range/iterator.hpp>
  15. #include <boost/range/irange.hpp>
  16. #include <algorithm>
  17. #include <list>
  18. #include <vector>
  19. namespace
  20. {
  21. struct DoubleValue
  22. {
  23. template< class Value >
  24. Value operator()(Value x)
  25. {
  26. return x * 2;
  27. }
  28. };
  29. template< class Container >
  30. void test_push_front_impl(std::size_t n)
  31. {
  32. Container reference;
  33. for (std::size_t i = 0; i < n; ++i)
  34. reference.push_back(i);
  35. Container test;
  36. boost::push_front(test, boost::irange<std::size_t>(0, n));
  37. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  38. test.begin(), test.end() );
  39. // copy the original reference sequence
  40. Container reference_copy(reference);
  41. std::transform(reference.begin(), reference.end(), reference.begin(), DoubleValue());
  42. // Do it again to push onto non-empty container
  43. reference.insert(reference.end(), reference_copy.begin(), reference_copy.end());
  44. boost::push_front(test, boost::irange<std::size_t>(0, n * 2, 2));
  45. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  46. test.begin(), test.end() );
  47. }
  48. template< class Container >
  49. void test_push_front_impl()
  50. {
  51. test_push_front_impl< Container >(0);
  52. test_push_front_impl< Container >(1);
  53. test_push_front_impl< Container >(2);
  54. test_push_front_impl< Container >(100);
  55. }
  56. void test_push_front()
  57. {
  58. test_push_front_impl< std::vector<std::size_t> >();
  59. test_push_front_impl< std::list<std::size_t> >();
  60. }
  61. }
  62. boost::unit_test::test_suite*
  63. init_unit_test_suite(int argc, char* argv[])
  64. {
  65. boost::unit_test::test_suite* test
  66. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.push_front" );
  67. test->add( BOOST_TEST_CASE( &test_push_front ) );
  68. return test;
  69. }