|
| 1 | +// dgb think() Phase-5 BEST-SHARE PUNISH WALK conformance KAT. |
| 2 | +// |
| 3 | +// FENCED, additive (no production code touched this slice). Pins |
| 4 | +// src/impl/dgb/think_p5_best_share_punish.hpp against the p2pool data.py |
| 5 | +// think() best-share resolution oracle (data.py:2142-2166): walk back off a |
| 6 | +// naughty head, then dive to the deepest non-naughty descendant; report the |
| 7 | +// punishment of the share finally landed on. |
| 8 | +// |
| 9 | +// Expectations are HAND-DERIVED by tracing the walk rules over a fake chain, |
| 10 | +// NOT read back from the code under test — a conformance KAT that asks its |
| 11 | +// subject for the answer passes vacuously. Pure graph traversal: links only |
| 12 | +// core (no chain standup). MUST appear in BOTH this dir s CMakeLists.txt AND the |
| 13 | +// build.yml --target allowlist, or it becomes a #143 NOT_BUILT sentinel |
| 14 | +// (compiled-out, silently "passing"). |
| 15 | + |
| 16 | +#include <impl/dgb/think_p5_best_share_punish.hpp> |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include <map> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +// A hand-built fake sharechain keyed by short string hashes. "" is the null |
| 27 | +// sentinel. Each node carries a naughty count and a prev pointer; children are |
| 28 | +// derived as the reverse of prev. |
| 29 | +struct FakeChain { |
| 30 | + std::map<std::string, int32_t> naughty; // present => contained |
| 31 | + std::map<std::string, std::string> prev; // hash -> prev hash ("" = none) |
| 32 | + |
| 33 | + dgb::P5ChainAccessors<std::string> accessors() const { |
| 34 | + dgb::P5ChainAccessors<std::string> a; |
| 35 | + a.naughty = [this](const std::string& h) -> int32_t { |
| 36 | + auto it = naughty.find(h); |
| 37 | + return it == naughty.end() ? 0 : it->second; |
| 38 | + }; |
| 39 | + a.prev_of = [this](const std::string& h) -> std::string { |
| 40 | + auto it = prev.find(h); |
| 41 | + return it == prev.end() ? std::string() : it->second; |
| 42 | + }; |
| 43 | + a.contains = [this](const std::string& h) -> bool { |
| 44 | + return naughty.find(h) != naughty.end(); |
| 45 | + }; |
| 46 | + a.children = [this](const std::string& h) -> std::vector<std::string> { |
| 47 | + std::vector<std::string> kids; |
| 48 | + for (const auto& [k, p] : prev) |
| 49 | + if (p == h) kids.push_back(k); |
| 50 | + return kids; |
| 51 | + }; |
| 52 | + a.is_null = [](const std::string& h) -> bool { return h.empty(); }; |
| 53 | + return a; |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +// ---- (1) a non-naughty head is its own best, punish 0 ---------------------- |
| 58 | +TEST(ThinkP5BestSharePunish, NonNaughtyHeadIsOwnBest) |
| 59 | +{ |
| 60 | + FakeChain c; |
| 61 | + c.naughty = {{"G", 0}, {"M", 0}}; |
| 62 | + c.prev = {{"M", "G"}, {"G", ""}}; |
| 63 | + auto r = dgb::think_p5_best_share_punish<std::string>("M", true, c.accessors()); |
| 64 | + EXPECT_EQ(r.best, "M"); // loop never entered |
| 65 | + EXPECT_EQ(r.punish_val, 0); |
| 66 | +} |
| 67 | + |
| 68 | +// ---- (2) start_valid == false is returned verbatim, walk skipped ----------- |
| 69 | +TEST(ThinkP5BestSharePunish, InvalidStartReturnedVerbatim) |
| 70 | +{ |
| 71 | + FakeChain c; |
| 72 | + c.naughty = {{"Z", 4}}; // naughty data present but must be ignored |
| 73 | + c.prev = {{"Z", ""}}; |
| 74 | + auto r = dgb::think_p5_best_share_punish<std::string>("Z", false, c.accessors()); |
| 75 | + EXPECT_EQ(r.best, "Z"); |
| 76 | + EXPECT_EQ(r.punish_val, 0); |
| 77 | +} |
| 78 | + |
| 79 | +// ---- (3) walk back off naughty heads, dive to deepest non-naughty descendant |
| 80 | +// Chain: G(0) -> A(3) -> X(2)=start |
| 81 | +// G(0) -> C(0) -> D(0) -> E(0, leaf) |
| 82 | +// Trace: X naughty -> A naughty -> G non-naughty; dive from G: child A skipped |
| 83 | +// (naughty), child C -> D -> E gives the deepest non-naughty chain => best = E. |
| 84 | +// idx rests on G (naughty 0) => punish 0. Hand-derived, independent of subject. |
| 85 | +TEST(ThinkP5BestSharePunish, WalkBackThenDiveToDeepestDescendant) |
| 86 | +{ |
| 87 | + FakeChain c; |
| 88 | + c.naughty = {{"G", 0}, {"A", 3}, {"X", 2}, {"C", 0}, {"D", 0}, {"E", 0}}; |
| 89 | + c.prev = {{"G", ""}, {"A", "G"}, {"X", "A"}, {"C", "G"}, {"D", "C"}, {"E", "D"}}; |
| 90 | + auto r = dgb::think_p5_best_share_punish<std::string>("X", true, c.accessors()); |
| 91 | + EXPECT_EQ(r.best, "E"); |
| 92 | + EXPECT_EQ(r.punish_val, 0); |
| 93 | +} |
| 94 | + |
| 95 | +// ---- (4) the dive skips naughty children ----------------------------------- |
| 96 | +// As (3) but C also has a DEEPER naughty branch C -> F(7) -> H(0). H is deeper |
| 97 | +// than E, but F is naughty so the whole branch is excluded; best is still E. |
| 98 | +TEST(ThinkP5BestSharePunish, DiveSkipsNaughtyChildren) |
| 99 | +{ |
| 100 | + FakeChain c; |
| 101 | + c.naughty = {{"G", 0}, {"A", 3}, {"X", 2}, {"C", 0}, {"D", 0}, {"E", 0}, |
| 102 | + {"F", 7}, {"H", 0}}; |
| 103 | + c.prev = {{"G", ""}, {"A", "G"}, {"X", "A"}, {"C", "G"}, {"D", "C"}, |
| 104 | + {"E", "D"}, {"F", "C"}, {"H", "F"}}; |
| 105 | + auto r = dgb::think_p5_best_share_punish<std::string>("X", true, c.accessors()); |
| 106 | + EXPECT_EQ(r.best, "E"); // C->F->H excluded (F naughty); C->D->E wins |
| 107 | + EXPECT_EQ(r.punish_val, 0); |
| 108 | +} |
| 109 | + |
| 110 | +// ---- (5) naughty all the way to a missing prev -> stop, report punishment --- |
| 111 | +// Chain: Q(absent) <- P(5)=start. prev(P)=Q but Q is NOT contained, so the |
| 112 | +// walk breaks ON P. idx still points at P => punish = P naughty = 5. |
| 113 | +TEST(ThinkP5BestSharePunish, NaughtyToMissingPrevReportsPunish) |
| 114 | +{ |
| 115 | + FakeChain c; |
| 116 | + c.naughty = {{"P", 5}}; // Q deliberately absent |
| 117 | + c.prev = {{"P", "Q"}}; |
| 118 | + auto r = dgb::think_p5_best_share_punish<std::string>("P", true, c.accessors()); |
| 119 | + EXPECT_EQ(r.best, "P"); |
| 120 | + EXPECT_EQ(r.punish_val, 5); |
| 121 | +} |
| 122 | + |
| 123 | +// ---- (6) naughty head with a null prev -> stop ON it, report punishment ----- |
| 124 | +TEST(ThinkP5BestSharePunish, NaughtyWithNullPrevReportsPunish) |
| 125 | +{ |
| 126 | + FakeChain c; |
| 127 | + c.naughty = {{"P", 2}}; |
| 128 | + c.prev = {{"P", ""}}; // null prev sentinel |
| 129 | + auto r = dgb::think_p5_best_share_punish<std::string>("P", true, c.accessors()); |
| 130 | + EXPECT_EQ(r.best, "P"); |
| 131 | + EXPECT_EQ(r.punish_val, 2); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace |
0 commit comments