add sort test

This commit is contained in:
Daniel Langbein 2024-12-16 21:07:18 +00:00
parent 8379570f9a
commit 43dfcd987e
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
4 changed files with 76 additions and 22 deletions

View 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.");
}
}
}
}

View File

@ -1,8 +1,8 @@
package de.uni_marburg.powersort.data; package de.uni_marburg.powersort.data;
import de.uni_marburg.powersort.JUnitUtil;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith; import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.converter.SimpleArgumentConverter;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import java.util.Arrays; import java.util.Arrays;
@ -10,26 +10,6 @@ import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class AscendingRunsTest { 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 @ParameterizedTest
@CsvSource({ @CsvSource({
"0,1,-1,''", "0,1,-1,''",
@ -38,7 +18,7 @@ class AscendingRunsTest {
"2,2,-2,0|1|-2|-1", "2,2,-2,0|1|-2|-1",
"2,4,-2,0|1|2|3|-2|-1|0|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(); Integer[] actualIntegers = AscendingRuns.newAscendingRuns(numOfRuns, runLength, decreaseBetweenRuns).getCopy();
int[] actual = Arrays.stream(actualIntegers).mapToInt(Integer::valueOf).toArray(); int[] actual = Arrays.stream(actualIntegers).mapToInt(Integer::valueOf).toArray();
assertArrayEquals(expected, actual); assertArrayEquals(expected, actual);

View File

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

View File

@ -0,0 +1,7 @@
package de.uni_marburg.powersort.sort;
public class FinnSortTest extends AbstractSortTest {
FinnSortTest() {
sortAlg = SortEnum.FIN_SORT;
}
}