Add a C++ translation of the closed form
- Literal transliteration of `Statement.closedForm` plus an equivalent summarised implementation - Borrowed input keys (`std::string_view` / `std::span`) and owned result keys (`std::string`) - Golden-vector test, differential/safety fuzzers, and a nanobench comparison The literal and summarised implementations agree on 40,000 Lean golden vectors, 18,971 sorted corpus cases, and the readable fuzzer; the raw fuzzer runs both implementations under ASan/UBSan. `build/` and the generated `oracle/golden.txt` are ignored.
This commit is contained in:
1 parent
6559cc582b
commit
e32e42d933
17 files changed
+8871
No files matched your search
@@ -0,0 +1,130 @@
|
||||
// Validates the C++ literal translation against golden vectors produced by
|
||||
// `cpp/oracle/gen.lean` (which calls the real `Statement.closedForm`).
|
||||
#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<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 (Byte b : *k) {
|
||||
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)) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
std::printf("golden: %zu cases, %zu mismatches\n", cases, bad);
|
||||
return bad == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user