Files
conflict-range/cpp/tests/extreme.cpp
T
andrew 3bb5d062ff Harden and fully cover the C++ translation
Add an exhaustive readable-instance sweep and a coverage gate requiring
100% line coverage of the summarised implementation. Delete branches that
are unreachable under `Readable`, centralise and assert the 32-bit offset
contract at the public entry points, and stop materialising `n` zero bytes
when deciding `keyAfterN n k < upper`.

Also add the extreme-offset regression test, harden the corpus parsers,
remove unused model helpers, document the literal-total / fast-readable
split, and refresh the validation and benchmark numbers.
2026-11-17 18:46:24 -05:00

103 lines
3.8 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.
// Regression test for offsets at the extremes of the input domain. Offsets
// supplied by callers are 32-bit; a positive `end.offset` near `INT32_MAX`
// would otherwise materialise about 2^31 zero bytes for Lean's
// `keyAfterN n k < upper` test, so both implementations decide it from key
// lengths (`keyAfterNLt`) instead of allocating.
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <utility>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
using namespace cr;
} // namespace
int main() {
// Sorted, duplicate-free, with an empty key and a two-byte `0xff` key.
const std::vector<Key> db{Key{}, Key(1, '\0'), Key("a"), Key("b"),
Key("\xff\xff", 2)};
// Selector keys are owned here and borrowed by the selectors below.
std::vector<OptKey> owned;
owned.reserve(4);
owned.push_back(std::nullopt); // ⊤
owned.push_back(OptKey{Key{}}); // ⊥
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)) {
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()};
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});
long long cases = 0, bad = 0;
for (const Selector& bs : sels)
for (const Selector& es : sels) {
std::vector<Key> complete =
gen::inRange(db, gen::resolve(db, bs), gen::resolve(db, es));
for (bool reverse : {false, true}) {
std::vector<Key> ordered = complete;
if (reverse) std::reverse(ordered.begin(), ordered.end());
std::vector<std::pair<std::size_t, bool>> keeps;
keeps.emplace_back(ordered.size(), false);
if (!ordered.empty()) {
keeps.emplace_back(1, true);
if (ordered.size() > 1)
keeps.emplace_back((ordered.size() + 1) / 2, true);
}
for (auto [keep, more] : keeps) {
std::vector<KeyView> views;
views.reserve(keep);
for (std::size_t i = 0; i < keep; ++i) views.push_back(ordered[i]);
RangeRead r{bs, es, reverse};
Result res{views, more};
ConflictRange a = literal::closedForm(r, res);
ConflictRange b = fast::closedForm(r, res);
++cases;
if (!gen::equivalent(a, b)) {
if (++bad <= 10)
std::fprintf(stderr,
"MISMATCH bo=%lld eo=%lld rev=%d more=%d keys=%zu\n",
static_cast<long long>(bs.offset),
static_cast<long long>(es.offset), reverse, more,
keep);
}
}
}
}
std::printf("extreme: %lld cases, %lld mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}