I am testing a new Insertion Sort with Junit5 in Eclipse. I have already written three other tests, which all pass with no errors. What I want to test in test04 is if the time the Insertion Sort takes to sort an already sorted int[] is less than the time it takes to sort an exactly backwards int[].
Here is my code:
@Test
void test04() {
// Let's test if sorting an already sorted array takes less time than sorting a backwards one
int[] sorted = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] backwards = {9, 8, 7, 6, 5, 4, 3, 2, 1};
long start = System.currentTimeMillis();
InsertionSort.Sort(sorted);
long sortedTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
InsertionSort.Sort(backwards);
long backwardsTime = System.currentTimeMillis() - start;
System.out.println(sortedTime);
System.out.println(backwardsTime);
System.out.println(System.currentTimeMillis());
assert(backwardsTime > sortedTime);
}
The problem is that my timing mechanism(using System.currentTimeMillis()) is not working at all! Here is the console output:
0
0
1587044842939
sortedTime and backwardsTime are equal to 0! So test04 is failing because 0 is not greater than 0. Interestingly enough, when I print out System.currentTimeMillis(), it gives me a good normal looking number. I'm wondering what is up? Is this a bug? Am I doing something wrong?
Any help is welcome!