184 lines
8.4 KiB
Java
184 lines
8.4 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 static java.util.Arrays.asList;
|
|
import static java.util.Collections.emptyList;
|
|
import static java.util.Collections.singletonList;
|
|
|
|
public class FlattenerTest {
|
|
|
|
private static final int MAX_POINTS = 100;
|
|
private static final int TEST_CASES = 11;
|
|
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")
|
|
public void testEmpty() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertIsEmpty(flattener.flatten(emptyList()));
|
|
});
|
|
}
|
|
|
|
@Test(name = "no nesting")
|
|
public void testFlatListIsPreserved() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList(0, '1', "two"), flattener.flatten(asList(0, '1', "two")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "flattens a nested array")
|
|
public void testNestedList() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertIsEmpty(flattener.flatten(singletonList(emptyList())));
|
|
});
|
|
}
|
|
|
|
@Test(name = "flattens array with just integers present")
|
|
public void testASingleLevelOfNestingWithNoNulls() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList(1, '2', 3, 4, 5, "six", "7", 8),
|
|
flattener.flatten(asList(1, asList('2', 3, 4, 5, "six", "7"), 8)));
|
|
});
|
|
}
|
|
|
|
@Test(name = "5 level nesting")
|
|
public void testFiveLevelsOfNestingWithNoNulls() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList(0, '2', 2, "three", '8', 100, "four", 50, "-2"),
|
|
flattener.flatten(asList(0,
|
|
'2',
|
|
asList(asList(2, "three"), '8', 100, "four", singletonList(
|
|
singletonList(singletonList(50)))),
|
|
"-2")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "6 level nesting")
|
|
public void testSixLevelsOfNestingWithNoNulls() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList("one", '2', 3, '4', 5, "six", 7, "8"),
|
|
flattener.flatten(asList("one",
|
|
asList('2', singletonList(singletonList(3)),
|
|
asList('4', singletonList(singletonList(5))),
|
|
"six", 7),
|
|
"8")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "null values are omitted from the final result")
|
|
public void testNullValuesAreOmitted() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList("1", "two"), flattener.flatten(asList("1", "two", null)));
|
|
});
|
|
}
|
|
|
|
@Test(name = "consecutive null values at the front of the list are omitted from the final result")
|
|
public void testConsecutiveNullValuesAtFrontOfListAreOmitted() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList(3), flattener.flatten(asList(null, null, 3)));
|
|
});
|
|
}
|
|
|
|
@Test(name = "consecutive null values in the middle of the list are omitted from the final result")
|
|
public void testConsecutiveNullValuesInMiddleOfListAreOmitted() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList(1, "4"), flattener.flatten(asList(1, null, null, "4")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "6 level nest list with null values")
|
|
public void testSixLevelsOfNestingWithNulls() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertListEquals(asList("0", 2, "two", '3', "8", "one hundred", "negative two"),
|
|
flattener.flatten(asList("0",
|
|
2,
|
|
asList(asList("two", '3'), "8",
|
|
singletonList(singletonList("one hundred")),
|
|
null, singletonList(singletonList(null))),
|
|
"negative two")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "all values in nested list are null")
|
|
public void testNestedListsFullOfNullsOnly() {
|
|
runTest(() -> {
|
|
Flattener flattener = new Flattener();
|
|
assertIsEmpty(flattener.flatten(asList(null,
|
|
singletonList(singletonList(singletonList(null))),
|
|
null,
|
|
null,
|
|
asList(asList(null, null), null), null)));
|
|
});
|
|
}
|
|
|
|
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(java.util.List<?> expected, java.util.List<?> actual) {
|
|
if (expected == null) {
|
|
if (actual != null)
|
|
throw new AssertionError("expected null but was: " + actual);
|
|
return;
|
|
}
|
|
if (!expected.equals(actual))
|
|
throw new AssertionError("expected: " + expected + " but was: " + actual);
|
|
}
|
|
|
|
private void assertIsEmpty(java.util.List<?> actual) {
|
|
if (actual == null || !actual.isEmpty())
|
|
throw new AssertionError("expected empty list but was: " + actual);
|
|
}
|
|
|
|
public int getResult() {
|
|
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
|
|
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
|
|
}
|
|
|
|
}
|