// Copyright (c) 2026 FormalConflictRange contributors. All rights reserved. // Released under Apache 2.0 license as described in the file LICENSE. // Authors: FormalConflictRange contributors // A deterministic exhaustive sweep of small readable instances, comparing the // literal translation with the summarised implementation. // // The fuzzers sample readable instances; this test enumerates them: 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 #include #include #include #include #include #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 Int 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(bo), static_cast(eo), reverse, keep); } } void run(const std::vector& db, const std::vector& 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) { const Selector begin{optView(beginKey), beginOffset}; const Selector end{optView(endKey), endOffset}; const std::vector 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 ordered = complete; if (reverse) std::reverse(ordered.begin(), ordered.end()); if (ordered.empty()) { // The only readable result for an empty traversal. const std::vector 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 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 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 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 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 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("exhaustive: %lld cases, %lld mismatches\n", cases, bad); return bad == 0 ? 0 : 1; }