Skip to content

chore: Gtest migration followups first pass - #7884

Open
godexsoft wants to merge 5 commits into
XRPLF:developfrom
godexsoft:chore/gtest-migration-followups-1
Open

chore: Gtest migration followups first pass#7884
godexsoft wants to merge 5 commits into
XRPLF:developfrom
godexsoft:chore/gtest-migration-followups-1

Conversation

@godexsoft

Copy link
Copy Markdown
Contributor

High Level Overview of Change

Follow-up PR that resolves review comments left open on seven already-merged
test-migration PRs: #7136, #7133, #7054, #7046, #7317, #7304, #7292.

Those PRs were merged with a number of "let's push this to the next PR" / "keep this
convo open" threads. This audits all 72 unresolved threads across them and clears the
ones that are mechanical, plus one real bug.

39 review threads can be resolved from this PR — 33 fixed in code, 6 answered
without a code change (see Deliberately not changed below).

Test, benchmark and build files only. No production code is touched.

Context of Change

The bulk is grouped into four mechanical sweeps rather than 15 scattered follow-ups,
because the same feedback recurred across PRs:

Theme What Threads
std::size_t loop counters 40 of 48 signed counters under src/tests/libxrpl + src/benchmarks 7136.14, 7133.14, 7317.19
Magic numbers extracted to named constexpr in resource/Logic.cpp, shamap/SHAMap.cpp, shamap/SHAMapSync.cpp, benchmarks/Backend.cpp 7133.5, 7133.16, 7133.17, 7133.18, 7317.15
std::to_array replaced sized std::array<…, N>{{…}} and raw C arrays 7136.6, 7136.7
ranges ranges::iota, ranges::shuffle, views::iota in the benchmark helpers 7317.21, 7317.23

Bug fix: randomWeightedShuffle

#7046 thread — flagged
in review, deferred as out of scope, never picked up. src/tests/libxrpl/csf/random.h:

std::discrete_distribution<> dd(w.begin() + i, w.end());
auto idx = dd(g);          // relative to w.begin() + i, used as an absolute index
std::swap(v[i], v[idx]);

dd is constructed from w.begin() + i, so its result is relative to i and has to be
offset back before indexing. As written, for i > 0 it swaps against the wrong prefix
element, so the result is not the weighted-without-replacement shuffle it claims to be.
Two further defects on the same two lines: v.size() - 1 underflows to SIZE_MAX on an
empty vector, and int i was compared against size_t.

This is consumed by csf/PeerGroup.h, so it silently skewed peer selection in consensus
simulations. This is the only behavioural change in the PR; the consensus and csf suites
still pass.

Other fixes

  • test: Migrate basics Beast tests to GTest #7136 — structured binding over the test pairs; using swuusing SharedWeak;
    ifelse if on five mutually-exclusive next == chains; collectionanddelimiter
    collectionAndDelimiter; comments now name kMaxRep correctly; dropped gratuitous
    parens in EXPECT_EQ; deleted a commented-out block referencing
    MantissaRange::mantissa_scale::small, an enum that no longer exists, so the block
    could never have been un-commented as written.
  • test: Migrate resource, shamap Beast tests to GTest #7133++logic.clock()logic.advance() (4 sites; TestLogic::advance() is
    literally ++member, so this is identical); push_back(std::move(item)) for
    Gossip::Item; auto for n / warned.
  • test: Add google benchmark dependency and migrate nodestore timing test as a benchmark #7317include_guard() in cmake/XrplAddBenchmark.cmake; sliceBatches
    slicePreciseBatches; rngcpy unified into one while (bytes > 0) loop using
    std::min; auto&auto const& on the destructured IterateContext (5 sites);
    RunState members documented with ///<, including harness which had none;
    "member order matters" note moved onto tempDir itself.
  • test: Migrate nodestore tests from Beast to GTest #7292allBackends() now returns directly per #if branch so nothing is ever
    mutated. This removes the const question entirely rather than suppressing it with a
    NOLINT, which is what the thread asked for.

API Impact

No impact.

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 20 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/benchmarks/libxrpl/nodestore/Backend.cpp 0.0% 10 Missing ⚠️
src/benchmarks/libxrpl/nodestore/NodeStoreBench.h 0.0% 10 Missing ⚠️

📢 Thoughts on this report? Let us know!

@godexsoft
godexsoft requested a review from mathbunnyru July 28, 2026 19:01

@mathbunnyru mathbunnyru left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of auto i = 0uz, in my opinion size_t i = 0 is as short, but doesn't make you remember what uz is, and the type is evident

Comment thread src/benchmarks/libxrpl/nodestore/NodeStoreBench.h Outdated
std::vector<std::uint64_t> coeffs;
coeffs.reserve(numCoeff);
for (int i = 0; i < numCoeff; ++i)
for (auto i = 0uz; i < numCoeff; ++i)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (auto i = 0uz; i < numCoeff; ++i)
for (size_t i = 0; i < numCoeff; ++i)

Can't we use this simple code?

And later too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer auto and proper C++23 with uz

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how auto is helpful here at all, if it makes the code less obvious

  1. you need to remember what uz means (I don't for example)
  2. it doesn't make code shorter
  3. makes it less expressive, because we now don't specify the type
  4. size_t i = 0; is perfectly proper C++23. Just because it worked before, doesn't make it bad 🙂

Comment thread src/tests/libxrpl/basics/base_uint.cpp Outdated
Comment thread src/tests/libxrpl/basics/IntrusiveShared.cpp Outdated
@godexsoft

Copy link
Copy Markdown
Contributor Author

I'm not a fan of auto i = 0uz, in my opinion size_t i = 0 is as short, but doesn't make you remember what uz is, and the type is evident

I'm totally on the opposite side in regards to that :) i don't like to spell out types everywhere and uz is a standard way to use auto in such places.

@godexsoft
godexsoft requested a review from mathbunnyru July 29, 2026 14:14
EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load());
}
if (next == Deleted)
else if (next == Deleted)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, make it switch here too

