Viewing file: comp352/assignment3/Tester.java | Back to directory listing
Author: Loren Segal | Last modified: February 20 2006 07:00 pm | Download

/**
 * Tester.java<BR>
 * Testing program for either a degree 2 MaxHeap or degree 3 Maxheap,
 * depending on the value of the variable "version" in the main 
 * method. 
 * <P>
 * You should not have to change any of the code in this Tester file
 * except for the "version" variable and the Heap object type. See below.
 * Just follow the instructions for the assignment.
 * <P>
 * Last Update:  Oct. 24, 2005
 * 
 */
 
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
 
public class Tester {
	
    /**
     * This method makes getting string input from the user easier. 
     * It assumes that you have set the Project Properties such
     * that your application is launched as a "Console Application"
     * Usage:<BR>
     * <PRE>
     *     String inputStr = inputString("Please input string: ");
     * </PRE>
     *
     * @param s a string that contains instructions for the user.
     */
    public static String inputString(String s) {
       String aLine = "";
       BufferedReader input = 
	 new BufferedReader(new InputStreamReader(System.in));
       System.out.print(s);
       try {
	 aLine = input.readLine();
	 }
       catch (Exception e) {
	 e.printStackTrace();
	 }
       return aLine;
    }
 
    public static void main(String argv[]) {
 
	long Memory = 0;
	long RealTime = 0;
 
	final int TestSize = 500000;    // Size of heap to test
	
	///////////////////////////////////////////////////////////
	// SET THIS VARIABLE ACCORDINGLY
	///////////////////////////////////////////////////////////
//        int version = 2; // change to 3 for degree 3 min-Heap
	int version = 3; // change to 3 for degree 3 min-Heap
	///////////////////////////////////////////////////////////
 
	int data[] = new int[TestSize];
 
	// Fill an array up with random data
 
	Random prng = new Random();
	for (int i=0; i<TestSize; i++)
	    data[i] = Math.abs(prng.nextInt());
 
	// Start the timer!
 
	Runtime myRuntime = Runtime.getRuntime();
	myRuntime.gc(); // garbage collection
	long startMemory = myRuntime.totalMemory() - myRuntime.freeMemory();
	long startRealTime = System.currentTimeMillis();
	
	Heap ipq;
	if (version == 2) {
	    System.out.println("Testing degree 2 heap");
	    ipq = new Deg2Heap(TestSize);
	} else {
	    System.out.println("Testing degree 3 heap");
	    ///////////////////////////////////////////////////////////
	    // SET THE HEAP TYPE AFTER DEFINING THE CLASS 
	    ///////////////////////////////////////////////////////////
	    // ipq = new Deg3Heap(TestSize); // add line for
					     // degree 3 heap
	    ipq = new Deg2Heap(TestSize); // comment out line
					  // for degree 3 heap
	    ///////////////////////////////////////////////////////////
	}
 
	// Put all the data in the PQ
 
	for (int i=0; i<TestSize; i++)
	    ipq.insert(data[i]);
 
	System.out.println("Finished building heap...");
	System.out.println("Testing MaxHeap order...");
	if (ipq.testHeap())
	    System.out.println("Passed heap order test.");
 
	// Now get the data out from smallest to largest
 
	for (int i=0; i<TestSize; i++) {
	    data[i] = ipq.removeMin();
	}
 
	// Read timer, etc.
	RealTime = (System.currentTimeMillis() - startRealTime);
	myRuntime.gc();
	long nowMemory = myRuntime.totalMemory() - 
			 myRuntime.freeMemory();
	Memory = (nowMemory - startMemory);
 
	 // Testing sortedness of final array.
	boolean sorted = true;
	for (int i=0; i<TestSize-1; i++) {
	    if (data[i] > data[i+1]) {
		sorted = false;
		System.out.print("i: " + i + ": data[i] = " + data[i]);
		System.out.println(": data[i+1] = " + data[i+1]);
	    }
	}
	if (sorted)
	    System.out.println("Final data set is sorted as expected\n");
	else
	    System.out.println("Final data set is NOT sorted as expected\n");
    
	// Diagnostics
 
	DecimalFormat threeDecPlcFormatter = new DecimalFormat("0.###");
	System.out.println("Time used: \n" + 
	       "Degree " + version + ":   " + 
	       threeDecPlcFormatter.format(0.001*RealTime) + 
	       " seconds. \n");
	System.out.println("Memory used: \n" + 
	       "Degree " + version + ":   " + Memory + " bytes. \n");
 
	inputString("Hit any key to finish... "); // needed for J++ 
	// console applications. Otherwise concole disappears...
    } // main
 
} // Tester