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,149 @@
|
||||
// nanobench comparison of the literal translation and the summarised
|
||||
// implementation on representative readable instances.
|
||||
#define ANKERL_NANOBENCH_IMPLEMENT
|
||||
#include "nanobench.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "../src/fast.h"
|
||||
#include "../src/literal.h"
|
||||
#include "readable_gen.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace cr;
|
||||
|
||||
struct Case {
|
||||
gen::Instance inst;
|
||||
RangeRead r;
|
||||
Result res;
|
||||
const char* name = "";
|
||||
|
||||
// Build the borrowed views once the case sits at its final address.
|
||||
void prepare() {
|
||||
inst.finish();
|
||||
r = inst.read();
|
||||
res = inst.result();
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<Key> makeDb(std::size_t n, std::size_t len) {
|
||||
std::vector<Key> db;
|
||||
db.reserve(n);
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
Key k;
|
||||
k.reserve(len);
|
||||
for (std::size_t j = 0; j < len; ++j)
|
||||
k.push_back(static_cast<Byte>((i >> (8 * j)) & 0xff));
|
||||
// Ensure strict ordering is preserved for the multi-byte encoding.
|
||||
if (!db.empty() && !keyLt(db.back(), k)) k.push_back(static_cast<Byte>(i));
|
||||
db.push_back(std::move(k));
|
||||
}
|
||||
std::sort(db.begin(), db.end());
|
||||
db.erase(std::unique(db.begin(), db.end()), db.end());
|
||||
return db;
|
||||
}
|
||||
|
||||
Case makeCase(const std::vector<Key>& db, OptKey beginKey, Int beginOff,
|
||||
OptKey endKey, Int endOff, bool reverse, bool truncate,
|
||||
const char* name) {
|
||||
gen::Instance inst;
|
||||
inst.beginKey = std::move(beginKey);
|
||||
inst.beginOffset = beginOff;
|
||||
inst.endKey = std::move(endKey);
|
||||
inst.endOffset = endOff;
|
||||
inst.reverse = reverse;
|
||||
|
||||
OptKey lo = gen::resolve(db, Selector{optView(inst.beginKey), beginOff});
|
||||
OptKey hi = gen::resolve(db, Selector{optView(inst.endKey), endOff});
|
||||
std::vector<Key> complete = gen::inRange(db, lo, hi);
|
||||
if (reverse) std::reverse(complete.begin(), complete.end());
|
||||
|
||||
if (truncate && complete.size() > 1) {
|
||||
inst.keys.assign(
|
||||
complete.begin(),
|
||||
complete.begin() + static_cast<std::ptrdiff_t>(complete.size() / 2));
|
||||
inst.more = true;
|
||||
} else {
|
||||
inst.keys = std::move(complete);
|
||||
inst.more = false;
|
||||
}
|
||||
Case c;
|
||||
c.inst = std::move(inst);
|
||||
c.name = name;
|
||||
return c;
|
||||
}
|
||||
|
||||
void check(const Case& c) {
|
||||
ConflictRange a = literal::closedForm(c.r, c.res);
|
||||
ConflictRange b = fast::closedForm(c.r, c.res);
|
||||
if (!gen::equivalent(a, b)) {
|
||||
std::fprintf(stderr, "benchmark case %s disagrees between implementations\n",
|
||||
c.name);
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
using ankerl::nanobench::Bench;
|
||||
|
||||
const std::vector<Key> smallDb = makeDb(8, 1);
|
||||
const std::vector<Key> mediumDb = makeDb(48, 2);
|
||||
const std::vector<Key> longDb = makeDb(512, 3);
|
||||
|
||||
const OptKey bot = bottomKey();
|
||||
const OptKey top = std::nullopt;
|
||||
|
||||
// A finite key above every key in `smallDb` and `longDb`, so the complete
|
||||
// reads below take the scanning branch (`residualT`, `allBelowEnd`, ...)
|
||||
// instead of short-circuiting on `⊤`.
|
||||
Key aboveKey;
|
||||
aboveKey.push_back(static_cast<char>(0xff));
|
||||
aboveKey.push_back(static_cast<char>(0xff));
|
||||
const OptKey above = OptKey{aboveKey};
|
||||
|
||||
std::vector<Case> cases;
|
||||
// A short complete read. An end offset of 2 (rather than 1) skips the
|
||||
// `end.offset <= 1` shortcut, so the literal translation actually rescans the
|
||||
// reported keys and the summary has something to summarise.
|
||||
cases.push_back(makeCase(smallDb, bot, 1, above, 2, false, false,
|
||||
"small complete"));
|
||||
// A short truncated read (cross-free).
|
||||
cases.push_back(makeCase(smallDb, smallDb[1], 1, top, 1, false, true,
|
||||
"small truncated fwd"));
|
||||
// A crossed read: the end selector resolves before the begin selector.
|
||||
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, false,
|
||||
true, "crossed truncated fwd"));
|
||||
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, true,
|
||||
true, "crossed truncated rev"));
|
||||
// Long complete / truncated reads.
|
||||
cases.push_back(
|
||||
makeCase(longDb, bot, 1, above, 2, false, false, "long complete fwd"));
|
||||
cases.push_back(makeCase(longDb, bot, 1, top, 1, false, true,
|
||||
"long truncated fwd"));
|
||||
cases.push_back(makeCase(longDb, bot, 1, top, 1, true, true,
|
||||
"long truncated rev"));
|
||||
|
||||
// The case vector is final now, so the borrowed views can point at it.
|
||||
for (Case& c : cases) c.prepare();
|
||||
|
||||
for (const Case& c : cases) {
|
||||
check(c);
|
||||
Bench bench;
|
||||
bench.title(c.name).relative(true).warmup(20).minEpochIterations(200);
|
||||
bench.run("literal", [&] {
|
||||
ankerl::nanobench::doNotOptimizeAway(
|
||||
literal::closedForm(c.r, c.res));
|
||||
});
|
||||
bench.run("fast", [&] {
|
||||
ankerl::nanobench::doNotOptimizeAway(fast::closedForm(c.r, c.res));
|
||||
});
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// Compare the literal translation with the summarised implementation on the
|
||||
// Lean-generated corpus (readable or not).
|
||||
#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<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 (Byte b : *k) { 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]);
|
||||
std::string line;
|
||||
std::size_t cases = 0, bad = 0;
|
||||
while (std::getline(in, line)) {
|
||||
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)) continue;
|
||||
if (!parseKey(bk, beginKey) || !parseKey(ek, endKey)) 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 != "=>") 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) 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
std::printf("diff: %zu cases, %zu mismatches\n", cases, bad);
|
||||
return bad == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// libFuzzer safety harness: arbitrary (not necessarily readable) instances.
|
||||
// Equivalence is only promised on readable inputs, so this harness merely runs
|
||||
// both implementations under ASan/UBSan to catch memory-safety or UB issues in
|
||||
// the defensive branches.
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "../src/fast.h"
|
||||
#include "../src/literal.h"
|
||||
#include "readable_gen.h"
|
||||
|
||||
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) {
|
||||
const std::size_t maxLen = 1 + bytes.u8() % 16;
|
||||
// Owned selector keys and reported keys; the borrows below point here.
|
||||
cr::OptKey beginKey = cr::gen::randomOptKey(bytes, maxLen);
|
||||
cr::OptKey endKey = cr::gen::randomOptKey(bytes, maxLen);
|
||||
cr::RangeRead r;
|
||||
r.begin = cr::Selector{cr::optView(beginKey),
|
||||
cr::gen::randomOffset(bytes)};
|
||||
r.end_ = cr::Selector{cr::optView(endKey), cr::gen::randomOffset(bytes)};
|
||||
r.reverse = (bytes.u8() & 1) != 0;
|
||||
|
||||
std::vector<cr::Key> keys;
|
||||
const std::size_t n = bytes.u8() % 16;
|
||||
keys.reserve(n);
|
||||
for (std::size_t j = 0; j < n; ++j)
|
||||
keys.push_back(cr::gen::randomKey(bytes, maxLen));
|
||||
std::vector<cr::KeyView> views;
|
||||
views.reserve(keys.size());
|
||||
for (const cr::Key& k : keys) views.push_back(k);
|
||||
cr::Result res{views, (bytes.u8() & 1) != 0};
|
||||
|
||||
(void)cr::literal::closedForm(r, res);
|
||||
(void)cr::fast::closedForm(r, res);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
// Deterministic, readable-by-construction instance generation shared by the
|
||||
// differential fuzzer and the benchmarks.
|
||||
//
|
||||
// A generated pair `(r, res)` always satisfies `Readable r res`: we build a
|
||||
// sorted, duplicate-free database, resolve the two selectors against it, and
|
||||
// report either the complete traversal or a nonempty prefix of it.
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "../src/model.h"
|
||||
|
||||
namespace cr::gen {
|
||||
|
||||
struct Bytes {
|
||||
const std::uint8_t* data = nullptr;
|
||||
std::size_t size = 0;
|
||||
std::size_t pos = 0;
|
||||
|
||||
std::uint8_t u8() {
|
||||
if (size == 0) return 0;
|
||||
std::uint8_t v = data[pos % size];
|
||||
++pos;
|
||||
return v;
|
||||
}
|
||||
std::uint16_t u16() {
|
||||
std::uint16_t lo = u8();
|
||||
std::uint16_t hi = u8();
|
||||
return static_cast<std::uint16_t>(lo | (hi << 8));
|
||||
}
|
||||
};
|
||||
|
||||
inline Byte weightedByte(Bytes& b) {
|
||||
// Weight the common boundary bytes; otherwise take the raw byte.
|
||||
std::uint8_t r = b.u8();
|
||||
if (r < 24) return 0;
|
||||
if (r < 40) return 1;
|
||||
if (r < 56) return 2;
|
||||
if (r < 72) return 255;
|
||||
return b.u8();
|
||||
}
|
||||
|
||||
inline Key randomKey(Bytes& b, std::size_t maxLen) {
|
||||
std::size_t len = b.u8() % (maxLen + 1);
|
||||
Key k;
|
||||
k.reserve(len);
|
||||
for (std::size_t i = 0; i < len; ++i) k.push_back(weightedByte(b));
|
||||
return k;
|
||||
}
|
||||
|
||||
inline OptKey randomOptKey(Bytes& b, std::size_t maxLen) {
|
||||
// One in eight selectors is anchored at `⊤`.
|
||||
if ((b.u8() & 7) == 0) return std::nullopt;
|
||||
return OptKey{randomKey(b, maxLen)};
|
||||
}
|
||||
|
||||
inline Int randomOffset(Bytes& b) {
|
||||
// Bias towards the offsets that decide the interesting boundary cases, but
|
||||
// still allow the full [-256, 255] window.
|
||||
std::uint8_t pick = b.u8();
|
||||
if (pick < 192) {
|
||||
static const Int small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
|
||||
return small[pick % 9];
|
||||
}
|
||||
return static_cast<Int>(b.u16() % 512) - 256;
|
||||
}
|
||||
|
||||
inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
|
||||
Int anchor;
|
||||
if (!s.key) {
|
||||
anchor = static_cast<Int>(db.size()) + s.offset - 1;
|
||||
} else if (s.key->empty()) {
|
||||
anchor = s.offset - 1;
|
||||
} else {
|
||||
std::size_t below = 0;
|
||||
for (const Key& k : db)
|
||||
if (keyLt(k, *s.key)) ++below;
|
||||
anchor = static_cast<Int>(below) + s.offset - 1;
|
||||
}
|
||||
if (anchor < 0) return OptKey{Key{}};
|
||||
if (static_cast<std::size_t>(anchor) >= db.size()) return std::nullopt;
|
||||
return OptKey{db[static_cast<std::size_t>(anchor)]};
|
||||
}
|
||||
|
||||
inline std::vector<Key> inRange(const std::vector<Key>& db, const OptKey& lo,
|
||||
const OptKey& hi) {
|
||||
std::vector<Key> out;
|
||||
for (const Key& k : db) {
|
||||
bool geLo = lo.has_value() && !keyLt(k, *lo);
|
||||
bool ltHi = !hi || keyLt(k, *hi);
|
||||
if (geLo && ltHi) out.push_back(k);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Owning storage for a generated instance. `RangeRead` and `Result` borrow
|
||||
// keys, so the instance keeps the keys alive and rebuilds views on demand.
|
||||
struct Instance {
|
||||
// Reported keys in traversal order; backing storage for `views`.
|
||||
std::vector<Key> keys;
|
||||
// Views into `keys`, built by `finish()`.
|
||||
std::vector<KeyView> views;
|
||||
// Owned selector keys; `read()` borrows them.
|
||||
OptKey beginKey;
|
||||
OptKey endKey;
|
||||
Int beginOffset = 0;
|
||||
Int endOffset = 0;
|
||||
bool reverse = false;
|
||||
bool more = false;
|
||||
|
||||
// Must be called once `keys` is final; safe again after a move.
|
||||
void finish() {
|
||||
views.clear();
|
||||
views.reserve(keys.size());
|
||||
for (const Key& k : keys) views.push_back(k);
|
||||
}
|
||||
|
||||
RangeRead read() const {
|
||||
RangeRead r;
|
||||
r.begin = Selector{optView(beginKey), beginOffset};
|
||||
r.end_ = Selector{optView(endKey), endOffset};
|
||||
r.reverse = reverse;
|
||||
return r;
|
||||
}
|
||||
|
||||
Result result() const { return Result{views, more}; }
|
||||
};
|
||||
|
||||
inline Instance readable(Bytes& b) {
|
||||
const std::size_t maxLen = 1 + b.u8() % 16; // key length 0..16
|
||||
std::vector<Key> db;
|
||||
const std::size_t count = b.u8() % 20; // 0..19 stored keys
|
||||
db.reserve(count);
|
||||
for (std::size_t i = 0; i < count; ++i) db.push_back(randomKey(b, maxLen));
|
||||
std::sort(db.begin(), db.end());
|
||||
db.erase(std::unique(db.begin(), db.end()), db.end());
|
||||
|
||||
Instance inst;
|
||||
inst.beginKey = randomOptKey(b, maxLen);
|
||||
inst.beginOffset = randomOffset(b);
|
||||
inst.endKey = randomOptKey(b, maxLen);
|
||||
inst.endOffset = randomOffset(b);
|
||||
inst.reverse = (b.u8() & 1) != 0;
|
||||
|
||||
OptKey lo = resolve(db, Selector{optView(inst.beginKey), inst.beginOffset});
|
||||
OptKey hi = resolve(db, Selector{optView(inst.endKey), inst.endOffset});
|
||||
std::vector<Key> complete = inRange(db, lo, hi);
|
||||
if (inst.reverse) std::reverse(complete.begin(), complete.end());
|
||||
|
||||
if (!complete.empty() && (b.u8() & 1) != 0) {
|
||||
std::size_t m = 1 + b.u8() % complete.size();
|
||||
inst.keys.assign(complete.begin(),
|
||||
complete.begin() + static_cast<std::ptrdiff_t>(m));
|
||||
inst.more = true;
|
||||
} else {
|
||||
inst.keys = std::move(complete);
|
||||
inst.more = false;
|
||||
}
|
||||
inst.finish();
|
||||
return inst;
|
||||
}
|
||||
|
||||
// Range equivalence used by the differential check: any encoding of the empty
|
||||
// interval is equivalent to any other; nonempty intervals must match exactly.
|
||||
inline bool isEmptyRange(const ConflictRange& c) {
|
||||
return !optLt(c.begin, c.end_);
|
||||
}
|
||||
|
||||
inline bool equivalent(const ConflictRange& a, const ConflictRange& b) {
|
||||
const bool ea = isEmptyRange(a);
|
||||
const bool eb = isEmptyRange(b);
|
||||
if (ea || eb) return ea && eb;
|
||||
return a.begin == b.begin && a.end_ == b.end_;
|
||||
}
|
||||
|
||||
} // namespace cr::gen
|
||||
Reference in new issue
Block a user