Remove dead branches from the fast translation

Drop predicate operands and unreachable arms that dispatch and readable invariants fix, and extend the exhaustive sweep to cover every branch side. Gate coverage on branch coverage as well.
This commit is contained in:
andrew committed 2026-11-17 19:26:09 -05:00
1 parent 3bb5d062ff
commit 73e371fb40
4 files changed
+101 -172

No files matched your search

+17 -25
View File
@@ -23,7 +23,7 @@ The Lean build is untouched.
| `tests/fuzz_raw.cpp` | libFuzzer safety test on arbitrary instances under ASan/UBSan. | | `tests/fuzz_raw.cpp` | libFuzzer safety test on arbitrary instances under ASan/UBSan. |
| `tests/bench.cpp` | nanobench literal-vs-fast benchmark. | | `tests/bench.cpp` | nanobench literal-vs-fast benchmark. |
| `run-fuzz.sh` | Rebuilds and runs both fuzzers for a fixed time. | | `run-fuzz.sh` | Rebuilds and runs both fuzzers for a fixed time. |
| `run-coverage.sh` | Builds with Clang source-based coverage and requires 100% line coverage of `src/fast.cpp`. | | `run-coverage.sh` | Builds with Clang source-based coverage and requires 100% line and branch coverage of `src/fast.cpp`. |
| `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). | | `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). |
## Data model ## Data model
@@ -92,7 +92,7 @@ cases that satisfy the readable sorting invariant:
`exhaustive_test` enumerates small readable instances instead of sampling them: `exhaustive_test` enumerates small readable instances instead of sampling them:
every sorted database of up to four keys over a fixed universe of boundary every sorted database of up to four keys over a fixed universe of boundary
keys, every selector key (including `⊥` and `⊤`) with offsets in `[-3, 4]`, and keys, every selector key (including `⊥` and `⊤`) with offsets in `[-3, 5]`, and
every readable result (the complete traversal, each nonempty truncated prefix, every readable result (the complete traversal, each nonempty truncated prefix,
or the empty result when the traversal is empty) in both directions: or the empty result when the traversal is empty) in both directions:
@@ -110,34 +110,26 @@ It runs in about a second and is also the coverage driver.
``` ```
The script configures a Clang source-based-coverage build, runs The script configures a Clang source-based-coverage build, runs
`exhaustive_test`, and fails unless `src/fast.cpp` has 100% line coverage. `exhaustive_test`, and fails unless `src/fast.cpp` has 100% line and branch
`exhaustive_test` alone covers every line of the summarised implementation: coverage. `exhaustive_test` alone covers every region, function, line, and
branch side of the summarised implementation:
```text ```text
src/fast.cpp 836 regions (12 missed) 131 functions (0 missed) 773 lines (0 missed) 670 branches (125 missed) src/fast.cpp 570 regions (0 missed) 123 functions (0 missed) 684 lines (0 missed) 388 branches (0 missed)
``` ```
That is 100.00% line and function coverage, 98.56% region coverage, and 81.34% That is 100.00% line, function, region, and branch coverage. Offset 5 is part of
branch coverage. The missed branches are short-circuit operands: line coverage the sweep specifically to exercise the last three short-circuit branch sides,
counts a condition as covered once any operand decides it, but branch coverage which the previous `[-3, 4]` window missed.
requires every operand to be the deciding one. Reaching 100% branch coverage
would need inputs that force each redundant operand to disagree with the rest of
its condition, which is not practical here.
Because equivalence is only promised on readable inputs, `src/fast.cpp` deletes Because equivalence is only promised on readable inputs, `src/fast.cpp` deletes
*control-flow branches* that no readable input can take: the complete-closed both unreachable control-flow branches and predicate operands that an enclosing
family, the offset-one total/any wrappers and their `end.offset <= 1` shortcut, dispatch invariant fixes. Examples include the complete-closed family, the
the `end.offset <= 1` shortcut in `completeForwardCrossedGeneral`, the offset-one total/any wrappers, the `end.offset <= 1` shortcuts, the
`crossedCompleteEndLeOne` guard, and one `lastK < end.key` test that readability `crossedCompleteEndLeOne` guard, and the repeated
always satisfies. `!c.r->reverse`/`!o.more`/offset tests inside subtrees that are only entered
after those tests succeeded. `literal.cpp` keeps the full Lean conditions so the
The deletion criterion is deliberately limited to unreachable arms. An translation can still be audited line by line.
individual predicate operand that a dispatch invariant makes always true at one
call site (for example `!c.r->reverse` inside a condition only reached after the
forward branch was selected) is kept, because it is part of the Lean condition
that the translation mirrors; re-proving the invariant at every site would make
the tree harder to audit. Those operands are why every line is covered while branch coverage stays
around 81%.
## Fuzzing ## Fuzzing
@@ -168,7 +160,7 @@ One representative validation run on this machine:
| literal vs. Lean golden vectors | 40,000 | 0 mismatches (exact endpoints) | | literal vs. Lean golden vectors | 40,000 | 0 mismatches (exact endpoints) |
| fast vs. literal on sorted corpus | 18,971 | 0 mismatches | | fast vs. literal on sorted corpus | 18,971 | 0 mismatches |
| extreme-offset regression (`extreme_test`) | 7,574 | 0 mismatches | | extreme-offset regression (`extreme_test`) | 7,574 | 0 mismatches |
| exhaustive readable sweep (`exhaustive_test`) | 10,562,068 | 0 mismatches | | exhaustive readable sweep (`exhaustive_test`) | 13,565,772 | 0 mismatches |
| readable differential fuzzing (60 s) | 1,182,424 | 0 mismatches | | readable differential fuzzing (60 s) | 1,182,424 | 0 mismatches |
| raw safety fuzzing (60 s) | 1,905,270 | no ASan/UBSan findings | | raw safety fuzzing (60 s) | 1,905,270 | no ASan/UBSan findings |
+10 -4
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Build with Clang source-based coverage and require 100% line coverage of the # Build with Clang source-based coverage and require 100% line and branch
# summarised implementation (`src/fast.cpp`). `exhaustive_test` enumerates # coverage of the summarised implementation (`src/fast.cpp`). `exhaustive_test`
# small readable instances deterministically and is sufficient on its own. # enumerates small readable instances deterministically and is sufficient on its
# own.
set -euo pipefail set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -39,8 +40,13 @@ report() {
output="$(report)" output="$(report)"
printf '%s\n' "$output" printf '%s\n' "$output"
line_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $10 }')" line_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $10 }')"
branch_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $13 }')"
if [[ "$line_cover" != "100.00%" ]]; then if [[ "$line_cover" != "100.00%" ]]; then
echo "fast.cpp line coverage is $line_cover, expected 100.00%" >&2 echo "fast.cpp line coverage is $line_cover, expected 100.00%" >&2
exit 1 exit 1
fi fi
echo "fast.cpp: 100.00% line coverage" if [[ "$branch_cover" != "100.00%" ]]; then
echo "fast.cpp branch coverage is $branch_cover, expected 100.00%" >&2
exit 1
fi
echo "fast.cpp: 100.00% line and branch coverage"
+69 -140
View File
@@ -4,14 +4,14 @@
// `literal.cpp` mirrors the Lean definition line by line and therefore rescans // `literal.cpp` mirrors the Lean definition line by line and therefore rescans
// `res.keys` (and re-derives small arithmetic facts) on almost every branch. // `res.keys` (and re-derives small arithmetic facts) on almost every branch.
// This version summarises the reported keys once (two binary searches on the // This version summarises the reported keys once (two binary searches on the
// sorted traversal order) and caches the handful of interval cardinalities the // sorted traversal order), then replays the same tree for readable inputs.
// decision tree needs, then replays the same tree for readable inputs. Branches // Branches and predicate operands that the `Readable` invariant or a dispatch
// that the `Readable` invariant makes unreachable are omitted. Reversed results // invariant makes unreachable/constant are omitted; `literal.cpp` keeps the
// are handled through a `reversed` view instead of copying the key list. // full Lean conditions for audit. Reversed results are handled through a
// `reversed` view instead of copying the key list.
#include "fast.h" #include "fast.h"
#include <algorithm> #include <algorithm>
#include <cassert>
#include <utility> #include <utility>
namespace cr::fast { namespace cr::fast {
@@ -34,14 +34,10 @@ struct Obs {
bool ascending = true; bool ascending = true;
std::size_t n = 0; std::size_t n = 0;
const KeyView& firstK() const { // `closedForm` handles an empty result before building the tree, so `keys` is
assert(!keys.empty()); // non-empty everywhere these are called.
return reversed ? keys.back() : keys.front(); const KeyView& firstK() const { return reversed ? keys.back() : keys.front(); }
} const KeyView& lastK() const { return reversed ? keys.front() : keys.back(); }
const KeyView& lastK() const {
assert(!keys.empty());
return reversed ? keys.front() : keys.back();
}
}; };
// Facts that only depend on the read and on the *multiset* of reported keys. // Facts that only depend on the read and on the *multiset* of reported keys.
@@ -51,27 +47,15 @@ struct Ctx {
std::size_t n = 0; std::size_t n = 0;
std::size_t below = 0; // number of reported keys < begin.key std::size_t below = 0; // number of reported keys < begin.key
std::size_t t = 0; // number of reported keys >= end.key std::size_t t = 0; // number of reported keys >= end.key
// Lazily computed interval cardinalities.
mutable bool haveBE = false;
mutable bool haveEB = false;
mutable NatInf be;
mutable NatInf eb;
}; };
NatInf cardBE(const Ctx& c) { NatInf cardBE(const Ctx& c) {
if (!c.haveBE) { return intervalCard(c.r->begin.key, c.r->end_.key);
c.be = intervalCard(c.r->begin.key, c.r->end_.key);
c.haveBE = true;
}
return c.be;
} }
// Each path consults this at most once, so it needs no cache.
NatInf cardEB(const Ctx& c) { NatInf cardEB(const Ctx& c) {
if (!c.haveEB) { return intervalCard(c.r->end_.key, c.r->begin.key);
c.eb = intervalCard(c.r->end_.key, c.r->begin.key);
c.haveEB = true;
}
return c.eb;
} }
NatInf cardKO(KeyView b, OptKeyView e) { NatInf cardKO(KeyView b, OptKeyView e) {
@@ -132,10 +116,6 @@ Ctx makeCtx(const RangeRead& r, const Obs& o) {
// Comparisons that avoid allocating a one-element `OptKey` // Comparisons that avoid allocating a one-element `OptKey`
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// ↑k < b
bool kLtO(KeyView k, OptKeyView b) { return !b || keyLt(k, *b); }
// ↑k <= b
bool kLeO(KeyView k, OptKeyView b) { return !b || !keyLt(*b, k); }
// a < ↑k // a < ↑k
bool oLtK(OptKeyView a, KeyView k) { return a.has_value() && keyLt(*a, k); } bool oLtK(OptKeyView a, KeyView k) { return a.has_value() && keyLt(*a, k); }
// a <= ↑k // a <= ↑k
@@ -149,7 +129,6 @@ std::uint64_t rB(const Ctx& c) { return residualB(*c.r); }
std::uint64_t npd(const Ctx& c) { return clampNonposD(*c.r); } std::uint64_t npd(const Ctx& c) { return clampNonposD(*c.r); }
bool allBelowEnd(const Ctx& c) { return c.t == 0; } bool allBelowEnd(const Ctx& c) { return c.t == 0; }
bool anyAtOrAboveEnd(const Ctx& c) { return c.t != 0; }
const KeyView& leastReportedK(const Ctx& c, const Obs& o) { const KeyView& leastReportedK(const Ctx& c, const Obs& o) {
return c.r->reverse ? o.lastK() : o.firstK(); return c.r->reverse ? o.lastK() : o.firstK();
@@ -205,13 +184,16 @@ bool clampNonposBottom(const Ctx& c) {
return niLe(lhs, NatInf::of(bSlack(c))); return niLe(lhs, NatInf::of(bSlack(c)));
} }
// The caller reaches this only after `begin.offset >= 1` failed, so the offset
// half of the condition is already known.
bool clampedNonpos(const Ctx& c, const Obs& o) { bool clampedNonpos(const Ctx& c, const Obs& o) {
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, leastReportedK(c, o)); return oLeK(c.r->begin.key, leastReportedK(c, o));
} }
bool clampsToBottom(const Ctx& c, const Obs& o) { // Called from the positive branch: if `begin.offset <= 0` then the entry
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, o.firstK()); // dispatch already established `clampedNonpos`, i.e. the selector key is at or
} // before the first reported key, so only the offset decides here.
bool clampsToBottom(const Ctx& c) { return c.r->begin.offset <= 0; }
std::uint64_t cELo(Int oB, Int oE) { std::uint64_t cELo(Int oB, Int oE) {
return std::max(toNat(2 - oE), toNat(oB - oE + 1)); return std::max(toNat(2 - oE), toNat(oB - oE + 1));
@@ -253,43 +235,34 @@ OptKeyView crossedBegin(const Ctx& c) {
struct Impl { struct Impl {
// ----- crossed complete -------------------------------------------------- // ----- crossed complete --------------------------------------------------
// The complete-crossed subtree is entered only with `!reverse` and `!more`
// (`closedFormCrossed` normalises a reversed complete read first). Within it,
// `crossedCompleteBeginZero` has already claimed every `begin.offset == 0`
// case before these clamp nodes are tried, so `begin.offset < 0` holds here.
bool negCrossClampEndOne(const Ctx& c, const Obs& o) const { bool negCrossClampEndOne(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && // `crossedCompleteEndLeOne` already failed, and for `end.offset == 1` it
c.r->end_.offset == 1 && kLtO(o.firstK(), c.r->begin.key) && // fails exactly when `bSlack >= below`.
c.below <= bSlack(c); return c.r->end_.offset == 1;
} }
bool negCrossHeadBottom(const Ctx& c, const Obs& o) const { bool negCrossHeadBottom(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
o.firstK().empty(); o.firstK().empty();
} }
bool negCrossClampNonposLastGe(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
oLeK(c.r->begin.key, o.lastK()) && c.below <= bSlack(c);
}
bool negCrossClampNonposBottom(const Ctx& c, const Obs& o) const { bool negCrossClampNonposBottom(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key);
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
kLtO(o.firstK(), c.r->begin.key) && clampNonposBottom(c);
} }
bool negCrossClampLongNonpos(const Ctx& c, const Obs& o) const { bool negCrossClampLongNonpos(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && return c.r->end_.offset <= 0 && optLt(c.r->end_.key, c.r->begin.key);
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
c.below <= bSlack(c) && clampNonposBottom(c) &&
optLt(c.r->end_.key, c.r->begin.key);
} }
ConflictRange closedFormNegCrossClampUnified(const Ctx& c, const Obs& o) const { ConflictRange closedFormNegCrossClampUnified(const Ctx& c, const Obs& o) const {
OptKey end; OptKey end;
if (negCrossClampEndOne(c, o)) if (negCrossClampEndOne(c, o))
end = clampEndOneUpper(c, o); end = clampEndOneUpper(c, o);
else if (negCrossClampLongNonpos(c, o) && else if (negCrossClampLongNonpos(c, o))
!negCrossClampNonposLastGe(c, o))
end = clampLongUpper(c, o); end = clampLongUpper(c, o);
else else
end = c.r->end_.key; end = c.r->end_.key;
@@ -316,9 +289,7 @@ struct Impl {
} }
bool negCrossCompleteHigh(const Ctx& c, const Obs& o) const { bool negCrossCompleteHigh(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && return oLtK(c.r->end_.key, o.firstK());
c.r->end_.offset >= 2 && kLtO(o.firstK(), c.r->begin.key) &&
oLtK(c.r->end_.key, o.firstK());
} }
ConflictRange closedFormNegCrossCompleteHigh(const Ctx& c, const Obs& o) const { ConflictRange closedFormNegCrossCompleteHigh(const Ctx& c, const Obs& o) const {
@@ -379,28 +350,17 @@ struct Impl {
// ----- crossed truncated ------------------------------------------------- // ----- crossed truncated -------------------------------------------------
bool negCrossTruncNonposGap(const Ctx& c, const Obs& o) const { // `closedFormForwardTruncatedNonpos` calls this only inside
return o.more && !c.r->reverse && c.r->begin.offset < 0 && // `below <= bSlack`, `end.offset <= 0`, `end < begin` and
kLtO(leastReportedK(c, o), c.r->begin.key) && // `clampNonposBottom`; the remaining Lean conditions of
c.r->end_.offset <= 0 && c.below <= bSlack(c) && // `negCrossTruncNonposGap`/`...A2aBottom` are invariants of the readable
kLtO(o.lastK(), c.r->begin.key) && // forward-crossed regime.
kLtO(o.lastK(), c.r->end_.key) &&
!isTop(c.r->end_.key) &&
(optLt(c.r->end_.key, c.r->begin.key) ||
niLt(cardBE(c), NatInf::of(npd(c) + 1)));
}
bool negCrossTruncNonposGapA2aBottom(const Ctx& c, const Obs& o) const {
return negCrossTruncNonposGap(c, o) &&
optLt(c.r->end_.key, c.r->begin.key) && clampNonposBottom(c);
}
bool negCrossTruncNonposGapA2aBottomHigh(const Ctx& c, const Obs& o) const { bool negCrossTruncNonposGapA2aBottomHigh(const Ctx& c, const Obs& o) const {
std::uint64_t extra = o.firstK().empty() ? 1 : 0; std::uint64_t extra = o.firstK().empty() ? 1 : 0;
NatInf lhs = NatInf::of(bSlack(c) + 1 + extra); NatInf lhs = NatInf::of(bSlack(c) + 1 + extra);
NatInf rhs = niAdd(NatInf::of(static_cast<std::uint64_t>(c.n)), NatInf rhs = niAdd(NatInf::of(static_cast<std::uint64_t>(c.n)),
cardKO(keyAfter(o.lastK()), c.r->begin.key)); cardKO(keyAfter(o.lastK()), c.r->begin.key));
return negCrossTruncNonposGapA2aBottom(c, o) && niLe(lhs, rhs); return niLe(lhs, rhs);
} }
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const Ctx& c, ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const Ctx& c,
@@ -416,10 +376,10 @@ struct Impl {
return niLe(lhs, NatInf::of(bSlack(c))); return niLe(lhs, NatInf::of(bSlack(c)));
} }
// Reached only in the reversed truncation branch (`more` set, `reverse` set,
// `begin.offset < 0`, `end.offset >= 2`).
bool negCrossTruncRevResidualFiniteOver(const Ctx& c, const Obs& o) const { bool negCrossTruncRevResidualFiniteOver(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 && return oLtK(c.r->end_.key, o.lastK());
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
kLtO(o.lastK(), c.r->begin.key) && c.t == rE(c);
} }
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const Ctx& c, ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const Ctx& c,
@@ -440,10 +400,7 @@ struct Impl {
} }
bool negCrossTruncRevResidualTopLarge(const Ctx& c, const Obs& o) const { bool negCrossTruncRevResidualTopLarge(const Ctx& c, const Obs& o) const {
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 && if (!(oLtK(c.r->end_.key, o.lastK()) && c.t < rE(c))) return false;
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
c.t < rE(c)))
return false;
NatInf lhs = NatInf::of(rE(c)); NatInf lhs = NatInf::of(rE(c));
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()), NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
NatInf::of(static_cast<std::uint64_t>(c.n))); NatInf::of(static_cast<std::uint64_t>(c.n)));
@@ -561,10 +518,8 @@ struct Impl {
} }
bool zeroCrossUnderEnd(const Ctx& c, const Obs& o) const { bool zeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !o.more && !c.r->reverse && // The only caller tests `crossedCompleteBeginZero`, i.e. `begin.offset == 0`.
c.r->end_.offset >= 2 && return optLe(c.r->begin.key, c.r->end_.key);
kLtO(leastReportedK(c, o), c.r->begin.key) &&
optLe(c.r->begin.key, c.r->end_.key);
} }
ConflictRange closedFormZeroCrossResidual(const Ctx& c, const Obs& o) const { ConflictRange closedFormZeroCrossResidual(const Ctx& c, const Obs& o) const {
@@ -598,16 +553,14 @@ struct Impl {
} else if (isTop(r.end_.key)) { } else if (isTop(r.end_.key)) {
inner = true; inner = true;
} else { } else {
bool firstFour = // Readability makes the two cardinality bounds and `ePlusEnd > 0`
niLe(NatInf::of(b), underBegin) && // automatic once the first two bounds hold, so only the endpoint
niLe(NatInf::of(e), underEnd) && // ordering remains to decide.
niLe(NatInf::of(natSub(b, e)), bool firstTwo = niLe(NatInf::of(b), underBegin) &&
intervalCard(r.end_.key, r.begin.key)) && niLe(NatInf::of(e), underEnd);
niLe(NatInf::of(natSub(e, b)),
intervalCard(r.begin.key, r.end_.key));
Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1; Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1;
Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1; Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1;
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd; inner = firstTwo && bPlusBegin < ePlusEnd;
} }
return !inner; return !inner;
} }
@@ -644,7 +597,7 @@ struct Impl {
// ----- complete and forward truncated results ---------------------------- // ----- complete and forward truncated results ----------------------------
OptKeyView closedFormBegin(const Ctx& c, const Obs& o) const { OptKeyView closedFormBegin(const Ctx& c, const Obs& o) const {
return clampsToBottom(c, o) ? bottomKeyView() : c.r->begin.key; return clampsToBottom(c) ? bottomKeyView() : c.r->begin.key;
} }
bool closedFormFinite(const Ctx& c) const { bool closedFormFinite(const Ctx& c) const {
@@ -655,7 +608,7 @@ struct Impl {
OptKeyView begin = closedFormBegin(c, o); OptKeyView begin = closedFormBegin(c, o);
if (c.r->end_.offset <= 1) if (c.r->end_.offset <= 1)
return mk(std::move(begin), c.r->end_.key); return mk(std::move(begin), c.r->end_.key);
if (clampsToBottom(c, o) || optLe(c.r->begin.key, c.r->end_.key)) { if (clampsToBottom(c) || optLe(c.r->begin.key, c.r->end_.key)) {
if (c.t == rE(c)) return mk(std::move(begin), after(o.lastK())); if (c.t == rE(c)) return mk(std::move(begin), after(o.lastK()));
if (allBelowEnd(c)) return mk(std::move(begin), topKey()); if (allBelowEnd(c)) return mk(std::move(begin), topKey());
if (closedFormFinite(c)) return mk(std::move(begin), after(o.lastK())); if (closedFormFinite(c)) return mk(std::move(begin), after(o.lastK()));
@@ -678,7 +631,7 @@ struct Impl {
} }
NatInf closedFormForwardCapacity(const Ctx& c, const Obs& o) const { NatInf closedFormForwardCapacity(const Ctx& c, const Obs& o) const {
if (clampsToBottom(c, o)) return NatInf::of(0); if (clampsToBottom(c)) return NatInf::of(0);
return cardEB(c); return cardEB(c);
} }
@@ -746,13 +699,11 @@ struct Impl {
} }
bool zeroCrossOvershootComplete(const Ctx& c, const Obs& o) const { bool zeroCrossOvershootComplete(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !o.more && !c.r->reverse && return c.r->begin.offset == 0 && overshoot(c);
overshoot(c) && kLtO(leastReportedK(c, o), c.r->begin.key);
} }
bool negCrossOvershoot(const Ctx& c, const Obs& o) const { bool negCrossOvershoot(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && overshoot(c) && return c.r->begin.offset < 0 && overshoot(c);
kLtO(o.firstK(), c.r->begin.key);
} }
bool crossedCompleteOvershoot(const Ctx& c, const Obs& o) const { bool crossedCompleteOvershoot(const Ctx& c, const Obs& o) const {
@@ -766,21 +717,10 @@ struct Impl {
!(c.below <= bSlack(c) && clampNonposBottom(c)); !(c.below <= bSlack(c) && clampNonposBottom(c));
} }
bool zeroCrossLongTop(const Ctx& c, const Obs& o) const { // The three `zeroCross*` predicates above cover every `begin.offset == 0`
return c.r->begin.offset == 0 && !o.more && !c.r->reverse && // case of the complete-crossed subtree, so the mode alone decides here.
c.r->end_.offset >= 2 && isTop(c.r->begin.key);
}
bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
c.r->end_.offset >= 2 && !isTop(c.r->begin.key) &&
optLt(c.r->end_.key, c.r->begin.key) &&
kLtO(leastReportedK(c, o), c.r->begin.key);
}
bool crossedCompleteBeginZero(const Ctx& c, const Obs& o) const { bool crossedCompleteBeginZero(const Ctx& c, const Obs& o) const {
return zeroCrossUnderEnd(c, o) || zeroCrossLongTop(c, o) || return c.r->begin.offset == 0;
zeroCrossLongFinite(c, o);
} }
bool crossedCompleteClamp(const Ctx& c, const Obs& o) const { bool crossedCompleteClamp(const Ctx& c, const Obs& o) const {
@@ -803,21 +743,19 @@ struct Impl {
// ----- crossed truncated dispatch --------------------------------------- // ----- crossed truncated dispatch ---------------------------------------
// Every node below is reached only from the reversed truncation branch, where
// `more` is set, `reverse` is set, `begin.offset < 0` and `end.offset >= 2`
// (the `end.offset <= 1` case is handled by the dispatch).
bool zeroCrossTruncRevCrossed(const Ctx& c, const Obs& o) const { bool zeroCrossTruncRevCrossed(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && c.r->reverse && o.more && return c.r->begin.offset == 0;
kLtO(leastReportedK(c, o), c.r->begin.key);
} }
bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const { bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 && return optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
kLtO(o.lastK(), c.r->begin.key) &&
optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
} }
bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const { bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 && return optLt(c.r->end_.key, c.r->begin.key) &&
kLtO(o.lastK(), c.r->begin.key) &&
optLt(c.r->end_.key, c.r->begin.key) &&
c.r->end_.offset <= 1 && c.r->end_.offset <= 1 &&
niLe(niAdd(niAdd(cardEB(c), niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))), NatInf::of(static_cast<std::uint64_t>(c.n))),
@@ -831,31 +769,24 @@ struct Impl {
} }
bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const { bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 && return overshoot(c);
kLtO(o.lastK(), c.r->begin.key) && overshoot(c);
} }
bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const { bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 && return niLe(niAdd(cardOK(c.r->end_.key, o.firstK()), NatInf::of(2)),
c.r->end_.offset >= 2 && anyAtOrAboveEnd(c) &&
niLe(niAdd(cardOK(c.r->end_.key, o.firstK()), NatInf::of(2)),
NatInf::of(rE(c))); NatInf::of(rE(c)));
} }
bool negCrossTruncRevResidualTopCrossedSmallGap(const Ctx& c, bool negCrossTruncRevResidualTopCrossedSmallGap(const Ctx& c,
const Obs& o) const { const Obs& o) const {
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 && if (!oLtK(c.r->end_.key, o.lastK())) return false;
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK())))
return false;
NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()), NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
NatInf::of(static_cast<std::uint64_t>(c.n))); NatInf::of(static_cast<std::uint64_t>(c.n)));
return niLt(lhs, NatInf::of(rE(c))); return niLt(lhs, NatInf::of(rE(c)));
} }
bool negCrossTruncRevResidualTopCrossed(const Ctx& c, const Obs& o) const { bool negCrossTruncRevResidualTopCrossed(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 && return !oLtK(c.r->end_.key, o.lastK()) && c.t < rE(c);
c.r->end_.offset >= 2 && kLeO(o.lastK(), c.r->end_.key) &&
c.t < rE(c);
} }
bool revTruncGeTwoTop(const Ctx& c, const Obs& o) const { bool revTruncGeTwoTop(const Ctx& c, const Obs& o) const {
@@ -867,7 +798,7 @@ struct Impl {
bool negCrossTruncRevResidualTopLargeCrossed(const Ctx& c, const Obs& o) const { bool negCrossTruncRevResidualTopLargeCrossed(const Ctx& c, const Obs& o) const {
return negCrossTruncRevResidualTopLarge(c, o) && return negCrossTruncRevResidualTopLarge(c, o) &&
kLtO(o.lastK(), c.r->begin.key) && !isTop(c.r->begin.key); !isTop(c.r->begin.key);
} }
bool revTruncGeTwoLarge(const Ctx& c, const Obs& o) const { bool revTruncGeTwoLarge(const Ctx& c, const Obs& o) const {
@@ -876,9 +807,7 @@ struct Impl {
} }
bool negCrossTruncTopTop(const Ctx& c, const Obs& o) const { bool negCrossTruncTopTop(const Ctx& c, const Obs& o) const {
return o.more && !c.r->reverse && c.r->begin.offset < 0 && return isTop(c.r->begin.key);
isTop(c.r->begin.key) && isTop(c.r->end_.key) &&
c.r->end_.offset <= 0;
} }
bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const { bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const {
+5 -3
View File
@@ -3,12 +3,12 @@
// //
// The fuzzers sample readable instances; this test enumerates them: every // The fuzzers sample readable instances; this test enumerates them: every
// sorted database (up to four keys) over a fixed universe of interesting keys, // sorted database (up to four keys) over a fixed universe of interesting keys,
// every selector key (including ⊥ and ⊤) paired with offsets in [-3, 4], and // every selector key (including ⊥ and ⊤) paired with offsets in [-3, 5], and
// every readable result (the complete traversal, each nonempty truncated // every readable result (the complete traversal, each nonempty truncated
// prefix, or the empty result when the traversal is empty) in both directions. // prefix, or the empty result when the traversal is empty) in both directions.
// //
// Together with `golden_test`, `diff_corpus`, and `extreme_test`, this covers // Together with `golden_test`, `diff_corpus`, and `extreme_test`, this covers
// every line of `src/fast.cpp`. // every line and branch side of `src/fast.cpp`.
#include <algorithm> #include <algorithm>
#include <bit> #include <bit>
#include <cstdint> #include <cstdint>
@@ -24,7 +24,9 @@ namespace {
using namespace cr; using namespace cr;
const Int kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4}; // Offset 5 is what lets the default sweep exercise the last three
// short-circuit branch sides in `fast.cpp` (all in crossed subtrees).
const Int kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
void check(const RangeRead& r, const Result& res, long long& cases, void check(const RangeRead& r, const Result& res, long long& cases,
long long& bad, const char* kind, Int bo, Int eo, bool reverse, long long& bad, const char* kind, Int bo, Int eo, bool reverse,