Files
Library/tests/standalone/Math/Numeric.cpp
T

508 lines
23 KiB
C++

#include <Void/Math/Numeric.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cfenv>
#include <cmath>
#include <cstdint>
#include <limits>
#include <string>
#include <string_view>
// ── checked_add ───────────────────────────────────────────────────────────────
Void::Testing::Test checked_add_ok()
{
const auto r = Void::Numeric::checked_add(3, 4);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 7);
}
Void::Testing::Test checked_add_overflow()
{
const auto r = Void::Numeric::checked_add(
std::numeric_limits<int>::max(), 1);
Void::Testing::expect(!r.has_value());
Void::Testing::expect(r.error() == Void::Numeric::Error::Overflow);
}
Void::Testing::Test checked_add_underflow()
{
const auto r = Void::Numeric::checked_add(
std::numeric_limits<int>::min(), -1);
Void::Testing::expect(!r.has_value());
Void::Testing::expect(r.error() == Void::Numeric::Error::Underflow);
}
// ── checked_sub ───────────────────────────────────────────────────────────────
Void::Testing::Test checked_sub_ok()
{
const auto r = Void::Numeric::checked_sub(10, 3);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 7);
}
Void::Testing::Test checked_sub_underflow()
{
const auto r = Void::Numeric::checked_sub(
std::numeric_limits<int>::min(), 1);
Void::Testing::expect(!r.has_value());
}
// ── checked_mul ───────────────────────────────────────────────────────────────
Void::Testing::Test checked_mul_ok()
{
const auto r = Void::Numeric::checked_mul(6, 7);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 42);
}
Void::Testing::Test checked_mul_overflow()
{
const auto r = Void::Numeric::checked_mul(
std::numeric_limits<int>::max(), 2);
Void::Testing::expect(!r.has_value());
Void::Testing::expect(r.error() == Void::Numeric::Error::Overflow);
}
// ── checked_div ───────────────────────────────────────────────────────────────
Void::Testing::Test checked_div_ok()
{
const auto r = Void::Numeric::checked_div(10, 2);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 5);
}
Void::Testing::Test checked_div_by_zero()
{
const auto r = Void::Numeric::checked_div(10, 0);
Void::Testing::expect(!r.has_value());
Void::Testing::expect(r.error() == Void::Numeric::Error::DivisionByZero);
}
Void::Testing::Test checked_div_min_by_neg1()
{
const auto r = Void::Numeric::checked_div(
std::numeric_limits<int>::min(), -1);
Void::Testing::expect(!r.has_value());
}
// ── saturating_add ────────────────────────────────────────────────────────────
Void::Testing::Test saturating_add_no_overflow()
{
Void::Testing::expect_eq(Void::Numeric::saturating_add(3, 4), 7);
}
Void::Testing::Test saturating_add_clamps_at_max()
{
Void::Testing::expect_eq(
Void::Numeric::saturating_add(std::numeric_limits<int>::max(), 1),
std::numeric_limits<int>::max());
}
// ── saturating_sub ────────────────────────────────────────────────────────────
Void::Testing::Test saturating_sub_clamps_at_min()
{
Void::Testing::expect_eq(
Void::Numeric::saturating_sub(std::numeric_limits<int>::min(), 1),
std::numeric_limits<int>::min());
}
// ── wrapping_add ──────────────────────────────────────────────────────────────
Void::Testing::Test wrapping_add_wraps()
{
const std::uint8_t r = Void::Numeric::wrapping_add(
std::uint8_t{255}, std::uint8_t{1});
Void::Testing::expect_eq(r, std::uint8_t{0});
}
// ── clamp ────────────────────────────────────────────────────────────────────
Void::Testing::Test clamp_in_range()
{
Void::Testing::expect_eq(Void::Numeric::clamp(5, 0, 10), 5);
}
Void::Testing::Test clamp_at_lo()
{
Void::Testing::expect_eq(Void::Numeric::clamp(-5, 0, 10), 0);
}
Void::Testing::Test clamp_at_hi()
{
Void::Testing::expect_eq(Void::Numeric::clamp(15, 0, 10), 10);
}
// ── parse_int ─────────────────────────────────────────────────────────────────
Void::Testing::Test parse_int_valid()
{
const auto r = Void::Numeric::parse_int("42");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 42);
}
Void::Testing::Test parse_int_negative()
{
const auto r = Void::Numeric::parse_int("-7");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, -7);
}
Void::Testing::Test parse_int_invalid()
{
Void::Testing::expect(!Void::Numeric::parse_int("abc").has_value());
}
Void::Testing::Test parse_int_hex()
{
const auto r = Void::Numeric::parse_int("ff", 16);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 255);
}
// ── parse_double ─────────────────────────────────────────────────────────────
Void::Testing::Test parse_double_valid()
{
const auto r = Void::Numeric::parse_double("3.14");
Void::Testing::expect(r.has_value());
Void::Testing::expect((*r) > 3.13 && (*r) < 3.15);
}
Void::Testing::Test parse_double_invalid()
{
Void::Testing::expect(!Void::Numeric::parse_double("xyz").has_value());
}
// ── narrow_cast ──────────────────────────────────────────────────────────────
Void::Testing::Test narrow_cast_in_range()
{
const auto r = Void::Numeric::narrow_cast<std::int8_t>(42LL);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::int8_t{42});
}
Void::Testing::Test narrow_cast_out_of_range()
{
Void::Testing::expect(!Void::Numeric::narrow_cast<std::int8_t>(500LL).has_value());
}
Void::Testing::Test in_range_supports_unsigned_64()
{
Void::Testing::expect(Void::Numeric::in_range<std::uint64_t>(42LL));
Void::Testing::expect(!Void::Numeric::in_range<std::uint64_t>(-1LL));
const auto r = Void::Numeric::narrow_cast<std::uint64_t>(42LL);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::uint64_t{42});
}
// ── span-safe parse_integer / parse_real ─────────────────────────────────────
Void::Testing::Test parse_non_nul_terminated_substrings()
{
const char int_buf[3] = {'4', '2', '9'};
const std::string_view int_slice{int_buf, 2};
const auto i1 = Void::Numeric::parse_integer<int>(int_slice);
const auto i2 = Void::Numeric::parse_int(int_slice);
Void::Testing::expect(i1.has_value() && *i1 == 42);
Void::Testing::expect(i2.has_value() && *i2 == 42);
const char real_buf[5] = {'3', '.', '1', '4', '9'};
const std::string_view real_slice{real_buf, 4};
const auto d1 = Void::Numeric::parse_real<double>(real_slice);
const auto d2 = Void::Numeric::parse_double(real_slice);
Void::Testing::expect(d1.has_value() && *d1 > 3.139 && *d1 < 3.141);
Void::Testing::expect(d2.has_value() && *d2 > 3.139 && *d2 < 3.141);
}
Void::Testing::Test parse_integer_rejections_and_bounds()
{
using Void::Numeric::Error;
using Void::Numeric::parse_integer;
Void::Testing::expect(parse_integer<int>("") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<int>(" 42") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<int>("+42") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<int>("42 ") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<int>("42abc") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<int>("10", 1) == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<int>("10", 37) == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<int>("10", 0) == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_integer<std::int8_t>("-9999xyz") == std::unexpected{Error::InvalidInput});
const std::string too_long(Void::Numeric::kMaxNumericTokenBytes + 1, '0');
Void::Testing::expect(parse_integer<int>(too_long) == std::unexpected{Error::InvalidInput});
std::string exact_max_len(Void::Numeric::kMaxNumericTokenBytes - 2, '0');
exact_max_len += "42";
Void::Testing::expect_eq(*parse_integer<int>(exact_max_len), 42);
Void::Testing::expect_eq(*parse_integer<std::int8_t>("127"), std::int8_t{127});
Void::Testing::expect_eq(*parse_integer<std::int8_t>("-128"), std::int8_t{-128});
Void::Testing::expect(parse_integer<std::int8_t>("128") == std::unexpected{Error::Overflow});
Void::Testing::expect(parse_integer<std::int8_t>("-129") == std::unexpected{Error::Underflow});
Void::Testing::expect_eq(
*parse_integer<std::uint64_t>("18446744073709551615"),
std::numeric_limits<std::uint64_t>::max());
Void::Testing::expect(
parse_integer<std::uint64_t>("18446744073709551616") == std::unexpected{Error::Overflow});
Void::Testing::expect(
parse_integer<std::uint64_t>("-1") == std::unexpected{Error::InvalidInput});
}
Void::Testing::Test parse_real_rejections_and_bounds()
{
using Void::Numeric::Error;
using Void::Numeric::parse_real;
Void::Testing::expect(parse_real<double>("") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>(" 1.5") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("+1.5") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("1.5 ") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("1.5xyz") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("nan") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("-nan") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("inf") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("-inf") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("INF") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("infinity") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("1e400xyz") == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("-.") == std::unexpected{Error::InvalidInput});
const std::string too_long(Void::Numeric::kMaxNumericTokenBytes + 1, '1');
Void::Testing::expect(parse_real<double>(too_long) == std::unexpected{Error::InvalidInput});
Void::Testing::expect(parse_real<double>("1e400") == std::unexpected{Error::Overflow});
Void::Testing::expect(parse_real<double>("-1e400") == std::unexpected{Error::Underflow});
Void::Testing::expect(parse_real<double>("1e-500") == std::unexpected{Error::Underflow});
const auto neg_zero = parse_real<double>("-0.0");
Void::Testing::expect(neg_zero.has_value());
Void::Testing::expect(std::signbit(*neg_zero));
const auto half_lead_dot = parse_real<double>("-.5");
Void::Testing::expect(half_lead_dot.has_value());
Void::Testing::expect(*half_lead_dot == -0.5);
}
// ── checked_cast ─────────────────────────────────────────────────────────────
Void::Testing::Test checked_cast_int_to_int()
{
using Void::Numeric::checked_cast;
using Void::Numeric::Error;
static_assert(checked_cast<std::int16_t>(std::int32_t{-32768}).value() == -32768);
static_assert(checked_cast<std::uint32_t>(std::int32_t{-1}).error() == Error::Underflow);
static_assert(checked_cast<std::int64_t>(std::numeric_limits<std::uint64_t>::max()).error() == Error::Overflow);
Void::Testing::expect(
checked_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::min()) ==
std::unexpected{Error::Underflow});
Void::Testing::expect_eq(
*checked_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()),
static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()));
}
Void::Testing::Test checked_cast_int_to_float()
{
using Void::Numeric::checked_cast;
using Void::Numeric::Error;
using Void::Numeric::Rounding;
const auto min_i64_f = checked_cast<float>(std::numeric_limits<std::int64_t>::min());
Void::Testing::expect(min_i64_f.has_value());
Void::Testing::expect(*min_i64_f == -std::ldexp(1.0F, 63));
constexpr std::uint64_t exact_53 = (std::uint64_t{1} << 53);
Void::Testing::expect_eq(*checked_cast<double>(exact_53), static_cast<double>(exact_53));
Void::Testing::expect_eq(*checked_cast<double>(exact_53 + 2ULL), static_cast<double>(exact_53) + 2.0);
Void::Testing::expect(checked_cast<double>(exact_53 + 1ULL) == std::unexpected{Error::Inexact});
Void::Testing::expect_eq(
*checked_cast<double>(exact_53 + 1ULL, Rounding::nearest_even),
static_cast<double>(exact_53));
Void::Testing::expect_eq(
*checked_cast<double>(exact_53 + 3ULL, Rounding::nearest_even),
static_cast<double>(exact_53 + 4ULL));
Void::Testing::expect_eq(
*checked_cast<double>(exact_53 + 3ULL, Rounding::toward_zero),
static_cast<double>(exact_53 + 2ULL));
const auto u64_max = std::numeric_limits<std::uint64_t>::max();
Void::Testing::expect(checked_cast<double>(u64_max) == std::unexpected{Error::Inexact});
Void::Testing::expect_eq(
*checked_cast<double>(u64_max, Rounding::nearest_even),
std::ldexp(1.0, 64));
Void::Testing::expect_eq(
*checked_cast<double>(u64_max, Rounding::toward_zero),
std::ldexp(1.0, 64) - 2048.0);
const auto ld_u64 = checked_cast<long double>(u64_max);
if constexpr (std::numeric_limits<long double>::digits >= 64) {
Void::Testing::expect(ld_u64.has_value());
Void::Testing::expect(*ld_u64 == static_cast<long double>(u64_max));
} else {
Void::Testing::expect(ld_u64 == std::unexpected{Error::Inexact});
}
}
Void::Testing::Test checked_cast_float_to_int_boundaries_and_rounding()
{
using Void::Numeric::checked_cast;
using Void::Numeric::Error;
using Void::Numeric::Rounding;
Void::Testing::expect(
checked_cast<std::int64_t>(std::numeric_limits<double>::quiet_NaN()) ==
std::unexpected{Error::NonFinite});
Void::Testing::expect(
checked_cast<std::int64_t>(std::numeric_limits<double>::infinity()) ==
std::unexpected{Error::NonFinite});
Void::Testing::expect(
checked_cast<std::int64_t>(-std::numeric_limits<double>::infinity()) ==
std::unexpected{Error::NonFinite});
// Exact 2^63 / 2^64 exclusive upper boundaries.
const double two_63 = std::ldexp(1.0, 63);
const double two_64 = std::ldexp(1.0, 64);
Void::Testing::expect(checked_cast<std::int64_t>(two_63) == std::unexpected{Error::Overflow});
Void::Testing::expect_eq(*checked_cast<std::int64_t>(-two_63), std::numeric_limits<std::int64_t>::min());
Void::Testing::expect(checked_cast<std::int64_t>(-two_63 - 2048.0) == std::unexpected{Error::Underflow});
Void::Testing::expect_eq(*checked_cast<std::int64_t>(two_63 - 1024.0), std::int64_t{9223372036854774784LL});
Void::Testing::expect(checked_cast<std::uint64_t>(two_64) == std::unexpected{Error::Overflow});
Void::Testing::expect_eq(*checked_cast<std::uint64_t>(two_64 - 2048.0), std::uint64_t{18446744073709549568ULL});
Void::Testing::expect_eq(*checked_cast<std::uint64_t>(-0.0), std::uint64_t{0});
Void::Testing::expect(checked_cast<std::uint64_t>(-1.0) == std::unexpected{Error::Underflow});
// Exact vs toward_zero vs nearest_even (independent of FE_ROUND).
Void::Testing::expect(checked_cast<int>(1.5) == std::unexpected{Error::Inexact});
Void::Testing::expect_eq(*checked_cast<int>(1.9, Rounding::toward_zero), 1);
Void::Testing::expect_eq(*checked_cast<int>(-1.9, Rounding::toward_zero), -1);
const int saved_round = std::fegetround();
std::fesetround(FE_DOWNWARD);
Void::Testing::expect_eq(*checked_cast<int>(0.5, Rounding::nearest_even), 0);
Void::Testing::expect_eq(*checked_cast<int>(1.5, Rounding::nearest_even), 2);
Void::Testing::expect_eq(*checked_cast<int>(2.5, Rounding::nearest_even), 2);
Void::Testing::expect_eq(*checked_cast<int>(-0.5, Rounding::nearest_even), 0);
Void::Testing::expect_eq(*checked_cast<int>(-1.5, Rounding::nearest_even), -2);
Void::Testing::expect_eq(*checked_cast<int>(-2.5, Rounding::nearest_even), -2);
std::fesetround(FE_UPWARD);
Void::Testing::expect_eq(*checked_cast<std::int8_t>(127.5, Rounding::toward_zero), std::int8_t{127});
Void::Testing::expect(
checked_cast<std::int8_t>(127.5, Rounding::nearest_even) ==
std::unexpected{Error::Overflow});
Void::Testing::expect_eq(*checked_cast<std::int8_t>(-128.5, Rounding::nearest_even), std::int8_t{-128});
std::fesetround(saved_round);
}
Void::Testing::Test checked_cast_float_to_float()
{
using Void::Numeric::checked_cast;
using Void::Numeric::Error;
using Void::Numeric::Rounding;
Void::Testing::expect(
checked_cast<float>(std::numeric_limits<double>::quiet_NaN()) ==
std::unexpected{Error::NonFinite});
Void::Testing::expect(
checked_cast<double>(std::numeric_limits<float>::infinity()) ==
std::unexpected{Error::NonFinite});
Void::Testing::expect(checked_cast<float>(1e40) == std::unexpected{Error::Overflow});
Void::Testing::expect(checked_cast<float>(-1e40) == std::unexpected{Error::Underflow});
const auto neg_zero_f = checked_cast<float>(-0.0);
Void::Testing::expect(neg_zero_f.has_value());
Void::Testing::expect(std::signbit(*neg_zero_f));
Void::Testing::expect_eq(*checked_cast<float>(0.5), 0.5F);
Void::Testing::expect(checked_cast<float>(0.1) == std::unexpected{Error::Inexact});
Void::Testing::expect(checked_cast<float>(1e-300) == std::unexpected{Error::Inexact});
Void::Testing::expect(checked_cast<float>(0.1, Rounding::nearest_even).has_value());
const double upper_gap =
static_cast<double>(std::numeric_limits<float>::max()) + std::ldexp(1.0, 102);
Void::Testing::expect(checked_cast<float>(upper_gap) == std::unexpected{Error::Overflow});
const double trunc_src = 1.0 + std::ldexp(1.0, -24) + std::ldexp(1.0, -25);
Void::Testing::expect_eq(*checked_cast<float>(trunc_src, Rounding::toward_zero), 1.0F);
}
// ── checked_mul_add & portable overflow helpers ──────────────────────────────
Void::Testing::Test checked_mul_add_handles_intermediate_and_real_overflow()
{
using Void::Numeric::checked_mul_add;
using Void::Numeric::Error;
// Product 2 * INT32_MAX overflows int32_t, but + INT32_MIN brings it back in range.
constexpr auto i32_max = std::numeric_limits<std::int32_t>::max();
constexpr auto i32_min = std::numeric_limits<std::int32_t>::min();
static_assert(checked_mul_add<std::int32_t>(2, i32_max, i32_min).value() == i32_max - 1);
constexpr auto i64_max = std::numeric_limits<std::int64_t>::max();
constexpr auto i64_min = std::numeric_limits<std::int64_t>::min();
Void::Testing::expect_eq(
*checked_mul_add<std::int64_t>(2, i64_max, i64_min),
i64_max - 1);
Void::Testing::expect_eq(
*checked_mul_add<std::int64_t>(-2, i64_max, i64_max),
-i64_max);
Void::Testing::expect(
checked_mul_add<std::int64_t>(i64_max, 2, -1) == std::unexpected{Error::Overflow});
Void::Testing::expect(
checked_mul_add<std::int64_t>(i64_min, 2, 1) == std::unexpected{Error::Underflow});
constexpr auto u64_max = std::numeric_limits<std::uint64_t>::max();
Void::Testing::expect_eq(
*checked_mul_add<std::uint64_t>(u64_max / 2ULL, 2ULL, 1ULL),
u64_max);
Void::Testing::expect(
checked_mul_add<std::uint64_t>(u64_max, 1ULL, 1ULL) == std::unexpected{Error::Overflow});
Void::Testing::expect(
checked_mul_add<std::uint64_t>(u64_max, u64_max, 0ULL) == std::unexpected{Error::Overflow});
}
Void::Testing::Test portable_overflow_helpers_boundary_cases()
{
using Void::Numeric::detail::portable_add_overflow;
using Void::Numeric::detail::portable_mul_overflow;
using Void::Numeric::detail::portable_sub_overflow;
constexpr auto verify_constexpr = []() consteval {
std::int8_t s8 = 0;
if (!portable_add_overflow<std::int8_t>(127, 1, &s8)) return false;
if (!portable_add_overflow<std::int8_t>(-128, -1, &s8)) return false;
if (portable_add_overflow<std::int8_t>(127, -128, &s8) || s8 != -1) return false;
if (!portable_sub_overflow<std::int8_t>(-128, 1, &s8)) return false;
if (!portable_sub_overflow<std::int8_t>(127, -1, &s8)) return false;
if (portable_sub_overflow<std::int8_t>(-1, -128, &s8) || s8 != 127) return false;
if (!portable_mul_overflow<std::int8_t>(-128, -1, &s8)) return false;
if (!portable_mul_overflow<std::int8_t>(-65, 2, &s8)) return false;
if (portable_mul_overflow<std::int8_t>(-64, 2, &s8) || s8 != -128) return false;
std::uint16_t u16 = 0;
if (!portable_mul_overflow<std::uint16_t>(65535, 65535, &u16)) return false;
if (portable_mul_overflow<std::uint16_t>(257, 255, &u16) || u16 != 65535) return false;
return true;
};
static_assert(verify_constexpr());
std::uint64_t u64 = 0;
Void::Testing::expect(portable_add_overflow<std::uint64_t>(
std::numeric_limits<std::uint64_t>::max(), 1ULL, &u64));
Void::Testing::expect(portable_sub_overflow<std::uint64_t>(0ULL, 1ULL, &u64));
Void::Testing::expect(portable_mul_overflow<std::uint64_t>(
std::numeric_limits<std::uint64_t>::max(), 2ULL, &u64));
}