reverse_copy.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #include <boost/range/algorithm/reverse_copy.hpp>
  10. #include <boost/test/test_tools.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/assign.hpp>
  13. #include <boost/bind.hpp>
  14. #include <algorithm>
  15. #include <functional>
  16. #include <list>
  17. #include <numeric>
  18. #include <deque>
  19. #include <vector>
  20. namespace
  21. {
  22. template<class OutputIterator, class Value>
  23. void test_append(OutputIterator out, Value value)
  24. {
  25. *out++ = value;
  26. }
  27. template<class Container>
  28. void test_reverse_copy_impl(Container& cont)
  29. {
  30. typedef BOOST_DEDUCED_TYPENAME boost::range_value<Container>::type value_type;
  31. std::vector<value_type> reference;
  32. std::vector<value_type> test;
  33. test_append(
  34. std::reverse_copy(cont.begin(), cont.end(), std::back_inserter(reference)),
  35. value_type()
  36. );
  37. test_append(
  38. boost::reverse_copy(cont, std::back_inserter(test)),
  39. value_type()
  40. );
  41. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  42. test.begin(), test.end() );
  43. test.clear();
  44. test_append(
  45. boost::reverse_copy(boost::make_iterator_range(cont),
  46. std::back_inserter(test)),
  47. value_type()
  48. );
  49. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  50. test.begin(), test.end() );
  51. }
  52. template<class Container>
  53. void test_reverse_copy_impl()
  54. {
  55. using namespace boost::assign;
  56. Container cont;
  57. test_reverse_copy_impl(cont);
  58. cont.clear();
  59. cont += 1;
  60. test_reverse_copy_impl(cont);
  61. cont.clear();
  62. cont += 1,2,3,4,5,6,7,8,9;
  63. test_reverse_copy_impl(cont);
  64. }
  65. void test_reverse_copy()
  66. {
  67. test_reverse_copy_impl< std::vector<int> >();
  68. test_reverse_copy_impl< std::list<int> >();
  69. test_reverse_copy_impl< std::deque<int> >();
  70. }
  71. }
  72. boost::unit_test::test_suite*
  73. init_unit_test_suite(int argc, char* argv[])
  74. {
  75. boost::unit_test::test_suite* test
  76. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.reverse_copy" );
  77. test->add( BOOST_TEST_CASE( &::test_reverse_copy ) );
  78. return test;
  79. }