package rebelsky.linear; import java.io.PrintWriter; /** * A simple set of tests for linear structures. * * @author Samuel A. Rebelsky * @version 1.0 of October 2004 */ public class LinearTester { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The linear structure being tested. */ Linear structure; /** * The pen used for output. */ PrintWriter pen; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new tester for a particular structure, * using a designated pen. */ public LinearTester(Linear _structure, PrintWriter _pen) { this.structure = _structure; this.pen = _pen; } // LinearTester(Lienar,PrintWriter) // +---------+------------------------------------------------- // | Methods | // +---------+ /** * Do all the key tests. */ public void testSuite() { this.testEmpty(); this.testOneElement(); this.testPutsAndGets(); this.testEmpty(); } // testSuite() /** * Test adding and removing one element. If the * structure is initially empty, this should return * the same element. */ public void testOneElement() { this.pen.println("put(\"ONE\")"); this.structure.put("ONE"); this.pen.println("get()"); this.pen.println(" " + this.structure.get()); } // testOneElement() /** * Test getting an element from an empty structure. */ public void testEmpty() { this.pen.println("get() // from an empty structure"); try { this.pen.println(" " + this.structure.get()); this.pen.println(" " + "That's WRONG"); } catch (Exception e) { this.pen.println(" get() failed, as it should have."); } } // testEmpty() /** * Test a lot of putsAndGets. */ public void testPutsAndGets() { // Add seven items for (int i = 0; i < 7; i++) { String str = "value 1/" + i; this.pen.println("put(\"" + str + "\")"); this.structure.put(str); } // for // Get four items for (int i = 0; i < 4; i++) { this.pen.print("get() -> "); this.pen.println(this.structure.get()); } // for // Add three items for (int i = 0; i < 3; i++) { String str = "value 2/" + i; this.pen.println("put(\"" + str + "\")"); this.structure.put(str); } // for // Get all remaining values while (!this.structure.isEmpty()) { this.pen.print("get() -> "); this.pen.println(this.structure.get()); } // while } // testPutsAndGets() } // class LinearTester