2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2015-07-06 11:24:09 +00:00
|
|
|
|
2022-10-18 07:35:35 +00:00
|
|
|
#include "util/heap.h"
|
|
|
|
|
2015-07-06 11:24:09 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <climits>
|
|
|
|
#include <queue>
|
2015-07-16 09:22:21 +00:00
|
|
|
#include <random>
|
2015-07-06 11:24:09 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2022-10-18 07:35:35 +00:00
|
|
|
#include "port/stack_trace.h"
|
2015-07-06 11:24:09 +00:00
|
|
|
|
|
|
|
#ifndef GFLAGS
|
|
|
|
const int64_t FLAGS_iters = 100000;
|
|
|
|
#else
|
2017-12-01 18:40:45 +00:00
|
|
|
#include "util/gflags_compat.h"
|
2015-07-06 11:24:09 +00:00
|
|
|
DEFINE_int64(iters, 100000, "number of pseudo-random operations in each test");
|
|
|
|
#endif // GFLAGS
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compares the custom heap implementation in util/heap.h against
|
|
|
|
* std::priority_queue on a pseudo-random sequence of operations.
|
|
|
|
*/
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-07-06 11:24:09 +00:00
|
|
|
|
|
|
|
using HeapTestValue = uint64_t;
|
|
|
|
using Params = std::tuple<size_t, HeapTestValue, int64_t>;
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
class HeapTest : public ::testing::TestWithParam<Params> {};
|
2015-07-06 11:24:09 +00:00
|
|
|
|
|
|
|
TEST_P(HeapTest, Test) {
|
|
|
|
// This test performs the same pseudorandom sequence of operations on a
|
|
|
|
// BinaryHeap and an std::priority_queue, comparing output. The three
|
|
|
|
// possible operations are insert, replace top and pop.
|
|
|
|
//
|
|
|
|
// Insert is chosen slightly more often than the others so that the size of
|
|
|
|
// the heap slowly grows. Once the size heats the MAX_HEAP_SIZE limit, we
|
|
|
|
// disallow inserting until the heap becomes empty, testing the "draining"
|
|
|
|
// scenario.
|
|
|
|
|
|
|
|
const auto MAX_HEAP_SIZE = std::get<0>(GetParam());
|
|
|
|
const auto MAX_VALUE = std::get<1>(GetParam());
|
|
|
|
const auto RNG_SEED = std::get<2>(GetParam());
|
|
|
|
|
|
|
|
BinaryHeap<HeapTestValue> heap;
|
|
|
|
std::priority_queue<HeapTestValue> ref;
|
|
|
|
|
2015-07-16 09:22:21 +00:00
|
|
|
std::mt19937 rng(static_cast<unsigned int>(RNG_SEED));
|
2015-07-06 11:24:09 +00:00
|
|
|
std::uniform_int_distribution<HeapTestValue> value_dist(0, MAX_VALUE);
|
|
|
|
int ndrains = 0;
|
2022-10-26 19:08:20 +00:00
|
|
|
bool draining = false; // hit max size, draining until we empty the heap
|
2015-07-06 11:24:09 +00:00
|
|
|
size_t size = 0;
|
|
|
|
for (int64_t i = 0; i < FLAGS_iters; ++i) {
|
|
|
|
if (size == 0) {
|
|
|
|
draining = false;
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
if (!draining && (size == 0 || std::bernoulli_distribution(0.4)(rng))) {
|
2015-07-06 11:24:09 +00:00
|
|
|
// insert
|
|
|
|
HeapTestValue val = value_dist(rng);
|
|
|
|
heap.push(val);
|
|
|
|
ref.push(val);
|
|
|
|
++size;
|
|
|
|
if (size == MAX_HEAP_SIZE) {
|
|
|
|
draining = true;
|
|
|
|
++ndrains;
|
|
|
|
}
|
|
|
|
} else if (std::bernoulli_distribution(0.5)(rng)) {
|
|
|
|
// replace top
|
|
|
|
HeapTestValue val = value_dist(rng);
|
|
|
|
heap.replace_top(val);
|
|
|
|
ref.pop();
|
|
|
|
ref.push(val);
|
|
|
|
} else {
|
|
|
|
// pop
|
|
|
|
assert(size > 0);
|
|
|
|
heap.pop();
|
|
|
|
ref.pop();
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After every operation, check that the public methods give the same
|
|
|
|
// results
|
|
|
|
assert((size == 0) == ref.empty());
|
|
|
|
ASSERT_EQ(size == 0, heap.empty());
|
|
|
|
if (size > 0) {
|
|
|
|
ASSERT_EQ(ref.top(), heap.top());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probabilities should be set up to occasionally hit the max heap size and
|
|
|
|
// drain it
|
|
|
|
assert(ndrains > 0);
|
|
|
|
|
|
|
|
heap.clear();
|
|
|
|
ASSERT_TRUE(heap.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic test, MAX_VALUE = 3*MAX_HEAP_SIZE (occasional duplicates)
|
2022-10-26 19:08:20 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(Basic, HeapTest,
|
|
|
|
::testing::Values(Params(1000, 3000,
|
|
|
|
0x1b575cf05b708945)));
|
2015-07-06 11:24:09 +00:00
|
|
|
// Mid-size heap with small values (many duplicates)
|
2022-10-26 19:08:20 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(SmallValues, HeapTest,
|
|
|
|
::testing::Values(Params(100, 10, 0x5ae213f7bd5dccd0)));
|
2015-07-06 11:24:09 +00:00
|
|
|
// Small heap, large value range (no duplicates)
|
2022-10-26 19:08:20 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(SmallHeap, HeapTest,
|
|
|
|
::testing::Values(Params(10, ULLONG_MAX,
|
|
|
|
0x3e1fa8f4d01707cf)));
|
2015-07-06 11:24:09 +00:00
|
|
|
// Two-element heap
|
2022-10-26 19:08:20 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(TwoElementHeap, HeapTest,
|
|
|
|
::testing::Values(Params(2, 5, 0x4b5e13ea988c6abc)));
|
2015-07-06 11:24:09 +00:00
|
|
|
// One-element heap
|
2022-10-26 19:08:20 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(OneElementHeap, HeapTest,
|
|
|
|
::testing::Values(Params(1, 3, 0x176a1019ab0b612e)));
|
2015-07-06 11:24:09 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2015-07-06 11:24:09 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-07-06 11:24:09 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
#ifdef GFLAGS
|
2017-12-01 18:40:45 +00:00
|
|
|
GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
|
2015-07-06 11:24:09 +00:00
|
|
|
#endif // GFLAGS
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|