Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
193 lines
6.7 KiB
C++
193 lines
6.7 KiB
C++
#include <Void/Math/Numeric.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <limits>
|
|
|
|
// ── 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());
|
|
}
|