// Stuart Reges // 1/6/07 // // This program tests stage 3 of the SortedIntList project. The methods // called in main each perform a series of checks and throw an exception if // anything test fails. The program uses the list size as its primary // measure of correctness. It doesn't verify, for example, that the list // stores the correct numbers. It just verifies that it has the expected // number of values. import java.util.*; public class Test3 { public static void main(String[] args) { // the program performs 10 tests each for test sizes from 0 to 99 for (int testSize = 0; testSize < 100; 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 basic getUnique/setUnique tests"); } // This method tests the case where unique is set to true most of the time public static void testTrue(SortedIntList list, int testSize) { setAndTestUnique(list, true, testSize); boolean[] chosen = new boolean[testSize]; int count = 0; Random r = new Random(); // this loop generates random numbers until all test numbers have // been added to the list while (count < testSize) { int next = r.nextInt(testSize); list.add(next); if (!chosen[next]) { count++; chosen[next] = true; } checkSize(list, count, testSize); } // make sure we can turn unique off and on again setAndTestUnique(list, false, testSize); setAndTestUnique(list, true, testSize); checkSize(list, testSize, testSize); // batter the list with duplicates to make sure it works for (int i = 0; i < testSize * 10; i++) { int next = r.nextInt(testSize); list.add(next); } checkSize(list, testSize, testSize); } // This method tests the case where unique is set to false most of the time public static void testFalse(SortedIntList list, int testSize) { setAndTestUnique(list, false, testSize); boolean[] chosen = new boolean[testSize]; int count = 0; Random r = new Random(); // this loop will add each test number exactly once to the list while (count < testSize) { int next = r.nextInt(testSize); if (!chosen[next]) { list.add(next); count++; chosen[next] = true; } checkSize(list, count, testSize); } // then we add testSize duplicates to the list for (int i = 1; i <= testSize; i++) { int next = r.nextInt(testSize); list.add(next); checkSize(list, testSize + i, testSize); } // and make sure that they are removed when we set unique back to true setAndTestUnique(list, true, testSize); checkSize(list, testSize, testSize); // then add testSize more duplicates setAndTestUnique(list, false, testSize); for (int i = 1; i <= testSize; i++) { int next = r.nextInt(testSize); list.add(next); checkSize(list, testSize + i, testSize); } // and again make sure they are removed when unique is set to false setAndTestUnique(list, true, testSize); checkSize(list, testSize, testSize); } // 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, int testSize) { list.setUnique(unique); if (list.getUnique() != unique) throw new RuntimeException("getUnique = " + list.getUnique() + " after setting it to " + unique + " with test size = " + testSize); } // Checks to see if the list has the appropriate size. public static void checkSize(SortedIntList list, int size, int testSize) { if (list.size() != size) throw new RuntimeException("size = " + list.size() + " when it " + " should be " + size + " with test size = " + testSize + " and list = " + list); } }