Files
conflict-range/cpp/tests/golden.cpp
T
andrew 3bb5d062ff 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.
2026-11-17 18:46:24 -05:00

139 lines
4.1 KiB
C++

// 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>
#include <string>
#include <vector>
#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;
Key k;
k.reserve(tok.size() / 2);
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;
};
for (std::size_t i = 0; i < tok.size(); i += 2) {
int hi = hex(tok[i]);
int 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* digits = "0123456789abcdef";
std::string out;
out.reserve(k->size() * 2);
for (char ch : *k) {
const Byte b = static_cast<Byte>(ch);
out.push_back(digits[b >> 4]);
out.push_back(digits[b & 0xf]);
}
return out;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: %s golden.txt\n", argv[0]);
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;
std::size_t bad = 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 the literal translation borrows.
std::vector<Key> keys;
keys.reserve(n);
bool parseOk = true;
for (std::size_t i = 0; i < n; ++i) {
std::string kt;
if (!(ss >> kt)) { parseOk = false; break; }
OptKey parsed;
if (!parseKey(kt, parsed) || !parsed.has_value()) { parseOk = false; break; }
keys.push_back(std::move(*parsed));
}
if (!parseOk || !(ss >> arrow >> ob >> oe) || arrow != "=>") {
std::fprintf(stderr, "line %zu: bad body: %s\n", lineNo, line.c_str());
++bad;
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)}, rev != 0};
Result res{keyViews, more != 0};
OptKey wantBegin, wantEnd;
if (!parseKey(ob, wantBegin) || !parseKey(oe, wantEnd)) {
std::fprintf(stderr, "line %zu: bad expected output\n", lineNo);
++bad;
continue;
}
ConflictRange got = literal::closedForm(r, res);
++cases;
if (got.begin != wantBegin || got.end_ != wantEnd) {
++bad;
if (bad <= 20) {
std::fprintf(stderr,
"MISMATCH line %zu: got [%s,%s) want [%s,%s)\n %s\n",
lineNo, encKey(got.begin).c_str(),
encKey(got.end_).c_str(), encKey(wantBegin).c_str(),
encKey(wantEnd).c_str(), line.c_str());
}
}
}
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;
}