mirror of
https://gitlab.uni-marburg.de/langbeid/powersort.git
synced 2025-01-21 19:50:35 +01:00
add sort test
This commit is contained in:
parent
8379570f9a
commit
43dfcd987e
41
app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java
Normal file
41
app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java
Normal file
@ -0,0 +1,41 @@
|
||||
package de.uni_marburg.powersort;
|
||||
|
||||
import org.junit.jupiter.params.converter.SimpleArgumentConverter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class JUnitUtil {
|
||||
/**
|
||||
* https://stackoverflow.com/a/46850299/6334421
|
||||
*/
|
||||
public static class IntArrayConverter extends SimpleArgumentConverter {
|
||||
@Override
|
||||
protected int[] convert(Object source, Class<?> targetType) {
|
||||
if (source instanceof String s && int[].class.isAssignableFrom(targetType)) {
|
||||
if (s.isEmpty()) {
|
||||
return new int[0];
|
||||
}
|
||||
String[] strings = s.split("\\s*\\|\\s*");
|
||||
return Arrays.stream(strings).mapToInt(Integer::valueOf).toArray();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Conversion from " + source.getClass()
|
||||
+ " to " + targetType + " not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class IntegerArrayConverter extends SimpleArgumentConverter {
|
||||
@Override
|
||||
protected Integer[] convert(Object source, Class<?> targetType) {
|
||||
if (source instanceof String s && Integer[].class.isAssignableFrom(targetType)) {
|
||||
if (s.isEmpty()) {
|
||||
return new Integer[0];
|
||||
}
|
||||
String[] strings = s.split("\\s*\\|\\s*");
|
||||
return Arrays.stream(strings).map(Integer::valueOf).toArray(Integer[]::new);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Conversion from " + source.getClass()
|
||||
+ " to " + targetType + " not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package de.uni_marburg.powersort.data;
|
||||
|
||||
import de.uni_marburg.powersort.JUnitUtil;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.converter.ConvertWith;
|
||||
import org.junit.jupiter.params.converter.SimpleArgumentConverter;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -10,26 +10,6 @@ import java.util.Arrays;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AscendingRunsTest {
|
||||
/**
|
||||
* https://stackoverflow.com/a/46850299/6334421
|
||||
*/
|
||||
private static class IntArrayConverter extends SimpleArgumentConverter {
|
||||
@Override
|
||||
protected Object convert(Object source, Class<?> targetType) {
|
||||
if (source instanceof String s && int[].class.isAssignableFrom(targetType)) {
|
||||
if (s.isEmpty()) {
|
||||
return new int[0];
|
||||
}
|
||||
String[] strings = s.split("\\s*\\|\\s*");
|
||||
return Arrays.stream(strings).mapToInt(Integer::valueOf).toArray();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Conversion from " + source.getClass()
|
||||
+ " to " + targetType + " not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"0,1,-1,''",
|
||||
@ -38,7 +18,7 @@ class AscendingRunsTest {
|
||||
"2,2,-2,0|1|-2|-1",
|
||||
"2,4,-2,0|1|2|3|-2|-1|0|1",
|
||||
})
|
||||
void testAscendingRuns1(int numOfRuns, int runLength, int decreaseBetweenRuns, @ConvertWith(IntArrayConverter.class) int[] expected) {
|
||||
void testAscendingRuns1(int numOfRuns, int runLength, int decreaseBetweenRuns, @ConvertWith(JUnitUtil.IntArrayConverter.class) int[] expected) {
|
||||
Integer[] actualIntegers = AscendingRuns.newAscendingRuns(numOfRuns, runLength, decreaseBetweenRuns).getCopy();
|
||||
int[] actual = Arrays.stream(actualIntegers).mapToInt(Integer::valueOf).toArray();
|
||||
assertArrayEquals(expected, actual);
|
||||
|
@ -0,0 +1,26 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
import de.uni_marburg.powersort.JUnitUtil;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.converter.ConvertWith;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
public abstract class AbstractSortTest {
|
||||
SortEnum sortAlg;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"'',''",
|
||||
"'1337','1337'",
|
||||
"'3|2|1','1|2|3'",
|
||||
"'1|1','1|1'",
|
||||
"'2|1','1|2'",
|
||||
"'2|1|2','1|2|2'",
|
||||
})
|
||||
void test1(@ConvertWith(JUnitUtil.IntegerArrayConverter.class) Integer[] array, @ConvertWith(JUnitUtil.IntegerArrayConverter.class) Integer[] expected) {
|
||||
sortAlg.get().sort(array);
|
||||
assertArrayEquals(expected, array);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
public class FinnSortTest extends AbstractSortTest {
|
||||
FinnSortTest() {
|
||||
sortAlg = SortEnum.FIN_SORT;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user