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.
128 lines
4.1 KiB
C++
128 lines
4.1 KiB
C++
// Compare the literal translation with the summarised implementation on the
|
|
// Lean-generated corpus (readable or not).
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "../src/fast.h"
|
|
#include "../src/literal.h"
|
|
|
|
namespace {
|
|
using namespace cr;
|
|
|
|
bool parseKey(const std::string& tok, OptKey& out) {
|
|
if (tok == "T") { out = std::nullopt; return true; }
|
|
if (tok == "-") { out = Key{}; return true; }
|
|
if (tok.size() % 2 != 0) return false;
|
|
auto hex = [](char c) -> int {
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
return -1;
|
|
};
|
|
Key k;
|
|
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<char>(static_cast<Byte>(hi * 16 + lo)));
|
|
}
|
|
out = std::move(k);
|
|
return true;
|
|
}
|
|
|
|
std::string encKey(const OptKey& k) {
|
|
if (!k) return "T";
|
|
if (k->empty()) return "-";
|
|
static const char* d = "0123456789abcdef";
|
|
std::string s;
|
|
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
|
|
|
|
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 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;
|
|
long long bo = 0, eo = 0;
|
|
int rev = 0, more = 0;
|
|
std::size_t n = 0;
|
|
OptKey beginKey, endKey;
|
|
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);
|
|
bool ok = true;
|
|
for (std::size_t i = 0; i < n; ++i) {
|
|
std::string kt;
|
|
if (!(ss >> kt)) { ok = false; break; }
|
|
OptKey p;
|
|
if (!parseKey(kt, p) || !p) { ok = false; break; }
|
|
keys.push_back(std::move(*p));
|
|
}
|
|
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;
|
|
bool sorted = true;
|
|
for (std::size_t i = 1; i < keys.size(); ++i) {
|
|
if (reverse ? !keyLt(keys[i], keys[i - 1])
|
|
: !keyLt(keys[i - 1], keys[i])) {
|
|
sorted = false;
|
|
break;
|
|
}
|
|
}
|
|
if (!sorted) { ++skipped; continue; }
|
|
std::vector<KeyView> keyViews;
|
|
keyViews.reserve(keys.size());
|
|
for (const Key& k : keys) keyViews.push_back(k);
|
|
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)},
|
|
Selector{optView(endKey), static_cast<Int>(eo)}, reverse};
|
|
Result res{keyViews, more != 0};
|
|
++cases;
|
|
ConflictRange a = literal::closedForm(r, res);
|
|
ConflictRange b = fast::closedForm(r, res);
|
|
if (a.begin != b.begin || a.end_ != b.end_) {
|
|
if (++bad <= 20) {
|
|
std::fprintf(stderr, "MISMATCH: lit [%s,%s) fast [%s,%s)\n %s\n",
|
|
encKey(a.begin).c_str(), encKey(a.end_).c_str(),
|
|
encKey(b.begin).c_str(), encKey(b.end_).c_str(),
|
|
line.c_str());
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
}
|