push_back.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_back.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. template< class Container >
  22. void test_push_back_impl(std::size_t n)
  23. {
  24. Container reference;
  25. for (std::size_t i = 0; i < n; ++i)
  26. reference.push_back(i);
  27. Container test;
  28. boost::push_back(test, boost::irange<std::size_t>(0, n));
  29. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  30. test.begin(), test.end() );
  31. // Do it again to push onto non-empty container
  32. for (std::size_t j = 0; j < n; ++j)
  33. reference.push_back(j);
  34. boost::push_back(test, boost::irange<std::size_t>(0, n));
  35. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  36. test.begin(), test.end() );
  37. }
  38. template< class Container >
  39. void test_push_back_impl()
  40. {
  41. test_push_back_impl< Container >(0);
  42. test_push_back_impl< Container >(1);
  43. test_push_back_impl< Container >(2);
  44. test_push_back_impl< Container >(100);
  45. }
  46. void test_push_back()
  47. {
  48. test_push_back_impl< std::vector<std::size_t> >();
  49. test_push_back_impl< std::list<std::size_t> >();
  50. }
  51. }
  52. boost::unit_test::test_suite*
  53. init_unit_test_suite(int argc, char* argv[])
  54. {
  55. boost::unit_test::test_suite* test
  56. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.push_back" );
  57. test->add( BOOST_TEST_CASE( &test_push_back ) );
  58. return test;
  59. }