Repository: TheAlgorithms/Java
Pull request #3782 (2022-12-01 12:15:57)

Move common tests for sorting algorithms to the base test class
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


github-actions[bot]: This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/SortUtils.java b/src/main/java/com/thealgorithms/sorts/SortUtils.java
index 83b94c9a..f9f99055 100644
--- a/src/main/java/com/thealgorithms/sorts/SortUtils.java
+++ b/src/main/java/com/thealgorithms/sorts/SortUtils.java
@@ -112,4 +112,11 @@ final class SortUtils {
                 return false;
         return true;
     }
+
+    static <T extends Comparable<T>> boolean isSorted(List<T> list) {
+        for (int i = 1; i < list.size(); i++)
+            if (less(list.get(i), list.get(i - 1)))
+                return false;
+        return true;
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/HeapSortTest.java b/src/test/java/com/thealgorithms/sorts/HeapSortTest.java
index 71a5ec3a..90b2ebd6 100644
--- a/src/test/java/com/thealgorithms/sorts/HeapSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/HeapSortTest.java
@@ -1,95 +1,8 @@
 package com.thealgorithms.sorts;
 
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-public class HeapSortTest {
-    private HeapSort heapSort;
-
-    @BeforeEach
-    void setUp() {
-        heapSort = new HeapSort();
-    }
-
-    @Test
-    void shouldAcceptWhenEmptyArrayIsPassed() {
-        Integer[] array = new Integer[]{};
-        Integer[] expected = new Integer[]{};
-
-        Integer[] sorted = heapSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenSingleValuedArrayIsPassed() {
-        Integer[] array = new Integer[]{2};
-        Integer[] expected = new Integer[]{2};
-
-        Integer[] sorted = heapSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithAllPositiveValuesIsPassed() {
-        Integer[] array = new Integer[]{60, 7, 55, 9, 999, 3};
-        Integer[] expected = new Integer[]{3, 7, 9, 55, 60, 999};
-
-        Integer[] sorted = heapSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithAllNegativeValuesIsPassed() {
-        Integer[] array = new Integer[]{-60, -7, -55, -9, -999, -3};
-        Integer[] expected = new Integer[]{-999, -60, -55, -9, -7, -3};
-
-        Integer[] sorted = heapSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
+public class HeapSortTest extends SortingAlgorithmTest {
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return new HeapSort();
     }
-
-    @Test
-    void shouldAcceptWhenArrayWithRealNumberValuesIsPassed() {
-        Integer[] array = new Integer[]{60, -7, 55, 9, -999, -3};
-        Integer[] expected = new Integer[]{-999, -7, -3, 9, 55, 60};
-
-        Integer[] sorted = heapSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithDuplicateValueIsPassed() {
-        Integer[] array = new Integer[]{60, 7, 55, 55, 999, 3};
-        Integer[] expected = new Integer[]{3, 7, 55, 55, 60, 999};
-
-        Integer[] sorted = heapSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenStringValueArrayIsPassed() {
-        String[] array = {"z", "a", "x", "b", "y"};
-        String[] expected = {"a", "b", "x", "y", "z"};
-
-        String[] sorted = heapSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenRandomArrayIsPassed() {
-        int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
-        Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
-        Double[] sorted = heapSort.sort(array);
-        assertTrue(SortUtils.isSorted(sorted));
-    }
-
 }
diff --git a/src/test/java/com/thealgorithms/sorts/MergeSortTest.java b/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
index 2f127705..af06e360 100644
--- a/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
@@ -1,95 +1,8 @@
 package com.thealgorithms.sorts;
 
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-public class MergeSortTest {
-
-    private MergeSort mergeSort;
-
-    @BeforeEach
-    void setUp() {
-        mergeSort = new MergeSort();
-    }
-
-    @Test
-    void shouldAcceptWhenEmptyArrayIsPassed() {
-        Integer [] array = new Integer[]{};
-        Integer [] expected = new Integer[]{};
-
-        Integer []sorted = mergeSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenSingleValuedArrayIsPassed() {
-        Integer [] array = new Integer[]{2};
-        Integer [] expected = new Integer[]{2};
-
-        Integer [] sorted = mergeSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithAllPositiveValuesIsPassed() {
-        Integer [] array = new Integer[]{60, 7, 55, 9, 999, 3};
-        Integer [] expected = new Integer[]{3, 7, 9, 55, 60, 999};
-
-        Integer [] sorted = mergeSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithAllNegativeValuesIsPassed() {
-        Integer [] array = new Integer[]{-60, -7, -55, -9, -999, -3};
-        Integer [] expected = new Integer[]{-999, -60, -55, -9, -7, -3};
-
-        Integer [] sorted = mergeSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithRealNumberValuesIsPassed() {
-        Integer [] array = new Integer[]{60, -7, 55, 9, -999, -3};
-        Integer [] expected = new Integer[]{-999, -7, -3, 9, 55, 60};
-
-        Integer [] sorted = mergeSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithDuplicateValueIsPassed() {
-        Integer [] array = new Integer[]{60, 7, 55, 55, 999, 3};
-        Integer [] expected = new Integer[]{3, 7, 55, 55, 60, 999};
-
-        Integer [] sorted = mergeSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenStringValueArrayIsPassed() {
-        String[] array = {"z", "a", "x", "b", "y"};
-        String[] expected = {"a", "b", "x", "y", "z"};
-
-        String[] sorted = mergeSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenRandomArrayIsPassed() {
-        int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
-        Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
-        Double[] sorted = mergeSort.sort(array);
-        assertTrue(SortUtils.isSorted(sorted));
+public class MergeSortTest extends SortingAlgorithmTest {
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return new MergeSort();
     }
 }
diff --git a/src/test/java/com/thealgorithms/sorts/QuickSortTest.java b/src/test/java/com/thealgorithms/sorts/QuickSortTest.java
index 6d66f51e..fca57626 100644
--- a/src/test/java/com/thealgorithms/sorts/QuickSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/QuickSortTest.java
@@ -1,62 +1,12 @@
 package com.thealgorithms.sorts;
 
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-
-import org.junit.jupiter.api.Test;
-
 /**
  * @author Akshay Dubey (https://github.com/itsAkshayDubey)
  * @see QuickSort
  */
-class QuickSortTest {
-
-    private QuickSort quickSort = new QuickSort();
-
-    @Test
-    void quickSortEmptyArrayShouldPass() {
-        Integer[] array = {};
-        Integer[] sorted = quickSort.sort(array);
-        Integer[] expected = {};
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void quickSortSingleValueArrayShouldPass() {
-        Integer[] array = { 7 };
-        Integer[] sorted = quickSort.sort(array);
-        Integer[] expected = { 7 };
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void quickSortWithIntegerArrayShouldPass() {
-        Integer[] array = { 49, 4, 36, 9, 144, 1 };
-        Integer[] sorted = quickSort.sort(array);
-        Integer[] expected = { 1, 4, 9, 36, 49, 144 };
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void quickSortForArrayWithNegativeValuesShouldPass() {
-        Integer[] array = { 49, -36, -144, -49, 1, 9 };
-        Integer[] sorted = quickSort.sort(array);
-        Integer[] expected = { -144, -49, -36, 1, 9, 49 };
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void quickSortForArrayWithDuplicateValuesShouldPass() {
-        Integer[] array = { 36, 1, 49, 1, 4, 9 };
-        Integer[] sorted = quickSort.sort(array);
-        Integer[] expected = { 1, 1, 4, 9, 36, 49 };
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void quickSortWithStringArrayShouldPass() {
-        String[] array = { "c", "a", "e", "b", "d" };
-        String[] sorted = quickSort.sort(array);
-        String[] expected = { "a", "b", "c", "d", "e" };
-        assertArrayEquals(expected, sorted);
+class QuickSortTest extends SortingAlgorithmTest {
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return new QuickSort();
     }
 }
diff --git a/src/test/java/com/thealgorithms/sorts/SortUtilsTest.java b/src/test/java/com/thealgorithms/sorts/SortUtilsTest.java
index bb558727..249c251d 100644
--- a/src/test/java/com/thealgorithms/sorts/SortUtilsTest.java
+++ b/src/test/java/com/thealgorithms/sorts/SortUtilsTest.java
@@ -2,6 +2,8 @@ package com.thealgorithms.sorts;
 
 import org.junit.jupiter.api.Test;
 
+import java.util.List;
+
 import static org.junit.jupiter.api.Assertions.*;
 
 class SortUtilsTest {
@@ -19,7 +21,7 @@ class SortUtilsTest {
     }
 
     @Test
-    void isSortedTrue() {
+    void isSortedArrayTrue() {
         Integer[] array = {1, 1, 2, 3, 5, 8, 11};
         assertTrue(SortUtils.isSorted(array));
 
@@ -31,7 +33,7 @@ class SortUtilsTest {
     }
 
     @Test
-    void isSortedFalse() {
+    void isSortedArrayFalse() {
         Double[] array = {1.0, 3.0, -0.15};
         assertFalse(SortUtils.isSorted(array));
 
@@ -41,4 +43,28 @@ class SortUtilsTest {
         Integer[] array3 = {5, 4, 3, 2, 1};
         assertFalse(SortUtils.isSorted(array3));
     }
+
+    @Test
+    void isSortedListTrue() {
+        List<Integer> list = List.of(1, 1, 2, 3, 5, 8, 11);
+        assertTrue(SortUtils.isSorted(list));
+
+        List<Integer> identicalList = List.of(1, 1, 1, 1, 1);
+        assertTrue(SortUtils.isSorted(identicalList));
+
+        List<Double> doubles = List.of(-15.123, -15.111, 0.0, 0.12, 0.15);
+        assertTrue(SortUtils.isSorted(doubles));
+    }
+
+    @Test
+    void isSortedListFalse() {
+        List<Double> list = List.of(1.0, 3.0, -0.15);
+        assertFalse(SortUtils.isSorted(list));
+
+        List<Integer> array2 = List.of(14, 15, 16, 1);
+        assertFalse(SortUtils.isSorted(array2));
+
+        List<Integer> array3 = List.of(5, 4, 3, 2, 1);
+        assertFalse(SortUtils.isSorted(array3));
+    }
 }
diff --git a/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java b/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java
new file mode 100644
index 00000000..d7230f6d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java
@@ -0,0 +1,171 @@
+package com.thealgorithms.sorts;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public abstract class SortingAlgorithmTest {
+    abstract SortAlgorithm getSortAlgorithm();
+
+    @Test
+    void shouldAcceptWhenEmptyArrayIsPassed() {
+        Integer[] array = new Integer[]{};
+        Integer[] expected = new Integer[]{};
+
+        Integer[] sorted = getSortAlgorithm().sort(array);
+
+        assertArrayEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenEmptyListIsPassed() {
+        List<Integer> list = new ArrayList<>();
+        List<Integer> expected = new ArrayList<>();
+
+        List<Integer> sorted = getSortAlgorithm().sort(list);
+
+        assertIterableEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenSingleValuedArrayIsPassed() {
+        Integer[] array = new Integer[]{2};
+        Integer[] expected = new Integer[]{2};
+
+        Integer[] sorted = getSortAlgorithm().sort(array);
+
+        assertArrayEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenSingleValuedListIsPassed() {
+        List<Integer> list = List.of(2);
+        List<Integer> expected = List.of(2);
+
+        List<Integer> sorted = getSortAlgorithm().sort(list);
+
+        assertIterableEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenListWithAllPositiveValuesIsPassed() {
+        Integer[] array = new Integer[]{60, 7, 55, 9, 999, 3};
+        Integer[] expected = new Integer[]{3, 7, 9, 55, 60, 999};
+
+        Integer[] sorted = getSortAlgorithm().sort(array);
+
+        assertArrayEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenArrayWithAllPositiveValuesIsPassed() {
+        List<Integer> list = List.of(60, 7, 55, 9, 999, 3);
+        List<Integer> expected = List.of(3, 7, 9, 55, 60, 999);
+
+        List<Integer> sorted = getSortAlgorithm().sort(list);
+
+        assertIterableEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenArrayWithAllNegativeValuesIsPassed() {
+        Integer[] array = new Integer[]{-60, -7, -55, -9, -999, -3};
+        Integer[] expected = new Integer[]{-999, -60, -55, -9, -7, -3};
+
+        Integer[] sorted = getSortAlgorithm().sort(array);
+
+        assertArrayEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenListWithAllNegativeValuesIsPassed() {
+        List<Integer> list = List.of(-60, -7, -55, -9, -999, -3);
+        List<Integer> expected = List.of(-999, -60, -55, -9, -7, -3);
+
+        List<Integer> sorted = getSortAlgorithm().sort(list);
+
+        assertIterableEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenArrayWithRealNumberValuesIsPassed() {
+        Integer[] array = new Integer[]{60, -7, 55, 9, -999, -3};
+        Integer[] expected = new Integer[]{-999, -7, -3, 9, 55, 60};
+
+        Integer[] sorted = getSortAlgorithm().sort(array);
+
+        assertArrayEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenListWithRealNumberValuesIsPassed() {
+        List<Integer> list = List.of(60, -7, 55, 9, -999, -3);
+        List<Integer> expected = List.of(-999, -7, -3, 9, 55, 60);
+
+        List<Integer> sorted = getSortAlgorithm().sort(list);
+
+        assertIterableEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenArrayWithDuplicateValueIsPassed() {
+        Integer[] array = new Integer[]{60, 7, 55, 55, 999, 3};
+        Integer[] expected = new Integer[]{3, 7, 55, 55, 60, 999};
+
+        Integer[] sorted = getSortAlgorithm().sort(array);
+
+        assertArrayEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenListWithDuplicateValueIsPassed() {
+        List<Integer> list = List.of(60, 7, 55, 55, 999, 3);
+        List<Integer> expected = List.of(3, 7, 55, 55, 60, 999);
+
+        List<Integer> sorted = getSortAlgorithm().sort(list);
+
+        assertIterableEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenStringValueArrayIsPassed() {
+        String[] array = {"z", "a", "x", "b", "y"};
+        String[] expected = {"a", "b", "x", "y", "z"};
+
+        String[] sorted = getSortAlgorithm().sort(array);
+
+        assertArrayEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenStringValueListIsPassed() {
+        List<String> list = List.of("z", "a", "x", "b", "y");
+        List<String> expected = List.of("a", "b", "x", "y", "z");
+
+        List<String> sorted = getSortAlgorithm().sort(list);
+
+        assertIterableEquals(expected, sorted);
+    }
+
+    @Test
+    void shouldAcceptWhenRandomArrayIsPassed() {
+        int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
+        Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
+        Double[] sorted = getSortAlgorithm().sort(array);
+        assertTrue(SortUtils.isSorted(sorted));
+    }
+
+    @Test
+    void shouldAcceptWhenRandomListIsPassed() {
+        int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
+        Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
+        List<Double> list = List.of(array);
+        List<Double> sorted = getSortAlgorithm().sort(list);
+        assertTrue(SortUtils.isSorted(sorted));
+    }
+}
diff --git a/src/test/java/com/thealgorithms/sorts/TimSortTest.java b/src/test/java/com/thealgorithms/sorts/TimSortTest.java
index b319aa23..85df96f5 100644
--- a/src/test/java/com/thealgorithms/sorts/TimSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/TimSortTest.java
@@ -5,91 +5,9 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.*;
 
-class TimSortTest {
-
-    private TimSort timSort;
-
-    @BeforeEach
-    void setUp() {
-        timSort = new TimSort();
+class TimSortTest extends SortingAlgorithmTest {
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return new TimSort();
     }
-
-    @Test
-    void shouldAcceptWhenEmptyArrayIsPassed() {
-        Integer [] array = new Integer[]{};
-        Integer [] expected = new Integer[]{};
-
-        Integer []sorted = timSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenSingleValuedArrayIsPassed() {
-        Integer [] array = new Integer[]{2};
-        Integer [] expected = new Integer[]{2};
-
-        Integer [] sorted = timSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithAllPositiveValuesIsPassed() {
-        Integer [] array = new Integer[]{60, 7, 55, 9, 999, 3};
-        Integer [] expected = new Integer[]{3, 7, 9, 55, 60, 999};
-
-        Integer [] sorted = timSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithAllNegativeValuesIsPassed() {
-        Integer [] array = new Integer[]{-60, -7, -55, -9, -999, -3};
-        Integer [] expected = new Integer[]{-999, -60, -55, -9, -7, -3};
-
-        Integer [] sorted = timSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithRealNumberValuesIsPassed() {
-        Integer [] array = new Integer[]{60, -7, 55, 9, -999, -3};
-        Integer [] expected = new Integer[]{-999, -7, -3, 9, 55, 60};
-
-        Integer [] sorted = timSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenArrayWithDuplicateValueIsPassed() {
-        Integer [] array = new Integer[]{60, 7, 55, 55, 999, 3};
-        Integer [] expected = new Integer[]{3, 7, 55, 55, 60, 999};
-
-        Integer [] sorted = timSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenStringValueArrayIsPassed() {
-        String[] array = {"z", "a", "x", "b", "y"};
-        String[] expected = {"a", "b", "x", "y", "z"};
-
-        String[] sorted = timSort.sort(array);
-
-        assertArrayEquals(expected, sorted);
-    }
-
-    @Test
-    void shouldAcceptWhenRandomArrayIsPassed() {
-        int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
-        Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
-        Double[] sorted = timSort.sort(array);
-        assertTrue(SortUtils.isSorted(sorted));
-    }
-
 }