Files
Testate/WS2526/Testat 3b/Tests.java
T

935 lines
28 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 {
private static final String TESTAT_NAME = "Testat 3B";
// Aufgabe 1 - Tree - 60 Punkte
@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 existing once - 10 Punkte")
public void testTreeCountExisitingOnce() {
int points = 10;
// non-sorted tree
double[] values = new double[] { 3, 5, 1, 1, 4, 2, 1 };
Tree tree = new Tree();
setRoot(tree, buildTree(values));
int count = tree.count(5);
if (count != 1) {
points -= 10;
TracingFramework.printError("Tree.count(5) should return 1 , but was " + count);
}
addPoints(points);
}
@Test(name = "Aufgabe 1 - Tree - count non exisiting - 10 Punkte")
public void testTreeCountNonExisiting() {
int points = 10;
// non-sorted tree
double[] values = new double[] { 3, 5, 1, 1, 4, 2, 1 };
Tree tree = new Tree();
setRoot(tree, buildTree(values));
int count = tree.count(7);
if (count != 0) {
points -= 10;
TracingFramework.printError("Tree.count(7) should return 0 , but was " + count);
}
addPoints(points);
}
@Test(name = "Aufgabe 1 - Tree - count, should find once - 10 Punkte")
public void testTreeCountShouldFindOnce() {
int points = 10;
// non-sorted tree
double[] values = new double[] { 3, 5, 1, 1, 4, 2, 1 };
Tree tree = new Tree();
setRoot(tree, buildTree(values));
int count = tree.count(1);
if (count == 2) {
// correct, to nothing
} else if (count == 3) {
points -= 10;
TracingFramework
.printError("Tree.count(1) should return 2 with efficient implementation, but was " + count);
} else {
points -= 10;
TracingFramework.printError("Tree.count(1) should return 2 , but was " + count);
}
addPoints(points);
}
@Test(name = "Aufgabe 1 - Tree - height - 10 Punkte")
public void testTreeHeight() {
int points = 10;
// 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;
TracingFramework.printError("Height should return 3 , but was " + height);
}
addPoints(points);
}
// Aufgabe 2 - IO - 20 Punkte
@Test(name = "Aufgabe 2 - IO - write - 10 Punkte")
public void testIOWrite() {
int points = 10;
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);
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;
if (alwaysEmptyRead()) {
TracingFramework.printError(
"Reading from non-empty file returns empty list. Assuming read always returns empty list. Deducting 5 points.");
addPoints(0);
return;
}
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);
}
// Aufgabe 3 - Deque - 20 Punkte
@Test(name = "Aufgabe 3 - Deque - addFirst null - 3 Punkte")
public void testDequeAddFirstNull() {
int points = 3;
if (addFirstAlwaysException()) {
points -= 3;
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 3 points.");
} else {
try {
Deque deque = new Deque();
deque.addFirst(null);
points -= 3;
TracingFramework
.printError("Deque.addFirst(null) should throw an exception, but did not. Deducting 3 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 - 3 Punkte")
public void testDequeAddFirstEmpty() {
int points = 3;
if (addFirstAlwaysException()) {
points -= 3;
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 3 points.");
} else {
try {
Deque deque = new Deque();
deque.addFirst("");
points -= 3;
TracingFramework
.printError("Deque.addFirst(\"\") should throw an exception, but did not. Deducting 3 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;
Method method = findMethod(Deque.class, "addFirst", String.class);
if (method == null) {
TracingFramework.printError("Method Deque.addFirst(String) not found. Class was mutated illegally.");
addPoints(0);
return;
}
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.");
}
}
addPoints(points);
}
@Test(name = "Aufgabe 3 - Deque - addLast null - 3 Punkte")
public void testDequeAddLastNull() {
int points = 3;
if (addLastAlwaysException()) {
points -= 3;
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 3 points.");
} else {
try {
Deque deque = new Deque();
deque.addLast(null);
points -= 3;
TracingFramework
.printError("Deque.addLast(null) should throw an exception, but did not. Deducting 3 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 - 3 Punkte")
public void testDequeAddLastEmpty() {
int points = 3;
if (addLastAlwaysException()) {
points -= 3;
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 3 points.");
} else {
try {
Deque deque = new Deque();
deque.addLast("");
points -= 3;
TracingFramework
.printError("Deque.addLast(\"\") should throw an exception, but did not. Deducting 3 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;
Method method = findMethod(Deque.class, "addLast", String.class);
if (method == null) {
TracingFramework.printError("Method Deque.addLast(String) not found. Class was mutated illegally.");
addPoints(0);
return;
}
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.");
}
}
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;
Method method = findMethod(Deque.class, "getFirst");
if (method == null) {
TracingFramework.printError("Method Deque.getFirst() not found. Class was mutated illegally.");
addPoints(0);
return;
}
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.");
}
}
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;
Method method = findMethod(Deque.class, "getLast");
if (method == null) {
TracingFramework.printError("Method Deque.getLast() not found. Class was mutated illegally.");
addPoints(0);
return;
}
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.");
}
}
addPoints(points);
}
/**
* Checks if reading from a non-empty file always returns an empty list.
*
* @return True if reading always returns an empty list, false otherwise.
*/
private boolean alwaysEmptyRead() {
Path path = InMemoryPathFactory.createPath("/test/foo.txt");
List<String> content = Arrays.asList("This", "is", "not", "empty");
try {
Files.write(path, content);
} catch (IOException e) {
return false;
}
StringListSerializer serializer = new StringListSerializer();
try {
List<String> readData = serializer.read(path);
return readData != null && readData.isEmpty();
} catch (Exception e) {
return false;
}
}
/**
* 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.");
}
}
/**
* Finds a method in a class by name and parameter types.
*
* @param clazz The class to search in.
* @param name The name of the method.
* @param parameterTypes The parameter types of the method.
* @return The found method, or null if not found.
*/
public static Method findMethod(Class<?> clazz, String name, Class<?>... parameterTypes) {
try {
return clazz.getDeclaredMethod(name, parameterTypes);
} catch (NoSuchMethodException e) {
return null;
}
}
/**
* START: Deque helper methods
*/
/**
* 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 false;
}
return true;
}
/**
* 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 getterThrowsAnyException(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 getterThrowsAnyException(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 getterThrowsAnyException(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;
}
/**
* Gets the back item of the deque.
*
* @param deque The deque to get the back item from.
* @return The back item, or null if the deque is empty.
*/
private Item getBack(Deque deque) {
try {
Field field = Deque.class.getDeclaredField("back");
field.setAccessible(true);
return (Item) field.get(deque);
} catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
throw new RuntimeException("Error accessing getBack method", e);
}
}
/**
* Sets the front item of the deque.
*
* @param deque The deque to set the front item for.
* @param front The front item to set.
*/
private void setFront(Deque deque, Item front) {
try {
Field field = Deque.class.getDeclaredField("front");
field.setAccessible(true);
field.set(deque, front);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Error setting front item", e);
}
}
/**
* Sets the back item of the deque.
*
* @param deque The deque to set the back item for.
* @param back The back item to set.
*/
private void setBack(Deque deque, Item back) {
try {
Field field = Deque.class.getDeclaredField("back");
field.setAccessible(true);
field.set(deque, back);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Error setting back item", e);
}
}
/**
* Sets the next item for the given item.
*
* @param item The item to set the next item for.
* @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 (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Error setting next item", e);
}
}
/**
* Sets the previous item for the given item.
*
* @param item The item to set the previous item for.
* @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 (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Error setting previous item", e);
}
}
/**
* END: Deque helper methods
*/
/**
* The points of each test case.
*/
private List<Integer> testPoints = 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 : testPoints) {
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) {
testPoints.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 {
Class<Tests> clazz = Tests.class;
Tests test = new Tests();
System.out.println(String.format("Running tests for: %s", TESTAT_NAME));
System.out.println();
for (Method method : clazz.getMethods()) {
if (method.isAnnotationPresent(Test.class)) {
System.out.println(String.format("Currently testing: %s", method.getName()));
int size = test.testPoints.size();
try {
method.invoke(test);
if (test.testPoints.size() != size + 1) {
System.err.println("ERROR: Test yields no or multiple results. Erroneous test.");
} else {
System.out.println(
String.format("PASS: Test passed with %d points!",
test.testPoints.get(test.testPoints.size() - 1)));
}
} catch (Exception e) {
System.err.println(String.format("ERROR: Test failed with exception: %s", e.getMessage()));
e.printStackTrace();
}
System.out.println();
}
}
System.out.println();
System.out.println(String.format("Total points: %d", test.getResult()));
}
public static void main(String[] args) {
try {
runAll();
} catch (Exception e) {
e.printStackTrace();
}
}
}