heap_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "util/heap.h"
  6. #include <gtest/gtest.h>
  7. #include <climits>
  8. #include <queue>
  9. #include <random>
  10. #include <utility>
  11. #include "port/stack_trace.h"
  12. #ifndef GFLAGS
  13. const int64_t FLAGS_iters = 100000;
  14. #else
  15. #include "util/gflags_compat.h"
  16. DEFINE_int64(iters, 100000, "number of pseudo-random operations in each test");
  17. #endif // GFLAGS
  18. /*
  19. * Compares the custom heap implementation in util/heap.h against
  20. * std::priority_queue on a pseudo-random sequence of operations.
  21. */
  22. namespace ROCKSDB_NAMESPACE {
  23. using HeapTestValue = uint64_t;
  24. using Params = std::tuple<size_t, HeapTestValue, int64_t>;
  25. class HeapTest : public ::testing::TestWithParam<Params> {};
  26. TEST_P(HeapTest, Test) {
  27. // This test performs the same pseudorandom sequence of operations on a
  28. // BinaryHeap and an std::priority_queue, comparing output. The three
  29. // possible operations are insert, replace top and pop.
  30. //
  31. // Insert is chosen slightly more often than the others so that the size of
  32. // the heap slowly grows. Once the size heats the MAX_HEAP_SIZE limit, we
  33. // disallow inserting until the heap becomes empty, testing the "draining"
  34. // scenario.
  35. const auto MAX_HEAP_SIZE = std::get<0>(GetParam());
  36. const auto MAX_VALUE = std::get<1>(GetParam());
  37. const auto RNG_SEED = std::get<2>(GetParam());
  38. BinaryHeap<HeapTestValue> heap;
  39. std::priority_queue<HeapTestValue> ref;
  40. std::mt19937 rng(static_cast<unsigned int>(RNG_SEED));
  41. std::uniform_int_distribution<HeapTestValue> value_dist(0, MAX_VALUE);
  42. int ndrains = 0;
  43. bool draining = false; // hit max size, draining until we empty the heap
  44. size_t size = 0;
  45. for (int64_t i = 0; i < FLAGS_iters; ++i) {
  46. if (size == 0) {
  47. draining = false;
  48. }
  49. if (!draining && (size == 0 || std::bernoulli_distribution(0.4)(rng))) {
  50. // insert
  51. HeapTestValue val = value_dist(rng);
  52. heap.push(val);
  53. ref.push(val);
  54. ++size;
  55. if (size == MAX_HEAP_SIZE) {
  56. draining = true;
  57. ++ndrains;
  58. }
  59. } else if (std::bernoulli_distribution(0.5)(rng)) {
  60. // replace top
  61. HeapTestValue val = value_dist(rng);
  62. heap.replace_top(val);
  63. ref.pop();
  64. ref.push(val);
  65. } else {
  66. // pop
  67. assert(size > 0);
  68. heap.pop();
  69. ref.pop();
  70. --size;
  71. }
  72. // After every operation, check that the public methods give the same
  73. // results
  74. assert((size == 0) == ref.empty());
  75. ASSERT_EQ(size == 0, heap.empty());
  76. if (size > 0) {
  77. ASSERT_EQ(ref.top(), heap.top());
  78. }
  79. }
  80. // Probabilities should be set up to occasionally hit the max heap size and
  81. // drain it
  82. assert(ndrains > 0);
  83. heap.clear();
  84. ASSERT_TRUE(heap.empty());
  85. }
  86. // Basic test, MAX_VALUE = 3*MAX_HEAP_SIZE (occasional duplicates)
  87. INSTANTIATE_TEST_CASE_P(Basic, HeapTest,
  88. ::testing::Values(Params(1000, 3000,
  89. 0x1b575cf05b708945)));
  90. // Mid-size heap with small values (many duplicates)
  91. INSTANTIATE_TEST_CASE_P(SmallValues, HeapTest,
  92. ::testing::Values(Params(100, 10, 0x5ae213f7bd5dccd0)));
  93. // Small heap, large value range (no duplicates)
  94. INSTANTIATE_TEST_CASE_P(SmallHeap, HeapTest,
  95. ::testing::Values(Params(10, ULLONG_MAX,
  96. 0x3e1fa8f4d01707cf)));
  97. // Two-element heap
  98. INSTANTIATE_TEST_CASE_P(TwoElementHeap, HeapTest,
  99. ::testing::Values(Params(2, 5, 0x4b5e13ea988c6abc)));
  100. // One-element heap
  101. INSTANTIATE_TEST_CASE_P(OneElementHeap, HeapTest,
  102. ::testing::Values(Params(1, 3, 0x176a1019ab0b612e)));
  103. } // namespace ROCKSDB_NAMESPACE
  104. int main(int argc, char** argv) {
  105. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  106. ::testing::InitGoogleTest(&argc, argv);
  107. #ifdef GFLAGS
  108. GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
  109. #endif // GFLAGS
  110. return RUN_ALL_TESTS();
  111. }