#include #include #include #include #include #include #include #include #include // ── 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::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::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::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::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::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::max(), 1), std::numeric_limits::max()); } // ── saturating_sub ──────────────────────────────────────────────────────────── Void::Testing::Test saturating_sub_clamps_at_min() { Void::Testing::expect_eq( Void::Numeric::saturating_sub(std::numeric_limits::min(), 1), std::numeric_limits::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(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(500LL).has_value()); } Void::Testing::Test in_range_supports_unsigned_64() { Void::Testing::expect(Void::Numeric::in_range(42LL)); Void::Testing::expect(!Void::Numeric::in_range(-1LL)); const auto r = Void::Numeric::narrow_cast(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_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(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("") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer(" 42") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer("+42") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer("42 ") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer("42abc") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer("10", 1) == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer("10", 37) == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer("10", 0) == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_integer("-9999xyz") == std::unexpected{Error::InvalidInput}); const std::string too_long(Void::Numeric::kMaxNumericTokenBytes + 1, '0'); Void::Testing::expect(parse_integer(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(exact_max_len), 42); Void::Testing::expect_eq(*parse_integer("127"), std::int8_t{127}); Void::Testing::expect_eq(*parse_integer("-128"), std::int8_t{-128}); Void::Testing::expect(parse_integer("128") == std::unexpected{Error::Overflow}); Void::Testing::expect(parse_integer("-129") == std::unexpected{Error::Underflow}); Void::Testing::expect_eq( *parse_integer("18446744073709551615"), std::numeric_limits::max()); Void::Testing::expect( parse_integer("18446744073709551616") == std::unexpected{Error::Overflow}); Void::Testing::expect( parse_integer("-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("") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real(" 1.5") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("+1.5") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("1.5 ") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("1.5xyz") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("nan") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("-nan") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("inf") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("-inf") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("INF") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("infinity") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("1e400xyz") == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("-.") == std::unexpected{Error::InvalidInput}); const std::string too_long(Void::Numeric::kMaxNumericTokenBytes + 1, '1'); Void::Testing::expect(parse_real(too_long) == std::unexpected{Error::InvalidInput}); Void::Testing::expect(parse_real("1e400") == std::unexpected{Error::Overflow}); Void::Testing::expect(parse_real("-1e400") == std::unexpected{Error::Underflow}); Void::Testing::expect(parse_real("1e-500") == std::unexpected{Error::Underflow}); const auto neg_zero = parse_real("-0.0"); Void::Testing::expect(neg_zero.has_value()); Void::Testing::expect(std::signbit(*neg_zero)); const auto half_lead_dot = parse_real("-.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::int32_t{-32768}).value() == -32768); static_assert(checked_cast(std::int32_t{-1}).error() == Error::Underflow); static_assert(checked_cast(std::numeric_limits::max()).error() == Error::Overflow); Void::Testing::expect( checked_cast(std::numeric_limits::min()) == std::unexpected{Error::Underflow}); Void::Testing::expect_eq( *checked_cast(std::numeric_limits::max()), static_cast(std::numeric_limits::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(std::numeric_limits::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(exact_53), static_cast(exact_53)); Void::Testing::expect_eq(*checked_cast(exact_53 + 2ULL), static_cast(exact_53) + 2.0); Void::Testing::expect(checked_cast(exact_53 + 1ULL) == std::unexpected{Error::Inexact}); Void::Testing::expect_eq( *checked_cast(exact_53 + 1ULL, Rounding::nearest_even), static_cast(exact_53)); Void::Testing::expect_eq( *checked_cast(exact_53 + 3ULL, Rounding::nearest_even), static_cast(exact_53 + 4ULL)); Void::Testing::expect_eq( *checked_cast(exact_53 + 3ULL, Rounding::toward_zero), static_cast(exact_53 + 2ULL)); const auto u64_max = std::numeric_limits::max(); Void::Testing::expect(checked_cast(u64_max) == std::unexpected{Error::Inexact}); Void::Testing::expect_eq( *checked_cast(u64_max, Rounding::nearest_even), std::ldexp(1.0, 64)); Void::Testing::expect_eq( *checked_cast(u64_max, Rounding::toward_zero), std::ldexp(1.0, 64) - 2048.0); const auto ld_u64 = checked_cast(u64_max); if constexpr (std::numeric_limits::digits >= 64) { Void::Testing::expect(ld_u64.has_value()); Void::Testing::expect(*ld_u64 == static_cast(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::numeric_limits::quiet_NaN()) == std::unexpected{Error::NonFinite}); Void::Testing::expect( checked_cast(std::numeric_limits::infinity()) == std::unexpected{Error::NonFinite}); Void::Testing::expect( checked_cast(-std::numeric_limits::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(two_63) == std::unexpected{Error::Overflow}); Void::Testing::expect_eq(*checked_cast(-two_63), std::numeric_limits::min()); Void::Testing::expect(checked_cast(-two_63 - 2048.0) == std::unexpected{Error::Underflow}); Void::Testing::expect_eq(*checked_cast(two_63 - 1024.0), std::int64_t{9223372036854774784LL}); Void::Testing::expect(checked_cast(two_64) == std::unexpected{Error::Overflow}); Void::Testing::expect_eq(*checked_cast(two_64 - 2048.0), std::uint64_t{18446744073709549568ULL}); Void::Testing::expect_eq(*checked_cast(-0.0), std::uint64_t{0}); Void::Testing::expect(checked_cast(-1.0) == std::unexpected{Error::Underflow}); // Exact vs toward_zero vs nearest_even (independent of FE_ROUND). Void::Testing::expect(checked_cast(1.5) == std::unexpected{Error::Inexact}); Void::Testing::expect_eq(*checked_cast(1.9, Rounding::toward_zero), 1); Void::Testing::expect_eq(*checked_cast(-1.9, Rounding::toward_zero), -1); const int saved_round = std::fegetround(); std::fesetround(FE_DOWNWARD); Void::Testing::expect_eq(*checked_cast(0.5, Rounding::nearest_even), 0); Void::Testing::expect_eq(*checked_cast(1.5, Rounding::nearest_even), 2); Void::Testing::expect_eq(*checked_cast(2.5, Rounding::nearest_even), 2); Void::Testing::expect_eq(*checked_cast(-0.5, Rounding::nearest_even), 0); Void::Testing::expect_eq(*checked_cast(-1.5, Rounding::nearest_even), -2); Void::Testing::expect_eq(*checked_cast(-2.5, Rounding::nearest_even), -2); std::fesetround(FE_UPWARD); Void::Testing::expect_eq(*checked_cast(127.5, Rounding::toward_zero), std::int8_t{127}); Void::Testing::expect( checked_cast(127.5, Rounding::nearest_even) == std::unexpected{Error::Overflow}); Void::Testing::expect_eq(*checked_cast(-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(std::numeric_limits::quiet_NaN()) == std::unexpected{Error::NonFinite}); Void::Testing::expect( checked_cast(std::numeric_limits::infinity()) == std::unexpected{Error::NonFinite}); Void::Testing::expect(checked_cast(1e40) == std::unexpected{Error::Overflow}); Void::Testing::expect(checked_cast(-1e40) == std::unexpected{Error::Underflow}); const auto neg_zero_f = checked_cast(-0.0); Void::Testing::expect(neg_zero_f.has_value()); Void::Testing::expect(std::signbit(*neg_zero_f)); Void::Testing::expect_eq(*checked_cast(0.5), 0.5F); Void::Testing::expect(checked_cast(0.1) == std::unexpected{Error::Inexact}); Void::Testing::expect(checked_cast(1e-300) == std::unexpected{Error::Inexact}); Void::Testing::expect(checked_cast(0.1, Rounding::nearest_even).has_value()); const double upper_gap = static_cast(std::numeric_limits::max()) + std::ldexp(1.0, 102); Void::Testing::expect(checked_cast(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(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::max(); constexpr auto i32_min = std::numeric_limits::min(); static_assert(checked_mul_add(2, i32_max, i32_min).value() == i32_max - 1); constexpr auto i64_max = std::numeric_limits::max(); constexpr auto i64_min = std::numeric_limits::min(); Void::Testing::expect_eq( *checked_mul_add(2, i64_max, i64_min), i64_max - 1); Void::Testing::expect_eq( *checked_mul_add(-2, i64_max, i64_max), -i64_max); Void::Testing::expect( checked_mul_add(i64_max, 2, -1) == std::unexpected{Error::Overflow}); Void::Testing::expect( checked_mul_add(i64_min, 2, 1) == std::unexpected{Error::Underflow}); constexpr auto u64_max = std::numeric_limits::max(); Void::Testing::expect_eq( *checked_mul_add(u64_max / 2ULL, 2ULL, 1ULL), u64_max); Void::Testing::expect( checked_mul_add(u64_max, 1ULL, 1ULL) == std::unexpected{Error::Overflow}); Void::Testing::expect( checked_mul_add(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(127, 1, &s8)) return false; if (!portable_add_overflow(-128, -1, &s8)) return false; if (portable_add_overflow(127, -128, &s8) || s8 != -1) return false; if (!portable_sub_overflow(-128, 1, &s8)) return false; if (!portable_sub_overflow(127, -1, &s8)) return false; if (portable_sub_overflow(-1, -128, &s8) || s8 != 127) return false; if (!portable_mul_overflow(-128, -1, &s8)) return false; if (!portable_mul_overflow(-65, 2, &s8)) return false; if (portable_mul_overflow(-64, 2, &s8) || s8 != -128) return false; std::uint16_t u16 = 0; if (!portable_mul_overflow(65535, 65535, &u16)) return false; if (portable_mul_overflow(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::numeric_limits::max(), 1ULL, &u64)); Void::Testing::expect(portable_sub_overflow(0ULL, 1ULL, &u64)); Void::Testing::expect(portable_mul_overflow( std::numeric_limits::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(a), static_cast(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::min(), std::numeric_limits::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::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::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::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::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); }