Files
conflict-range/cpp/tests/sweep.cpp
T
andrew 023ed53579 Stop recording churning metrics in the C++ docs
Remove the exact coverage totals and the offset-5 branch-coverage
explanation: both change whenever fast.cpp or the sweep is adjusted.
The coverage script already reports the current totals and enforces the
100% gate.

Replace the benchmark table's absolute nanoseconds with the stable
shape, and point readers at cr_bench for current numbers.  The recorded
crossed-truncated-reverse figure was already stale after the fast.cpp
change.
2026-11-18 14:17:25 -05:00

116 lines
4.4 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;
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;
}