Files
conflict-range/cpp/tests/extreme.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

107 lines
4.0 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
// 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;
}