// Stuart Reges // 3/31/05 // // Short program that tests some aspects of the SortedIntList class. // This is by no means an exhaustive set of tests. The program throws // an exception if a test fails or simply takes too long to run if // methods like indexOf are not implemented properly. public class SortedIntListTest2 { public static void main(String[] args) { testConstructors(); testIndexOf(); testUnique(); } // This method guarantees that all four versions of the // constructor exist and does some minor checking that they work. // The arrays are filled to capacity and duplicates are added when // unique is true. The key thing is that this code should execute // without generating any exceptions. public static void testConstructors() { SortedIntList list; list = new SortedIntList(); for (int i = 0; i < SortedIntList.DEFAULT_CAPACITY; i++) list.add(i); list = new SortedIntList(true); for (int i = 0; i < SortedIntList.DEFAULT_CAPACITY; i++) for (int j = 0; j < 10; j++) list.add(i); list = new SortedIntList(2 * SortedIntList.DEFAULT_CAPACITY); for (int i = 0; i < 2 * SortedIntList.DEFAULT_CAPACITY; i++) list.add(i); list = new SortedIntList(true, 2 * SortedIntList.DEFAULT_CAPACITY); for (int i = 0; i < 2 * SortedIntList.DEFAULT_CAPACITY; i++) for (int j = 0; j < 10; j++) list.add(i); System.out.println("Constructors pass basic sanity checks"); } public static void testIndexOf() { // fill up a big list with even numbers int size = 100000; int dot = size / 75; SortedIntList list = new SortedIntList(size); System.out.println("building list, please wait, done after 75 dots:"); for (int i = 0; i < size; i++) { list.add(2 * i); if (i % dot == 0) System.out.print("."); } System.out.println(); // keep track of starting time before calling indexOf long start = System.currentTimeMillis(); // do some basic index tests for (int i = 0; i < size; i++) for (int j = 0; j < 100; j++) if (list.indexOf(2 * i) != i) throw new RuntimeException("indexOf failed for " + 2 * i); if (list.indexOf(-8) != -1 || list.indexOf(2 * size + 10) != -1) throw new RuntimeException("indexOf failed for not found case"); double elapsed = (System.currentTimeMillis() - start)/1000.0; System.out.println("indexOf test passed in " + elapsed + " seconds"); } public static void testUnique() { int size = 40; int quarterSize = size / 4; SortedIntList list = new SortedIntList(size); if (list.getUnique()) throw new RuntimeException("getUnique should be false"); // create a list with some duplicates for (int i = 0; i < size; i++) list.add((int)(Math.random() * quarterSize)); if (list.size() != size) throw new RuntimeException("add not allowing duplicates"); // now let's disallow duplicates list.setUnique(true); if (list.size() > quarterSize) throw new RuntimeException("setUnique didn't elminate duplicates"); // let's batter it with duplicates for (int i = 0; i < quarterSize * 1000; i++) list.add((int)(Math.random() * quarterSize)); if (list.size() != quarterSize) throw new RuntimeException("suspicious list length"); if (!list.getUnique()) throw new RuntimeException("getUnique should be true"); // now let's go back to allowing duplicates and fill it up list.setUnique(false); for (int i = 0; i < size - quarterSize; i++) list.add((int)(Math.random() * quarterSize)); if (list.size() != size) throw new RuntimeException("wrong list size"); if (list.getUnique()) throw new RuntimeException("getUnique should be false"); System.out.println("getUnique passes basic tests"); } }