Initial commit that has most of what I need.
This commit is contained in:
parent
933f47a440
commit
ad143dca05
11 changed files with 317 additions and 0 deletions
39
tests/dbc.cpp
Normal file
39
tests/dbc.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include "dbc.hpp"
|
||||
|
||||
using namespace dbc;
|
||||
|
||||
TEST_CASE("basic feature tests", "[utils]") {
|
||||
log("Logging a message.");
|
||||
|
||||
try {
|
||||
sentinel("This shouldn't happen.");
|
||||
} catch(SentinelError) {
|
||||
log("Sentinel happened.");
|
||||
}
|
||||
|
||||
pre("confirm positive cases work", 1 == 1);
|
||||
pre("confirm positive lambda", [&]{ return 1 == 1;});
|
||||
post("confirm positive post", 1 == 1);
|
||||
post("confirm postitive post with lamdba", [&]{ return 1 == 1;});
|
||||
|
||||
check(1 == 1, "one equals 1");
|
||||
|
||||
try {
|
||||
check(1 == 2, "this should fail");
|
||||
} catch(CheckError err) {
|
||||
log("check fail worked");
|
||||
}
|
||||
|
||||
try {
|
||||
pre("failing pre", 1 == 3);
|
||||
} catch(PreCondError err) {
|
||||
log("pre fail worked");
|
||||
}
|
||||
|
||||
try {
|
||||
post("failing post", 1 == 4);
|
||||
} catch(PostCondError err) {
|
||||
log("post faile worked");
|
||||
}
|
||||
}
|
67
tests/fsm.cpp
Normal file
67
tests/fsm.cpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <fmt/core.h>
|
||||
#include <string>
|
||||
#include "../fsm.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
using std::string;
|
||||
|
||||
enum class MyState {
|
||||
START, RUNNING, END
|
||||
};
|
||||
|
||||
enum class MyEvent {
|
||||
STARTED, PUSH, QUIT
|
||||
};
|
||||
|
||||
class MyFSM : public DeadSimpleFSM<MyState, MyEvent> {
|
||||
public:
|
||||
void event(MyEvent ev, string data="") {
|
||||
switch(_state) {
|
||||
FSM_STATE(MyState, START, ev);
|
||||
FSM_STATE(MyState, RUNNING, ev, data);
|
||||
FSM_STATE(MyState, END, ev);
|
||||
}
|
||||
}
|
||||
|
||||
void START(MyEvent ev) {
|
||||
println("<<< START");
|
||||
state(MyState::RUNNING);
|
||||
}
|
||||
|
||||
void RUNNING(MyEvent ev, string &data) {
|
||||
if(ev == MyEvent::QUIT) {
|
||||
println("<<< QUITTING {}", data);
|
||||
state(MyState::END);
|
||||
} else {
|
||||
println("<<< RUN: {}", data);
|
||||
state(MyState::RUNNING);
|
||||
}
|
||||
}
|
||||
|
||||
void END(MyEvent ev) {
|
||||
println("<<< STOP");
|
||||
state(MyState::END);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("confirm fsm works with optional data", "[utils]") {
|
||||
MyFSM fsm;
|
||||
|
||||
REQUIRE(fsm.in_state(MyState::START));
|
||||
|
||||
fsm.event(MyEvent::STARTED);
|
||||
REQUIRE(fsm.in_state(MyState::RUNNING));
|
||||
|
||||
fsm.event(MyEvent::PUSH);
|
||||
REQUIRE(fsm.in_state(MyState::RUNNING));
|
||||
|
||||
fsm.event(MyEvent::PUSH);
|
||||
REQUIRE(fsm.in_state(MyState::RUNNING));
|
||||
|
||||
fsm.event(MyEvent::PUSH);
|
||||
REQUIRE(fsm.in_state(MyState::RUNNING));
|
||||
|
||||
fsm.event(MyEvent::QUIT, "DONE!");
|
||||
REQUIRE(fsm.in_state(MyState::END));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue