#include #include #include #include // ── Simple traffic-light FSM ────────────────────────────────────────────────── struct Red {}; struct Green {}; struct Yellow {}; struct GoEvent {}; struct SlowEvent {}; struct StopEvent {}; using TrafficLight = Void::StateMachine::StateMachine< std::tuple, std::tuple< Void::StateMachine::Transition, Void::StateMachine::Transition, Void::StateMachine::Transition >>; Void::Testing::Test initial_state_is_first() { TrafficLight fsm; Void::Testing::expect(fsm.is()); } Void::Testing::Test valid_transition_taken() { TrafficLight fsm; const bool ok = fsm.send(GoEvent{}); Void::Testing::expect(ok); Void::Testing::expect(fsm.is()); } 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()); } 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()); } Void::Testing::Test entry_callback_fired() { TrafficLight fsm; bool fired = false; fsm.on_enter([&](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([&](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([&](const Green&) { entry = true; }); fsm.on_exit([&](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, std::tuple< Void::StateMachine::Transition, Void::StateMachine::Transition >>; Void::Testing::Test toggle_back_and_forth() { Switch sw; Void::Testing::expect(sw.is()); sw.send(Toggle{}); Void::Testing::expect(sw.is()); sw.send(Toggle{}); Void::Testing::expect(sw.is()); }