mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
refactor: SelectionSort
like classes and their tests (#5265)
* Refactor: Adding test common approach, adding javadocs, renaming variables * Refactor: Fix failed build, when generated test case for recursion is too big. To avoid stackoverflow * Checkstyle: Adding newline to end of class * refactor: simplify assign minIndex in recursiveSelectionSort, and improving SelectionSort * checkstyle: adding newline to SelectionSort --------- Co-authored-by: Alex Klymenko <alx@alx.com> Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -5,4 +5,8 @@ public class SelectionSortRecursiveTest extends SortingAlgorithmTest {
|
||||
SortAlgorithm getSortAlgorithm() {
|
||||
return new SelectionSortRecursive();
|
||||
}
|
||||
|
||||
protected int getGeneratedArraySize() {
|
||||
return 5000;
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +1,8 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SelectionSortTest {
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
void integerArrTest() {
|
||||
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
|
||||
SelectionSort selectionSort = new SelectionSort();
|
||||
|
||||
assertArrayEquals(new Integer[] {1, 4, 6, 9, 12, 23, 54, 78, 231}, selectionSort.sort(arr));
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
void stringArrTest() {
|
||||
String[] arr = {"c", "a", "e", "b", "d"};
|
||||
SelectionSort selectionSort = new SelectionSort();
|
||||
|
||||
assertArrayEquals(new String[] {"a", "b", "c", "d", "e"}, selectionSort.sort(arr));
|
||||
}
|
||||
|
||||
@Test
|
||||
// invalid test case
|
||||
void emptyArrTest() {
|
||||
Integer[] arr = {};
|
||||
SelectionSort selectionSort = new SelectionSort();
|
||||
|
||||
assertArrayEquals(new Integer[] {}, selectionSort.sort(arr));
|
||||
class SelectionSortTest extends SortingAlgorithmTest {
|
||||
@Override
|
||||
SortAlgorithm getSortAlgorithm() {
|
||||
return new SelectionSort();
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,10 @@ import org.junit.jupiter.api.Test;
|
||||
public abstract class SortingAlgorithmTest {
|
||||
abstract SortAlgorithm getSortAlgorithm();
|
||||
|
||||
protected int getGeneratedArraySize() {
|
||||
return 10_000;
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAcceptWhenEmptyArrayIsPassed() {
|
||||
Integer[] array = new Integer[] {};
|
||||
@ -153,7 +157,7 @@ public abstract class SortingAlgorithmTest {
|
||||
|
||||
@Test
|
||||
void shouldAcceptWhenRandomArrayIsPassed() {
|
||||
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
|
||||
int randomSize = SortUtilsRandomGenerator.generateInt(getGeneratedArraySize());
|
||||
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
|
||||
Double[] sorted = getSortAlgorithm().sort(array);
|
||||
assertTrue(SortUtils.isSorted(sorted));
|
||||
@ -161,7 +165,7 @@ public abstract class SortingAlgorithmTest {
|
||||
|
||||
@Test
|
||||
void shouldAcceptWhenRandomListIsPassed() {
|
||||
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
|
||||
int randomSize = SortUtilsRandomGenerator.generateInt(getGeneratedArraySize());
|
||||
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
|
||||
List<Double> list = List.of(array);
|
||||
List<Double> sorted = getSortAlgorithm().sort(list);
|
||||
|
Reference in New Issue
Block a user