Add Numeric and Format modules
Void::Numeric — checked_add/sub/mul/div returning std::expected<T,Error>;
saturating_add/sub/mul clamping at min/max; wrapping_add/sub/mul for unsigned;
clamp, parse_int (with base), parse_double, narrow_cast.
Void::Format — render() named-placeholder template engine ({key} syntax,
{{}} escaping); render_pos() positional ({0},{1}); indent() per-line prefix;
word_wrap() at whitespace boundaries; format_table() with auto column alignment.
44/44 tests pass.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
3c3ff46e39
commit
b54f2156c5
4 files changed
+673
No files matched your search
@@ -0,0 +1,167 @@
|
||||
#pragma once
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Format
|
||||
{
|
||||
|
||||
// ── Named-placeholder template rendering ──────────────────────────────────────
|
||||
// render("Hello, {name}! You are {age} years old.", {{"name","Alice"},{"age","30"}})
|
||||
// → "Hello, Alice! You are 30 years old."
|
||||
//
|
||||
// Unrecognised keys leave the placeholder untouched.
|
||||
|
||||
[[nodiscard]] inline std::string
|
||||
render(std::string_view tmpl,
|
||||
const std::unordered_map<std::string, std::string>& vars)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(tmpl.size());
|
||||
std::size_t i = 0;
|
||||
while (i < tmpl.size()) {
|
||||
if (tmpl[i] == '{') {
|
||||
if (i + 1 < tmpl.size() && tmpl[i + 1] == '{') {
|
||||
out += '{'; i += 2; continue; // escaped {{
|
||||
}
|
||||
const auto close = tmpl.find('}', i + 1);
|
||||
if (close == std::string_view::npos) {
|
||||
out += tmpl[i++]; continue; // unclosed brace — copy verbatim
|
||||
}
|
||||
const std::string key{tmpl.substr(i + 1, close - i - 1)};
|
||||
const auto it = vars.find(key);
|
||||
if (it != vars.end()) {
|
||||
out += it->second;
|
||||
} else {
|
||||
out += '{'; out += key; out += '}';
|
||||
}
|
||||
i = close + 1;
|
||||
} else if (tmpl[i] == '}' && i + 1 < tmpl.size() && tmpl[i+1] == '}') {
|
||||
out += '}'; i += 2; // escaped }}
|
||||
} else {
|
||||
out += tmpl[i++];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Positional template rendering ────────────────────────────────────────────
|
||||
// render_pos("Hello, {0}! You are {1} years old.", {"Alice", "30"})
|
||||
// → "Hello, Alice! You are 30 years old."
|
||||
|
||||
[[nodiscard]] inline std::string
|
||||
render_pos(std::string_view tmpl, const std::vector<std::string>& args)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(tmpl.size());
|
||||
std::size_t i = 0;
|
||||
while (i < tmpl.size()) {
|
||||
if (tmpl[i] == '{') {
|
||||
if (i + 1 < tmpl.size() && tmpl[i + 1] == '{') {
|
||||
out += '{'; i += 2; continue;
|
||||
}
|
||||
const auto close = tmpl.find('}', i + 1);
|
||||
if (close == std::string_view::npos) { out += tmpl[i++]; continue; }
|
||||
const std::string_view key = tmpl.substr(i + 1, close - i - 1);
|
||||
std::size_t idx = 0;
|
||||
bool valid = !key.empty();
|
||||
for (char c : key) {
|
||||
if (c < '0' || c > '9') { valid = false; break; }
|
||||
idx = idx * 10 + static_cast<std::size_t>(c - '0');
|
||||
}
|
||||
if (valid && idx < args.size()) {
|
||||
out += args[idx];
|
||||
} else {
|
||||
out += '{'; out += key; out += '}';
|
||||
}
|
||||
i = close + 1;
|
||||
} else {
|
||||
out += tmpl[i++];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Indent ────────────────────────────────────────────────────────────────────
|
||||
// Add `indent` spaces to the beginning of every line.
|
||||
|
||||
[[nodiscard]] inline std::string indent(std::string_view text,
|
||||
std::size_t spaces = 4)
|
||||
{
|
||||
const std::string prefix(spaces, ' ');
|
||||
std::string out;
|
||||
out.reserve(text.size() + spaces * 8);
|
||||
bool start_of_line = true;
|
||||
for (char c : text) {
|
||||
if (start_of_line && c != '\n') { out += prefix; }
|
||||
out += c;
|
||||
start_of_line = (c == '\n');
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Word-wrap ─────────────────────────────────────────────────────────────────
|
||||
// Wrap text at `width` columns, breaking at whitespace.
|
||||
|
||||
[[nodiscard]] inline std::string word_wrap(std::string_view text,
|
||||
std::size_t width = 80)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(text.size() + text.size() / width + 1);
|
||||
std::size_t col = 0;
|
||||
std::size_t i = 0;
|
||||
while (i < text.size()) {
|
||||
// Find next word boundary
|
||||
const auto space = text.find_first_of(" \t\n", i);
|
||||
const auto word_end = (space == std::string_view::npos) ? text.size() : space;
|
||||
const std::size_t word_len = word_end - i;
|
||||
if (col > 0 && col + 1 + word_len > width) {
|
||||
out += '\n'; col = 0;
|
||||
} else if (col > 0) {
|
||||
out += ' '; ++col;
|
||||
}
|
||||
out.append(text.data() + i, word_len);
|
||||
col += word_len;
|
||||
i = word_end;
|
||||
// Skip whitespace (normalise runs to single space/newline)
|
||||
while (i < text.size() && (text[i] == ' ' || text[i] == '\t')) ++i;
|
||||
if (i < text.size() && text[i] == '\n') { out += '\n'; col = 0; ++i; }
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Table formatting ─────────────────────────────────────────────────────────
|
||||
// Format a 2-D table of strings as aligned columns separated by `sep`.
|
||||
|
||||
[[nodiscard]] inline std::string
|
||||
format_table(const std::vector<std::vector<std::string>>& rows,
|
||||
std::string_view sep = " ")
|
||||
{
|
||||
if (rows.empty()) return {};
|
||||
const std::size_t ncols = rows[0].size();
|
||||
std::vector<std::size_t> widths(ncols, 0);
|
||||
for (const auto& row : rows) {
|
||||
for (std::size_t c = 0; c < row.size() && c < ncols; ++c) {
|
||||
widths[c] = std::max(widths[c], row[c].size());
|
||||
}
|
||||
}
|
||||
std::string out;
|
||||
for (const auto& row : rows) {
|
||||
for (std::size_t c = 0; c < ncols; ++c) {
|
||||
const auto& cell = c < row.size() ? row[c] : std::string{};
|
||||
out += cell;
|
||||
if (c + 1 < ncols) {
|
||||
out.append(widths[c] - cell.size(), ' ');
|
||||
out += sep;
|
||||
}
|
||||
}
|
||||
out += '\n';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Format
|
||||
@@ -0,0 +1,183 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <cstdlib>
|
||||
#include <expected>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Void::Numeric
|
||||
{
|
||||
|
||||
// ── Error tags ────────────────────────────────────────────────────────────────
|
||||
|
||||
enum class Error
|
||||
{
|
||||
Overflow,
|
||||
Underflow,
|
||||
DivisionByZero,
|
||||
InvalidInput,
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr std::string_view to_string(Error e) noexcept
|
||||
{
|
||||
switch (e) {
|
||||
case Error::Overflow: return "overflow";
|
||||
case Error::Underflow: return "underflow";
|
||||
case Error::DivisionByZero: return "division by zero";
|
||||
case Error::InvalidInput: return "invalid input";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
// ── Checked arithmetic ────────────────────────────────────────────────────────
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, Error> checked_add(T a, T b) noexcept
|
||||
{
|
||||
T result;
|
||||
if (__builtin_add_overflow(a, b, &result)) {
|
||||
return std::unexpected{b > 0 ? Error::Overflow : Error::Underflow};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, Error> checked_sub(T a, T b) noexcept
|
||||
{
|
||||
T result;
|
||||
if (__builtin_sub_overflow(a, b, &result)) {
|
||||
return std::unexpected{b > 0 ? Error::Underflow : Error::Overflow};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, Error> checked_mul(T a, T b) noexcept
|
||||
{
|
||||
T result;
|
||||
if (__builtin_mul_overflow(a, b, &result)) {
|
||||
return std::unexpected{Error::Overflow};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, Error> checked_div(T a, T b) noexcept
|
||||
{
|
||||
if (b == T{0}) return std::unexpected{Error::DivisionByZero};
|
||||
if constexpr (std::is_signed_v<T>) {
|
||||
if (a == std::numeric_limits<T>::min() && b == T{-1}) {
|
||||
return std::unexpected{Error::Overflow};
|
||||
}
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, Error>
|
||||
checked_neg(T a) noexcept
|
||||
{
|
||||
if constexpr (std::is_unsigned_v<T>) {
|
||||
if (a != T{0}) return std::unexpected{Error::Underflow};
|
||||
return T{0};
|
||||
} else {
|
||||
if (a == std::numeric_limits<T>::min()) return std::unexpected{Error::Overflow};
|
||||
return static_cast<T>(-a);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Saturating arithmetic (never overflows, clamps to min/max) ───────────────
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr T saturating_add(T a, T b) noexcept
|
||||
{
|
||||
return checked_add(a, b).value_or(
|
||||
b > 0 ? std::numeric_limits<T>::max() : std::numeric_limits<T>::min());
|
||||
}
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr T saturating_sub(T a, T b) noexcept
|
||||
{
|
||||
return checked_sub(a, b).value_or(
|
||||
b > 0 ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max());
|
||||
}
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr T saturating_mul(T a, T b) noexcept
|
||||
{
|
||||
return checked_mul(a, b).value_or(
|
||||
(a > 0) == (b > 0) ? std::numeric_limits<T>::max()
|
||||
: std::numeric_limits<T>::min());
|
||||
}
|
||||
|
||||
// ── Wrapping arithmetic (defined modular wrap) ───────────────────────────────
|
||||
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] constexpr T wrapping_add(T a, T b) noexcept { return static_cast<T>(a + b); }
|
||||
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] constexpr T wrapping_sub(T a, T b) noexcept { return static_cast<T>(a - b); }
|
||||
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] constexpr T wrapping_mul(T a, T b) noexcept { return static_cast<T>(a * b); }
|
||||
|
||||
// ── Clamping ─────────────────────────────────────────────────────────────────
|
||||
|
||||
template <typename T>
|
||||
requires std::totally_ordered<T>
|
||||
[[nodiscard]] constexpr T clamp(T val, T lo, T hi) noexcept
|
||||
{
|
||||
if (val < lo) return lo;
|
||||
if (val > hi) return hi;
|
||||
return val;
|
||||
}
|
||||
|
||||
// ── Numeric parsing ───────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::expected<int, Error>
|
||||
parse_int(std::string_view s, int base = 10) noexcept
|
||||
{
|
||||
if (s.empty()) return std::unexpected{Error::InvalidInput};
|
||||
char* end = nullptr;
|
||||
const long v = std::strtol(s.data(), &end, base);
|
||||
if (end == s.data() || end != s.data() + s.size()) {
|
||||
return std::unexpected{Error::InvalidInput};
|
||||
}
|
||||
if (v > std::numeric_limits<int>::max()) return std::unexpected{Error::Overflow};
|
||||
if (v < std::numeric_limits<int>::min()) return std::unexpected{Error::Underflow};
|
||||
return static_cast<int>(v);
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::expected<double, Error>
|
||||
parse_double(std::string_view s) noexcept
|
||||
{
|
||||
if (s.empty()) return std::unexpected{Error::InvalidInput};
|
||||
char* end = nullptr;
|
||||
const double v = std::strtod(s.data(), &end);
|
||||
if (end == s.data() || end != s.data() + s.size()) {
|
||||
return std::unexpected{Error::InvalidInput};
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
// ── Integer traits helpers ────────────────────────────────────────────────────
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr bool in_range(long long v) noexcept
|
||||
{
|
||||
return v >= static_cast<long long>(std::numeric_limits<T>::min()) &&
|
||||
v <= static_cast<long long>(std::numeric_limits<T>::max());
|
||||
}
|
||||
|
||||
template <std::integral T>
|
||||
[[nodiscard]] constexpr std::optional<T>
|
||||
narrow_cast(long long v) noexcept
|
||||
{
|
||||
if (!in_range<T>(v)) return std::nullopt;
|
||||
return static_cast<T>(v);
|
||||
}
|
||||
|
||||
} // namespace Void::Numeric
|
||||
@@ -0,0 +1,131 @@
|
||||
#include <Void/Format/Format.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// ── render (named) ────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test render_single_placeholder()
|
||||
{
|
||||
const auto r = Void::Format::render("Hello, {name}!",
|
||||
{{"name", "World"}});
|
||||
Void::Testing::expect_eq(r, std::string{"Hello, World!"});
|
||||
}
|
||||
|
||||
Void::Testing::Test render_multiple_placeholders()
|
||||
{
|
||||
const auto r = Void::Format::render("{a} + {b} = {c}",
|
||||
{{"a","1"},{"b","2"},{"c","3"}});
|
||||
Void::Testing::expect_eq(r, std::string{"1 + 2 = 3"});
|
||||
}
|
||||
|
||||
Void::Testing::Test render_unknown_key_left_as_is()
|
||||
{
|
||||
const auto r = Void::Format::render("Hello {unknown}!", {});
|
||||
Void::Testing::expect_eq(r, std::string{"Hello {unknown}!"});
|
||||
}
|
||||
|
||||
Void::Testing::Test render_escaped_braces()
|
||||
{
|
||||
const auto r = Void::Format::render("{{not a placeholder}}", {});
|
||||
Void::Testing::expect_eq(r, std::string{"{not a placeholder}"});
|
||||
}
|
||||
|
||||
Void::Testing::Test render_no_placeholders()
|
||||
{
|
||||
const auto r = Void::Format::render("plain text", {});
|
||||
Void::Testing::expect_eq(r, std::string{"plain text"});
|
||||
}
|
||||
|
||||
// ── render_pos (positional) ───────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test render_pos_basic()
|
||||
{
|
||||
const auto r = Void::Format::render_pos("{0} and {1}", {"hello", "world"});
|
||||
Void::Testing::expect_eq(r, std::string{"hello and world"});
|
||||
}
|
||||
|
||||
Void::Testing::Test render_pos_repeat_arg()
|
||||
{
|
||||
const auto r = Void::Format::render_pos("{0} {0}", {"echo"});
|
||||
Void::Testing::expect_eq(r, std::string{"echo echo"});
|
||||
}
|
||||
|
||||
Void::Testing::Test render_pos_out_of_range_left_as_is()
|
||||
{
|
||||
const auto r = Void::Format::render_pos("{5}", {"a"});
|
||||
Void::Testing::expect_eq(r, std::string{"{5}"});
|
||||
}
|
||||
|
||||
// ── indent ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test indent_adds_spaces()
|
||||
{
|
||||
const auto r = Void::Format::indent("hello\nworld", 2);
|
||||
Void::Testing::expect_eq(r, std::string{" hello\n world"});
|
||||
}
|
||||
|
||||
Void::Testing::Test indent_empty_string()
|
||||
{
|
||||
Void::Testing::expect(Void::Format::indent("", 4).empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test indent_single_line()
|
||||
{
|
||||
const auto r = Void::Format::indent("hi", 3);
|
||||
Void::Testing::expect_eq(r, std::string{" hi"});
|
||||
}
|
||||
|
||||
// ── word_wrap ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test word_wrap_short_text_unchanged()
|
||||
{
|
||||
const auto r = Void::Format::word_wrap("hello world", 80);
|
||||
Void::Testing::expect_eq(r, std::string{"hello world"});
|
||||
}
|
||||
|
||||
Void::Testing::Test word_wrap_breaks_at_width()
|
||||
{
|
||||
const auto r = Void::Format::word_wrap("hello world foo", 11);
|
||||
Void::Testing::expect(r.find('\n') != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test word_wrap_preserves_explicit_newlines()
|
||||
{
|
||||
const auto r = Void::Format::word_wrap("line1\nline2", 80);
|
||||
Void::Testing::expect(r.find('\n') != std::string::npos);
|
||||
}
|
||||
|
||||
// ── format_table ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test format_table_aligns_columns()
|
||||
{
|
||||
const std::vector<std::vector<std::string>> rows = {
|
||||
{"Name", "Age"},
|
||||
{"Alice", "30"},
|
||||
{"Bob", "25"},
|
||||
};
|
||||
const auto t = Void::Format::format_table(rows);
|
||||
Void::Testing::expect(t.find("Name") != std::string::npos);
|
||||
Void::Testing::expect(t.find("Alice") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test format_table_empty_returns_empty()
|
||||
{
|
||||
Void::Testing::expect(
|
||||
Void::Format::format_table({}).empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test format_table_row_count()
|
||||
{
|
||||
const std::vector<std::vector<std::string>> rows = {
|
||||
{"a", "b"},
|
||||
{"c", "d"},
|
||||
};
|
||||
const auto t = Void::Format::format_table(rows);
|
||||
int newlines = 0;
|
||||
for (char c : t) { if (c == '\n') ++newlines; }
|
||||
Void::Testing::expect_eq(newlines, 2);
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
#include <Void/Numeric/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());
|
||||
}
|
||||
Reference in new issue
Block a user