Skip to content

Commit 4cbc012

Browse files
fix: resolve checkstyle, dead code, and add test coverage
1 parent 22e62e3 commit 4cbc012

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/main/java/com/thealgorithms/sorts/ConcurrentMergeSort.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
public class ConcurrentMergeSort {
2626

27+
private ConcurrentMergeSort() {
28+
}
29+
2730
/**
2831
* Fallback threshold where the algorithm switches to standard sequential
2932
* Merge Sort to prevent thread-creation overhead from ruining performance.
@@ -75,10 +78,6 @@ public static void sort(int[] array) {
7578
* @param depth the remaining depth for allowing concurrent execution
7679
*/
7780
private static void concurrentMergeSort(int[] array, int[] temp, int left, int right, ThreadPoolExecutor executor, int depth) {
78-
if (left >= right) {
79-
return;
80-
}
81-
8281
int length = right - left + 1;
8382

8483
// Switch to sequential sort if the array is small or we have reached the maximum concurrent depth

src/test/java/com/thealgorithms/sorts/ConcurrentMergeSortTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,28 @@ public void testSingleElementArray() {
8383

8484
assertArrayEquals(expected, array, "Single element array should be handled without errors.");
8585
}
86+
87+
@Test
88+
public void testNullArray() {
89+
int[] array = null;
90+
ConcurrentMergeSort.sort(array);
91+
org.junit.jupiter.api.Assertions.assertNull(array, "Null array should be handled without errors.");
92+
}
93+
94+
@Test
95+
public void testInterruptedException() {
96+
int[] array = new int[20000]; // Large enough to trigger concurrent threads
97+
Thread.currentThread().interrupt();
98+
try {
99+
ConcurrentMergeSort.sort(array);
100+
} catch (RuntimeException e) {
101+
org.junit.jupiter.api.Assertions.assertTrue(
102+
e.getCause() instanceof InterruptedException || e.getCause() instanceof java.util.concurrent.ExecutionException,
103+
"Should catch and wrap InterruptedException or ExecutionException"
104+
);
105+
} finally {
106+
// Clear interrupted status so it doesn't affect subsequent tests
107+
Thread.interrupted();
108+
}
109+
}
86110
}

0 commit comments

Comments
 (0)