// Stuart Reges // 4/3/10 // // This program tests stage 3 of the SortedIntList project. The methods called // in main each perform a series of checks and halt execution if any test // fails. // // The basic approach of the testing code is to use an ArrayList and a // TreeSet to compute the correct answer and to continually compare // the SortedIntList to these structures. The manipulations performed on these // structures are highly inefficient. We can live with this kind of // inefficiency in a testing program, but it should not be taken as an example // of good coding. import java.util.*; public class Test3 { public static void main(String[] args) { // the program performs 10 tests each for test sizes from 0 to 24 for (int testSize = 0; testSize < 25; testSize++) { System.out.print("Testing with size " + testSize + ": "); for (int i = 0; i < 10; i ++) { SortedIntList list1 = new SortedIntList(testSize); testTrue(list1, testSize); SortedIntList list2 = new SortedIntList(2 * testSize); testFalse(list2, testSize); } System.out.println("Passed"); } } // This method tests the case where unique is set to true most of the time public static void testTrue(SortedIntList list, int testSize) { Set result = new TreeSet(); String history = "adding with unique=true:"; Random r = new Random(); setAndTestUnique(list, true); // this loop generates random numbers until all test numbers have // been added to the list while (result.size() < testSize) { int next = r.nextInt(testSize); history += " " + next; result.add(next); list.add(next); check(list, result, history); } // make sure we can turn unique off and on again setAndTestUnique(list, false); setAndTestUnique(list, true); check(list, result, history); // batter the list with duplicates to make sure it works for (int i = 0; i < testSize * 10; i++) { int next = r.nextInt(testSize); history += " " + next; list.add(next); result.add(next); check(list, result, history); } } // This method tests the case where unique is set to false most of the time public static void testFalse(SortedIntList list, int testSize) { List result = new ArrayList(); String history = "adding with unique=false:"; Random r = new Random(); setAndTestUnique(list, false); // this loop will add each test number exactly once to the list while (result.size() < testSize) { int next = r.nextInt(testSize); if (!result.contains(next)) { history += " " + next; result.add(next); Collections.sort(result); list.add(next); check(list, result, history); } } // then we add testSize duplicates to the list for (int i = 1; i <= testSize; i++) { int next = r.nextInt(testSize); history += " " + next; result.add(next); Collections.sort(result); list.add(next); check(list, result, history); } // and make sure that they are removed when we set unique back to true setAndTestUnique(list, true); history += " unique=true"; result = new ArrayList(new TreeSet(result)); check(list, new TreeSet(result), history); // then add testSize more duplicates setAndTestUnique(list, false); history += " unique=false"; for (int i = 1; i <= testSize; i++) { int next = r.nextInt(testSize); history += " " + next; result.add(next); Collections.sort(result); list.add(next); check(list, result, history); } // and again make sure they are removed when unique is set to true setAndTestUnique(list, true); history += " unique=true"; check(list, new TreeSet(result), history); } // This method sets the unique property to the given value and makes sure // that getUnique returns the same value. public static void setAndTestUnique(SortedIntList list, boolean unique) { list.setUnique(unique); boolean result = list.getUnique(); if (result != unique) { System.out.println(" error: getUnique = " + result + " after setting it to " + unique); System.exit(1); } } // Checks to see if the list has the appropriate size. public static void check(SortedIntList list, Collection result, String history) { String correct = result.toString(); String test = list.toString(); if (!correct.equals(test)) { System.out.println("error occurred after these actions:"); System.out.println(" " + history); System.out.println(" list should = " + correct); System.out.println(" actual list = " + test); System.exit(1); } } }