5#include <boost/test/unit_test.hpp>
22 BOOST_REQUIRE_EQUAL(a->
nHeight, height);
39 BOOST_REQUIRE_EQUAL(a, b);
90 const auto init_branch{[](
auto& blocks,
CBlockIndex* parent,
int start_height) {
91 for (
size_t i{0}; i < blocks.size(); ++i) {
92 blocks[i].pprev = i == 0 ? parent : &blocks[i - 1];
93 blocks[i].nHeight = start_height + i;
97 const auto check_same{[](
const CChain& chain,
const auto& blocks) {
98 for (
const auto& block : blocks) {
103 const auto check_fork_point{[](
const CChain& chain,
const auto& blocks,
const CBlockIndex& fork_point) {
104 for (
const auto& block : blocks) {
109 std::array<CBlockIndex, 10> blocks_common;
110 init_branch(blocks_common,
nullptr, 0);
112 std::array<CBlockIndex, 10> blocks_long;
113 init_branch(blocks_long, &blocks_common.back(), blocks_common.size());
115 std::array<CBlockIndex, 5> blocks_short;
116 init_branch(blocks_short, &blocks_common.back(), blocks_common.size());
120 chain_long.
SetTip(blocks_long.back());
123 check_same(chain_long, blocks_common);
125 check_same(chain_long, blocks_long);
127 check_fork_point(chain_long, blocks_short, blocks_common.back());
131 chain_short.
SetTip(blocks_short.back());
134 check_same(chain_short, blocks_common);
136 check_same(chain_short, blocks_short);
138 check_fork_point(chain_short, blocks_long, blocks_common.back());
148 std::vector<std::unique_ptr<CBlockIndex>> block_index;
150 for (
int i = 0; i < 10; ++i) {
153 auto genesis = std::make_unique<CBlockIndex>();
154 genesis->nHeight = 0;
155 block_index.push_back(std::move(genesis));
157 for (
int b = 0; b < 10000; ++b) {
158 auto new_index = std::make_unique<CBlockIndex>();
161 new_index->pprev = block_index.back().get();
163 new_index->pprev = block_index[ctx.
randrange(block_index.size())].get();
165 new_index->nHeight = new_index->pprev->nHeight + 1;
166 new_index->BuildSkip();
167 block_index.push_back(std::move(new_index));
170 for (
int q = 0; q < 10000; ++q) {
174 BOOST_CHECK(result == NaiveGetAncestor(block, height));
177 for (
int q = 0; q < 10000; ++q) {
181 BOOST_CHECK(result == NaiveLastCommonAncestor(block1, block2));
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
BOOST_AUTO_TEST_CASE(basic_tests)
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
int nHeight
height of the entry in the chain. The genesis block has height 0
An in-memory indexed chain of blocks.
bool Contains(const CBlockIndex &index) const
Efficiently check whether a block is present in this chain.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
const CBlockIndex * FindFork(const CBlockIndex &index) const
Find the last common block between this chain and a block index entry.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
CBlockIndex * Next(const CBlockIndex &index) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
int Height() const
Return the maximal height in the chain.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)