00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/io/File.h>
00018 #include <spl/Log.h>
00019 #include <spl/math/StateMachine.h>
00020
00021 #ifdef DEBUG
00022
00023 class _SmTest2 : public IStateEventListener
00024 {
00025 public:
00026 String m_enter;
00027 String m_leave;
00028 String m_trans;
00029
00030 _SmTest2();
00031 ~_SmTest2();
00032
00033 virtual void OnStateEnter( const String &input, State *to, State *from );
00034 virtual void OnStateLeave( const String &input, State *to, State *from );
00035 virtual void OnStateTransition( const String &input, Transition *trans );
00036
00037 virtual void ValidateMem() const;
00038 virtual void CheckMem() const;
00039 };
00040
00041 _SmTest2::_SmTest2()
00042 : m_enter(), m_leave(), m_trans()
00043 {
00044 }
00045
00046 _SmTest2::~_SmTest2()
00047 {
00048 }
00049
00050 void _SmTest2::OnStateEnter( const String &input, State *to, State *from )
00051 {
00052 m_enter.Set( input );
00053 }
00054
00055 void _SmTest2::OnStateLeave( const String &input, State *to, State *from )
00056 {
00057 m_leave.Set( input );
00058 }
00059
00060 void _SmTest2::OnStateTransition( const String &input, Transition *trans )
00061 {
00062 m_trans.Set( input );
00063 }
00064
00065 void _SmTest2::ValidateMem() const
00066 {
00067 }
00068
00069 void _SmTest2::CheckMem() const
00070 {
00071 m_enter.CheckMem();
00072 m_leave.CheckMem();
00073 m_trans.CheckMem();
00074 }
00075
00076 static void _TestStateMachine1()
00077 {
00078 StateMachine *sm = new StateMachine();
00079
00080 DEBUG_CLEAR_MEM_CHECK_POINTS();
00081 DEBUG_NOTE_MEM(sm);
00082 sm->CheckMem();
00083 DEBUG_DUMP_MEM_LEAKS();
00084 UNIT_ASSERT_MEM_NOTED("StateMachine test 1.0");
00085
00086 delete sm;
00087
00088 DEBUG_CLEAR_MEM_CHECK_POINTS();
00089 DEBUG_DUMP_MEM_LEAKS();
00090 UNIT_ASSERT_MEM_NOTED("StateMachine test 1.1");
00091
00092 Log::SWriteOkFail( "StateMachine test 1" );
00093 }
00094
00095 static void _TestStateMachine2()
00096 {
00097 StateMachine sm;
00098 _SmTest2 *listener = new _SmTest2();
00099
00100 sm.GetResolver().Add("SmTest", listener);
00101 sm.ValidateMem();
00102 sm.DefineState("start", "SmTest", "SmTest");
00103 sm.DefineState("end", "SmTest", "SmTest");
00104 sm.DefineTransition("one", "start", "end", "SmTest");
00105 sm.DefineTransition("*", "end", "start", "SmTest");
00106 sm.SetState("start");
00107
00108 sm.ChangeState( "one" );
00109
00110 UNIT_ASSERT("enter", listener->m_enter.Equals("one"));
00111 UNIT_ASSERT("leave", listener->m_leave.Equals("one"));
00112 UNIT_ASSERT("trans", listener->m_trans.Equals("one"));
00113
00114 sm.ChangeState( "lamda" );
00115
00116 UNIT_ASSERT("enter", listener->m_enter.Equals("lamda"));
00117 UNIT_ASSERT("leave", listener->m_leave.Equals("lamda"));
00118 UNIT_ASSERT("trans", listener->m_trans.Equals("lamda"));
00119
00120 DEBUG_CLEAR_MEM_CHECK_POINTS();
00121 sm.CheckMem();
00122 DEBUG_DUMP_MEM_LEAKS();
00123 UNIT_ASSERT_MEM_NOTED("StateMachine test 2.0");
00124
00125 delete listener;
00126
00127 Log::SWriteOkFail( "StateMachine test 2" );
00128 }
00129
00130 static void _TestStateMachine3()
00131 {
00132 StateMachine sm;
00133 _SmTest2 *listener = new _SmTest2();
00134 sm.GetResolver().Add("SmTest", listener);
00135 if ( File::Exists("test/StateMachine1.xml") )
00136 {
00137 sm.Load( "test/StateMachine1.xml" );
00138 }
00139 else if ( File::Exists("../test/StateMachine1.xml") )
00140 {
00141 sm.Load( "../test/StateMachine1.xml" );
00142 }
00143 else if ( File::Exists("../../test/StateMachine1.xml") )
00144 {
00145 sm.Load( "../../test/StateMachine1.xml" );
00146 }
00147 else if ( File::Exists("../../spl/test/StateMachine1.xml") )
00148 {
00149 sm.Load( "../../spl/test/StateMachine1.xml" );
00150 }
00151 else
00152 {
00153 sm.Load( "StateMachine1.xml" );
00154 }
00155
00156 sm.SetState("start");
00157 sm.ChangeState( "one" );
00158
00159 UNIT_ASSERT("enter", listener->m_enter.Equals("one"));
00160 UNIT_ASSERT("leave", listener->m_leave.Equals("one"));
00161 UNIT_ASSERT("trans", listener->m_trans.Equals("one"));
00162
00163 sm.ChangeState( "lamda" );
00164
00165 UNIT_ASSERT("enter", listener->m_enter.Equals("lamda"));
00166 UNIT_ASSERT("leave", listener->m_leave.Equals("lamda"));
00167 UNIT_ASSERT("trans", listener->m_trans.Equals("lamda"));
00168
00169 DEBUG_CLEAR_MEM_CHECK_POINTS();
00170 sm.CheckMem();
00171 DEBUG_DUMP_MEM_LEAKS();
00172 UNIT_ASSERT_MEM_NOTED("StateMachine test 3.0");
00173
00174 delete listener;
00175
00176 Log::SWriteOkFail( "StateMachine test 3" );
00177 }
00178
00179 void _TestStateMachine()
00180 {
00181 _TestStateMachine1();
00182 DEBUG_CLEAR_MEM_CHECK_POINTS();
00183 DEBUG_DUMP_MEM_LEAKS();
00184
00185 _TestStateMachine2();
00186 DEBUG_CLEAR_MEM_CHECK_POINTS();
00187 DEBUG_DUMP_MEM_LEAKS();
00188
00189 _TestStateMachine3();
00190 DEBUG_CLEAR_MEM_CHECK_POINTS();
00191 DEBUG_DUMP_MEM_LEAKS();
00192 }
00193
00194 #endif
00195