Merge pull request 'Checked Euclidean division for signed grid coordinates (#11)' (#27) from issue/11-euclidean-division into main
This commit was merged in pull request #27.
This commit is contained in:
commit
86bde8b172
2 files changed
+157
No files matched your search
@@ -505,3 +505,119 @@ Void::Testing::Test portable_overflow_helpers_boundary_cases()
|
||||
Void::Testing::expect(portable_mul_overflow<std::uint64_t>(
|
||||
std::numeric_limits<std::uint64_t>::max(), 2ULL, &u64));
|
||||
}
|
||||
|
||||
// ── checked_euclidean_div ────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test checked_euclidean_div_examples_width_32()
|
||||
{
|
||||
using Void::Numeric::checked_euclidean_div;
|
||||
const auto check = [](std::int32_t a, std::int32_t q, std::int32_t r) {
|
||||
const auto result = checked_euclidean_div(a, std::int32_t{32});
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect_eq(result->quotient, q);
|
||||
Void::Testing::expect_eq(result->remainder, r);
|
||||
};
|
||||
|
||||
check(-33, -2, 31);
|
||||
check(-32, -1, 0);
|
||||
check(-1, -1, 31);
|
||||
check(0, 0, 0);
|
||||
check(31, 0, 31);
|
||||
check(32, 1, 0);
|
||||
check(33, 1, 1);
|
||||
|
||||
// The existing truncating contract is intentionally unchanged.
|
||||
Void::Testing::expect_eq(*Void::Numeric::checked_div(-1, 32), 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test checked_euclidean_div_is_constexpr()
|
||||
{
|
||||
using Void::Numeric::checked_euclidean_div;
|
||||
static_assert(checked_euclidean_div(-1, 32).value().quotient == -1);
|
||||
static_assert(checked_euclidean_div(-1, 32).value().remainder == 31);
|
||||
static_assert(checked_euclidean_div(-33, 32).value().quotient == -2);
|
||||
static_assert(checked_euclidean_div(31, 32).value().quotient == 0);
|
||||
static_assert(checked_euclidean_div(32, 32).value().remainder == 0);
|
||||
Void::Testing::expect(true);
|
||||
}
|
||||
|
||||
Void::Testing::Test checked_euclidean_div_exhaustive_int8()
|
||||
{
|
||||
bool ok = true;
|
||||
for (int a = -128; a <= 127 && ok; ++a) {
|
||||
for (int d = 1; d <= 127 && ok; ++d) {
|
||||
const auto result = Void::Numeric::checked_euclidean_div(
|
||||
static_cast<std::int8_t>(a), static_cast<std::int8_t>(d));
|
||||
ok = result.has_value();
|
||||
if (ok) {
|
||||
const int q = result->quotient;
|
||||
const int r = result->remainder;
|
||||
ok = r >= 0 && r < d && q * d + r == a;
|
||||
}
|
||||
}
|
||||
}
|
||||
Void::Testing::expect(ok);
|
||||
}
|
||||
|
||||
Void::Testing::Test checked_euclidean_div_extremes()
|
||||
{
|
||||
using Void::Numeric::checked_euclidean_div;
|
||||
const std::int32_t divisors[] = {1, 3, 32};
|
||||
bool ok = true;
|
||||
for (const auto d : divisors) {
|
||||
const std::int32_t values[] = {
|
||||
std::numeric_limits<std::int32_t>::min(),
|
||||
std::numeric_limits<std::int32_t>::max()};
|
||||
for (const auto a : values) {
|
||||
const auto result = checked_euclidean_div(a, d);
|
||||
ok = ok && result.has_value();
|
||||
if (ok) {
|
||||
const std::int64_t q = result->quotient;
|
||||
const std::int64_t r = result->remainder;
|
||||
ok = r >= 0 && r < d && q * d + r == a;
|
||||
}
|
||||
}
|
||||
}
|
||||
Void::Testing::expect(ok);
|
||||
|
||||
const auto min32 = checked_euclidean_div(
|
||||
std::numeric_limits<std::int64_t>::min(), std::int64_t{32});
|
||||
Void::Testing::require(min32.has_value());
|
||||
Void::Testing::expect_eq(min32->quotient,
|
||||
std::int64_t{-288230376151711744});
|
||||
Void::Testing::expect_eq(min32->remainder, std::int64_t{0});
|
||||
|
||||
const auto min3 = checked_euclidean_div(
|
||||
std::numeric_limits<std::int64_t>::min(), std::int64_t{3});
|
||||
Void::Testing::require(min3.has_value());
|
||||
Void::Testing::expect_eq(min3->quotient,
|
||||
std::int64_t{-3074457345618258603});
|
||||
Void::Testing::expect_eq(min3->remainder, std::int64_t{1});
|
||||
|
||||
const auto max32 = checked_euclidean_div(
|
||||
std::numeric_limits<std::int64_t>::max(), std::int64_t{32});
|
||||
Void::Testing::require(max32.has_value());
|
||||
Void::Testing::expect_eq(max32->quotient,
|
||||
std::int64_t{288230376151711743});
|
||||
Void::Testing::expect_eq(max32->remainder, std::int64_t{31});
|
||||
|
||||
const auto max3 = checked_euclidean_div(
|
||||
std::numeric_limits<std::int64_t>::max(), std::int64_t{3});
|
||||
Void::Testing::require(max3.has_value());
|
||||
Void::Testing::expect_eq(max3->quotient,
|
||||
std::int64_t{3074457345618258602});
|
||||
Void::Testing::expect_eq(max3->remainder, std::int64_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test checked_euclidean_div_invalid_width()
|
||||
{
|
||||
using Void::Numeric::checked_euclidean_div;
|
||||
using Void::Numeric::Error;
|
||||
const auto zero = checked_euclidean_div(5, 0);
|
||||
Void::Testing::require(!zero.has_value());
|
||||
Void::Testing::expect(zero.error() == Error::DivisionByZero);
|
||||
|
||||
const auto negative = checked_euclidean_div(5, -1);
|
||||
Void::Testing::require(!negative.has_value());
|
||||
Void::Testing::expect(negative.error() == Error::InvalidInput);
|
||||
}
|
||||
Reference in new issue
Block a user