test: Add CChain basic tests

Add basic unit tests to the `CChain` class, filling a gap.

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
This commit is contained in:
optout
2026-01-20 16:58:53 +01:00
parent ad0545ba96
commit 8333abdd91

View File

@@ -41,6 +41,50 @@ const CBlockIndex* NaiveLastCommonAncestor(const CBlockIndex* a, const CBlockInd
} // namespace
BOOST_AUTO_TEST_CASE(basic_tests)
{
// An empty chain
const CChain chain_0;
// A chain with 2 blocks
CChain chain_2;
CBlockIndex genesis;
genesis.nHeight = 0;
chain_2.SetTip(genesis);
CBlockIndex bi1;
bi1.pprev = &genesis;
bi1.nHeight = 1;
chain_2.SetTip(bi1);
BOOST_CHECK_EQUAL(chain_0.Height(), -1);
BOOST_CHECK_EQUAL(chain_2.Height(), 1);
BOOST_CHECK_EQUAL(chain_0.Tip(), nullptr);
BOOST_CHECK_EQUAL(chain_2.Tip(), &bi1);
// Indexer accessor: call with valid and invalid (low & high) values
BOOST_CHECK_EQUAL(chain_2[-1], nullptr);
BOOST_CHECK_EQUAL(chain_2[0], &genesis);
BOOST_CHECK_EQUAL(chain_2[1], &bi1);
BOOST_CHECK_EQUAL(chain_2[2], nullptr);
// Contains: call with contained & non-contained blocks
BOOST_CHECK(chain_2.Contains(&genesis));
BOOST_CHECK(chain_2.Contains(&bi1));
BOOST_CHECK(!chain_0.Contains(&genesis));
// BOOST_CHECK(!chain_0.Contains(nullptr)); // fail with memory access violation
// Call with non-tip & tip blocks
BOOST_CHECK_EQUAL(chain_2.Next(&genesis), &bi1);
BOOST_CHECK_EQUAL(chain_2.Next(&bi1), nullptr);
BOOST_CHECK_EQUAL(chain_0.Next(&genesis), nullptr);
// BOOST_CHECK_EQUAL(chain_0.Next(nullptr), nullptr); // fail with memory access violation
BOOST_CHECK_EQUAL(chain_2.Genesis(), &genesis);
BOOST_CHECK_EQUAL(chain_0.Genesis(), nullptr);
}
BOOST_AUTO_TEST_CASE(chain_test)
{
FastRandomContext ctx;