Harden and fully cover the C++ translation

Add an exhaustive readable-instance sweep and a coverage gate requiring
100% line coverage of the summarised implementation. Delete branches that
are unreachable under `Readable`, centralise and assert the 32-bit offset
contract at the public entry points, and stop materialising `n` zero bytes
when deciding `keyAfterN n k < upper`.

Also add the extreme-offset regression test, harden the corpus parsers,
remove unused model helpers, document the literal-total / fast-readable
split, and refresh the validation and benchmark numbers.
This commit is contained in:
andrew committed 2026-11-17 18:46:24 -05:00
1 parent e32e42d933
commit 3bb5d062ff
15 files changed
+620 -196

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
// A deterministic exhaustive sweep of small readable instances, comparing the
// literal translation with the summarised implementation.
//
// The fuzzers sample readable instances; this test enumerates them: every
// 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 readable result (the complete traversal, each nonempty truncated
// prefix, or the empty result when the traversal is empty) in both directions.
//
// Together with `golden_test`, `diff_corpus`, and `extreme_test`, this covers
// every line of `src/fast.cpp`.
#include <algorithm>
#include <bit>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
using namespace cr;
const Int kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4};
void check(const RangeRead& r, const Result& res, long long& cases,
long long& bad, const char* kind, Int bo, Int eo, bool reverse,
std::size_t keep) {
++cases;
if (!gen::equivalent(literal::closedForm(r, res), fast::closedForm(r, res))) {
if (++bad <= 10)
std::fprintf(stderr,
"MISMATCH %s bo=%lld eo=%lld rev=%d keep=%zu\n", kind,
static_cast<long long>(bo), static_cast<long long>(eo),
reverse, keep);
}
}
void run(const std::vector<Key>& db, const std::vector<OptKey>& keys,
long long& cases, long long& bad) {
for (const OptKey& beginKey : keys) {
for (const OptKey& endKey : keys) {
for (Int beginOffset : kOffsets) {
for (Int endOffset : kOffsets) {
const Selector begin{optView(beginKey), beginOffset};
const Selector end{optView(endKey), endOffset};
const std::vector<Key> complete = gen::inRange(
db, gen::resolve(db, begin), gen::resolve(db, end));
for (bool reverse : {false, true}) {
const RangeRead r{begin, end, reverse};
std::vector<Key> ordered = complete;
if (reverse) std::reverse(ordered.begin(), ordered.end());
if (ordered.empty()) {
// The only readable result for an empty traversal.
const std::vector<KeyView> none;
check(r, Result{none, false}, cases, bad, "empty", beginOffset,
endOffset, reverse, 0);
continue;
}
// Every nonempty prefix, including the whole traversal with
// `more = true` (readability does not require a proper prefix).
for (std::size_t keep = 1; keep <= ordered.size(); ++keep) {
std::vector<KeyView> views;
views.reserve(keep);
for (std::size_t i = 0; i < keep; ++i) views.push_back(ordered[i]);
check(r, Result{views, true}, cases, bad, "truncated",
beginOffset, endOffset, reverse, keep);
}
// The complete result.
std::vector<KeyView> views;
views.reserve(ordered.size());
for (const Key& k : ordered) views.push_back(k);
check(r, Result{views, false}, cases, bad, "complete", beginOffset,
endOffset, reverse, ordered.size());
}
}
}
}
}
}
} // namespace
int main() {
// Prefix chains exercise the `intervalCard` and `keyAfter` cases; `⊤` and
// the empty key are handled separately as selector keys.
const std::vector<Key> universe{
Key(""), Key("\0", 1), Key("\0\0", 2), Key("\0\xff", 2),
Key("a"), Key("a\0", 2), Key("a\0\0", 3), Key("a\0\0\0", 4),
Key("b"), Key("\xff", 1)};
std::vector<OptKey> keys;
keys.emplace_back(std::nullopt);
for (const Key& k : universe) keys.emplace_back(k);
long long cases = 0, bad = 0;
const std::uint32_t limit = 1u << universe.size();
for (std::uint32_t mask = 0; mask < limit; ++mask) {
if (std::popcount(mask) > 4) continue;
std::vector<Key> db;
for (std::size_t i = 0; i < universe.size(); ++i)
if (mask & (1u << i)) db.push_back(universe[i]);
run(db, keys, cases, bad);
}
std::printf("exhaustive: %lld cases, %lld mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}