// Stuart Reges // 1/6/06 // // Short program that calls many methods of the SortedIntList class, // displaying the state of the list as it goes. import java.util.*; public class SortedIntListTest { public static final int MAX = 5; // program will add numbers in the // range of -MAX to +MAX public static void main(String[] args) { SortedIntList list = new SortedIntList(); Random r = new Random(); // add a few elements addElements(list, r, MAX); // check the indexOf method for a range of values checkIndexOf(list); // check the remove method by emptying the list checkRemove(list, r); // now fill the list back up addElements(list, r, 2 * MAX); // and see whether setting unique to true eliminates duplicates list.setUnique(true); System.out.println("after setting unique to true, list = " + list); System.out.println(); // now bombard the list to make sure no duplicates appear addElements(list, r, 5 * MAX); // and check the indexOf and remove methods in this mode checkIndexOf(list); checkRemove(list, r); } // adds given number of elements to the list, reporting the result. public static void addElements(SortedIntList list, Random r, int number) { System.out.println("initially list = " + list); for (int i = 0; i < number; i++) { int next = r.nextInt(2 * MAX + 1) - MAX; list.add(next); System.out.println("adding " + next + ", list = " + list); } System.out.println(); } // checks the index of values from -(2 * Max) to +(2 * MAX); obviously // many of these should yield -1 when indexOf is called public static void checkIndexOf(SortedIntList list) { System.out.println("testing indexOf with list = " + list); for (int i = -2 * MAX; i <= 2 * MAX; i++) { System.out.println("indexOf " + i + " = " + list.indexOf(i)); } System.out.println(); } // empties the list in random order, reporting the result as it goes public static void checkRemove(SortedIntList list, Random r) { System.out.println("testing remove with list = " + list); while (list.size() > 0) { int index = r.nextInt(list.size()); list.remove(index); System.out.println("removing at " + index + ", list = " + list); } System.out.println(); } }