Files
conflict-range/cpp/tests/exhaustive.cpp
T
andrew 273ad72f0c Polish release docs and add CI checks
- Correct stale references and headline-path wording in READMEs and Lean docstrings
- Document build steps, comparator requirements, sweep scope, and coverage
- Unify Apache headers across Lean and C++ sources
- Add a bwrap-free axiom audit for the headline theorems
- Add a Gitea workflow running lake build, the axiom audit, C++ tests, and coverage
- Match LLVM coverage tool versions to the compiler and recreate stale build dirs
2026-11-17 21:29:16 -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.
// 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 <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 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<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 (Int beginOffset : kOffsets) {
for (Int 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("exhaustive: %lld cases, %lld mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}