/**
* Code by Shin Kondo and Josh Kameraad's solutions, combined by Dr. Cusack.
*/
package ThreadExamples.Problem1;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
public class LongestSeriesTests {
static Result computeLongestSeries(int num, int[] arr, int threshold) {
LongestSeries ls = new LongestSeries(num, arr, 0, arr.length, threshold);
return ForkJoinPool.commonPool().invoke(ls);
}
public static void main(String[] args) {
Random random = new Random();
// Test that it works correctly. (From Shin Kondo, I think)
System.out.println("If this prints nothing between the lines it passes all the tests.\n-----------");
int[] arr = { 2, 17, 17, 8, 17, 17, 17, 0, 17, 1 };
Result r = computeLongestSeries(17, arr, 1);
if (r.numLongest != 3) {
System.out.println("1: Incorrect answer!");
}
int[] arr2 = { 1, 2, 3, 4, 4, 5, 6, 7, 8, 8 };
r = computeLongestSeries(17, arr2, 1);
if (r.numLongest != 0) {
System.out.println("2: Incorrect answer!");
}
int[] arr3 = { 17, 17, 17, 17, 17, 17 };
r = computeLongestSeries(17, arr3, 1);
if (r.numLongest != 6) {
System.out.println("3: Incorrect answer!");
}
int[] arr4 = { 17, 17, 17, 0, 0, 0, 17, 17, 17, 17 };
r = computeLongestSeries(17, arr4, 1);
if (r.numLongest != 4) {
System.out.println("4: Incorrect answer!");
}
int[] arr5 = { 0, 17, 0, 17, 0, 17, 0, 17, 0 };
r = computeLongestSeries(17, arr5, 1);
if (r.numLongest != 1) {
System.out.println("5: Incorrect answer!");
}
int[] arr6 = { 17, 17, 0, 17, 17, 17, 17, 17 };
r = computeLongestSeries(17, arr6, 1);
if (r.numLongest != 5) {
System.out.println("6: Incorrect answer!");
}
// Larger array, answer in the middle.
int[]arr7 = new int[1000];
for(int i=0;i<1000;i++) {
arr7[i]=random.nextInt(10000);
}
for(int i=400;i<600;i++) {
arr7[i]=23;
}
arr7[399]=0;
arr7[600]=0;
// Try it with different cutoffs.
r = computeLongestSeries(23, arr7, 1);
if (r.numLongest != 200) {
System.out.println("7a: Incorrect answer!");
}
r = computeLongestSeries(23, arr7, 10);
if (r.numLongest != 200) {
System.out.println("7b: Incorrect answer!");
}
r = computeLongestSeries(23, arr7, 100);
if (r.numLongest != 200) {
System.out.println("7b: Incorrect answer!");
}
// Larger array, answer on right half.
for(int i=0;i<1000;i++) {
arr7[i]=random.nextInt(10000);
}
for(int i=550;i<600;i++) {
arr7[i]=23;
}
arr7[549]=0;
arr7[600]=0;
// Try it with different cutoffs.
r = computeLongestSeries(23, arr7, 1);
if (r.numLongest != 50) {
System.out.println("7c: Incorrect answer!");
}
r = computeLongestSeries(23, arr7, 10);
if (r.numLongest != 50) {
System.out.println("7d: Incorrect answer!");
}
r = computeLongestSeries(23, arr7, 100);
if (r.numLongest != 50) {
System.out.println("7e: Incorrect answer!");
}
System.out.println("-----------");
// Now we assume it works correctly and we run it on larger
// arrays and see how it performs.
System.out.println("Array Size Cutoff Time (microsecond)");
// Now run it to see the times for various thresholds.
// Tests from Josh Kameraad's solution, with addition by Dr. Cusack.
final int MAX_ARRAY_SIZE = 1000000000, BIGGEST_NUM = 100;
for (int size = 10000; size <= MAX_ARRAY_SIZE; size *= 10) {
// Create the new array and populate it.
int[] array = new int[size];
for (int index = 0; index < array.length; index++) {
array[index] = random.nextInt(BIGGEST_NUM) + 1;
}
for (int i = 1; i <= size; i *= 10) {
long start = System.nanoTime();
Result result = computeLongestSeries(1, array, i);
long finish = System.nanoTime();
System.out.printf("%10d %10d %10d\n", size, i, ((finish - start)/1000));
}
}
}
}