Store public selector offsets as int32

This commit is contained in:
andrew committed 2026-11-18 11:28:47 -05:00
1 parent c8e49aead8
commit 24f28dd9c8
10 files changed
+247 -227

No files matched your search

+2 -2
View File
@@ -49,8 +49,8 @@ std::vector<Key> makeDb(std::size_t n, std::size_t len) {
return db;
}
Case makeCase(const std::vector<Key>& db, OptKey beginKey, Int beginOff,
OptKey endKey, Int endOff, bool reverse, bool truncate,
Case makeCase(const std::vector<Key>& db, OptKey beginKey, std::int32_t beginOff,
OptKey endKey, std::int32_t endOff, bool reverse, bool truncate,
const char* name) {
gen::Instance inst;
inst.beginKey = std::move(beginKey);
+2 -2
View File
@@ -104,8 +104,8 @@ int main(int argc, char** argv) {
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};
RangeRead r{Selector{optView(beginKey), static_cast<std::int32_t>(bo)},
Selector{optView(endKey), static_cast<std::int32_t>(eo)}, reverse};
Result res{keyViews, more != 0};
++cases;
ConflictRange a = literal::closedForm(r, res);
+3 -3
View File
@@ -28,7 +28,7 @@ using namespace cr;
// Offset 5 is what lets the default sweep exercise the last three
// short-circuit branch sides in `fast.cpp` (all in crossed subtrees).
const Int kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
const std::int32_t kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
void check(const RangeRead& r, const Result& res, long long& cases,
long long& bad, const char* kind, Int bo, Int eo, bool reverse,
@@ -47,8 +47,8 @@ void run(const std::vector<Key>& db, const std::vector<OptKey>& keys,
long long& cases, long long& bad) {
for (const OptKey& beginKey : keys) {
for (const OptKey& endKey : keys) {
for (Int beginOffset : kOffsets) {
for (Int endOffset : kOffsets) {
for (std::int32_t beginOffset : kOffsets) {
for (std::int32_t endOffset : kOffsets) {
const Selector begin{optView(beginKey), beginOffset};
const Selector end{optView(endKey), endOffset};
const std::vector<Key> complete = gen::inRange(
+21 -20
View File
@@ -9,6 +9,7 @@
#include <cstdint>
#include <cstdio>
#include <limits>
#include <type_traits>
#include <utility>
#include <vector>
@@ -33,33 +34,33 @@ int main() {
owned.push_back(OptKey{Key("a")});
owned.push_back(OptKey{Key("\xff\xff", 2)});
// The public offset contract accepts exactly the 32-bit boundaries; the
// entry-point assertions and corpus parsers share this predicate.
const RangeRead boundary{Selector{std::nullopt, kMinInputOffset},
Selector{std::nullopt, kMaxInputOffset}, false};
const RangeRead outside{Selector{std::nullopt, kMinInputOffset - 1},
Selector{std::nullopt, 0}, false};
if (!inputOffsetsFitInt32(boundary) || inputOffsetsFitInt32(outside)) {
// `Selector` stores offsets as `std::int32_t`, so `RangeRead` itself cannot
// violate the input contract. The corpus parsers still validate wider
// parsed values with `offsetsFitInt32` before narrowing.
static_assert(std::is_same_v<decltype(Selector::offset), std::int32_t>,
"Selector::offset must match the 32-bit input contract");
if (!offsetsFitInt32(kMinInputOffset, kMaxInputOffset) ||
offsetsFitInt32(kMinInputOffset - 1, kMaxInputOffset + 1)) {
std::fprintf(stderr, "offset contract check failed\n");
return 1;
}
const Int offs[] = {std::numeric_limits<std::int32_t>::min(),
std::numeric_limits<std::int32_t>::min() + 1,
-1000000000,
-3,
-1,
0,
1,
2,
3,
1000000000,
std::numeric_limits<std::int32_t>::max() - 1,
std::numeric_limits<std::int32_t>::max()};
const std::int32_t offs[] = {std::numeric_limits<std::int32_t>::min(),
std::numeric_limits<std::int32_t>::min() + 1,
-1000000000,
-3,
-1,
0,
1,
2,
3,
1000000000,
std::numeric_limits<std::int32_t>::max() - 1,
std::numeric_limits<std::int32_t>::max()};
std::vector<Selector> sels;
sels.reserve(owned.size() * (sizeof(offs) / sizeof(offs[0])));
for (const OptKey& k : owned)
for (Int off : offs) sels.push_back(Selector{optView(k), off});
for (std::int32_t off : offs) sels.push_back(Selector{optView(k), off});
long long cases = 0, bad = 0;
for (const Selector& bs : sels)
+2 -2
View File
@@ -109,8 +109,8 @@ int main(int argc, char** argv) {
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};
RangeRead r{Selector{optView(beginKey), static_cast<std::int32_t>(bo)},
Selector{optView(endKey), static_cast<std::int32_t>(eo)}, rev != 0};
Result res{keyViews, more != 0};
OptKey wantBegin, wantEnd;
if (!parseKey(ob, wantBegin) || !parseKey(oe, wantEnd)) {
+6 -6
View File
@@ -61,15 +61,15 @@ inline OptKey randomOptKey(Bytes& b, std::size_t maxLen) {
return OptKey{randomKey(b, maxLen)};
}
inline Int randomOffset(Bytes& b) {
inline std::int32_t 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};
static const std::int32_t small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
return small[pick % 9];
}
return static_cast<Int>(b.u16() % 512) - 256;
return static_cast<std::int32_t>(b.u16() % 512) - 256;
}
inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
@@ -77,7 +77,7 @@ inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
if (!s.key) {
anchor = static_cast<Int>(db.size()) + s.offset - 1;
} else if (s.key->empty()) {
anchor = s.offset - 1;
anchor = static_cast<Int>(s.offset) - 1;
} else {
std::size_t below = 0;
for (const Key& k : db)
@@ -110,8 +110,8 @@ struct Instance {
// Owned selector keys; `read()` borrows them.
OptKey beginKey;
OptKey endKey;
Int beginOffset = 0;
Int endOffset = 0;
std::int32_t beginOffset = 0;
std::int32_t endOffset = 0;
bool reverse = false;
bool more = false;