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:
1 parent
e32e42d933
commit
3bb5d062ff
15 files changed
+620
-196
No files matched your search
+12
-11
@@ -23,7 +23,8 @@ struct Case {
|
||||
Result res;
|
||||
const char* name = "";
|
||||
|
||||
// Build the borrowed views once the case sits at its final address.
|
||||
// Build the borrowed views in place. `Case` is move-only (through
|
||||
// `Instance`), and the views must be rebuilt after any move.
|
||||
void prepare() {
|
||||
inst.finish();
|
||||
r = inst.read();
|
||||
@@ -38,9 +39,7 @@ std::vector<Key> makeDb(std::size_t n, std::size_t len) {
|
||||
Key k;
|
||||
k.reserve(len);
|
||||
for (std::size_t j = 0; j < len; ++j)
|
||||
k.push_back(static_cast<Byte>((i >> (8 * j)) & 0xff));
|
||||
// Ensure strict ordering is preserved for the multi-byte encoding.
|
||||
if (!db.empty() && !keyLt(db.back(), k)) k.push_back(static_cast<Byte>(i));
|
||||
k.push_back(static_cast<char>(static_cast<Byte>((i >> (8 * j)) & 0xff)));
|
||||
db.push_back(std::move(k));
|
||||
}
|
||||
std::sort(db.begin(), db.end());
|
||||
@@ -117,10 +116,12 @@ int main() {
|
||||
// A short truncated read (cross-free).
|
||||
cases.push_back(makeCase(smallDb, smallDb[1], 1, top, 1, false, true,
|
||||
"small truncated fwd"));
|
||||
// A crossed read: the end selector resolves before the begin selector.
|
||||
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, false,
|
||||
// A crossed read: the end *key* is below the begin key, but the offsets move
|
||||
// the resolved anchors back into order, so the range is nonempty and the
|
||||
// dispatch takes the crossed branch.
|
||||
cases.push_back(makeCase(mediumDb, mediumDb[40], -30, mediumDb[3], 40, false,
|
||||
true, "crossed truncated fwd"));
|
||||
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, true,
|
||||
cases.push_back(makeCase(mediumDb, mediumDb[40], -30, mediumDb[3], 40, true,
|
||||
true, "crossed truncated rev"));
|
||||
// Long complete / truncated reads.
|
||||
cases.push_back(
|
||||
@@ -130,10 +131,10 @@ int main() {
|
||||
cases.push_back(makeCase(longDb, bot, 1, top, 1, true, true,
|
||||
"long truncated rev"));
|
||||
|
||||
// The case vector is final now, so the borrowed views can point at it.
|
||||
for (Case& c : cases) c.prepare();
|
||||
|
||||
for (const Case& c : cases) {
|
||||
// Prepare each case in place (its views borrow from the instance) and check
|
||||
// that both implementations agree before timing them.
|
||||
for (Case& c : cases) {
|
||||
c.prepare();
|
||||
check(c);
|
||||
Bench bench;
|
||||
bench.title(c.name).relative(true).warmup(20).minEpochIterations(200);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Compare the literal translation with the summarised implementation on the
|
||||
// Lean-generated corpus (readable or not).
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
@@ -26,7 +27,7 @@ bool parseKey(const std::string& tok, OptKey& out) {
|
||||
for (std::size_t i = 0; i < tok.size(); i += 2) {
|
||||
int hi = hex(tok[i]), lo = hex(tok[i + 1]);
|
||||
if (hi < 0 || lo < 0) return false;
|
||||
k.push_back(static_cast<Byte>(hi * 16 + lo));
|
||||
k.push_back(static_cast<char>(static_cast<Byte>(hi * 16 + lo)));
|
||||
}
|
||||
out = std::move(k);
|
||||
return true;
|
||||
@@ -37,7 +38,8 @@ std::string encKey(const OptKey& k) {
|
||||
if (k->empty()) return "-";
|
||||
static const char* d = "0123456789abcdef";
|
||||
std::string s;
|
||||
for (Byte b : *k) { s.push_back(d[b >> 4]); s.push_back(d[b & 15]); }
|
||||
for (char ch : *k) {
|
||||
const Byte b = static_cast<Byte>(ch); s.push_back(d[b >> 4]); s.push_back(d[b & 15]); }
|
||||
return s;
|
||||
}
|
||||
} // namespace
|
||||
@@ -45,9 +47,15 @@ std::string encKey(const OptKey& k) {
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) return 2;
|
||||
std::ifstream in(argv[1]);
|
||||
if (!in) {
|
||||
std::fprintf(stderr, "cannot open %s\n", argv[1]);
|
||||
return 2;
|
||||
}
|
||||
std::string line;
|
||||
std::size_t cases = 0, bad = 0;
|
||||
std::size_t lineNo = 0;
|
||||
std::size_t cases = 0, bad = 0, skipped = 0;
|
||||
while (std::getline(in, line)) {
|
||||
++lineNo;
|
||||
if (line.empty() || line[0] == '#') continue;
|
||||
std::istringstream ss(line);
|
||||
std::string bk, ek, arrow, ob, oe;
|
||||
@@ -55,8 +63,14 @@ int main(int argc, char** argv) {
|
||||
int rev = 0, more = 0;
|
||||
std::size_t n = 0;
|
||||
OptKey beginKey, endKey;
|
||||
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n)) continue;
|
||||
if (!parseKey(bk, beginKey) || !parseKey(ek, endKey)) continue;
|
||||
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n) ||
|
||||
!parseKey(bk, beginKey) || !parseKey(ek, endKey) || n > line.size() ||
|
||||
!cr::offsetsFitInt32(static_cast<cr::Int>(bo),
|
||||
static_cast<cr::Int>(eo))) {
|
||||
std::fprintf(stderr, "line %zu: bad header: %s\n", lineNo, line.c_str());
|
||||
++bad;
|
||||
continue;
|
||||
}
|
||||
// Owned reported keys plus the views borrowed by both implementations.
|
||||
std::vector<Key> keys;
|
||||
keys.reserve(n);
|
||||
@@ -68,7 +82,11 @@ int main(int argc, char** argv) {
|
||||
if (!parseKey(kt, p) || !p) { ok = false; break; }
|
||||
keys.push_back(std::move(*p));
|
||||
}
|
||||
if (!ok || !(ss >> arrow >> ob >> oe) || arrow != "=>") continue;
|
||||
if (!ok || !(ss >> arrow >> ob >> oe) || arrow != "=>") {
|
||||
std::fprintf(stderr, "line %zu: bad body: %s\n", lineNo, line.c_str());
|
||||
++bad;
|
||||
continue;
|
||||
}
|
||||
// `fast` assumes the readable sorting invariant, so only compare cases that
|
||||
// satisfy it (this is a superset of the readable cases).
|
||||
const bool reverse = rev != 0;
|
||||
@@ -80,7 +98,7 @@ int main(int argc, char** argv) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!sorted) continue;
|
||||
if (!sorted) { ++skipped; continue; }
|
||||
std::vector<KeyView> keyViews;
|
||||
keyViews.reserve(keys.size());
|
||||
for (const Key& k : keys) keyViews.push_back(k);
|
||||
@@ -99,6 +117,11 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
}
|
||||
std::printf("diff: %zu cases, %zu mismatches\n", cases, bad);
|
||||
if (cases == 0) {
|
||||
std::fprintf(stderr, "diff: no comparable cases in %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
std::printf("diff: %zu cases, %zu skipped, %zu mismatches\n", cases, skipped,
|
||||
bad);
|
||||
return bad == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
// Regression test for offsets at the extremes of the input domain. Offsets
|
||||
// supplied by callers are 32-bit; a positive `end.offset` near `INT32_MAX`
|
||||
// would otherwise materialise about 2^31 zero bytes for Lean's
|
||||
// `keyAfterN n k < upper` test, so both implementations decide it from key
|
||||
// lengths (`keyAfterNLt`) instead of allocating.
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "../src/fast.h"
|
||||
#include "../src/literal.h"
|
||||
#include "readable_gen.h"
|
||||
|
||||
namespace {
|
||||
using namespace cr;
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
// Sorted, duplicate-free, with an empty key and a two-byte `0xff` key.
|
||||
const std::vector<Key> db{Key{}, Key(1, '\0'), Key("a"), Key("b"),
|
||||
Key("\xff\xff", 2)};
|
||||
|
||||
// Selector keys are owned here and borrowed by the selectors below.
|
||||
std::vector<OptKey> owned;
|
||||
owned.reserve(4);
|
||||
owned.push_back(std::nullopt); // ⊤
|
||||
owned.push_back(OptKey{Key{}}); // ⊥
|
||||
owned.push_back(OptKey{Key("a")});
|
||||
owned.push_back(OptKey{Key("\xff\xff", 2)});
|
||||
|
||||
// The public offset contract accepts exactly the 32-bit boundaries; the
|
||||
// entry-point assertions and corpus parsers share this predicate.
|
||||
const RangeRead boundary{Selector{std::nullopt, kMinInputOffset},
|
||||
Selector{std::nullopt, kMaxInputOffset}, false};
|
||||
const RangeRead outside{Selector{std::nullopt, kMinInputOffset - 1},
|
||||
Selector{std::nullopt, 0}, false};
|
||||
if (!inputOffsetsFitInt32(boundary) || inputOffsetsFitInt32(outside)) {
|
||||
std::fprintf(stderr, "offset contract check failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const Int offs[] = {std::numeric_limits<std::int32_t>::min(),
|
||||
std::numeric_limits<std::int32_t>::min() + 1,
|
||||
-1000000000,
|
||||
-3,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
1000000000,
|
||||
std::numeric_limits<std::int32_t>::max() - 1,
|
||||
std::numeric_limits<std::int32_t>::max()};
|
||||
std::vector<Selector> sels;
|
||||
sels.reserve(owned.size() * (sizeof(offs) / sizeof(offs[0])));
|
||||
for (const OptKey& k : owned)
|
||||
for (Int off : offs) sels.push_back(Selector{optView(k), off});
|
||||
|
||||
long long cases = 0, bad = 0;
|
||||
for (const Selector& bs : sels)
|
||||
for (const Selector& es : sels) {
|
||||
std::vector<Key> complete =
|
||||
gen::inRange(db, gen::resolve(db, bs), gen::resolve(db, es));
|
||||
for (bool reverse : {false, true}) {
|
||||
std::vector<Key> ordered = complete;
|
||||
if (reverse) std::reverse(ordered.begin(), ordered.end());
|
||||
|
||||
std::vector<std::pair<std::size_t, bool>> keeps;
|
||||
keeps.emplace_back(ordered.size(), false);
|
||||
if (!ordered.empty()) {
|
||||
keeps.emplace_back(1, true);
|
||||
if (ordered.size() > 1)
|
||||
keeps.emplace_back((ordered.size() + 1) / 2, true);
|
||||
}
|
||||
|
||||
for (auto [keep, more] : keeps) {
|
||||
std::vector<KeyView> views;
|
||||
views.reserve(keep);
|
||||
for (std::size_t i = 0; i < keep; ++i) views.push_back(ordered[i]);
|
||||
RangeRead r{bs, es, reverse};
|
||||
Result res{views, more};
|
||||
ConflictRange a = literal::closedForm(r, res);
|
||||
ConflictRange b = fast::closedForm(r, res);
|
||||
++cases;
|
||||
if (!gen::equivalent(a, b)) {
|
||||
if (++bad <= 10)
|
||||
std::fprintf(stderr,
|
||||
"MISMATCH bo=%lld eo=%lld rev=%d more=%d keys=%zu\n",
|
||||
static_cast<long long>(bs.offset),
|
||||
static_cast<long long>(es.offset), reverse, more,
|
||||
keep);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::printf("extreme: %lld cases, %lld mismatches\n", cases, bad);
|
||||
return bad == 0 ? 0 : 1;
|
||||
}
|
||||
+11
-3
@@ -1,5 +1,6 @@
|
||||
// Validates the C++ literal translation against golden vectors produced by
|
||||
// `cpp/oracle/gen.lean` (which calls the real `Statement.closedForm`).
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
@@ -34,7 +35,7 @@ bool parseKey(const std::string& tok, OptKey& out) {
|
||||
int hi = hex(tok[i]);
|
||||
int lo = hex(tok[i + 1]);
|
||||
if (hi < 0 || lo < 0) return false;
|
||||
k.push_back(static_cast<Byte>(hi * 16 + lo));
|
||||
k.push_back(static_cast<char>(static_cast<Byte>(hi * 16 + lo)));
|
||||
}
|
||||
out = std::move(k);
|
||||
return true;
|
||||
@@ -46,7 +47,8 @@ std::string encKey(const OptKey& k) {
|
||||
static const char* digits = "0123456789abcdef";
|
||||
std::string out;
|
||||
out.reserve(k->size() * 2);
|
||||
for (Byte b : *k) {
|
||||
for (char ch : *k) {
|
||||
const Byte b = static_cast<Byte>(ch);
|
||||
out.push_back(digits[b >> 4]);
|
||||
out.push_back(digits[b & 0xf]);
|
||||
}
|
||||
@@ -79,7 +81,9 @@ int main(int argc, char** argv) {
|
||||
std::size_t n = 0;
|
||||
OptKey beginKey, endKey;
|
||||
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n) ||
|
||||
!parseKey(bk, beginKey) || !parseKey(ek, endKey)) {
|
||||
!parseKey(bk, beginKey) || !parseKey(ek, endKey) || n > line.size() ||
|
||||
!cr::offsetsFitInt32(static_cast<cr::Int>(bo),
|
||||
static_cast<cr::Int>(eo))) {
|
||||
std::fprintf(stderr, "line %zu: bad header: %s\n", lineNo, line.c_str());
|
||||
++bad;
|
||||
continue;
|
||||
@@ -125,6 +129,10 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cases == 0) {
|
||||
std::fprintf(stderr, "golden: no cases in %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
std::printf("golden: %zu cases, %zu mismatches\n", cases, bad);
|
||||
return bad == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -48,7 +48,8 @@ inline Key randomKey(Bytes& b, std::size_t maxLen) {
|
||||
std::size_t len = b.u8() % (maxLen + 1);
|
||||
Key k;
|
||||
k.reserve(len);
|
||||
for (std::size_t i = 0; i < len; ++i) k.push_back(weightedByte(b));
|
||||
for (std::size_t i = 0; i < len; ++i)
|
||||
k.push_back(static_cast<char>(weightedByte(b)));
|
||||
return k;
|
||||
}
|
||||
|
||||
@@ -82,7 +83,7 @@ inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
|
||||
anchor = static_cast<Int>(below) + s.offset - 1;
|
||||
}
|
||||
if (anchor < 0) return OptKey{Key{}};
|
||||
if (static_cast<std::size_t>(anchor) >= db.size()) return std::nullopt;
|
||||
if (anchor >= static_cast<Int>(db.size())) return std::nullopt;
|
||||
return OptKey{db[static_cast<std::size_t>(anchor)]};
|
||||
}
|
||||
|
||||
@@ -112,7 +113,17 @@ struct Instance {
|
||||
bool reverse = false;
|
||||
bool more = false;
|
||||
|
||||
// Must be called once `keys` is final; safe again after a move.
|
||||
Instance() = default;
|
||||
// Move-only: `views` borrow from `keys`. Moving a `std::vector` keeps its
|
||||
// elements at the same addresses, so the views remain valid; a copy would
|
||||
// leave the new instance's views pointing into the source's storage.
|
||||
Instance(const Instance&) = delete;
|
||||
Instance& operator=(const Instance&) = delete;
|
||||
Instance(Instance&&) = default;
|
||||
Instance& operator=(Instance&&) = default;
|
||||
|
||||
// Must be called once `keys` is final; call again after mutating `keys` or
|
||||
// after moving keys in by hand.
|
||||
void finish() {
|
||||
views.clear();
|
||||
views.reserve(keys.size());
|
||||
|
||||
Reference in new issue
Block a user