heap.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/heap_algorithm.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 boost
  21. {
  22. namespace
  23. {
  24. template<class Container1, class Container2>
  25. void check_equal(const Container1& cont1, const Container2& cont2)
  26. {
  27. BOOST_CHECK_EQUAL_COLLECTIONS(
  28. cont1.begin(), cont1.end(),
  29. cont2.begin(), cont2.end()
  30. );
  31. }
  32. void test()
  33. {
  34. using namespace boost::assign;
  35. std::vector<int> reference;
  36. reference += 1,2,3,4,5,6,7,8,9;
  37. std::vector<int> test_cont(reference);
  38. std::vector<int> test_cont2(reference);
  39. std::make_heap(reference.begin(), reference.end());
  40. boost::make_heap(test_cont);
  41. check_equal(reference, test_cont);
  42. boost::make_heap(boost::make_iterator_range(test_cont2));
  43. check_equal(reference, test_cont2);
  44. std::push_heap(reference.begin(), reference.end());
  45. boost::push_heap(test_cont);
  46. check_equal(reference, test_cont);
  47. boost::push_heap(boost::make_iterator_range(test_cont2));
  48. check_equal(reference, test_cont2);
  49. std::make_heap(reference.begin(), reference.end());
  50. boost::make_heap(test_cont);
  51. boost::make_heap(boost::make_iterator_range(test_cont2));
  52. std::sort_heap(reference.begin(), reference.end());
  53. boost::sort_heap(test_cont);
  54. check_equal(reference, test_cont);
  55. boost::sort_heap(boost::make_iterator_range(test_cont2));
  56. check_equal(reference, test_cont2);
  57. std::make_heap(reference.begin(), reference.end());
  58. boost::make_heap(test_cont);
  59. boost::make_heap(boost::make_iterator_range(test_cont2));
  60. std::pop_heap(reference.begin(), reference.end());
  61. boost::pop_heap(test_cont);
  62. check_equal(reference, test_cont);
  63. boost::pop_heap(boost::make_iterator_range(test_cont2));
  64. check_equal(reference, test_cont2);
  65. }
  66. template<class BinaryPredicate>
  67. void test_pred(BinaryPredicate pred)
  68. {
  69. using namespace boost::assign;
  70. std::vector<int> reference;
  71. reference += 1,2,3,4,5,6,7,8,9;
  72. std::sort(reference.begin(), reference.end(), pred);
  73. std::vector<int> test_cont(reference);
  74. std::vector<int> test_cont2(reference);
  75. std::make_heap(reference.begin(), reference.end(), pred);
  76. boost::make_heap(test_cont, pred);
  77. check_equal(reference, test_cont);
  78. boost::make_heap(boost::make_iterator_range(test_cont2), pred);
  79. check_equal(reference, test_cont2);
  80. reference.push_back(5);
  81. test_cont.push_back(5);
  82. test_cont2.push_back(5);
  83. std::push_heap(reference.begin(), reference.end(), pred);
  84. boost::push_heap(test_cont, pred);
  85. check_equal(reference, test_cont);
  86. boost::push_heap(boost::make_iterator_range(test_cont2), pred);
  87. check_equal(reference, test_cont2);
  88. std::make_heap(reference.begin(), reference.end(), pred);
  89. boost::make_heap(test_cont, pred);
  90. boost::make_heap(boost::make_iterator_range(test_cont2), pred);
  91. std::sort_heap(reference.begin(), reference.end(), pred);
  92. boost::sort_heap(test_cont, pred);
  93. check_equal(reference, test_cont);
  94. boost::sort_heap(boost::make_iterator_range(test_cont2), pred);
  95. check_equal(reference, test_cont2);
  96. std::make_heap(reference.begin(), reference.end(), pred);
  97. boost::make_heap(test_cont, pred);
  98. boost::make_heap(boost::make_iterator_range(test_cont2), pred);
  99. std::pop_heap(reference.begin(), reference.end(), pred);
  100. boost::pop_heap(test_cont, pred);
  101. check_equal(reference, test_cont);
  102. boost::pop_heap(boost::make_iterator_range(test_cont2), pred);
  103. check_equal(reference, test_cont2);
  104. }
  105. void test_heap()
  106. {
  107. test();
  108. test_pred(std::less<int>());
  109. test_pred(std::greater<int>());
  110. }
  111. }
  112. }
  113. boost::unit_test::test_suite*
  114. init_unit_test_suite(int argc, char* argv[])
  115. {
  116. boost::unit_test::test_suite* test
  117. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.heap" );
  118. test->add( BOOST_TEST_CASE( &boost::test_heap ) );
  119. return test;
  120. }