From d863cb62af53934f097895934a002ada9c30fe45 Mon Sep 17 00:00:00 2001 From: alxkm Date: Wed, 16 Jul 2025 21:37:44 +0200 Subject: [PATCH] testing: improving test coverage CountingInversionsTest --- .../CountingInversionsTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/java/com/thealgorithms/divideandconquer/CountingInversionsTest.java b/src/test/java/com/thealgorithms/divideandconquer/CountingInversionsTest.java index d12614d6fd06..f8356a87eb31 100644 --- a/src/test/java/com/thealgorithms/divideandconquer/CountingInversionsTest.java +++ b/src/test/java/com/thealgorithms/divideandconquer/CountingInversionsTest.java @@ -29,4 +29,35 @@ public void testAllInversions() { int[] arr = {5, 4, 3, 2, 1}; assertEquals(10, CountingInversions.countInversions(arr)); } + + @Test + public void testEmptyArray() { + int[] arr = {}; + assertEquals(0, CountingInversions.countInversions(arr)); + } + + @Test + public void testArrayWithDuplicates() { + int[] arr = {1, 3, 2, 3, 1}; + // Inversions: (3,2), (3,1), (3,1), (2,1) + assertEquals(4, CountingInversions.countInversions(arr)); + } + + @Test + public void testLargeArray() { + int n = 1000; + int[] arr = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = n - i; // descending order -> max inversions = n*(n-1)/2 + } + int expected = n * (n - 1) / 2; + assertEquals(expected, CountingInversions.countInversions(arr)); + } + + @Test + public void testArrayWithAllSameElements() { + int[] arr = {7, 7, 7, 7}; + // No inversions since all elements are equal + assertEquals(0, CountingInversions.countInversions(arr)); + } }