swap_ranges.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/swap_ranges.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/bind.hpp>
  16. #include <algorithm>
  17. #include <functional>
  18. #include <list>
  19. #include <numeric>
  20. #include <deque>
  21. #include <vector>
  22. namespace
  23. {
  24. template<class Container1, class Container2>
  25. void test_swap_ranges_impl(const Container1& source1, const Container2& source2)
  26. {
  27. Container1 reference1(source1);
  28. Container2 reference2(source2);
  29. std::swap_ranges(reference1.begin(), reference1.end(), reference2.begin());
  30. Container1 test1(source1);
  31. Container2 test2(source2);
  32. boost::swap_ranges(test1, test2);
  33. BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
  34. test1.begin(), test1.end() );
  35. BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
  36. test2.begin(), test2.end() );
  37. test1 = source1;
  38. test2 = source2;
  39. boost::swap_ranges(boost::make_iterator_range(test1), test2);
  40. BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
  41. test1.begin(), test1.end() );
  42. BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
  43. test2.begin(), test2.end() );
  44. test1 = source1;
  45. test2 = source2;
  46. boost::swap_ranges(test1, boost::make_iterator_range(test2));
  47. BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
  48. test1.begin(), test1.end() );
  49. BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
  50. test2.begin(), test2.end() );
  51. test1 = source1;
  52. test2 = source2;
  53. boost::swap_ranges(boost::make_iterator_range(test1),
  54. boost::make_iterator_range(test2));
  55. BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
  56. test1.begin(), test1.end() );
  57. BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
  58. test2.begin(), test2.end() );
  59. }
  60. template<class Container1, class Container2>
  61. void test_swap_ranges_impl()
  62. {
  63. using namespace boost::assign;
  64. Container1 c1;
  65. Container2 c2;
  66. test_swap_ranges_impl(c1, c2);
  67. c1.clear();
  68. c1 += 1;
  69. c2.clear();
  70. c2 += 2;
  71. test_swap_ranges_impl(c1, c2);
  72. c1.clear();
  73. c1 += 1,2,3,4,5,6,7,8,9,10;
  74. c2.clear();
  75. c2 += 10,9,8,7,6,5,4,3,2,1;
  76. test_swap_ranges_impl(c1, c2);
  77. }
  78. inline void test_swap_ranges()
  79. {
  80. test_swap_ranges_impl< std::vector<int>, std::vector<int> >();
  81. test_swap_ranges_impl< std::vector<int>, std::list<int> >();
  82. test_swap_ranges_impl< std::vector<int>, std::deque<int> >();
  83. test_swap_ranges_impl< std::list<int>, std::vector<int> >();
  84. test_swap_ranges_impl< std::list<int>, std::list<int> >();
  85. test_swap_ranges_impl< std::list<int>, std::deque<int> >();
  86. test_swap_ranges_impl< std::deque<int>, std::vector<int> >();
  87. test_swap_ranges_impl< std::deque<int>, std::list<int> >();
  88. test_swap_ranges_impl< std::deque<int>, std::deque<int> >();
  89. }
  90. }
  91. boost::unit_test::test_suite*
  92. init_unit_test_suite(int argc, char* argv[])
  93. {
  94. boost::unit_test::test_suite* test
  95. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.swap_ranges" );
  96. test->add( BOOST_TEST_CASE( &test_swap_ranges ) );
  97. return test;
  98. }