/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.arrays;

import java.util.Random;
import net.ranides.assira.collection.IntComparator;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ArraySortTest {
    
    @Test
    public void testQuickSort_cmp() {
        int[] input3 = rangeR(100,200);
        int[] input4 = rangeR(100,200);
           
        
        
        ArraySort.quickSort(input3, IntComparator.ASC);
        assertArrayEquals(rangeA(100,200), input3);
        
        ArraySort.quickSort(input4, IntComparator.DESC);
        assertArrayEquals(rangeD(100, 200), input4);
    }
    
    @Test
    public void testQuickSort_range() {
        int[] input3 = rangeR(100,200);
        int[] input4 = rangeR(100,200);
        
        ArraySort.quickSort(input3, 0, 100, IntComparator.ASC);
        assertArrayEquals(rangeA(100,200), input3);
        
        ArraySort.quickSort(input4, 0, 100, IntComparator.DESC);
        assertArrayEquals(rangeD(100, 200), input4);
    }
    
    
    @Test
    public void testMergeSort() {
        int[] input1 = rangeR(100,200);
           
        ArraySort.mergeSort(input1);
        assertArrayEquals(rangeA(100,200), input1);
    }
    
    @Test
    public void testMergeSort_range() {
        int[] input1 = rangeR(100,200);
           
        ArraySort.mergeSort(input1, 0, 100);
        assertArrayEquals(rangeA(100,200), input1);
    }
    
    @Test
    public void testMergeSort_cmp() {
        int[] input1 = rangeR(100,200);
        int[] input2 = rangeR(100,200);
           
        ArraySort.mergeSort(input1, IntComparator.ASC);
        assertArrayEquals(rangeA(100,200), input1);
        
        ArraySort.mergeSort(input2, IntComparator.DESC);
        assertArrayEquals(rangeD(100,200), input2);
    }
    
    @Test
    public void testMergeSort_cmp_range() {
        int[] input1 = rangeR(100,200);
        int[] input2 = rangeR(100,200);
           
        ArraySort.mergeSort(input1, 0, 100, IntComparator.ASC);
        assertArrayEquals(rangeA(100,200), input1);
        
        ArraySort.mergeSort(input2, 0, 100, IntComparator.DESC);
        assertArrayEquals(rangeD(100,200), input2);
    }
    
    private static int[] rangeR(int begin, int end) {
        return ArrayUtils.shuffle(new Random(333), rangeA(begin,end));
    }
    
    private static int[] rangeA(int begin, int end) {
        int[] array = new int[end-begin];
        for(int i=0; i<array.length; i++) {
            array[i] = i+begin;
        }
        return array;
    }
    
    private static int[] rangeD(int begin, int end) {
        int[] array = new int[end-begin];
        for(int i=0; i<array.length; i++) {
            array[i] = end-i-1;
        }
        return array;
    }
    
}
