copy_n.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/copy_n.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/range/iterator.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <vector>
  18. namespace
  19. {
  20. template< class Container >
  21. void test_copy_n_impl()
  22. {
  23. std::vector<std::size_t> source;
  24. for (std::size_t i = 0; i < 10; ++i)
  25. source.push_back(i);
  26. for (std::size_t k = 0; k < 10; ++k)
  27. {
  28. std::vector<std::size_t> reference;
  29. for (std::size_t j = 0; j < k; ++j)
  30. reference.push_back(j);
  31. Container test;
  32. boost::copy_n(source, k, std::back_inserter(test));
  33. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  34. test.begin(), test.end() );
  35. }
  36. }
  37. void test_copy_n()
  38. {
  39. test_copy_n_impl< std::vector<std::size_t> >();
  40. test_copy_n_impl< std::list<std::size_t> >();
  41. }
  42. }
  43. boost::unit_test::test_suite*
  44. init_unit_test_suite(int argc, char* argv[])
  45. {
  46. boost::unit_test::test_suite* test
  47. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.copy_n" );
  48. test->add( BOOST_TEST_CASE( &test_copy_n ) );
  49. return test;
  50. }