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

+31 -8
View File
@@ -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;
}