Files
Library/tests/standalone/StateMachine/StateMachine.cpp
T
faumarayandClaude Sonnet 4.6 a9a2e2b59a 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>
2026-05-14 22:49:22 +03:00

115 lines
3.2 KiB
C++

#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>());
}