std::vector<std::uint64_t> coeffs;
coeffs.reserve(numCoeff);
for (int i = 0; i < numCoeff; ++i)
for (auto i = 0uz; i < numCoeff; ++i)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how auto is helpful here at all, if it makes the code less obvious

  1. you need to remember what uz means (I don't for example)
  2. it doesn't make code shorter
  3. makes it less expressive, because we now don't specify the type
  4. size_t i = 0; is perfectly proper C++23. Just because it worked before, doesn't make it bad 🙂

std::discrete_distribution<> dd(w.begin() + i, w.end()); // NOLINT(misc-const-correctness)
auto idx = dd(g);
// Pick a random item from the unplaced tail, weighted by w.
// NOLINTNEXTLINE(misc-const-correctness)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, you can probably just make it const.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re uz: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#nl11-make-literals-readable
You don't need to understand or like it, it's just one way to write this and i prefer it :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re const: can't make it const because dd() below will not compile unfortunately :(

Comment thread src/tests/libxrpl/resource/Logic.cpp

@mathbunnyru mathbunnyru left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with usage of the auto in following cases, but since we don't have a style guide for this, approving

auto i = 0uz;
auto whether = true;
auto arr = std::to_array<type>({some_values});

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR applies follow-up cleanups from prior GTest-migration work across the test and benchmark suites, and includes a correctness fix in CSF’s randomWeightedShuffle used by consensus simulations.

Changes:

  • Fix randomWeightedShuffle to correctly offset std::discrete_distribution results and avoid empty-vector underflow.
  • Mechanical test/benchmark cleanup: migrate many loop counters to std::size_t/0uz, extract/rename magic numbers, and simplify some assertions/iteration logic.
  • Benchmark helper refinements: adopt std::ranges algorithms, tighten const-correctness in structured bindings, and clarify helper naming/docs.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/tests/libxrpl/shamap/SHAMapSync.cpp Replaces magic numbers with named constants and uses std::size_t loop counters.
src/tests/libxrpl/shamap/SHAMap.cpp Simplifies traversal assertion logic and names proof-path key bounds.
src/tests/libxrpl/resource/Logic.cpp Minor test cleanups: move-into containers, use advance(), replace raw arrays with std::array, and update loop counters.
src/tests/libxrpl/nodestore/Database.cpp Simplifies backend-list construction to avoid mutation under #if branches.
src/tests/libxrpl/csf/TrustGraph.h Converts nested loops to std::size_t-friendly counters.
src/tests/libxrpl/csf/random.h Fixes weighted shuffle indexing/underflow and updates loop counter types.
src/tests/libxrpl/consensus/CensorshipDetector.cpp Converts loop counter to std::size_t literal-suffixed form.
src/tests/libxrpl/basics/Number.cpp Comment/cleanup adjustments and removes dead commented-out code.
src/tests/libxrpl/basics/MallocTrim.cpp Converts loop counter to std::size_t literal-suffixed form.
src/tests/libxrpl/basics/join.cpp Renames lambda parameter for clarity/consistency.
src/tests/libxrpl/basics/IntrusiveShared.cpp Converts counters/types to std::size_t and refactors optional-state handling to a switch.
src/tests/libxrpl/basics/base58.cpp Converts multiple loop counters to std::size_t literal-suffixed form.
src/tests/libxrpl/basics/base_uint.cpp Uses std::to_array and structured bindings for more readable constexpr test vectors.
src/benchmarks/libxrpl/nodestore/NodeStoreBench.h Uses std::ranges algorithms, simplifies RNG buffer fill, and renames batch slicing helper.
src/benchmarks/libxrpl/nodestore/Backend.cpp Replaces magic numbers with constants, improves const-correctness, and updates helper name usage.
cmake/XrplAddBenchmark.cmake Adds include_guard() to prevent multiple inclusion.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// `batchSize` is dropped, so every returned batch has exactly `batchSize`.
inline std::vector<Batch>
sliceBatches(Batch const& pool, std::size_t batchSize)
slicePreciseBatches(Batch const& pool, std::size_t batchSize)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of this renaming? If renaming is necessary, wouldn't sliceFixedBatches be more appropriate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that was suggested by @mathbunnyru : #7317 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sliceFixedBatches is a great name, let's use that

Comment thread src/tests/libxrpl/csf/random.h
Comment thread src/tests/libxrpl/resource/Logic.cpp Outdated
Comment on lines +217 to +225
static constexpr auto kGossipSources = 5uz;

std::array<Gossip, kGossipSources> gossip;

for (auto& i : g)
populateGossip(i);
for (auto& g : gossip)
populateGossip(g);

for (int i = 0; i < 5; ++i)
logic.importConsumers(std::to_string(i), g[i]);
for (auto i = 0uz; i < gossip.size(); ++i)
logic.importConsumers(std::to_string(i), gossip[i]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more elegant way to write this? It looks quite convoluted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea with ranges and enumerate_view i guess. I can try. It is supposed to be in c++23 so let's hope all compilers can do it by now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately enumerate_view is not yet shipped by libc++ so i can't think of a better loop than the one we already have here.

for (auto const i : std::views::iota(0uz, gossip.size()))
    logic.importConsumers(std::to_string(i), gossip[i]);

does not look much better to me.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bummer, thanks for trying!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have found a simpler way that works fine and since this is in tests i think it's better.

@godexsoft
godexsoft requested a review from bthomee July 30, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants