Files
conflict-range/cpp/tests/sweep.cpp
T
andrew a0b68d148b Simplify crossed dispatch and add SMT-derived cases
Drop the redundant begin.offset split in crossedCompleteOvershoot and
the rE > 0 guard in closedFormForwardSaturated; both are implied by
the call-site invariants.

Add an SMT-derived empty-interval case as the smt_cases CTest and as a
committed fuzz_readable seed.  run-fuzz.sh now loads the read-only seed
corpus alongside the writable one.

Rename exhaustive to sweep and document that it is exhaustive only
over its bounded family of instances.
2026-11-18 13:03:55 -05:00

118 lines
4.6 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.
// Released under Apache 2.0 license as described in the file LICENSE.
// A deterministic sweep over a finite class of small readable instances,
// comparing the literal translation with the summarised implementation.
//
// It is exhaustive over this bounded family, but not over the input space.
// The fuzzers sample readable instances; this test enumerates a fixed finite
// family: every sorted database (up to four keys) over a fixed universe of
// interesting keys, every selector key (including ⊥ and ⊤) paired with offsets
// in [-3, 5], and every readable result (the complete traversal, each nonempty
// truncated prefix, or the empty result when the traversal is empty) in both
// directions.
//
// Together with `golden_test`, `diff_corpus`, and `extreme_test`, this covers
// every line and branch side of `src/fast.cpp`.
#include <algorithm>
#include <bit>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
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 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,
std::size_t keep) {
++cases;
if (!gen::equivalent(literal::closedForm(r, res), fast::closedForm(r, res))) {
if (++bad <= 10)
std::fprintf(stderr,
"MISMATCH %s bo=%lld eo=%lld rev=%d keep=%zu\n", kind,
static_cast<long long>(bo), static_cast<long long>(eo),
reverse, keep);
}
}
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 (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(
db, gen::resolve(db, begin), gen::resolve(db, end));
for (bool reverse : {false, true}) {
const RangeRead r{begin, end, reverse};
std::vector<Key> ordered = complete;
if (reverse) std::reverse(ordered.begin(), ordered.end());
if (ordered.empty()) {
// The only readable result for an empty traversal.
const std::vector<KeyView> none;
check(r, Result{none, false}, cases, bad, "empty", beginOffset,
endOffset, reverse, 0);
continue;
}
// Every nonempty prefix, including the whole traversal with
// `more = true` (readability does not require a proper prefix).
for (std::size_t keep = 1; keep <= ordered.size(); ++keep) {
std::vector<KeyView> views;
views.reserve(keep);
for (std::size_t i = 0; i < keep; ++i) views.push_back(ordered[i]);
check(r, Result{views, true}, cases, bad, "truncated",
beginOffset, endOffset, reverse, keep);
}
// The complete result.
std::vector<KeyView> views;
views.reserve(ordered.size());
for (const Key& k : ordered) views.push_back(k);
check(r, Result{views, false}, cases, bad, "complete", beginOffset,
endOffset, reverse, ordered.size());
}
}
}
}
}
}
} // namespace
int main() {
// Prefix chains exercise the `intervalCard` and `keyAfter` cases; `⊤` and
// the empty key are handled separately as selector keys.
const std::vector<Key> universe{
Key(""), Key("\0", 1), Key("\0\0", 2), Key("\0\xff", 2),
Key("a"), Key("a\0", 2), Key("a\0\0", 3), Key("a\0\0\0", 4),
Key("b"), Key("\xff", 1)};
std::vector<OptKey> keys;
keys.emplace_back(std::nullopt);
for (const Key& k : universe) keys.emplace_back(k);
long long cases = 0, bad = 0;
const std::uint32_t limit = 1u << universe.size();
for (std::uint32_t mask = 0; mask < limit; ++mask) {
if (std::popcount(mask) > 4) continue;
std::vector<Key> db;
for (std::size_t i = 0; i < universe.size(); ++i)
if (mask & (1u << i)) db.push_back(universe[i]);
run(db, keys, cases, bad);
}
std::printf("sweep: %lld cases, %lld mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}