Files
jack/strain/StrainTest.java

193 lines
7.2 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.LinkedList;
import java.util.List;
public class StrainTest {
private static final int MAX_POINTS = 100;
private static final int TEST_CASES = 12;
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 keep")
public void emptyKeep() {
runTest(() -> {
List<Integer> input = new LinkedList<>();
List<Integer> expectedOutput = new LinkedList<>();
assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
});
}
@Test(name = "keep everything")
public void keepEverything() {
runTest(() -> {
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
});
}
@Test(name = "keep first and last")
public void keepFirstAndLast() {
runTest(() -> {
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(1, 3);
assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 != 0));
});
}
@Test(name = "keep neither first nor last")
public void keepNeitherFirstNorLast() {
runTest(() -> {
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expectedOutput = Arrays.asList(2, 4);
assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 == 0));
});
}
@Test(name = "keep strings")
public void keepStrings() {
runTest(() -> {
List<String> words = Arrays.asList("apple zebra banana zombies cherimoya zelot".split(" "));
List<String> expectedOutput = Arrays.asList("zebra", "zombies", "zelot");
assertEquals(expectedOutput, Strain.keep(words, x -> x.startsWith("z")));
});
}
@Test(name = "keep arrays")
public void keepArrays() {
runTest(() -> {
List<List<Integer>> actual = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(5, 5, 5),
Arrays.asList(5, 1, 2),
Arrays.asList(2, 1, 2),
Arrays.asList(1, 5, 2),
Arrays.asList(2, 2, 1),
Arrays.asList(1, 2, 5));
List<List<Integer>> expectedOutput = Arrays.asList(
Arrays.asList(5, 5, 5),
Arrays.asList(5, 1, 2),
Arrays.asList(1, 5, 2),
Arrays.asList(1, 2, 5));
assertEquals(expectedOutput, Strain.keep(actual, col -> col.contains(5)));
});
}
@Test(name = "empty discard")
public void emptyDiscard() {
runTest(() -> {
List<Integer> input = new LinkedList<>();
List<Integer> expectedOutput = new LinkedList<>();
assertEquals(expectedOutput, Strain.discard(input, x -> x < 10));
});
}
@Test(name = "discard nothing")
public void discardNothing() {
runTest(() -> {
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
assertEquals(expectedOutput, Strain.discard(input, x -> x > 10));
});
}
@Test(name = "discard first and last")
public void discardFirstAndLast() {
runTest(() -> {
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(2);
assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 != 0));
});
}
@Test(name = "discard neither first nor last")
public void discardNeitherFirstNorLast() {
runTest(() -> {
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expectedOutput = Arrays.asList(1, 3, 5);
assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 == 0));
});
}
@Test(name = "discard strings")
public void discardStrings() {
runTest(() -> {
List<String> words = Arrays.asList("apple zebra banana zombies cherimoya zelot".split(" "));
List<String> expectedOutput = Arrays.asList("apple", "banana", "cherimoya");
assertEquals(expectedOutput, Strain.discard(words, x -> x.startsWith("z")));
});
}
@Test(name = "discard arrays")
public void discardArrays() {
runTest(() -> {
List<List<Integer>> actual = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(5, 5, 5),
Arrays.asList(5, 1, 2),
Arrays.asList(2, 1, 2),
Arrays.asList(1, 5, 2),
Arrays.asList(2, 2, 1),
Arrays.asList(1, 2, 5));
List<List<Integer>> expectedOutput = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(2, 1, 2),
Arrays.asList(2, 2, 1));
assertEquals(expectedOutput, Strain.discard(actual, col -> col.contains(5)));
});
}
private void assertEquals(Object expected, Object actual) {
if (!expected.equals(actual)) {
throw new AssertionError("Expected: " + expected + " but was: " + actual);
}
}
private void runTest(Runnable test) {
try {
test.run();
testPoints.add(SUCCESS_POINTS);
} catch (AssertionError | Exception e) {
TracingFramework.printError(e.getMessage());
testPoints.add(FAILURE_POINTS);
}
}
public int getResult() {
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
}
}