stable_heap_tests.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*=============================================================================
  2. Copyright (c) 2010 Tim Blechmann
  3. Use, modification and distribution is subject to the Boost Software
  4. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #include <boost/foreach.hpp>
  8. #include "common_heap_tests.hpp"
  9. struct q_tester
  10. {
  11. q_tester(int i = 0, int j = 0):
  12. value(i), id(j)
  13. {}
  14. bool operator< (q_tester const & rhs) const
  15. {
  16. return value < rhs.value;
  17. }
  18. bool operator> (q_tester const & rhs) const
  19. {
  20. return value > rhs.value;
  21. }
  22. bool operator== (q_tester const & rhs) const
  23. {
  24. return (value == rhs.value) && (id == rhs.id);
  25. }
  26. int value;
  27. int id;
  28. };
  29. std::ostream& operator<< (std::ostream& out, q_tester const & t)
  30. {
  31. out << "[" << t.value << " " << t.id << "]";
  32. return out;
  33. }
  34. typedef std::vector<q_tester> stable_test_data;
  35. stable_test_data make_stable_test_data(int size, int same_count = 3,
  36. int offset = 0, int strive = 1)
  37. {
  38. stable_test_data ret;
  39. for (int i = 0; i != size; ++i)
  40. for (int j = 0; j != same_count; ++j)
  41. ret.push_back(q_tester(i * strive + offset, j));
  42. return ret;
  43. }
  44. struct compare_by_id
  45. {
  46. bool operator()(q_tester const & lhs, q_tester const & rhs)
  47. {
  48. return lhs.id > rhs.id;
  49. }
  50. };
  51. template <typename pri_queue>
  52. void pri_queue_stable_test_sequential_push(void)
  53. {
  54. stable_test_data data = make_stable_test_data(test_size);
  55. pri_queue q;
  56. fill_q(q, data);
  57. std::stable_sort(data.begin(), data.end(), compare_by_id());
  58. std::stable_sort(data.begin(), data.end(), std::less<q_tester>());
  59. check_q(q, data);
  60. }
  61. template <typename pri_queue>
  62. void pri_queue_stable_test_sequential_reverse_push(void)
  63. {
  64. stable_test_data data = make_stable_test_data(test_size);
  65. pri_queue q;
  66. stable_test_data push_data(data);
  67. std::stable_sort(push_data.begin(), push_data.end(), std::greater<q_tester>());
  68. fill_q(q, push_data);
  69. std::stable_sort(data.begin(), data.end(), compare_by_id());
  70. std::stable_sort(data.begin(), data.end(), std::less<q_tester>());
  71. check_q(q, data);
  72. }
  73. template <typename pri_queue>
  74. void run_stable_heap_tests(void)
  75. {
  76. pri_queue_stable_test_sequential_push<pri_queue>();
  77. pri_queue_stable_test_sequential_reverse_push<pri_queue>();
  78. }