309 lines
11 KiB
Java
309 lines
11 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 java.util.Arrays;
|
|
import java.util.List;
|
|
import java.lang.reflect.Method;
|
|
import java.util.stream.Collectors;
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
import java.lang.annotation.Target;
|
|
import java.lang.annotation.ElementType;
|
|
|
|
public class ListOpsMain {
|
|
|
|
@Test(name = "empty lists")
|
|
public void testAppendingEmptyLists() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.append(List.of(), List.of()).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "list to empty list")
|
|
@Disabled
|
|
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")
|
|
@Disabled
|
|
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")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
public void testConcatEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.concat(List.of()).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "list of lists")
|
|
@Disabled
|
|
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")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
public void testFilteringEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.filter(List.<Integer>of(), integer -> integer % 2 == 1).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (filter)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
public void testSizeOfEmptyList() {
|
|
runTest(() -> {
|
|
assertIntEquals(0, ListOps.size(List.of()));
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (size)")
|
|
@Disabled
|
|
public void testSizeOfNonEmptyList() {
|
|
runTest(() -> {
|
|
assertIntEquals(4, ListOps.size(List.of("one", "two", "three", "four")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "empty list (map)")
|
|
@Disabled
|
|
public void testTransformingEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.map(List.<Integer>of(), integer -> integer + 1).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (map)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
public void testReversingEmptyList() {
|
|
runTest(() -> {
|
|
assertTrue(ListOps.reverse(List.of()).isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-empty list (reverse)")
|
|
@Disabled
|
|
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)")
|
|
@Disabled
|
|
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();
|
|
System.out.println("TEST PASSED");
|
|
} catch (Exception | AssertionError t) {
|
|
System.out.println("TEST FAILED: " + t.getMessage());
|
|
}
|
|
}
|
|
|
|
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 static void main(String[] args) {
|
|
new ListOpsMain().runTests();
|
|
}
|
|
|
|
/**
|
|
* Annotation to mark test methods. The name parameter is used to identify the
|
|
* test in the output.
|
|
*/
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Target(ElementType.METHOD)
|
|
private @interface Test {
|
|
String name();
|
|
}
|
|
|
|
/**
|
|
* Annotation to mark test methods as disabled. Disabled tests will not be
|
|
* executed when running the tests.
|
|
*/
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Target(ElementType.METHOD)
|
|
private @interface Disabled {
|
|
}
|
|
|
|
private void runTests() {
|
|
Method[] methods = this.getClass().getDeclaredMethods();
|
|
List<Method> allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class))
|
|
.collect(Collectors.toList());
|
|
List<Method> enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class))
|
|
.collect(Collectors.toList());
|
|
System.out
|
|
.println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n");
|
|
for (Method method : enabledTestMethods) {
|
|
try {
|
|
System.out.println("Running test: " + method.getAnnotation(Test.class).name());
|
|
method.invoke(this);
|
|
} catch (Exception e) {
|
|
System.out.println("Test " + method.getAnnotation(Test.class).name()
|
|
+ " failed with an unexpected exception: " + e.getCause());
|
|
}
|
|
}
|
|
System.out.println("Finished running tests.");
|
|
if (allTestMethods.size() != enabledTestMethods.size()) {
|
|
System.out.println("\nThe following tests are disabled and were not executed:");
|
|
for (Method method : allTestMethods) {
|
|
if (method.isAnnotationPresent(Disabled.class)) {
|
|
System.out.println("- " + method.getAnnotation(Test.class).name());
|
|
}
|
|
}
|
|
System.out.println(
|
|
"To enable these tests, remove the @Disabled annotation from the corresponding test methods.");
|
|
}
|
|
}
|
|
}
|