package algorithm.sort.impl;

import algorithm.sort.DefaultSortingTest;
import algorithm.sort.SortingAlgorithm;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

/**
 * Created by Francisco Yllera.
 */
public class MergeSortTest extends DefaultSortingTest {
    private int[] ONE_ELEM_ARR = {2};
    private int[] ONE_ELEM_ARR_2 = {1};
    private int[] TWO_ELEM_EXPECTED_ARR = {1,2};

    private int[] SEVERAL_ELEM_ARR = {4,15,16,50};
    private int[] SEVERAL_ELEM_ARR_2 = {8,23,42,108};
    private int[] SEVERAL_ELEM_EXPECTED_ARR = {4,8,15,16,23,42,50,108};

    private int[] EMPTY_ELEM_ARR = {};

    MergeSort mergeSort;

    public MergeSortTest() {
        mergeSort = new MergeSort();
    }

    @Override
    protected SortingAlgorithm getSortingAlgorithm() {
        return mergeSort;
    }

    @Test
    public void testMergeArraysOfOneElement() {
        int[] mergedArr = new int[ONE_ELEM_ARR.length + ONE_ELEM_ARR_2.length];
        mergeSort.mergeOrderedArrays(ONE_ELEM_ARR, ONE_ELEM_ARR_2, mergedArr);
        assertArrayEquals(mergedArr, TWO_ELEM_EXPECTED_ARR);

        int[] mergedArr2 = new int[ONE_ELEM_ARR.length + ONE_ELEM_ARR_2.length];
        mergeSort.mergeOrderedArrays(ONE_ELEM_ARR_2, ONE_ELEM_ARR, mergedArr2);
        assertArrayEquals(mergedArr2, TWO_ELEM_EXPECTED_ARR);
    }

    @Test
    public void testMergeOrderedElementsArrays() {
        int[] mergedArr = new int[SEVERAL_ELEM_ARR.length + SEVERAL_ELEM_ARR_2.length];
        mergeSort.mergeOrderedArrays(SEVERAL_ELEM_ARR, SEVERAL_ELEM_ARR_2, mergedArr);
        assertArrayEquals(mergedArr, SEVERAL_ELEM_EXPECTED_ARR);

        int[] mergedArr2 = new int[SEVERAL_ELEM_ARR.length + SEVERAL_ELEM_ARR_2.length];
        mergeSort.mergeOrderedArrays(SEVERAL_ELEM_ARR_2, SEVERAL_ELEM_ARR, mergedArr2);
        assertArrayEquals(mergedArr2, SEVERAL_ELEM_EXPECTED_ARR);
    }

    @Test
    public void testMergeOrderedElementsArrayWithEmptyArray() {
        int[] mergedArr = new int[SEVERAL_ELEM_ARR.length + EMPTY_ELEM_ARR.length];
        mergeSort.mergeOrderedArrays(SEVERAL_ELEM_ARR, EMPTY_ELEM_ARR, mergedArr);
        assertArrayEquals(mergedArr, SEVERAL_ELEM_ARR);

        int[] mergedArr2 = new int[EMPTY_ELEM_ARR.length + SEVERAL_ELEM_ARR.length];
        mergeSort.mergeOrderedArrays(EMPTY_ELEM_ARR, SEVERAL_ELEM_ARR, mergedArr2);
        assertArrayEquals(mergedArr2, SEVERAL_ELEM_ARR);
    }
}

@@ -294,28 +294,28 @@
 e int%5B%5D 
-ONE_ELEM_ARR
+oneElemArray
  = %7B2%7D;%0A
@@ -332,29 +332,28 @@
 e int%5B%5D 
-ONE_ELEM_ARR_
+oneElemArray
 2 = %7B1%7D;
@@ -371,37 +371,36 @@
 e int%5B%5D 
-TWO_ELEM_EXPECTED_ARR
+twoElemExpectedArray
  = %7B1,2%7D
