From f9af12fdb09fe705127789bb3d0891849e336d62 Mon Sep 17 00:00:00 2001 From: Bart <11445373+bthomee@users.noreply.github.com> Date: Tue, 9 Jun 2026 09:04:14 -0400 Subject: [PATCH] Add tests --- src/tests/libxrpl/CMakeLists.txt | 1 + src/tests/libxrpl/shamap/SHAMapNodeSize.cpp | 81 +++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/tests/libxrpl/shamap/SHAMapNodeSize.cpp diff --git a/src/tests/libxrpl/CMakeLists.txt b/src/tests/libxrpl/CMakeLists.txt index 2dae6fccb9..93c3010bb6 100644 --- a/src/tests/libxrpl/CMakeLists.txt +++ b/src/tests/libxrpl/CMakeLists.txt @@ -27,6 +27,7 @@ set(test_modules basics crypto json + shamap tx protocol_autogen ) diff --git a/src/tests/libxrpl/shamap/SHAMapNodeSize.cpp b/src/tests/libxrpl/shamap/SHAMapNodeSize.cpp new file mode 100644 index 0000000000..3948431cde --- /dev/null +++ b/src/tests/libxrpl/shamap/SHAMapNodeSize.cpp @@ -0,0 +1,81 @@ +#include +#include // IWYU pragma: keep +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +namespace xrpl { + +// Build an inner node with exactly n populated branches via the full wire format round-trip so we +// get a properly initialized SHAMapInnerNode. +static SHAMapTreeNodePtr +makeInnerWithBranches(unsigned int n) +{ + std::vector buf(SHAMapInnerNode::kBranchFactor * uint256::kBytes, 0); + for (unsigned int i = 0; i < n; ++i) + { + std::uint8_t* p = buf.data() + (i * uint256::kBytes); + std::fill(p, p + uint256::kBytes, static_cast(i + 1)); + } + + return SHAMapInnerNode::makeFullInner(Slice(buf.data(), buf.size()), SHAMapHash{}, false); +} + +// Verifies sizeForWire() for every branch count from 1 to 16, which covers the compressed format +// (< kCompressedThreshold), the threshold boundary, and the full format (>= kCompressedThreshold). +TEST(SHAMapNodeSize, InnerNode) +{ + for (unsigned int n = 1; n <= SHAMapInnerNode::kBranchFactor; ++n) + { + auto const node = makeInnerWithBranches(n); + Serializer s(node->sizeForWire()); + node->serializeForWire(s); + EXPECT_EQ(node->sizeForWire(), s.size()) << "branch count: " << n; + } +} + +// Verifies sizeForWire() for each leaf node type across a range of payload sizes. +TEST(SHAMapNodeSize, LeafNodes) +{ + for (std::size_t const payloadSize : {12u, 64u, 256u}) + { + uint256 const key{}; + std::vector const payload(payloadSize, 0xAB); + Slice const data(payload.data(), payload.size()); + auto item = makeShamapitem(key, data); + + { + auto const tx = intr_ptr::makeShared(item, 1); + Serializer s(tx->sizeForWire()); + tx->serializeForWire(s); + EXPECT_EQ(tx->sizeForWire(), s.size()) << "payload: " << payloadSize; + } + + { + auto const txMeta = intr_ptr::makeShared(item, 1); + Serializer s(txMeta->sizeForWire()); + txMeta->serializeForWire(s); + EXPECT_EQ(txMeta->sizeForWire(), s.size()) << "payload: " << payloadSize; + } + + { + auto const acct = intr_ptr::makeShared(item, 1); + Serializer s(acct->sizeForWire()); + acct->serializeForWire(s); + EXPECT_EQ(acct->sizeForWire(), s.size()) << "payload: " << payloadSize; + } + } +} + +} // namespace xrpl