292 lines
12 KiB
Java
292 lines
12 KiB
Java
|
|
/**
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2021 Exercism
|
|
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class ListOpsTest {
|
|
|
|
private static final int MAX_POINTS = 100;
|
|
private static final int TEST_CASES = 22;
|
|
private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES;
|
|
private static final int FAILURE_POINTS = 0;
|
|
|
|
private final java.util.List<Integer> testPoints = new java.util.ArrayList<>();
|
|
|
|
@Test(name = "empty lists")
|
|
public void testAppendingEmptyLists() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.append(List.of(), List.of()).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "list to empty list")
|
|
public void testAppendingListToEmptyList() {
|
|
runTest(() -> {
|
|
assertListEquals(List.of('1', '2', '3', '4'),
|
|
ListOps.append(List.of(), List.of('1', '2', '3', '4')));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list to list")
|
|
public void testAppendingEmptyListToList() {
|
|
runTest(() -> {
|
|
assertListEquals(List.of('1', '2', '3', '4'),
|
|
ListOps.append(List.of('1', '2', '3', '4'), List.of()));
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty lists")
|
|
public void testAppendingNonEmptyLists() {
|
|
runTest(() -> {
|
|
assertListEquals(List.of("1", "2", "2", "3", "4", "5"),
|
|
ListOps.append(List.of("1", "2"), List.of("2", "3", "4", "5")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (concat)")
|
|
public void testConcatEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.concat(List.of()).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "list of lists")
|
|
public void testConcatListOfLists() {
|
|
runTest(() -> {
|
|
List<List<Character>> listOfLists = List.of(
|
|
List.of('1', '2'),
|
|
List.of('3'),
|
|
List.of(),
|
|
List.of('4', '5', '6'));
|
|
assertListEquals(List.of('1', '2', '3', '4', '5', '6'), ListOps.concat(listOfLists));
|
|
});
|
|
}
|
|
|
|
@Test(name = "list of nested lists")
|
|
public void testConcatListOfNestedLists() {
|
|
runTest(() -> {
|
|
List<List<List<Character>>> listOfNestedLists = List.of(
|
|
List.of(
|
|
List.of('1'),
|
|
List.of('2')),
|
|
List.of(
|
|
List.of('3')),
|
|
List.of(
|
|
List.of()),
|
|
List.of(
|
|
List.of('4', '5', '6')));
|
|
assertListEquals(List.of(List.of('1'), List.of('2'), List.of('3'), List.of(),
|
|
List.of('4', '5', '6')), ListOps.concat(listOfNestedLists));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (filter)")
|
|
public void testFilteringEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.filter(List.<Integer>of(), integer -> integer % 2 == 1).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (filter)")
|
|
public void testFilteringNonEmptyList() {
|
|
runTest(() -> {
|
|
assertListEquals(List.of(1, 3, 5),
|
|
ListOps.filter(List.of(1, 2, 3, 5), integer -> integer % 2 == 1));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (size)")
|
|
public void testSizeOfEmptyList() {
|
|
runTest(() -> {
|
|
assertIntEquals(0, ListOps.size(List.of()));
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (size)")
|
|
public void testSizeOfNonEmptyList() {
|
|
runTest(() -> {
|
|
assertIntEquals(4, ListOps.size(List.of("one", "two", "three", "four")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (map)")
|
|
public void testTransformingEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.map(List.<Integer>of(), integer -> integer + 1).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (map)")
|
|
public void testTransformingNonEmptyList() {
|
|
runTest(() -> {
|
|
assertListEquals(List.of(2, 4, 6, 8), ListOps.map(List.of(1, 3, 5, 7), integer -> integer + 1));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (foldLeft)")
|
|
public void testFoldLeftEmptyList() {
|
|
runTest(() -> {
|
|
assertDoubleEquals(2.0,
|
|
ListOps.foldLeft(
|
|
List.<Double>of(),
|
|
2.0,
|
|
(acc, el) -> el * acc));
|
|
});
|
|
}
|
|
|
|
@Test(name = "direction independent function applied to non-empty list (foldLeft)")
|
|
public void testFoldLeftDirectionIndependentFunctionAppliedToNonEmptyList() {
|
|
runTest(() -> {
|
|
assertIntEquals(15,
|
|
ListOps.foldLeft(
|
|
List.of(1, 2, 3, 4),
|
|
5,
|
|
(acc, el) -> el + acc));
|
|
});
|
|
}
|
|
|
|
@Test(name = "direction dependent function applied to non-empty list (foldLeft)")
|
|
public void testFoldLeftDirectionDependentFunctionAppliedToNonEmptyList() {
|
|
runTest(() -> {
|
|
assertDoubleEquals(64.0,
|
|
ListOps.foldLeft(
|
|
List.of(1.0, 2.0, 3.0, 4.0),
|
|
24.0,
|
|
(acc, el) -> el / acc));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (foldRight)")
|
|
public void testFoldRightEmptyList() {
|
|
runTest(() -> {
|
|
assertDoubleEquals(2.0,
|
|
ListOps.foldRight(
|
|
List.<Double>of(),
|
|
2.0,
|
|
(el, acc) -> acc * el));
|
|
});
|
|
}
|
|
|
|
@Test(name = "direction independent function applied to non-empty list (foldRight)")
|
|
public void testFoldRightDirectionIndependentFunctionAppliedToNonEmptyList() {
|
|
runTest(() -> {
|
|
assertIntEquals(15,
|
|
ListOps.foldRight(
|
|
List.of(1, 2, 3, 4),
|
|
5,
|
|
(el, acc) -> acc + el));
|
|
});
|
|
}
|
|
|
|
@Test(name = "direction dependent function applied to non-empty list (foldRight)")
|
|
public void testFoldRightDirectionDependentFunctionAppliedToNonEmptyList() {
|
|
runTest(() -> {
|
|
assertDoubleEquals(9.0,
|
|
ListOps.foldRight(
|
|
List.of(1.0, 2.0, 3.0, 4.0),
|
|
24.0,
|
|
(el, acc) -> el / acc));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (reverse)")
|
|
public void testReversingEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.reverse(List.of()).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (reverse)")
|
|
public void testReversingNonEmptyList() {
|
|
runTest(() -> {
|
|
assertListEquals(List.of('7', '5', '3', '1'), ListOps.reverse(List.of('1', '3', '5', '7')));
|
|
});
|
|
}
|
|
|
|
@Test(name = "list of lists is not flattened (reverse)")
|
|
public void testReversingListOfListIsNotFlattened() {
|
|
runTest(() -> {
|
|
List<List<Character>> listOfLists = List.of(
|
|
List.of('1', '2'),
|
|
List.of('3'),
|
|
List.of(),
|
|
List.of('4', '5', '6'));
|
|
|
|
assertListEquals(
|
|
List.of(
|
|
List.of('4', '5', '6'),
|
|
List.of(),
|
|
List.of('3'),
|
|
List.of('1', '2')),
|
|
ListOps.reverse(listOfLists));
|
|
});
|
|
}
|
|
|
|
private void runTest(Runnable body) {
|
|
try {
|
|
body.run();
|
|
testPoints.add(SUCCESS_POINTS);
|
|
} catch (Exception | AssertionError t) {
|
|
TracingFramework.printError(t.getMessage());
|
|
testPoints.add(FAILURE_POINTS);
|
|
}
|
|
}
|
|
|
|
private void assertListEquals(List<?> expected, List<?> actual) {
|
|
if (!expected.equals(actual)) {
|
|
throw new AssertionError("Expected " + expected + " but got " + actual);
|
|
}
|
|
}
|
|
|
|
private void assertIntEquals(int expected, int actual) {
|
|
if (expected != actual) {
|
|
throw new AssertionError("Expected " + expected + " but got " + actual);
|
|
}
|
|
}
|
|
|
|
private void assertDoubleEquals(double expected, double actual) {
|
|
if (Double.compare(expected, actual) != 0) {
|
|
throw new AssertionError("Expected " + expected + " but got " + actual);
|
|
}
|
|
}
|
|
|
|
private void assertTrue(boolean condition) {
|
|
if (!condition) {
|
|
throw new AssertionError("Expected true but got false");
|
|
}
|
|
}
|
|
|
|
public int getResult() {
|
|
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
|
|
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
|
|
}
|
|
}
|