820 lines
26 KiB
Java
820 lines
26 KiB
Java
import java.io.IOException;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.NoSuchElementException;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Supplier;
|
|
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
|
|
|
|
public class Tests {
|
|
|
|
@Test(name = "Aufgabe 1 - Tree - traverse - 20 Punkte")
|
|
public void testTreeTraverse() {
|
|
int points = 20;
|
|
double[] values = new double[] { 3, 5, 1, 6, 4, 2, 0 };
|
|
Tree tree = new Tree();
|
|
setRoot(tree, buildTree(values));
|
|
// post-order
|
|
double[] expect = new double[] { 6, 4, 5, 2, 0, 1, 3 };
|
|
TreeVisitor visitor = new TreeVisitor();
|
|
tree.traverse(visitor);
|
|
double[] actual = visitor.toArray();
|
|
|
|
if (!Arrays.equals(actual, expect)) {
|
|
double[] expectSort = expect.clone();
|
|
double[] actualSort = actual.clone();
|
|
Arrays.sort(expectSort);
|
|
Arrays.sort(actualSort);
|
|
if (Arrays.equals(expectSort, actualSort)) {
|
|
points -= 10;
|
|
TracingFramework.printError(
|
|
"Traversal " + Arrays.toString(actual)
|
|
+ " is not post-order but complete. Deducting 10 points.");
|
|
} else {
|
|
points -= 20;
|
|
TracingFramework.printError("Traversal " + Arrays.toString(actual) + " is incomplete (no points).");
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - Tree - count - 30 Punkte")
|
|
public void testTreeCount() {
|
|
int points = 0;
|
|
// non-sorted tree
|
|
double[] values = new double[] { 3, 5, 1, 1, 4, 2, 1 };
|
|
Tree tree = new Tree();
|
|
setRoot(tree, buildTree(values));
|
|
|
|
// 5 should be found once
|
|
int count = tree.count(5);
|
|
if (count == 1) {
|
|
points += 7;
|
|
} else {
|
|
TracingFramework.printError("Count(5) should return 1 , but was " + count);
|
|
}
|
|
|
|
// 7 should not be found
|
|
count = tree.count(7);
|
|
if (count == 0) {
|
|
points += 8;
|
|
} else {
|
|
TracingFramework.printError("Count(7) should return 0 , but was " + count);
|
|
}
|
|
|
|
// 1 should only be found 2 (if search is efficient)
|
|
count = tree.count(1);
|
|
if (count == 2) {
|
|
points += 15;
|
|
} else if (count == 3) {
|
|
points += 8;
|
|
TracingFramework.printError("Count(1) should return 2 with efficient implementation, but was " + count);
|
|
} else {
|
|
TracingFramework.printError("Count(1) should return 2 , but was " + count);
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - Tree - height - 10 Punkte")
|
|
public void testTreeHeight() {
|
|
int points = 0;
|
|
// non-sorted tree
|
|
double[] values = new double[] { 3, 1, 5, 1, 2, 4, 1 };
|
|
Tree tree = new Tree();
|
|
setRoot(tree, buildTree(values));
|
|
|
|
int height = tree.height();
|
|
if (height == 3) {
|
|
points += 10;
|
|
} else {
|
|
TracingFramework.printError("Height should return 3 , but was " + height);
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - IO - write - 10 Punkte")
|
|
public void testIOWrite() {
|
|
StringListSerializer serializer = new StringListSerializer();
|
|
Path path = InMemoryPathFactory.createPath("/test/write.txt");
|
|
List<String> data = Arrays.asList("Hello", "World", "This is a test");
|
|
serializer.write(path, data);
|
|
int points = 10;
|
|
try {
|
|
List<String> content = Files.readAllLines(path);
|
|
if (!content.equals(data)) {
|
|
points -= 10;
|
|
TracingFramework.printError("Written content does not match original content. Deducting 10 points.");
|
|
}
|
|
} catch (Exception e) {
|
|
points -= 10;
|
|
TracingFramework.printError("Failed to read written content: " + e.getMessage() + ". Deducting 10 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - IO - read - errorless- 5 Punkte")
|
|
public void testIOReadErrorless() {
|
|
int points = 5;
|
|
StringListSerializer serializer = new StringListSerializer();
|
|
Path path = InMemoryPathFactory.createPath("/test/read.txt");
|
|
List<String> data = Arrays.asList("Hello", "World", "This is a test");
|
|
try {
|
|
Files.write(path, data);
|
|
} catch (IOException e) {
|
|
points -= 5;
|
|
e.printStackTrace();
|
|
TracingFramework.printError(
|
|
"Failed to write test data: " + e.getMessage() + ". Erroneous test. Giving full points.");
|
|
addPoints(points);
|
|
return;
|
|
}
|
|
|
|
List<String> readData = serializer.read(path);
|
|
if (!data.equals(readData)) {
|
|
points -= 5;
|
|
TracingFramework.printError("Read content does not match original content. Deducting 5 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - IO - read - forced IOException - 5 Punkte")
|
|
public void testIOReadForcedIOException() {
|
|
int points = 5;
|
|
StringListSerializer serializer = new StringListSerializer();
|
|
Path path = InMemoryPathFactory.createPath("/test/forcedIOException.txt");
|
|
try {
|
|
List<String> readData = serializer.read(path);
|
|
if (readData == null) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"Reading from /test/forcedIOException.txt path should return an empty list, but returned null. Deducting 5 points.");
|
|
} else if (!readData.isEmpty()) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"Reading from /test/forcedIOException.txt path should return an empty list, but returned: "
|
|
+ readData
|
|
+ ". Deducting 5 points.");
|
|
}
|
|
} catch (Exception e) {
|
|
if (e instanceof IOException) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"Reading from /test/forcedIOException.txt path should process IOException to empty list, but did not. Deducting 5 points.");
|
|
} else {
|
|
points -= 5;
|
|
TracingFramework
|
|
.printError("Reading from /test/forcedIOException.txt path throws an unexpected exception: "
|
|
+ e.getMessage());
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - addFirst null - 2 Punkte")
|
|
public void testDequeAddFirstNull() {
|
|
int points = 2;
|
|
if (addFirstAlwaysException()) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addFirst(\"A\"), Deque.addFirst(\"BCDEF\"), or Deque.addFirst(\"Hallo\") threw an exception. Assuming method always throws an exception on valid values. Deducting 2 points.");
|
|
} else {
|
|
try {
|
|
Deque deque = new Deque();
|
|
deque.addFirst(null);
|
|
points -= 2;
|
|
TracingFramework
|
|
.printError("Deque.addFirst(null) should throw an exception, but did not. Deducting 2 points.");
|
|
} catch (Exception e) {
|
|
if (!(e instanceof IllegalArgumentException)) {
|
|
points -= 1;
|
|
TracingFramework
|
|
.printError("Deque.addFirst(null) should throw an IllegalArgumentException, but threw: "
|
|
+ e.getClass().getName() + ". Deducting 1 point.");
|
|
}
|
|
if (e.getMessage() == null || !e.getMessage().equals("Value cannot be null")) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.addFirst(null) should throw an IllegalArgumentException with message 'Value cannot be null', but got: "
|
|
+ e.getMessage() + ". Deducting 1 point.");
|
|
}
|
|
}
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - addFirst empty - 2 Punkte")
|
|
public void testDequeAddFirstEmpty() {
|
|
int points = 2;
|
|
if (addFirstAlwaysException()) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addFirst(\"A\"), Deque.addFirst(\"BCDEF\"), or Deque.addFirst(\"Hallo\") threw an exception. Assuming method always throws an exception on valid values. Deducting 2 points.");
|
|
} else {
|
|
try {
|
|
Deque deque = new Deque();
|
|
deque.addFirst("");
|
|
points -= 2;
|
|
TracingFramework
|
|
.printError("Deque.addFirst(\"\") should throw an exception, but did not. Deducting 2 points.");
|
|
} catch (Exception e) {
|
|
if (!(e instanceof IllegalArgumentException)) {
|
|
points -= 1;
|
|
TracingFramework
|
|
.printError("Deque.addFirst(\"\") should throw an IllegalArgumentException, but threw: "
|
|
+ e.getClass().getName() + ". Deducting 1 point.");
|
|
}
|
|
if (e.getMessage() == null || !e.getMessage().equals("Value cannot be empty")) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.addFirst(\"\") should throw an IllegalArgumentException with message 'Value cannot be empty', but got: "
|
|
+ e.getMessage() + ". Deducting 1 point.");
|
|
}
|
|
}
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - addFirst throws IllegalArgumentException - 2 Punkte")
|
|
public void testDequeAddFirstThrowsIllegalArgumentException() {
|
|
int points = 2;
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("addFirst", String.class);
|
|
Class<?>[] exceptionTypes = method.getExceptionTypes();
|
|
if (exceptionTypes.length == 0) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addFirst(String) should throw an exception, but did not. Deducting 2 point.");
|
|
} else if (exceptionTypes.length != 1) {
|
|
points -= 2;
|
|
TracingFramework.printError("Deque.addFirst(String) should only throw one exception, but threw: "
|
|
+ Arrays.toString(exceptionTypes) + ". Deducting 2 points.");
|
|
} else {
|
|
if (exceptionTypes[0] != IllegalArgumentException.class) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addFirst(String) should throw an IllegalArgumentException, but threw: "
|
|
+ exceptionTypes[0].getName() + ". Deducting 2 points.");
|
|
}
|
|
}
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
TracingFramework.printError("Method Deque.addFirst(String) not found. Class was mutated illegally.");
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - addLast null - 2 Punkte")
|
|
public void testDequeAddLastNull() {
|
|
int points = 2;
|
|
if (addLastAlwaysException()) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addLast(\"A\"), Deque.addLast(\"BCDEF\"), or Deque.addLast(\"Hallo\") threw an exception. Assuming method always throws an exception on valid values. Deducting 2 points.");
|
|
} else {
|
|
try {
|
|
Deque deque = new Deque();
|
|
deque.addLast(null);
|
|
points -= 2;
|
|
TracingFramework
|
|
.printError("Deque.addLast(null) should throw an exception, but did not. Deducting 2 points.");
|
|
} catch (Exception e) {
|
|
if (!(e instanceof IllegalArgumentException)) {
|
|
points -= 1;
|
|
TracingFramework
|
|
.printError("Deque.addLast(null) should throw an IllegalArgumentException, but threw: "
|
|
+ e.getClass().getName() + ". Deducting 1 point.");
|
|
}
|
|
if (e.getMessage() == null || !e.getMessage().equals("Value cannot be null")) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.addLast(null) should throw an IllegalArgumentException with message 'Value cannot be null', but got: "
|
|
+ e.getMessage() + ". Deducting 1 point.");
|
|
}
|
|
}
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - addLast empty - 2 Punkte")
|
|
public void testDequeAddLastEmpty() {
|
|
int points = 2;
|
|
if (addLastAlwaysException()) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addLast(\"A\"), Deque.addLast(\"BCDEF\"), or Deque.addLast(\"Hallo\") threw an exception. Assuming method always throws an exception on valid values. Deducting 2 points.");
|
|
} else {
|
|
try {
|
|
Deque deque = new Deque();
|
|
deque.addLast("");
|
|
points -= 2;
|
|
TracingFramework
|
|
.printError("Deque.addLast(\"\") should throw an exception, but did not. Deducting 2 points.");
|
|
} catch (Exception e) {
|
|
if (!(e instanceof IllegalArgumentException)) {
|
|
points -= 1;
|
|
TracingFramework
|
|
.printError("Deque.addLast(\"\") should throw an IllegalArgumentException, but threw: "
|
|
+ e.getClass().getName() + ". Deducting 1 point.");
|
|
}
|
|
if (e.getMessage() == null || !e.getMessage().equals("Value cannot be empty")) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.addLast(\"\") should throw an IllegalArgumentException with message 'Value cannot be empty', but got: "
|
|
+ e.getMessage() + ". Deducting 1 point.");
|
|
}
|
|
}
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - addLast throws IllegalArgumentException - 2 Punkte")
|
|
public void testDequeAddLastThrowsIllegalArgumentException() {
|
|
int points = 2;
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("addLast", String.class);
|
|
Class<?>[] exceptionTypes = method.getExceptionTypes();
|
|
if (exceptionTypes.length == 0) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addLast(String) should throw an exception, but did not. Deducting 2 point.");
|
|
} else if (exceptionTypes.length != 1) {
|
|
points -= 2;
|
|
TracingFramework.printError("Deque.addLast(String) should only throw one exception, but threw: "
|
|
+ Arrays.toString(exceptionTypes) + ". Deducting 2 points.");
|
|
} else {
|
|
if (exceptionTypes[0] != IllegalArgumentException.class) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.addLast(String) should throw an IllegalArgumentException, but threw: "
|
|
+ exceptionTypes[0].getName() + ". Deducting 2 points.");
|
|
}
|
|
}
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
TracingFramework.printError("Method Deque.addLast(String) not found. Class was mutated illegally.");
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - getFirst empty exception - 2 Punkte")
|
|
public void testDequeGetFirstEmptyDeque() {
|
|
int points = 2;
|
|
if (getFirstAlwaysException()) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.getFirst() threw an exception on Deque with five elements. Assuming method always throws an exception on valid deque. Deducting 2 points.");
|
|
} else {
|
|
try {
|
|
Deque deque = new Deque();
|
|
deque.getFirst();
|
|
points -= 2;
|
|
TracingFramework
|
|
.printError(
|
|
"Deque.getFirst() should throw an exception on empty deque, but did not. Deducting 2 points.");
|
|
} catch (Exception e) {
|
|
if (!(e instanceof NoSuchElementException)) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.getFirst() should throw a NoSuchElementException on empty deque, but threw: "
|
|
+ e.getClass().getName() + ". Deducting 1 point.");
|
|
}
|
|
if (e.getMessage() == null || !e.getMessage().equals("Deque is empty")) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.getFirst() should throw a NoSuchElementException with message 'Deque is empty', but got: "
|
|
+ e.getMessage() + ". Deducting 1 point.");
|
|
}
|
|
}
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - getFirst throws NoSuchElementException - 2 Punkte")
|
|
public void testDequeGetFirstThrowsNoSuchElementException() {
|
|
int points = 2;
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("getFirst");
|
|
Class<?>[] exceptionTypes = method.getExceptionTypes();
|
|
if (exceptionTypes.length == 0) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.getFirst() should throw an exception, but did not. Deducting 2 point.");
|
|
} else if (exceptionTypes.length != 1) {
|
|
points -= 2;
|
|
TracingFramework.printError("Deque.getFirst() should only throw one exception, but threw: "
|
|
+ Arrays.toString(exceptionTypes) + ". Deducting 2 points.");
|
|
} else {
|
|
if (exceptionTypes[0] != NoSuchElementException.class) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.getFirst() should throw a NoSuchElementException, but threw: "
|
|
+ exceptionTypes[0].getName() + ". Deducting 2 points.");
|
|
}
|
|
}
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
TracingFramework.printError("Method Deque.getFirst() not found. Class was mutated illegally.");
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - getLast empty exception - 2 Punkte")
|
|
public void testDequeGetLastEmptyDeque() {
|
|
int points = 2;
|
|
if (getLastAlwaysException()) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.getLast() threw an exception on Deque with five elements. Assuming method always throws an exception on valid deque. Deducting 2 points.");
|
|
} else {
|
|
try {
|
|
Deque deque = new Deque();
|
|
deque.getLast();
|
|
points -= 2;
|
|
TracingFramework
|
|
.printError(
|
|
"Deque.getLast() should throw an exception on empty deque, but did not. Deducting 2 points.");
|
|
} catch (Exception e) {
|
|
if (!(e instanceof NoSuchElementException)) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.getLast() should throw a NoSuchElementException on empty deque, but threw: "
|
|
+ e.getClass().getName() + ". Deducting 1 point.");
|
|
}
|
|
if (e.getMessage() == null || !e.getMessage().equals("Deque is empty")) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"Deque.getLast() should throw a NoSuchElementException with message 'Deque is empty', but got: "
|
|
+ e.getMessage() + ". Deducting 1 point.");
|
|
}
|
|
}
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3 - Deque - getLast throws NoSuchElementException - 2 Punkte")
|
|
public void testDequeGetLastThrowsNoSuchElementException() {
|
|
int points = 2;
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("getLast");
|
|
Class<?>[] exceptionTypes = method.getExceptionTypes();
|
|
if (exceptionTypes.length == 0) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.getLast() should throw an exception, but did not. Deducting 2 point.");
|
|
} else if (exceptionTypes.length != 1) {
|
|
points -= 2;
|
|
TracingFramework.printError("Deque.getLast() should only throw one exception, but threw: "
|
|
+ Arrays.toString(exceptionTypes) + ". Deducting 2 points.");
|
|
} else {
|
|
if (exceptionTypes[0] != NoSuchElementException.class) {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"Deque.getLast() should throw a NoSuchElementException, but threw: "
|
|
+ exceptionTypes[0].getName() + ". Deducting 2 points.");
|
|
}
|
|
}
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
TracingFramework.printError("Method Deque.getLast() not found. Class was mutated illegally.");
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
/**
|
|
* Checks if addFirst throws any exception when called.
|
|
*
|
|
* We assume that if any exception is thrown on valid values, it always throws
|
|
* an exception.
|
|
*
|
|
* @return True if addFirst throws any exception, false otherwise.
|
|
*/
|
|
private boolean addFirstAlwaysException() {
|
|
Deque deque = new Deque();
|
|
return adderThrowsAnyException(deque::addFirst, "A", "BCDEF", "Hallo");
|
|
}
|
|
|
|
/**
|
|
* Checks if addLast throws any exception when called.
|
|
*
|
|
* We assume that if any exception is thrown on valid values, it always throws
|
|
* an exception.
|
|
*
|
|
* @return True if addLast throws any exception, false otherwise.
|
|
*/
|
|
private boolean addLastAlwaysException() {
|
|
Deque deque = new Deque();
|
|
return adderThrowsAnyException(deque::addLast, "A", "BCDEF", "Hallo");
|
|
}
|
|
|
|
/**
|
|
* Checks if the specified adder method throws any exception when called
|
|
* with the given values.
|
|
*
|
|
* @param adder The adder method to test.
|
|
* @param values The values to add.
|
|
* @return True if the adder throws any exception, false otherwise.
|
|
*/
|
|
private boolean adderThrowsAnyException(Consumer<String> adder, String... values) {
|
|
try {
|
|
for (String value : values) {
|
|
adder.accept(value);
|
|
}
|
|
} catch (Exception e) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Checks if getFirst throws any exception when called,
|
|
* while the corresponding poller method is called successfully.
|
|
*
|
|
* We assume that if any exception is thrown on valid values, it always throws
|
|
* an exception.
|
|
*
|
|
* @return True if the getter throws any exception, false otherwise.
|
|
*/
|
|
private boolean getFirstAlwaysException() {
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
// should be executable five times when deque is not empty
|
|
return getterAnyThrowsException(deque::getFirst, deque::pollFirst, 5);
|
|
}
|
|
|
|
/**
|
|
* Checks if getLast throws any exception when called,
|
|
* while the corresponding poller method is called successfully.
|
|
*
|
|
* We assume that if any exception is thrown on valid values, it always throws
|
|
* an exception.
|
|
*
|
|
* @return True if getLast throws any exception, false otherwise.
|
|
*/
|
|
private boolean getLastAlwaysException() {
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
// should be executable five times when deque is not empty
|
|
return getterAnyThrowsException(deque::getLast, deque::pollLast, 5);
|
|
}
|
|
|
|
/**
|
|
* Checks if the specified getter method throws any exception when called,
|
|
* while the corresponding poller method is called successfully.
|
|
*
|
|
* @param getter The getter method to test.
|
|
* @param poller The poller method to call.
|
|
* @param n The number of times to call the methods.
|
|
* @return True if the getter throws any exception, false otherwise.
|
|
*/
|
|
private boolean getterAnyThrowsException(Supplier<String> getter, Supplier<String> poller, int n) {
|
|
try {
|
|
for (int i = 0; i < n; i++) {
|
|
getter.get();
|
|
poller.get();
|
|
}
|
|
} catch (Exception e) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Builds a deque from the given values.
|
|
*
|
|
* @param values The values to add.
|
|
* @return The built deque.
|
|
*/
|
|
public Deque buildDeque(String... values) {
|
|
Deque deque = new Deque();
|
|
for (String value : values) {
|
|
try {
|
|
// Create new Item
|
|
Item item = new Item(value);
|
|
|
|
// If deque is empty, set both front and back to this item
|
|
if (deque.peekFirst() == null) {
|
|
setFront(deque, item);
|
|
setBack(deque, item);
|
|
} else {
|
|
// Add to the end (back) of the deque
|
|
Item currentBack = getBack(deque);
|
|
|
|
// Set the links
|
|
setNext(currentBack, item);
|
|
setPrev(item, currentBack);
|
|
|
|
// Update back pointer
|
|
setBack(deque, item);
|
|
}
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to build deque: " + e.getMessage(), e);
|
|
}
|
|
}
|
|
return deque;
|
|
}
|
|
|
|
/**
|
|
* Builds a binary tree from an array from top to bottom and left to right.
|
|
*
|
|
* @param values The values.
|
|
* @return The tree node representing the root.
|
|
*/
|
|
private TreeNode buildTree(double[] values) {
|
|
TreeNode root = new TreeNode(values[0]);
|
|
java.util.List<TreeNode> nodes = new ArrayList<>();
|
|
nodes.add(root);
|
|
for (int i = 1; i < values.length; i++) {
|
|
TreeNode parent = nodes.get(0);
|
|
TreeNode child = new TreeNode(values[i]);
|
|
nodes.add(child);
|
|
if (i % 2 == 1) {
|
|
parent.setLeft(child);
|
|
} else {
|
|
parent.setRight(child);
|
|
nodes.remove(0);
|
|
}
|
|
}
|
|
return root;
|
|
}
|
|
|
|
/**
|
|
* Sets the root in the tree.
|
|
*
|
|
* @param tree The tree.
|
|
* @param root The root to set in the tree.
|
|
*/
|
|
private void setRoot(Tree tree, TreeNode root) {
|
|
try {
|
|
Field field = Tree.class.getDeclaredField("root");
|
|
field.setAccessible(true);
|
|
field.set(tree, root);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to set root.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the front item in the deque using reflection.
|
|
*
|
|
* @param deque The deque.
|
|
* @param item The item to set as front.
|
|
*/
|
|
private void setFront(Deque deque, Item item) {
|
|
try {
|
|
Field field = Deque.class.getDeclaredField("front");
|
|
field.setAccessible(true);
|
|
field.set(deque, item);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to set front.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the back item in the deque using reflection.
|
|
*
|
|
* @param deque The deque.
|
|
* @param item The item to set as back.
|
|
*/
|
|
private void setBack(Deque deque, Item item) {
|
|
try {
|
|
Field field = Deque.class.getDeclaredField("back");
|
|
field.setAccessible(true);
|
|
field.set(deque, item);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to set back.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the back item from the deque using reflection.
|
|
*
|
|
* @param deque The deque.
|
|
* @return The back item.
|
|
*/
|
|
private Item getBack(Deque deque) {
|
|
try {
|
|
Field field = Deque.class.getDeclaredField("back");
|
|
field.setAccessible(true);
|
|
return (Item) field.get(deque);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to get back.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the next item in an Item using reflection.
|
|
*
|
|
* @param item The item.
|
|
* @param next The next item to set.
|
|
*/
|
|
private void setNext(Item item, Item next) {
|
|
try {
|
|
Field field = Item.class.getDeclaredField("next");
|
|
field.setAccessible(true);
|
|
field.set(item, next);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to set next.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the previous item in an Item using reflection.
|
|
*
|
|
* @param item The item.
|
|
* @param prev The previous item to set.
|
|
*/
|
|
private void setPrev(Item item, Item prev) {
|
|
try {
|
|
Field field = Item.class.getDeclaredField("prev");
|
|
field.setAccessible(true);
|
|
field.set(item, prev);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to set prev.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The points of each test case.
|
|
*/
|
|
private java.util.List<Integer> points = new ArrayList<>();
|
|
|
|
/**
|
|
* This method will be called by jack after all test have been executed to get
|
|
* the resulting points.
|
|
*
|
|
* @return The points resulting from the test.
|
|
*/
|
|
public int getResult() {
|
|
int sum = 0;
|
|
for (Integer p : points) {
|
|
sum += p;
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
/**
|
|
* Adds points for a test case. This class assumes that the method is called
|
|
* <b>exactly</b> once per test case.
|
|
*
|
|
* @param value The points to add.
|
|
*/
|
|
protected void addPoints(int value) {
|
|
points.add(value);
|
|
}
|
|
|
|
/**
|
|
* A utility method to execute any test driver class extending abstract test
|
|
* locally without going through Jack.
|
|
*
|
|
* @param test The test driver to execute.
|
|
* @throws Exception Thrown if something fails.
|
|
*/
|
|
public static void runAll() throws Exception {
|
|
Tests test = new Tests();
|
|
|
|
System.out.println(test.getClass().getName());
|
|
System.out.println();
|
|
for (Method method : test.getClass().getMethods()) {
|
|
if (method.isAnnotationPresent(Test.class)) {
|
|
int size = test.points.size();
|
|
try {
|
|
method.invoke(test);
|
|
if (test.points.size() != size + 1) {
|
|
System.err.println("Der Test " + method.getAnnotation(Test.class).name()
|
|
+ " lieferte kein oder mehrere Ergebnisse.");
|
|
} else {
|
|
System.out.println("Punkte: " + test.points.get(test.points.size() - 1) + " ("
|
|
+ method.getAnnotation(Test.class).name() + ")");
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println(
|
|
"Der Test " + method.getAnnotation(Test.class).name() + " lieferte ein Exception.");
|
|
e.printStackTrace();
|
|
}
|
|
System.out.println("");
|
|
}
|
|
}
|
|
System.out.println();
|
|
System.out.println("Summe: " + test.getResult());
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
try {
|
|
runAll();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|