@@ -420,32 +420,32 @@
 e int%5B%5D 
-SEVERAL_ELEM_ARR
+severalElemArray
  = %7B4,15
@@ -471,33 +471,32 @@
 e int%5B%5D 
-SEVERAL_ELEM_ARR_
+severalElemArray
 2 = %7B8,2
@@ -528,33 +528,32 @@
 t%5B%5D 
-SEVERAL_ELEM_EXPECTED_ARR
+severalElemExpectedArray
  = %7B
@@ -596,30 +596,30 @@
 e int%5B%5D 
-EMPTY_ELEM_ARR
+emptyElemArray
  = %7B%7D;%0A%0A
@@ -915,43 +915,42 @@
 int%5B
-ONE_ELEM_ARR.length + ONE_ELEM_ARR_
+oneElemArray.length + oneElemArray
 2.le
@@ -997,35 +997,34 @@
 ays(
-ONE_ELEM_ARR, ONE_ELEM_ARR_
+oneElemArray, oneElemArray
 2, m
@@ -1071,37 +1071,36 @@
 gedArr, 
-TWO_ELEM_EXPECTED_ARR
+twoElemExpectedArray
 );%0A%0A    
@@ -1134,43 +1134,42 @@
 int%5B
-ONE_ELEM_ARR.length + ONE_ELEM_ARR_
+oneElemArray.length + oneElemArray
 2.le
@@ -1216,36 +1216,35 @@
 ays(
-ONE_ELEM_ARR_2, ONE_ELEM_ARR
+oneElemArray2, oneElemArray
 , me
@@ -1296,29 +1296,28 @@
 r2, 
-TWO_ELEM_EXPECTED_ARR
+twoElemExpectedArray
 );%0A 
@@ -1421,51 +1421,50 @@
 int%5B
-SEVERAL_ELEM_ARR.length + SEVERAL_ELEM_ARR_
+severalElemArray.length + severalElemArray
 2.le
@@ -1511,43 +1511,42 @@
 ays(
-SEVERAL_ELEM_ARR, SEVERAL_ELEM_ARR_
+severalElemArray, severalElemArray
 2, m
@@ -1597,33 +1597,32 @@
 rr, 
-SEVERAL_ELEM_EXPECTED_ARR
+severalElemExpectedArray
 );%0A%0A
@@ -1660,51 +1660,50 @@
 int%5B
-SEVERAL_ELEM_ARR.length + SEVERAL_ELEM_ARR_
+severalElemArray.length + severalElemArray
 2.le
@@ -1750,44 +1750,43 @@
 ays(
-SEVERAL_ELEM_ARR_2, SEVERAL_ELEM_ARR
+severalElemArray2, severalElemArray
 , me
@@ -1838,33 +1838,32 @@
 r2, 
-SEVERAL_ELEM_EXPECTED_ARR
+severalElemExpectedArray
 );%0A 
@@ -1980,48 +1980,48 @@
 int%5B
-SEVERAL_ELEM_ARR.length + EMPTY_ELEM_ARR
+severalElemArray.length + emptyElemArray
 .len
@@ -2067,40 +2067,40 @@
 ays(
-SEVERAL_ELEM_ARR, EMPTY_ELEM_ARR
+severalElemArray, emptyElemArray
 , me
@@ -2146,32 +2146,32 @@
 gedArr, 
-SEVERAL_ELEM_ARR
+severalElemArray
 );%0A%0A    
@@ -2205,48 +2205,48 @@
 int%5B
-EMPTY_ELEM_ARR.length + SEVERAL_ELEM_ARR
+emptyElemArray.length + severalElemArray
 .len
@@ -2292,40 +2292,40 @@
 ays(
-EMPTY_ELEM_ARR, SEVERAL_ELEM_ARR
+emptyElemArray, severalElemArray
 , me
@@ -2377,24 +2377,24 @@
 r2, 
-SEVERAL_ELEM_ARR
+severalElemArray
 );%0A 

Clean code