// This class drives the FiniteStateMachine class and all dependent classes. // It has been designed to test various features of the FSM code and // the error checking built into it. The output is a number of // test cases, with descriptive text for each case, and the word // "passed" or "failed" for each case. // This code should be modified when any changes are added to the FSM // so that those changes can be automatically tested. public class Driver { int caseNumber = 0; // Used to associated numbers with test cases String testCaseString; // Used to build up the string displaying a test case result public static void main(String argv[]) { Driver myself = new Driver(); myself.buildMachine(); myself.foobarMachine(); } // end main // print header for test case void newCase( String description ) { caseNumber++; testCaseString = caseNumber + ". " + description; } // end newCase // print result of test case void caseResult( String result ) { // line up passed/failed statement on column 60 or later int len = testCaseString.length(); if (len < 60) for (int i = 0; i < (60 - len); i++) testCaseString += " "; testCaseString += result; System.out.println( testCaseString ); testCaseString = null; // deliberately set to null so we catch // unbalanced use of the case formatting // routines } // end caseResult // print result of test case, with amplifying info indented on next line void caseResult( String result, String ampInfo ) { // line up passed/failed statement on column 60 or later int len = testCaseString.length(); if (len < 60) for (int i = 0; i < (60 - len); i++) testCaseString += " "; testCaseString += result; System.out.println( testCaseString ); System.out.print(" "); System.out.println(ampInfo); } // end caseResult public void testMachine( FiniteStateMachine m, String testString, String expectedState ) { // Given a machine m, and series of single-character events testString, // verify that the machine ends up in expectedState. // The expectedState of "exception" is special, and indicates an exception. int i; Exception ex=null; m.reset(); String curstate=m.getCurrentState().getLabel(); newCase("testing "+testString+": "); // each time through this loop we extract a character from the // string and feed it as an event to the state machine. When // the loop is done, the state machine is excepted to be // in a particular state. for (i=0; i