package tests;

import static org.junit.Assert.assertEquals;

import java.util.Random;

import jsr166y.ForkJoinPool;
import maxInParallel.MaxWithFJ;
import maxInParallel.SelectMaxProblem;
import mergeSortParallel.MergeSort;

import org.junit.Test;

public class MergeSortTests {
	private final MergeSort mergeSortProblem;
//    private final int threshold;
    private final int nThreads;
    private final int[] number = new int[500000];
	
	public MergeSortTests(){
		
		
		Random randomGenerator = new Random();
		
		for (int idx = 0; idx < 500000; idx++)
			number[idx] = randomGenerator.nextInt(1000000);

		this.mergeSortProblem = new MergeSort(number, 0, number.length-1);
        this.nThreads = 4;

	}
	
	@Test
	public void parallelTest(){
	    
		ForkJoinPool fjPool = new ForkJoinPool(nThreads);
		
		long startTime = System.currentTimeMillis();
		
        fjPool.invoke(mergeSortProblem);
        
        long endTime = System.currentTimeMillis();
		long elapsed = endTime - startTime;
        
        int[] result = mergeSortProblem.getResult();
        //System.out.println("The result of the execution is:{"+Character.LINE_SEPARATOR);
        //for(int i=0; i<result.length; i++)
        //	System.out.println(result[i]+Character.LINE_SEPARATOR);
        

		System.err.println("The PARALLEL execution time is: " + elapsed +"ms (threshold: 50000 threads: "+nThreads+")");

        
        for(int i=0; i<result.length-1; i++)
        	assert(result[i]<result[i+1]);

	}
}
