Skip to content

Commit fc477ee

Browse files
testing: improving test coverage CountingInversionsTest (TheAlgorithms#6393)
testing: improving test coverage CountingInversionsTest Co-authored-by: Deniz Altunkapan <[email protected]>
1 parent 44c572b commit fc477ee

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/test/java/com/thealgorithms/divideandconquer/CountingInversionsTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,35 @@ public void testAllInversions() {
2929
int[] arr = {5, 4, 3, 2, 1};
3030
assertEquals(10, CountingInversions.countInversions(arr));
3131
}
32+
33+
@Test
34+
public void testEmptyArray() {
35+
int[] arr = {};
36+
assertEquals(0, CountingInversions.countInversions(arr));
37+
}
38+
39+
@Test
40+
public void testArrayWithDuplicates() {
41+
int[] arr = {1, 3, 2, 3, 1};
42+
// Inversions: (3,2), (3,1), (3,1), (2,1)
43+
assertEquals(4, CountingInversions.countInversions(arr));
44+
}
45+
46+
@Test
47+
public void testLargeArray() {
48+
int n = 1000;
49+
int[] arr = new int[n];
50+
for (int i = 0; i < n; i++) {
51+
arr[i] = n - i; // descending order -> max inversions = n*(n-1)/2
52+
}
53+
int expected = n * (n - 1) / 2;
54+
assertEquals(expected, CountingInversions.countInversions(arr));
55+
}
56+
57+
@Test
58+
public void testArrayWithAllSameElements() {
59+
int[] arr = {7, 7, 7, 7};
60+
// No inversions since all elements are equal
61+
assertEquals(0, CountingInversions.countInversions(arr));
62+
}
3263
}

0 commit comments

Comments
 (0)