From 43dfcd987eb46cb7a3ed6135d75edaad93a72068 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Mon, 16 Dec 2024 21:07:18 +0000 Subject: [PATCH] add sort test --- .../de/uni_marburg/powersort/JUnitUtil.java | 41 +++++++++++++++++++ .../powersort/data/AscendingRunsTest.java | 24 +---------- .../powersort/sort/AbstractSortTest.java | 26 ++++++++++++ .../powersort/sort/FinnSortTest.java | 7 ++++ 4 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java create mode 100644 app/src/test/java/de/uni_marburg/powersort/sort/AbstractSortTest.java create mode 100644 app/src/test/java/de/uni_marburg/powersort/sort/FinnSortTest.java diff --git a/app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java b/app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java new file mode 100644 index 0000000..372b152 --- /dev/null +++ b/app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java @@ -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."); + } + } + } +} diff --git a/app/src/test/java/de/uni_marburg/powersort/data/AscendingRunsTest.java b/app/src/test/java/de/uni_marburg/powersort/data/AscendingRunsTest.java index 69f9b15..36e78aa 100644 --- a/app/src/test/java/de/uni_marburg/powersort/data/AscendingRunsTest.java +++ b/app/src/test/java/de/uni_marburg/powersort/data/AscendingRunsTest.java @@ -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); diff --git a/app/src/test/java/de/uni_marburg/powersort/sort/AbstractSortTest.java b/app/src/test/java/de/uni_marburg/powersort/sort/AbstractSortTest.java new file mode 100644 index 0000000..7fde10e --- /dev/null +++ b/app/src/test/java/de/uni_marburg/powersort/sort/AbstractSortTest.java @@ -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); + } +} diff --git a/app/src/test/java/de/uni_marburg/powersort/sort/FinnSortTest.java b/app/src/test/java/de/uni_marburg/powersort/sort/FinnSortTest.java new file mode 100644 index 0000000..17578ac --- /dev/null +++ b/app/src/test/java/de/uni_marburg/powersort/sort/FinnSortTest.java @@ -0,0 +1,7 @@ +package de.uni_marburg.powersort.sort; + +public class FinnSortTest extends AbstractSortTest { + FinnSortTest() { + sortAlg = SortEnum.FIN_SORT; + } +}