Move common tests for sorting algorithms to the base test class (#3782)

* bug fix for CircularBuffer + refactoring + add unit tests

* change Insertion sort to classical implementation + add isSorted function to SortUtils + add SortUtilsRandomGenerator for generating random values and arrays

* little fix

* move all common tests to SortingAlgorithmTest and utilize them

Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
Narek Karapetian 2023-01-01 06:50:56 -08:00 committed by GitHub
parent ce55420418
commit 1eedaeb073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 222 additions and 324 deletions

View File

@ -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;
}
}

View File

@ -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();
public class HeapSortTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return 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);
}
@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));
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}