Files
Testate/WS2526/Testat 3a/Tests.java
T
2025-09-12 14:25:16 +02:00

1012 lines
31 KiB
Java

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
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 3A";
// Aufgabe 1 - Tree - 60 Punkte
@Test(name = "Aufgabe 1 - Tree - traverse - 20 Punkte")
public void testTreeTraverse() {
int points = 20;
double[] values = new double[] { 3, 1, 5, 0, 2, 4, 6 };
Tree tree = new Tree();
setRoot(tree, buildTree(values));
// pre-order
double[] expect = new double[] { 3, 1, 0, 2, 5, 4, 6 };
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 pre-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, 1, 5, 1, 2, 4, 1 };
Tree tree = new Tree();
setRoot(tree, buildTree(values));
int count = tree.count(3);
if (count != 1) {
points -= 10;
TracingFramework.printError("Tree.count(3) 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, 1, 5, 1, 2, 4, 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, 1, 5, 1, 2, 4, 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("Tree.height() should return 3 , but was " + height);
}
addPoints(points);
}
// Aufgabe 2 - Generic Deque - 20 Punkte
@Test(name = "Aufgabe 2 - Deque - addFirst - takes generic type - 5 Punkte")
public void testDequeAddFirstGeneric() {
int points = 5;
if (!isDequeAdderGeneric("addFirst")) {
points -= 5;
}
addPoints(points);
}
@Test(name = "Aufgabe 2 - Deque - addLast - takes generic type - 5 Punkte")
public void testDequeAddLastGeneric() {
int points = 5;
if (!isDequeAdderGeneric("addLast")) {
points -= 5;
}
addPoints(points);
}
@Test(name = "Aufgabe 2 - Deque - getFirst - returns generic type - 5 Punkte")
public void testDequeGetFirstGeneric() {
int points = 5;
if (!isDequeGetterGeneric("getFirst")) {
points -= 5;
}
addPoints(points);
}
@Test(name = "Aufgabe 2 - Deque - getLast - returns generic type - 5 Punkte")
public void testDequeGetLastGeneric() {
int points = 5;
if (!isDequeGetterGeneric("getLast")) {
points -= 5;
}
addPoints(points);
}
/**
* Checks if an adder method takes the generic type parameter.
*
* @param name The name of the method.
* @return true if the method takes the generic type parameter, false otherwise.
*/
@SuppressWarnings("rawtypes")
private boolean isDequeAdderGeneric(String name) {
TypeVariable<Class<Deque>> classTypeParameter = getClassTypeParameter();
if (classTypeParameter == null) {
return false;
}
Method method = findUniqueMethod(Deque.class, name);
if (method == null) {
TracingFramework.printError("Method Deque." + name + " not found. Class was mutated illegally.");
return false;
}
Type[] genericParameterTypes = method.getGenericParameterTypes();
if (genericParameterTypes.length != 1) {
TracingFramework.printError("Deque." + name + " should take one parameter, but takes "
+ genericParameterTypes.length + " parameters.");
return false;
}
Type paramType = genericParameterTypes[0];
if (paramType instanceof TypeVariable) {
TypeVariable<?> typeVar = (TypeVariable<?>) paramType;
if (!typeVar.getName().equals(classTypeParameter.getName())) {
TracingFramework.printError("Deque." + name + " should take the generic type parameter, but takes "
+ typeVar.getName() + " instead.");
return false;
}
} else {
TracingFramework.printError("Deque." + name + " should take the generic type parameter, but takes "
+ paramType.getTypeName() + " instead.");
return false;
}
return true;
}
/**
* Checks if a getter method returns the generic type parameter.
*
* @param name The name of the method.
* @return true if the method returns the generic type parameter, false
* otherwise.
*/
@SuppressWarnings("rawtypes")
private boolean isDequeGetterGeneric(String name) {
TypeVariable<Class<Deque>> classTypeParameter = getClassTypeParameter();
if (classTypeParameter == null) {
return false;
}
Method method = findUniqueMethod(Deque.class, name);
if (method == null) {
TracingFramework.printError("Method Deque." + name + " not found. Class was mutated illegally.");
return false;
}
if (!returnsGenericClassType(method, classTypeParameter.getName())) {
return false;
}
return true;
}
/**
* Checks if a method returns the generic type parameter of the class.
*
* @param method The method to check.
* @param classTypeParameterName The name of the class's generic type parameter.
* @return true if the method returns the generic type parameter, false
* otherwise.
*/
private boolean returnsGenericClassType(Method method, String classTypeParameterName) {
Type returnType = method.getGenericReturnType();
String methodName = method.getName();
if (returnType instanceof TypeVariable) {
TypeVariable<?> typeVar = (TypeVariable<?>) returnType;
if (!typeVar.getName().equals(classTypeParameterName)) {
TracingFramework
.printError("Deque." + methodName + " should return the generic type parameter, but returns "
+ typeVar.getName() + " instead.");
return false;
}
} else {
TracingFramework
.printError("Deque." + methodName + " should return the generic type parameter, but returns "
+ returnType.getTypeName() + " instead.");
return false;
}
return true;
}
/**
* Gets the generic type parameter of the Deque class.
*
* @return The generic type parameter of the Deque class, null if not found.
*/
@SuppressWarnings("rawtypes")
private TypeVariable<Class<Deque>> getClassTypeParameter() {
Class<Deque> clazz = Deque.class;
TypeVariable<Class<Deque>>[] classTypeParameters = clazz.getTypeParameters();
if (classTypeParameters.length != 1) {
TracingFramework.printError("Deque should have one generic type parameter, but has "
+ classTypeParameters.length + " parameters.");
return null;
}
TypeVariable<Class<Deque>> classTypeParameter = classTypeParameters[0];
if (!classTypeParameter.getName().equals("T")) {
TracingFramework.printError("Deque's generic type parameter should be named T, but is named "
+ classTypeParameter.getName() + " instead.");
return null;
}
return classTypeParameter;
}
// Aufgabe 3 - Deque Exception - 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<String> 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<String> 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 = findUniqueMethod(Deque.class, "addFirst");
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<String> 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<String> 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<String> 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<String> 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);
}
/**
* 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;
}
}
/**
* Finds a unique method in a class by name.
*
* @param clazz The class to search in.
* @param name The name of the method.
* @return The found method, or null if not found or more than one method with
* the same name exists.
*/
public static Method findUniqueMethod(Class<?> clazz, String name) {
Method[] methods = clazz.getDeclaredMethods();
Method found = null;
for (Method method : methods) {
if (method.getName().equals(name)) {
if (found != null) {
TracingFramework.printError("More than one method named " + name
+ " found in class " + clazz.getName() + ". Class was mutated illegally.");
return null;
}
found = method;
}
}
return found;
}
/**
* 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<String> 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<String> 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<String> deque = buildDeque("A", "B", "C", "D", "E");
// should be executable five times when deque is not empty
Supplier<String> getter = () -> deque.getFirst().toString();
return getterThrowsAnyException(getter, 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<String> deque = buildDeque("A", "B", "C", "D", "E");
// should be executable five times when deque is not empty
Supplier<String> getter = () -> deque.getLast().toString();
return getterThrowsAnyException(getter, 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 <T> Deque<T> buildDeque(T... values) {
Deque<T> deque = new Deque<>();
for (T value : values) {
try {
// Create new Item
Item<T> 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<T> 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.
*/
@SuppressWarnings("unchecked")
private <T> Item<T> getBack(Deque<T> deque) {
try {
Field field = Deque.class.getDeclaredField("back");
field.setAccessible(true);
return (Item<T>) 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 <T> void setFront(Deque<T> deque, Item<T> 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 <T> void setBack(Deque<T> deque, Item<T> 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 <T> void setNext(Item<T> item, Item<T> 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 <T> void setPrev(Item<T> item, Item<T> 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();
}
}
}