Gosh, it's not as complicated as it sounds. State machine code is very simple and short.
Store the state in a variable, say myState.
You indicate that the machine will be a switch statement, branching out with the value of the myState variable, to implement code for each state.
The code will be filled with the following lines:
myState = newState;
To ensure that state transition requirements are met, you need to add another method instead, for example
void DoSafeStateTransition( int newState ) { // check myState -. newState is not forbidden // lots of ways to do this // perhaps nested switch statement switch( myState ) { … case X: switch( newState ) case A: case B: case Z: HorribleError( newState ); break; ... } // check that newState is not undetermined switch( newState ) { // all the determined states case A: case B: case C … case Z: myState = newState; break; default: HorribleError( newState ); } } void HorribleError( int newState ) { printf("Attempt to go from %d to %d - disallowed\n", myState, newState ); exit(1); }
I suggest that it is simple and short enough that validation will be better than unit testing - it will certainly be much faster!
The point, in my opinion, of unit testing is that the test code is simpler than the tested code, so it can be more easily checked for correctness and then used to test complex code. It is often easier to verify the state machine code than the test state machine code. It makes no sense to specify 100% throughput unit test if you have little idea if the unit tests are correct.
Put it another way: coding the final car is easy, developing the right one is difficult. Unit tests will only indicate whether you have encoded the design correctly, and not with the correct design.
ravenspoint
source share