I have a mergesort function that I am testing with JUnit. Below is one of my test cases:
@Test
public void MergeSortTest002()
{
long seed = System.currentTimeMillis();
Random rng = new Random(seed);
Integer[] TestArray = new Integer[1000];
int MAX_VALUE = Integer.MAX_VALUE;
for(int i=0; i<1000; i++) {
Integer newNum = rng.nextInt(MAX_VALUE/2) - MAX_VALUE;
TestArray[i] = newNum;
}
Integer[] correctArray = TestArray;
Arrays.sort(correctArray);
MergeSort.mergeSort(TestArray);
Assert.assertArrayEquals(correctArray,TestArray);
}
The strange thing is, even if I comment out the line where I call my function mergeSort, the test still passes.
I realized that one of two things happens: either it assertArrayEqualsdoes not care about the order of the elements (unlikely), when I copy TestArrayto correctArray, it copies by reference and thus calls Arrays.sorton correctArrayalso sorts TestArray.
Can anyone confirm which of the two is happening and what should be the solution? Is there one Assertthat preserves order in memory, or is there a way to copy arrays by values rather than a link without writing an explicit loop of the loop?