From 8333abdd916c506f778ef9a095e684e12b7fed9e Mon Sep 17 00:00:00 2001 From: optout <13562139+optout21@users.noreply.github.com> Date: Tue, 20 Jan 2026 16:58:53 +0100 Subject: [PATCH] test: Add CChain basic tests Add basic unit tests to the `CChain` class, filling a gap. Co-authored-by: l0rinc --- src/test/chain_tests.cpp | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/test/chain_tests.cpp b/src/test/chain_tests.cpp index a782e880f9f..d2d68289e73 100644 --- a/src/test/chain_tests.cpp +++ b/src/test/chain_tests.cpp @@ -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;