copied.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/adaptor/copied.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <algorithm>
  16. #include <deque>
  17. #include <string>
  18. #include <vector>
  19. #include <boost/range/algorithm_ext.hpp>
  20. namespace boost
  21. {
  22. namespace
  23. {
  24. template< class Container >
  25. void copied_test_impl( Container& c )
  26. {
  27. using namespace boost::adaptors;
  28. // This is my preferred syntax using the | operator.
  29. std::vector< int > test_result1;
  30. boost::push_back(test_result1, c | copied(0u, c.size()));
  31. // This is the alternative syntax preferred by some.
  32. std::vector< int > test_result2;
  33. boost::push_back(test_result2, adaptors::copy(c, 0u, c.size()));
  34. BOOST_CHECK_EQUAL_COLLECTIONS( test_result1.begin(), test_result1.end(),
  35. c.begin(), c.end() );
  36. BOOST_CHECK_EQUAL_COLLECTIONS( test_result2.begin(), test_result2.end(),
  37. c.begin(), c.end() );
  38. }
  39. template< class Container >
  40. void copied_test_impl()
  41. {
  42. using namespace boost::assign;
  43. Container c;
  44. // test empty collection
  45. copied_test_impl(c);
  46. // test one element
  47. c += 1;
  48. copied_test_impl(c);
  49. // test many elements
  50. c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
  51. copied_test_impl(c);
  52. }
  53. void copied_test()
  54. {
  55. copied_test_impl< std::vector< int > >();
  56. copied_test_impl< std::deque< int > >();
  57. }
  58. }
  59. }
  60. boost::unit_test::test_suite*
  61. init_unit_test_suite(int argc, char* argv[])
  62. {
  63. boost::unit_test::test_suite* test
  64. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.copied" );
  65. test->add( BOOST_TEST_CASE( &boost::copied_test ) );
  66. return test;
  67. }