#include <simgear/props/props.hxx>
#include <simgear/props/props_io.hxx>
#include <simgear/structure/commands.hxx>
+#include <simgear/misc/test_macros.hxx>
using std::string;
using std::cout;
bool _state;
};
-static int dummy_cmd_state = 0;
-
-bool dummyCommand(const SGPropertyNode* arg)
+class DummyThing
{
- ++dummy_cmd_state;
- return true;
-}
-
-#define COMPARE(a, b) \
- if ((a) != (b)) { \
- cerr << "failed:" << #a << " != " << #b << endl; \
- cerr << "\tgot:'" << a << "'" << endl; \
- exit(1); \
- }
-
-#define VERIFY(a) \
- if (!(a)) { \
- cerr << "failed:" << #a << endl; \
- exit(1); \
+public:
+ DummyThing() : dummy_cmd_state(0) { }
+
+ bool someCommand(const SGPropertyNode* arg)
+ {
+ dummy_cmd_state++;
+ return true;
}
+ int dummy_cmd_state;
+};
+
using namespace simgear;
#define BUILD_MACHINE_1() \
}
}
+void testNoSourcesTransition()
+{
+ BUILD_MACHINE_1();
+
+ DummyCondition* trigger4 = new DummyCondition;
+ StateMachine::Transition_ptr t4 = sm->createTransition("alwaysToA", stateA); \
+ t4->setTriggerCondition(trigger4); \
+ sm->init();
+
+ COMPARE(sm->state()->name(), "a");
+ trigger1->_state = true;
+ sm->update(1.0);
+ trigger1->_state = false;
+ COMPARE(sm->state()->name(), "b");
+
+ trigger4->_state = true;
+ sm->update(1.0);
+ trigger4->_state = false;
+ COMPARE(sm->state()->name(), "a");
+}
+
void testBindings()
{
SGCommandMgr* cmdMgr = SGCommandMgr::instance();
- cmdMgr->addCommand("dummy", dummyCommand);
+ DummyThing thing;
+ cmdMgr->addCommand("dummy", &thing, &DummyThing::someCommand);
BUILD_MACHINE_1();
t2->addBinding(new SGBinding("dummy"));
sm->update(1.0);
trigger1->_state = false;
COMPARE(sm->state()->name(), "b");
- COMPARE(dummy_cmd_state, 1); // exit state A
+ COMPARE(thing.dummy_cmd_state, 1); // exit state A
trigger2->_state = true;
sm->update(1.0);
trigger2->_state = false;
- COMPARE(dummy_cmd_state, 3); // fire transition 2, enter state C
+ COMPARE(thing.dummy_cmd_state, 3); // fire transition 2, enter state C
- dummy_cmd_state = 0;
+ thing.dummy_cmd_state = 0;
sm->changeToState(stateA);
- COMPARE(dummy_cmd_state, 1); // enter state A
+ COMPARE(thing.dummy_cmd_state, 1); // enter state A
trigger1->_state = true;
sm->update(1.0);
trigger1->_state = false;
- COMPARE(dummy_cmd_state, 2); // exit state A
+ COMPARE(thing.dummy_cmd_state, 2); // exit state A
////////////////////////
t3->addBinding(new SGBinding("dummy"));
t3->addBinding(new SGBinding("dummy"));
sm->changeToStateName("b");
- dummy_cmd_state = 0;
+ thing.dummy_cmd_state = 0;
trigger3->_state = true;
sm->update(1.0);
trigger3->_state = false;
- COMPARE(dummy_cmd_state, 4); // three transition bindings, enter A
+ COMPARE(thing.dummy_cmd_state, 4); // three transition bindings, enter A
}
void testParse()
testBasic();
testBindings();
testParse();
+ testNoSourcesTransition();
+
cout << __FILE__ << ": All tests passed" << endl;
return EXIT_SUCCESS;
}