-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHarness.java
More file actions
78 lines (63 loc) · 2.89 KB
/
TestHarness.java
File metadata and controls
78 lines (63 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ie.gmit.dip;
import ie.gmit.dip.algorithms.*;
import java.util.Arrays;
import java.util.List;
//A class to manage the gathering of the data and its output to the console
public class TestHarness {
final int TEST_CYCLES = 10;
//The input sizes to be tested are specified in the list below
private List<Integer> inputSizes = Arrays.asList(100, 1000, 2500, 5000, 10000, 20000, 50000, 100000);
private List<Algorithm> algorithms =
Arrays.asList(
new BubbleSort(), //Call each of the Sorting Algorithm classes
new CountingSort(),
new InsertionSort(),
new QuickSort(),
new SelectionSort()
);
public void runTests() throws Exception {
System.out.println("Algorithm, Array size, Average time"); //Output to console and csv
for (int i = 0; i < inputSizes.size(); i++) {
int inputSize = inputSizes.get(i);
int[] array = randomArray(inputSize); //Put the input size into the random array
for (int j = 0; j < algorithms.size(); j++) {
Algorithm algo = algorithms.get(j); //Apply the algorithm
long totalExecutionTime = 0L;
for (int k = 0; k < TEST_CYCLES; k++) {
int[] testArray = array.clone(); //Clone ensures an exact copy of input array for each algorithm
Timer timer = new Timer();
timer.startTimer(); //Start the timer
int[] sortedArray = algo.sort(testArray); //Sort the array
timer.endTimer(); //End the timer
long elapsedTime = timer.elapsedTime(); //Calculate the elapsed time
totalExecutionTime += elapsedTime;
verifySorted(sortedArray);
}
String result = algo.algorithmName + ", " +
inputSize + ", " +
totalExecutionTime / TEST_CYCLES;
System.out.println(result); //Output the average result
}
}
}
/*
If an error occurs and the array is not sorted an exception will
be thrown and "Array not sorted" sent to the console so the user
is aware of the problem
*/
private void verifySorted(int[] sortedArray) throws Exception {
for (int i = 0; i < sortedArray.length - 1; i++) {
if (sortedArray[i] > sortedArray[i + 1]) {
throw new Exception("Array not sorted");
}
}
}
//Code below returns an array of randomly generated numbers
private int[] randomArray(int n) {
int[] array = new int[n]; //Create an array of size n
for (int i = 0; i < n; i++) { //Loop over the above array
array[i] = (int) (Math.random() * 100); //Math.random returns a random number
}
return array;
}
}