]> git.mxchange.org Git - simgear.git/blobdiff - simgear/structure/state_machine_test.cxx
Some Linux platforms need <cstdio> for snprintf.
[simgear.git] / simgear / structure / state_machine_test.cxx
index a22ae6390dc2fcee340b812df18857e52207c8a0..0703ce622ef12e8016c1bd1b5d3c1ea611af5028 100644 (file)
@@ -23,6 +23,7 @@
 #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;
@@ -43,27 +44,20 @@ public:
     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() \
@@ -147,10 +141,32 @@ void testBasic()
     }
 }
 
+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"));
@@ -165,20 +181,20 @@ void testBindings()
     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"));
@@ -186,11 +202,11 @@ void testBindings()
     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()
@@ -220,6 +236,8 @@ int main(int argc, char* argv[])
     testBasic();
     testBindings();
     testParse();
+    testNoSourcesTransition();
+    
     cout << __FILE__ << ": All tests passed" << endl;
     return EXIT_SUCCESS;
 }