// Signature // IntStack emptyStack (); // IntStack push (int); // boolean isEmpty (); // int top (); // IntStack pop (); // boolean equals (Object); // // Specs: // s.push(i).top() == i // s.push(i).pop() == s // s.push(i).isEmpty() == false // IntStack.emptyStack().isEmpty() == true // IntStack.emptyStack().equals (obj) == // obj.isEmpty() if obj is a IntStack // false otherwise // s.push(i).equals (obj) == // false if obj is a IntStack & obj.isEmpty()==true // obj.top()==i && s.equals(obj.pop()) // if obj is a IntStack & obj.isEmpty()==false // false otherwise import java.util.Random; public class IntStackTester { /** Main testing method */ public static void main(String[] args){ /* Create a new Tester, run the tests, and Print out results. */ IntStackTester t = new IntStackTester (); /* See definitions below */ t.testIntStacks(); t.testExceptions(); /* A few Stats... */ System.out.println("\nNumber of stacks tested: " + t.totStacks); System.out.println("Number of tests performed: " + t.ntests); System.out.println("Number of errors found: " + t.nerr); } private int ntests = 0; // Total Tests Run private int nerr = 0; // Number of Errors private int totStacks = 0; // Number of Stacks Created /* How many stacks to create? */ private static final int NUM_TO_TEST = 200; /* Create random stacks, and test them */ private void testIntStacks(){ Random r = new Random(); // first off, make sure we test the empty stack testIntStack (new int[0],r.nextInt(10)); // bunch of random stacks (random size) for(int i = 0; i < NUM_TO_TEST; i++){ int size = r.nextInt(5); // number of elements in stack int[] vals = new int[size]; for (int j=0; j < size; j++) vals[j]=r.nextInt(10); /* Run the Tests */ testIntStack (vals,r.nextInt(10)); } } /* Test a single Stack */ private void testIntStack (int[] vals,int next){ try{ /* Create the stack from the array of ints */ IntStack s = IntStack.emptyStack (); for (int i=0; i