Files
conflict-range/cpp/tests/readable_gen.h
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

181 lines
5.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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