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

695 lines
22 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 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, 1, 5, 0, 2, 4, 6 };
Tree tree = new Tree();
setRoot(tree, buildTree(values));
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 - 30 Punkte")
public void testTreeCount() {
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));
// 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 exception - 5 Punkte")
public void testDequeAddFirst() {
int points = 5;
boolean valid = true;
Deque deque = new Deque();
try {
deque.addFirst("A");
deque.addFirst("BCDEF");
deque.addFirst("Hallo");
} catch (Exception e) {
valid = false;
points -= 5;
TracingFramework.printError(
"Deque.addFirst(\"A\"), Deque.addFirst(\"BCDEF\"), or Deque.addFirst(\"Hallo\") threw an exception: "
+ e.getMessage()
+ ". Assuming method always throws an exception on valid values. Deducting 5 points.");
}
if (valid) {
try {
deque.addFirst(null);
TracingFramework
.printError("Deque.addFirst(null) should throw an exception, but did not. Deducting 2 points.");
points -= 2;
} catch (Exception e) {
if (!(e instanceof IllegalArgumentException)) {
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")) {
TracingFramework.printError(
"Deque.addFirst(null) should throw an IllegalArgumentException with message 'Value cannot be null', but got: "
+ e.getMessage() + ". Deducting 1 point.");
points -= 1;
}
}
try {
deque.addFirst("");
TracingFramework
.printError("Deque.addFirst(\"\") should throw an exception, but did not. Deducting 2 points.");
points -= 2;
} catch (Exception e) {
if (!(e instanceof IllegalArgumentException)) {
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")) {
TracingFramework.printError(
"Deque.addFirst(\"\") should throw an IllegalArgumentException with message 'Value cannot be null', but got: "
+ e.getMessage() + ". Deducting 1 point.");
points -= 1;
}
}
}
try {
Method method = Deque.class.getDeclaredMethod("addFirst", String.class);
Class<?>[] exceptionTypes = method.getExceptionTypes();
if (exceptionTypes.length == 0) {
TracingFramework.printError(
"Deque.addFirst(String) should throw an exception, but did not. Deducting 1 point.");
points -= 1;
} else if (exceptionTypes.length != 1) {
TracingFramework.printError("Deque.addFirst(String) should only throw one exception, but threw: "
+ Arrays.toString(exceptionTypes) + ". Deducting 1 point.");
points -= 1;
} else {
if (exceptionTypes[0] != IllegalArgumentException.class) {
TracingFramework.printError(
"Deque.addFirst(String) should throw an IllegalArgumentException, but threw: "
+ exceptionTypes[0].getName() + ". Deducting 1 point.");
points -= 1;
}
}
} catch (NoSuchMethodException e) {
TracingFramework.printError("Method Deque.addFirst(String) not found. Class was mutated illegally.");
}
addPoints(points);
}
@Test(name = "Aufgabe 3 - Deque - addLast exception - 5 Punkte")
public void testDequeAddLast() {
int points = 5;
boolean valid = true;
Deque deque = new Deque();
try {
deque.addLast("A");
deque.addLast("BCDEF");
deque.addLast("Hallo");
} catch (Exception e) {
valid = false;
points -= 5;
TracingFramework
.printError(
"Deque.addLast(\"A\"), Deque.addLast(\"BCDEF\"), or Deque.addLast(\"Hallo\") threw an exception: "
+ e.getMessage()
+ ". Assuming method always throws an exception on valid values. Deducting 5 points.");
}
if (valid) {
try {
deque.addLast(null);
TracingFramework
.printError("Deque.addLast(null) should throw an exception, but did not. Deducting 2 points.");
points -= 2;
} catch (Exception e) {
if (!(e instanceof IllegalArgumentException)) {
TracingFramework
.printError("Deque.addLast(null) should throw an IllegalArgumentException, but threw: "
+ e.getClass().getName() + ". Deducting 1 point.");
points -= 1;
}
if (e.getMessage() == null || !e.getMessage().equals("Value cannot be null")) {
TracingFramework.printError(
"Deque.addLast(null) should throw an IllegalArgumentException with message 'Value cannot be null', but got: "
+ e.getMessage() + ". Deducting 1 point.");
points -= 1;
}
}
try {
deque.addLast("");
TracingFramework
.printError("Deque.addLast(\"\") should throw an exception, but did not. Deducting 2 points.");
points -= 2;
} catch (Exception e) {
if (!(e instanceof IllegalArgumentException)) {
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")) {
TracingFramework.printError(
"Deque.addLast(\"\") should throw an IllegalArgumentException with message 'Value cannot be null', but got: "
+ e.getMessage() + ". Deducting 1 point.");
points -= 1;
}
}
}
try {
Method method = Deque.class.getDeclaredMethod("addLast", String.class);
Class<?>[] exceptionTypes = method.getExceptionTypes();
if (exceptionTypes.length == 0) {
TracingFramework.printError(
"Deque.addLast(String) should throw an exception, but did not. Deducting 1 point.");
points -= 1;
} else if (exceptionTypes.length != 1) {
TracingFramework.printError("Deque.addLast(String) should only throw one exception, but threw: "
+ Arrays.toString(exceptionTypes) + ". Deducting 1 point.");
points -= 1;
} else {
if (exceptionTypes[0] != IllegalArgumentException.class) {
TracingFramework.printError(
"Deque.addLast(String) should throw an IllegalArgumentException, but threw: "
+ exceptionTypes[0].getName() + ". Deducting 1 point.");
points -= 1;
}
}
} catch (NoSuchMethodException e) {
TracingFramework.printError("Method Deque.addLast(String) not found. Class was mutated illegally.");
}
addPoints(points);
}
@Test(name = "Aufgabe 3 - Deque - getFirst exception - 5 Punkte")
public void testDequeGetFirst() {
int points = 5;
boolean valid = true;
Deque deque = buildDeque("A", "BCDEF", "Hallo");
try {
deque.getFirst();
} catch (Exception e) {
valid = false;
points -= 5;
TracingFramework.printError(
"Deque.getFirst() threw an exception: " + e.getMessage()
+ ". Assuming method always throws an exception on none empty deque. Deducting 5 points.");
}
if (valid) {
setFront(deque, null);
setBack(deque, null);
try {
deque.getFirst();
TracingFramework.printError(
"Deque.getFirst() should throw an exception on empty deque, but did not. Deducting 4 points.");
points -= 4;
} catch (Exception e) {
if (!(e instanceof NoSuchElementException)) {
TracingFramework.printError(
"Deque.getFirst() should throw a NoSuchElementException on empty deque, but threw: "
+ e.getClass().getName() + ". Deducting 2 point.");
points -= 2;
}
if (e.getMessage() == null || !e.getMessage().equals("Deque is empty")) {
TracingFramework.printError(
"Deque.getFirst() should throw a NoSuchElementException with message 'Deque is empty', but got: "
+ e.getMessage() + ". Deducting 2 points.");
points -= 2;
}
}
}
try {
Method method = Deque.class.getDeclaredMethod("getFirst");
Class<?>[] exceptionTypes = method.getExceptionTypes();
if (exceptionTypes.length == 0) {
TracingFramework.printError(
"Deque.getFirst() should throw an exception, but did not. Deducting 1 point.");
points -= 1;
} else if (exceptionTypes.length != 1) {
TracingFramework.printError("Deque.getFirst() should only throw one exception, but threw: "
+ Arrays.toString(exceptionTypes) + ". Deducting 1 point.");
points -= 1;
} else {
if (exceptionTypes[0] != NoSuchElementException.class) {
TracingFramework.printError(
"Deque.getFirst() should throw a NoSuchElementException, but threw: "
+ exceptionTypes[0].getName() + ". Deducting 1 point.");
points -= 1;
}
}
} catch (Exception e) {
TracingFramework.printError("Method Deque.getFirst() not found. Class was mutated illegally.");
}
addPoints(points);
}
@Test(name = "Aufgabe 3 - Deque - getLast exception - 5 Punkte")
public void testDequeGetLast() {
int points = 5;
boolean valid = true;
Deque deque = buildDeque("A", "BCDEF", "Hallo");
try {
deque.getLast();
} catch (Exception e) {
valid = false;
points -= 5;
TracingFramework.printError(
"Deque.getLast() threw an exception: " + e.getMessage()
+ ". Assuming method always throws an exception on none empty deque. Deducting 5 points.");
}
if (valid) {
setFront(deque, null);
setBack(deque, null);
try {
deque.getLast();
TracingFramework.printError(
"Deque.getLast() should throw an exception on empty deque, but did not. Deducting 4 points.");
points -= 4;
} catch (Exception e) {
if (!(e instanceof NoSuchElementException)) {
TracingFramework.printError(
"Deque.getLast() should throw a NoSuchElementException on empty deque, but threw: "
+ e.getClass().getName() + ". Deducting 2 point.");
points -= 2;
}
if (e.getMessage() == null || !e.getMessage().equals("Deque is empty")) {
TracingFramework.printError(
"Deque.getLast() should throw a NoSuchElementException with message 'Deque is empty', but got: "
+ e.getMessage() + ". Deducting 2 points.");
points -= 2;
}
}
}
try {
Method method = Deque.class.getDeclaredMethod("getLast");
Class<?>[] exceptionTypes = method.getExceptionTypes();
if (exceptionTypes.length == 0) {
TracingFramework.printError(
"Deque.getLast() should throw an exception, but did not. Deducting 1 point.");
points -= 1;
} else if (exceptionTypes.length != 1) {
TracingFramework.printError("Deque.getLast() should only throw one exception, but threw: "
+ Arrays.toString(exceptionTypes) + ". Deducting 1 point.");
points -= 1;
} else {
if (exceptionTypes[0] != NoSuchElementException.class) {
TracingFramework.printError(
"Deque.getLast() should throw a NoSuchElementException, but threw: "
+ exceptionTypes[0].getName() + ". Deducting 1 point.");
points -= 1;
}
}
} catch (Exception e) {
TracingFramework.printError("Method Deque.getLast() not found. Class was mutated illegally.");
}
addPoints(points);
}
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("Summe: " + test.getResult());
}
public static void main(String[] args) {
try {
runAll();
} catch (Exception e) {
e.printStackTrace();
}
}
}