import edu.neu.ccs.gui.*; import edu.neu.ccs.jpf.*; /** * Provides tests and analysis for sort algorithms. * @author Jeff Raab */ public class SortingAndAnalysisTests extends JPF { /** Starts the test application. */ public static void main(String[] args) { new SortingAndAnalysisTests(); } /** Tests the selection sort algorithm. */ public void TestSelectionSort() { testSort(new SelectionSort()); } /** Tests the insertion sort algorithm. */ public void TestInsertionSort() { testSort(new InsertionSort()); } /** Tests the merge sort algorithm. */ public void TestMergeSort() { testSort(new MergeSort()); } /** Tests the merge sort 3-way algorithm. */ public void TestMergeSort3() { testSort(new MergeSort3()); } /** Tests the given sort algorithm. */ protected void testSort(ISort anAlg) { console.out.println("Testing " + anAlg.getName()); console.out.println(); Comparable[] aNull = null; Comparable[] aZero = new Comparable[0]; Comparable[] aRandom = new Comparable[] { new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()), new Integer(randomInt()) }; Comparable[] aReversed = new Comparable[] { new Integer(8), new Integer(7), new Integer(6), new Integer(5), new Integer(4), new Integer(3), new Integer(2), new Integer(1) }; console.out.println("Null array"); test(anAlg, aNull); console.out.println("Zero length array"); test(anAlg, aZero); console.out.println("Random array"); test(anAlg, aRandom); console.out.println("Reversed array"); test(anAlg, aReversed); console.out.println(); } /** Runs the sort analyzer application. */ public void RunSortAnalyzer() { JPTFrame.createQuickJPTFrame("Sort analyzer", new SortAnalyzer()); } /** * Performs a test of the given algorithm on the given array. * @param anAlgorithm algorithm to test * @param anArray array for test */ private void test(ISort anAlgorithm, Comparable[] anArray) { console.out.print("before: "); print(anArray); anAlgorithm.sort(anArray); console.out.print("after: "); print(anArray); console.out.println("sorted: " + isSorted(anArray)); console.out.println(); } /** * Returns true if the given array of objects is sorted, * or false if it is not. * @param anArray array to test */ private boolean isSorted(Comparable[] anArray) { if (anArray == null) { return true; } if (anArray.length == 1) { return true; } for (int i = 1; i < anArray.length; i++) { if (anArray[i].compareTo(anArray[i - 1]) < 0) { return false; } } return true; } /** * Prints the given array of objects. * @param anArray array to print */ private void print(Comparable[] anArray) { if (anArray == null) { console.out.println(); return; } console.out.print("{ "); if (anArray.length > 0) { console.out.print(anArray[0]); } for (int i = 1; i < anArray.length; i++) { console.out.print(", " + anArray[i]); } console.out.println(" }"); } /** Returns a random integer in the range [1, 100]. */ private int randomInt() { return (int)(Math.random() * 100 + 1); } }