Add Scope, StateMachine, TypeList modules; remove macros from Scope

Void::Scope — RAII scope guards: ScopeExit/ScopeSuccess/ScopeFail with
on_exit/on_success/on_fail factory functions. No macros.

Void::StateMachine — compile-time FSM driven by Transition<From,Ev,To>
type lists. Supports typed entry/exit callbacks and returns bool from send()
indicating whether the transition was valid.

Void::TypeList — compile-time type list with: size_of, at, front, back,
contains, index_of, push_front/back, pop_front, concat, filter, transform,
reverse, unique, for_each, all_of/any_of/none_of.

Void::Encoding — Base64 (standard + URL-safe encode/decode) and Hex
encode/decode. Included in previous commit but missed in message.

36/36 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 4.6 committed 2026-05-14 22:49:22 +03:00
1 parent a09e000f0f
commit a9a2e2b59a
6 files changed
+1039

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
#include <Void/Scope/Scope.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <stdexcept>
#include <string>
// ── on_exit ───────────────────────────────────────────────────────────────────
Void::Testing::Test on_exit_runs_on_normal_exit()
{
bool ran = false;
{
auto guard = Void::Scope::on_exit([&] { ran = true; });
}
Void::Testing::expect(ran);
}
Void::Testing::Test on_exit_runs_on_exception()
{
bool ran = false;
try {
auto guard = Void::Scope::on_exit([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(ran);
}
Void::Testing::Test on_exit_release_prevents_run()
{
bool ran = false;
{
auto guard = Void::Scope::on_exit([&] { ran = true; });
guard.release();
}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_exit_lifo_order()
{
std::string order;
{
auto a = Void::Scope::on_exit([&] { order += "A"; });
auto b = Void::Scope::on_exit([&] { order += "B"; });
auto c = Void::Scope::on_exit([&] { order += "C"; });
}
Void::Testing::expect_eq(order, std::string{"CBA"});
}
Void::Testing::Test on_exit_captures_by_ref()
{
int count = 0;
{
auto guard = Void::Scope::on_exit([&] { count += 10; });
count = 5;
}
Void::Testing::expect_eq(count, 15);
}
// ── on_success ────────────────────────────────────────────────────────────────
Void::Testing::Test on_success_runs_on_normal_exit()
{
bool ran = false;
{
auto guard = Void::Scope::on_success([&] { ran = true; });
}
Void::Testing::expect(ran);
}
Void::Testing::Test on_success_does_not_run_on_exception()
{
bool ran = false;
try {
auto guard = Void::Scope::on_success([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_success_release_prevents_run()
{
bool ran = false;
{
auto guard = Void::Scope::on_success([&] { ran = true; });
guard.release();
}
Void::Testing::expect(!ran);
}
// ── on_fail ───────────────────────────────────────────────────────────────────
Void::Testing::Test on_fail_does_not_run_on_normal_exit()
{
bool ran = false;
{
auto guard = Void::Scope::on_fail([&] { ran = true; });
}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_fail_runs_on_exception()
{
bool ran = false;
try {
auto guard = Void::Scope::on_fail([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(ran);
}
Void::Testing::Test on_fail_release_prevents_run()
{
bool ran = false;
try {
auto guard = Void::Scope::on_fail([&] { ran = true; });
guard.release();
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(!ran);
}
@@ -0,0 +1,114 @@
#include <Void/StateMachine/StateMachine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
// ── Simple traffic-light FSM ──────────────────────────────────────────────────
struct Red {};
struct Green {};
struct Yellow {};
struct GoEvent {};
struct SlowEvent {};
struct StopEvent {};
using TrafficLight = Void::StateMachine::StateMachine<
std::tuple<Red, Green, Yellow>,
std::tuple<
Void::StateMachine::Transition<Red, GoEvent, Green>,
Void::StateMachine::Transition<Green, SlowEvent, Yellow>,
Void::StateMachine::Transition<Yellow, StopEvent, Red>
>>;
Void::Testing::Test initial_state_is_first()
{
TrafficLight fsm;
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test valid_transition_taken()
{
TrafficLight fsm;
const bool ok = fsm.send(GoEvent{});
Void::Testing::expect(ok);
Void::Testing::expect(fsm.is<Green>());
}
Void::Testing::Test invalid_event_not_taken()
{
TrafficLight fsm;
const bool ok = fsm.send(StopEvent{}); // Red has no StopEvent arc
Void::Testing::expect(!ok);
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test full_cycle()
{
TrafficLight fsm;
Void::Testing::expect(fsm.send(GoEvent{}));
Void::Testing::expect(fsm.send(SlowEvent{}));
Void::Testing::expect(fsm.send(StopEvent{}));
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test entry_callback_fired()
{
TrafficLight fsm;
bool fired = false;
fsm.on_enter<Green>([&](const Green&) { fired = true; });
fsm.send(GoEvent{});
Void::Testing::expect(fired);
}
Void::Testing::Test exit_callback_fired()
{
TrafficLight fsm;
bool fired = false;
fsm.on_exit<Red>([&](const Red&) { fired = true; });
fsm.send(GoEvent{});
Void::Testing::expect(fired);
}
Void::Testing::Test callbacks_not_fired_on_invalid_transition()
{
TrafficLight fsm;
bool entry = false, exit_cb = false;
fsm.on_enter<Green>([&](const Green&) { entry = true; });
fsm.on_exit<Red>([&](const Red&) { exit_cb = true; });
fsm.send(StopEvent{}); // invalid from Red
Void::Testing::expect(!entry);
Void::Testing::expect(!exit_cb);
}
Void::Testing::Test state_index_matches_variant_order()
{
TrafficLight fsm;
Void::Testing::expect_eq(fsm.state_index(), std::size_t{0}); // Red is first
fsm.send(GoEvent{});
Void::Testing::expect_eq(fsm.state_index(), std::size_t{1}); // Green is second
}
// ── Two-state toggle ──────────────────────────────────────────────────────────
struct Off {};
struct On {};
struct Toggle {};
using Switch = Void::StateMachine::StateMachine<
std::tuple<Off, On>,
std::tuple<
Void::StateMachine::Transition<Off, Toggle, On>,
Void::StateMachine::Transition<On, Toggle, Off>
>>;
Void::Testing::Test toggle_back_and_forth()
{
Switch sw;
Void::Testing::expect(sw.is<Off>());
sw.send(Toggle{});
Void::Testing::expect(sw.is<On>());
sw.send(Toggle{});
Void::Testing::expect(sw.is<Off>());
}
+200
View File
@@ -0,0 +1,200 @@
#include <Void/TypeList/TypeList.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstddef>
#include <string>
#include <type_traits>
using namespace Void::TypeList;
using List3 = TypeList<int, float, double>;
using Empty = TypeList<>;
// ── size_of ───────────────────────────────────────────────────────────────────
Void::Testing::Test size_of_empty()
{
static_assert(size_of<Empty> == 0);
Void::Testing::expect_eq(size_of<Empty>, std::size_t{0});
}
Void::Testing::Test size_of_nonempty()
{
static_assert(size_of<List3> == 3);
Void::Testing::expect_eq(size_of<List3>, std::size_t{3});
}
// ── at ────────────────────────────────────────────────────────────────────────
Void::Testing::Test at_first()
{
static_assert(std::is_same_v<at<0, List3>, int>);
Void::Testing::expect(true);
}
Void::Testing::Test at_last()
{
static_assert(std::is_same_v<at<2, List3>, double>);
Void::Testing::expect(true);
}
// ── front / back ─────────────────────────────────────────────────────────────
Void::Testing::Test front_of_list()
{
static_assert(std::is_same_v<front<List3>, int>);
Void::Testing::expect(true);
}
Void::Testing::Test back_of_list()
{
static_assert(std::is_same_v<back<List3>, double>);
Void::Testing::expect(true);
}
// ── contains ─────────────────────────────────────────────────────────────────
Void::Testing::Test contains_present()
{
static_assert(contains<float, List3>);
Void::Testing::expect(true);
}
Void::Testing::Test contains_absent()
{
static_assert(!contains<char, List3>);
Void::Testing::expect(true);
}
// ── index_of ─────────────────────────────────────────────────────────────────
Void::Testing::Test index_of_first()
{
static_assert(index_of<int, List3> == 0);
Void::Testing::expect_eq(index_of<int, List3>, std::size_t{0});
}
Void::Testing::Test index_of_last()
{
static_assert(index_of<double, List3> == 2);
Void::Testing::expect_eq(index_of<double, List3>, std::size_t{2});
}
// ── push_front / push_back ───────────────────────────────────────────────────
Void::Testing::Test push_front_adds_at_head()
{
using L = push_front<char, List3>;
static_assert(std::is_same_v<front<L>, char>);
static_assert(size_of<L> == 4);
Void::Testing::expect(true);
}
Void::Testing::Test push_back_adds_at_tail()
{
using L = push_back<char, List3>;
static_assert(std::is_same_v<back<L>, char>);
static_assert(size_of<L> == 4);
Void::Testing::expect(true);
}
// ── pop_front ─────────────────────────────────────────────────────────────────
Void::Testing::Test pop_front_removes_head()
{
using L = pop_front<List3>;
static_assert(std::is_same_v<front<L>, float>);
static_assert(size_of<L> == 2);
Void::Testing::expect(true);
}
// ── concat ───────────────────────────────────────────────────────────────────
Void::Testing::Test concat_two_lists()
{
using A = TypeList<int, float>;
using B = TypeList<double, char>;
using C = concat<A, B>;
static_assert(size_of<C> == 4);
static_assert(std::is_same_v<at<2, C>, double>);
Void::Testing::expect(true);
}
// ── filter ───────────────────────────────────────────────────────────────────
Void::Testing::Test filter_keeps_matching()
{
using L = filter<std::is_integral, TypeList<int, float, char, double>>;
static_assert(size_of<L> == 2);
static_assert(std::is_same_v<at<0, L>, int>);
static_assert(std::is_same_v<at<1, L>, char>);
Void::Testing::expect(true);
}
// ── transform ────────────────────────────────────────────────────────────────
Void::Testing::Test transform_applies_to_all()
{
using L = transform<std::add_pointer, TypeList<int, float>>;
static_assert(std::is_same_v<at<0, L>, int*>);
static_assert(std::is_same_v<at<1, L>, float*>);
Void::Testing::expect(true);
}
// ── reverse ──────────────────────────────────────────────────────────────────
Void::Testing::Test reverse_flips_order()
{
using R = reverse<List3>;
static_assert(std::is_same_v<at<0, R>, double>);
static_assert(std::is_same_v<at<2, R>, int>);
Void::Testing::expect(true);
}
// ── unique ───────────────────────────────────────────────────────────────────
Void::Testing::Test unique_removes_duplicates()
{
using L = TypeList<int, float, int, double, float>;
using U = unique<L>;
static_assert(size_of<U> == 3);
static_assert(std::is_same_v<at<0, U>, int>);
Void::Testing::expect(true);
}
// ── for_each ─────────────────────────────────────────────────────────────────
Void::Testing::Test for_each_visits_all_types()
{
std::size_t count = 0;
for_each<TypeList<int, float, double>>(
[&]<typename T>() { ++count; });
Void::Testing::expect_eq(count, std::size_t{3});
}
// ── all_of / any_of / none_of ────────────────────────────────────────────────
Void::Testing::Test all_of_true()
{
static_assert(all_of<std::is_integral, TypeList<int, char, long>>);
Void::Testing::expect(true);
}
Void::Testing::Test all_of_false()
{
static_assert(!all_of<std::is_integral, TypeList<int, float>>);
Void::Testing::expect(true);
}
Void::Testing::Test any_of_true()
{
static_assert(any_of<std::is_floating_point, TypeList<int, float>>);
Void::Testing::expect(true);
}
Void::Testing::Test none_of_true()
{
static_assert(none_of<std::is_pointer, TypeList<int, float>>);
Void::Testing::expect(true);
}