Files
conflict-range/cpp/tests/fuzz_readable.cpp
T
andrew e32e42d933 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.
2026-11-17 16:08:07 -05:00

60 lines
1.9 KiB
C++

// libFuzzer differential harness: the literal translation versus the
// summarised implementation, on readable-by-construction instances.
#include <cstddef>
#include <cstdint>
#include <string>
#include <cstdio>
#include <cstdlib>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
std::string enc(cr::OptKeyView k) {
if (!k) return "T";
if (k->empty()) return "-";
static const char* d = "0123456789abcdef";
std::string s;
for (char c : *k) {
auto b = static_cast<cr::Byte>(c);
s.push_back(d[b >> 4]);
s.push_back(d[b & 15]);
}
return s;
}
std::string enc(const cr::OptKey& k) { return enc(cr::optView(k)); }
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data,
std::size_t size) {
cr::gen::Bytes bytes{data, size, 0};
for (int i = 0; i < 8; ++i) {
cr::gen::Instance inst = cr::gen::readable(bytes);
cr::RangeRead r = inst.read();
cr::Result res = inst.result();
cr::ConflictRange lit = cr::literal::closedForm(r, res);
cr::ConflictRange fast = cr::fast::closedForm(r, res);
if (!cr::gen::equivalent(lit, fast)) {
std::fprintf(stderr,
"MISMATCH literal [%s,%s) fast [%s,%s)\n"
" beginKey=%s beginOffset=%lld endKey=%s endOffset=%lld "
"reverse=%d more=%d n=%zu\n",
enc(lit.begin).c_str(), enc(lit.end_).c_str(),
enc(fast.begin).c_str(), enc(fast.end_).c_str(),
enc(r.begin.key).c_str(),
static_cast<long long>(r.begin.offset),
enc(r.end_.key).c_str(),
static_cast<long long>(r.end_.offset), (int)r.reverse,
(int)res.more, res.keys.size());
for (cr::KeyView k : res.keys)
std::fprintf(stderr, " key %s\n", enc(k).c_str());
std::abort();
}
}
return 0;
}