chore: Gtest migration followups first pass - #7884
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
mathbunnyru
left a comment
There was a problem hiding this comment.
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
| std::vector<std::uint64_t> coeffs; | ||
| coeffs.reserve(numCoeff); | ||
| for (int i = 0; i < numCoeff; ++i) | ||
| for (auto i = 0uz; i < numCoeff; ++i) |
There was a problem hiding this comment.
| 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
There was a problem hiding this comment.
I prefer auto and proper C++23 with uz
There was a problem hiding this comment.
I don't understand how auto is helpful here at all, if it makes the code less obvious
- you need to remember what
uzmeans (I don't for example) - it doesn't make code shorter
- makes it less expressive, because we now don't specify the type
size_t i = 0;is perfectly proper C++23. Just because it worked before, doesn't make it bad 🙂
I'm totally on the opposite side in regards to that :) i don't like to spell out types everywhere and |
| EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load()); | ||
| } | ||
| if (next == Deleted) | ||
| else if (next == Deleted) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
I don't understand how auto is helpful here at all, if it makes the code less obvious
- you need to remember what
uzmeans (I don't for example) - it doesn't make code shorter
- makes it less expressive, because we now don't specify the type
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) |
There was a problem hiding this comment.
Btw, you can probably just make it const.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Re const: can't make it const because dd() below will not compile unfortunately :(
mathbunnyru
left a comment
There was a problem hiding this comment.
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});
There was a problem hiding this comment.
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
randomWeightedShuffleto correctly offsetstd::discrete_distributionresults 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::rangesalgorithms, 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) |
There was a problem hiding this comment.
What's the benefit of this renaming? If renaming is necessary, wouldn't sliceFixedBatches be more appropriate?
There was a problem hiding this comment.
Because that was suggested by @mathbunnyru : #7317 (comment)
There was a problem hiding this comment.
sliceFixedBatches is a great name, let's use that
| 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]); |
There was a problem hiding this comment.
Is there a more elegant way to write this? It looks quite convoluted.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Bummer, thanks for trying!
There was a problem hiding this comment.
I have found a simpler way that works fine and since this is in tests i think it's better.
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:
std::size_tloop counterssrc/tests/libxrpl+src/benchmarksconstexprinresource/Logic.cpp,shamap/SHAMap.cpp,shamap/SHAMapSync.cpp,benchmarks/Backend.cppstd::to_arraystd::array<…, N>{{…}}and raw C arraysrangesranges::iota,ranges::shuffle,views::iotain the benchmark helpersBug fix:
randomWeightedShuffle#7046 thread — flagged
in review, deferred as out of scope, never picked up.
src/tests/libxrpl/csf/random.h:ddis constructed fromw.begin() + i, so its result is relative toiand has to beoffset back before indexing. As written, for
i > 0it swaps against the wrong prefixelement, so the result is not the weighted-without-replacement shuffle it claims to be.
Two further defects on the same two lines:
v.size() - 1underflows toSIZE_MAXon anempty vector, and
int iwas compared againstsize_t.This is consumed by
csf/PeerGroup.h, so it silently skewed peer selection in consensussimulations. This is the only behavioural change in the PR; the consensus and csf suites
still pass.
Other fixes
using swu→using SharedWeak;if→else ifon five mutually-exclusivenext ==chains;collectionanddelimiter→
collectionAndDelimiter; comments now namekMaxRepcorrectly; dropped gratuitousparens in
EXPECT_EQ; deleted a commented-out block referencingMantissaRange::mantissa_scale::small, an enum that no longer exists, so the blockcould never have been un-commented as written.
++logic.clock()→logic.advance()(4 sites;TestLogic::advance()isliterally
++member, so this is identical);push_back(std::move(item))forGossip::Item;autoforn/warned.nodestoretiming test as a benchmark #7317 —include_guard()incmake/XrplAddBenchmark.cmake;sliceBatches→slicePreciseBatches;rngcpyunified into onewhile (bytes > 0)loop usingstd::min;auto&→auto const&on the destructuredIterateContext(5 sites);RunStatemembers documented with///<, includingharnesswhich had none;"member order matters" note moved onto
tempDiritself.nodestoretests from Beast to GTest #7292 —allBackends()now returns directly per#ifbranch so nothing is evermutated. This removes the
constquestion entirely rather than suppressing it with aNOLINT, which is what the thread asked for.API Impact
No impact.