package username.linear; import java.io.PrintWriter; import java.math.BigInteger; /** * A generic assistant that helps test linear structures. * To use the tester, try something like the following * *
 * LinearTesterHelper assistant =
 *   new LinearTesterHelper(new LinearStructureOfYourChoice());
 * assistant.runEmptyTest();
 * assistant.runPutTest();
 * assistant.runEmptyTest();
 * assistant.runGetTest();
 * assistant.runEmptyTest();
 * assistant.runSimpleTest();
 * assistant.runSimpleTest();
 * assistant.runClearTest();
 * assistant.runSimpleTest();
 * 
* * You can replicate that with * *
 * LinearTesterHelper assistant =
 *   new LinearTesterHelper(new LinearStructureOfYourChoice());
 * assistant.runStandardTests();
 * 
* * @author Samuel A. Rebelsky * @version 1.1 of March 2006 */ public class LinearTesterHelper { // +--------+------------------------------------------------------------ // | Fields | // +--------+ /** The linear structure we're testing. */ LinearStructure victim; /** The PrintWriter used for output. */ PrintWriter pen; // +-------------+------------------------------------------------------- // | Construtors | // +-------------+ /** * Build a new tester for a particular linear structure. */ public LinearTesterHelper(LinearStructure _victim) { this.victim = _victim; this.pen = new PrintWriter(System.out, true); } // LinearTesterHelper(LinearStructure) // +---------+----------------------------------------------------------- // | Methods | // +---------+ /** * Test the isEmpty method. */ public void runEmptyTest() { this.pen.println("Testing isEmpty"); if (this.victim.isEmpty()) { this.pen.println(" The structure is empty."); } else { this.pen.println(" The structure is not empty."); } } // runEmptyTest() /** * Test the put method. The method should succeed. */ public void runPutTest() { this.pen.println("Putting 'Alpha'"); this.victim.put("Alpha"); this.pen.println(" Done."); } // testPut() /** * Test the get method. This method should succeed. */ public void runGetTest() { this.pen.println("Getting a value"); this.pen.println(" Got: " + this.victim.get()); } // testGet() /** * Add a few values, remove fewer values. It's helpful * to call this a few times to verify that we can continue * building upon the structure after removal. It's also * helpful to call this after runClearTest to make sure that * that method works. */ public void runSimpleTest() { this.pen.println("Adding five elements"); for (int i = 0; i < 5; i++) { String istr = Integer.toString(i); this.pen.println(" Adding " + istr); this.victim.put(istr); } // for this.pen.println("Removing three elements"); for (int i = 0; i < 3; i++) { this.pen.println(" Removing: " + this.victim.get()); } // for } // runSimpleTest() /** * Remove all values from the linear structure. */ public void runClearTest() { this.pen.println("Removing all elements."); while (!this.victim.isEmpty()) { this.pen.println(" Removing: " + this.victim.get()); } // while } // runClearTest /** * The standard set of tests. */ public void runStandardTests() { this.runEmptyTest(); this.runPutTest(); this.runEmptyTest(); this.runGetTest(); this.runEmptyTest(); this.runSimpleTest(); this.runSimpleTest(); this.runClearTest(); this.runSimpleTest(); } // runStandardTests() } // class LinearTesterHelper