copy_n.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/algorithm/copy_n.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/range/iterator.hpp>
  16. #include <algorithm>
  17. #include <list>
  18. #include <set>
  19. #include <vector>
  20. namespace
  21. {
  22. template< class Container >
  23. void test_copy_n_impl()
  24. {
  25. Container source;
  26. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  27. std::vector<value_t> target;
  28. target.resize(source.size());
  29. typedef BOOST_DEDUCED_TYPENAME range_iterator< std::vector<value_t> >::type iterator_t;
  30. iterator_t it = boost::copy(source, target.begin());
  31. BOOST_CHECK( it == target.end() );
  32. BOOST_CHECK_EQUAL_COLLECTIONS(
  33. target.begin(), target.end(),
  34. source.begin(), source.end()
  35. );
  36. it = boost::copy(boost::make_iterator_range(source), target.begin());
  37. BOOST_CHECK( it == target.end() );
  38. BOOST_CHECK_EQUAL_COLLECTIONS(target.begin(), target.end(),
  39. source.begin(), source.end());
  40. }
  41. void test_copy_n()
  42. {
  43. test_copy_n_impl< std::vector<int> >();
  44. test_copy_n_impl< std::list<int> >();
  45. test_copy_n_impl< std::set<int> >();
  46. test_copy_n_impl< std::multiset<int> >();
  47. }
  48. }
  49. boost::unit_test::test_suite*
  50. init_unit_test_suite(int argc, char* argv[])
  51. {
  52. boost::unit_test::test_suite* test
  53. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.copy_n" );
  54. test->add( BOOST_TEST_CASE( &::test_copy_n ) );
  55. return test;
  56. }