MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / TestOptimalLinearization

Function TestOptimalLinearization

src/test/cluster_linearize_tests.cpp:57–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55}
56
57void TestOptimalLinearization(std::span<const uint8_t> enc, std::initializer_list<DepGraphIndex> optimal_linearization)
58{
59 DepGraphIndex tx_count = 0;
60 FastRandomContext rng;
61
62 auto test_fn = [&]<typename SetType>() {
63 DepGraph<SetType> depgraph;
64 SpanReader reader(enc);
65 reader >> Using<DepGraphFormatter>(depgraph);
66 SanityCheck(depgraph);
67 std::vector<DepGraphIndex> lin;
68 for (int iter = 0; iter < 200; ++iter) {
69 bool opt;
70 uint64_t cost{0};
71 bool is_topological{true};
72 switch (rng.randrange(4)) {
73 case 0:
74 // Use empty input linearization.
75 lin.clear();
76 break;
77 case 1:
78 // Reuse previous optimal linearization as input.
79 break;
80 case 2:
81 // Construct random valid input linearization.
82 std::shuffle(lin.begin(), lin.end(), rng);
83 std::sort(lin.begin(), lin.end(), [&](auto a, auto b) { return depgraph.Ancestors(a).Count() < depgraph.Ancestors(b).Count(); });
84 break;
85 case 3:
86 // Construct random potentially invalid input linearization.
87 std::shuffle(lin.begin(), lin.end(), rng);
88 is_topological = false;
89 break;
90 }
91 std::tie(lin, opt, cost) = Linearize(
92 /*depgraph=*/depgraph,
93 /*max_cost=*/1000000000000,
94 /*rng_seed=*/rng.rand64(),
95 /*fallback_order=*/IndexTxOrder{},
96 /*old_linearization=*/lin,
97 /*is_topological=*/is_topological);
98 BOOST_CHECK(opt);
99 BOOST_CHECK(cost <= MaxOptimalLinearizationCost(depgraph.TxCount()));
100 SanityCheck(depgraph, lin);
101 BOOST_CHECK(std::ranges::equal(lin, optimal_linearization));
102 }
103 tx_count = depgraph.PositionRange();
104 };
105
106 // Always run with 64-bit set types
107 // - The native one that will be used on this platform.
108 test_fn.template operator()<BitSet<64>>();
109 // - The one used on 32-bit platforms.
110 test_fn.template operator()<bitset_detail::MultiIntBitSet<uint32_t, 2>>();
111 // - An 8-bit one, which is maximally different in terms of bitset behavior.
112 test_fn.template operator()<bitset_detail::MultiIntBitSet<uint8_t, 8>>();
113
114 // Also run with 32-bit set types if the cluster doesn't use indexes above 31.

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 11

SanityCheckFunction · 0.85
LinearizeFunction · 0.85
randrangeMethod · 0.80
TxCountMethod · 0.80
PositionRangeMethod · 0.80
clearMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
CountMethod · 0.45
rand64Method · 0.45

Tested by

no test coverage detected