Files
conflict-range/cpp/tests/extreme.cpp
T

105 lines
3.9 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.
// 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;
}