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

328 lines
10 KiB
Java

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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 = "Stack - add - size - 15")
public void testStackAdd() {
int points = 0;
Stack stack = new Stack();
int size = stack.size();
if (size != 0) {
TracingFramework.printError("A new Stack should have size 0 but was " + size);
} else {
points += 3;
}
try {
stack.addToTop("A");
size = stack.size();
if (size != 1) {
TracingFramework.printError("addToTop(\"A\") should add one element to the stack");
} else {
points += 3;
}
} catch (Exception e) {
TracingFramework.printError("addToTop(\"A\") should not throw an Exception. " + e.getClass().getName());
}
try {
stack.addToTop(null);
size = stack.size();
if (size != 1) {
TracingFramework.printError("addToTop(null) should not change the stack");
} else {
points += 3;
}
} catch (Exception e) {
TracingFramework.printError("addToTop(null) should not throw an Exception. " + e.getClass().getName());
}
try {
stack.addToTop("B");
size = stack.size();
if (size != 2) {
TracingFramework.printError("addToTop(\"B\") should add one element to the stack");
} else {
points += 3;
}
} catch (Exception e) {
TracingFramework.printError("addToTop(\"B\") should not throw an Exception. " + e.getClass().getName());
}
try {
stack.addToTop("");
size = stack.size();
if (size != 2) {
TracingFramework.printError("addToTop(\"\") should not add elements to the stack");
} else {
points += 3;
}
} catch (Exception e) {
TracingFramework.printError("addToTop(\"\") should not throw an Exception. " + e.getClass().getName());
}
addPoints(points);
}
@Test(name = "Stack - remove - 5")
public void testStackRemove() {
int points = 0;
Stack stack = new Stack();
try {
String value = stack.removeFromTop();
if (value == null) {
points += 5;
} else {
points += 2;
TracingFramework
.printError("removeFromTop from empty stack should return null, but was: \"" + value + "\"");
}
} catch (Exception e) {
TracingFramework
.printError(
"removeFromTop from empty stack should not throw an Exception. " + e.getClass().getName());
}
addPoints(points);
}
@Test(name = "Stack - add - remove - size - 30")
public void testStack() {
int points = 0;
Stack stack = new Stack();
stack.addToTop("A");
stack.addToTop("B");
stack.addToTop("C");
String value = stack.removeFromTop();
int size = stack.size();
if (size == 2) {
points += 5;
} else {
TracingFramework.printError("A stack with 2 elements should have size 2 but was " + size);
}
if ("C".equals(value)) {
points += 5;
} else {
TracingFramework.printError("Remove top of A,B,C should return C but was " + value);
}
value = stack.removeFromTop();
size = stack.size();
if (size == 1) {
points += 5;
} else {
TracingFramework.printError("A stack with 1 element should have size 1 but was " + size);
}
if ("B".equals(value)) {
points += 5;
} else {
TracingFramework.printError("Remove top of A,B should return B but was " + value);
}
value = stack.removeFromTop();
size = stack.size();
if (size == 0) {
points += 5;
} else {
TracingFramework.printError("A stack with 0 elements should have size 0 but was " + size);
}
if ("A".equals(value)) {
points += 5;
} else {
TracingFramework.printError("Remove top of A should return A but was " + value);
}
addPoints(points);
}
@Test(name = "Tree - traverse - 25")
public void testTreeTraverse() {
int points = 0;
double[] values = new double[] { 3, 5, 1, 6, 4, 2, 0 };
Tree tree = new Tree();
setRoot(tree, buildTree(values));
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)) {
points += 25;
} else {
double[] expectSort = expect.clone();
double[] actualSort = actual.clone();
Arrays.sort(expectSort);
Arrays.sort(actualSort);
if (Arrays.equals(expectSort, actualSort)) {
points += 15;
TracingFramework.printError("Traversal " + Arrays.toString(actual)
+ " is not post-order (point deduction) but complete.");
} else {
TracingFramework.printError(
"Traversal " + Arrays.toString(actual) + " is incomplete (no points).");
}
}
addPoints(points);
}
@Test(name = "Tree - count - 25")
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 += 6;
} else {
TracingFramework.printError("Count(5) should return 1 , but was " + count);
}
// 7 should not be found
count = tree.count(7);
if (count == 0) {
points += 6;
} 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 += 13;
} else if (count == 3) {
points += 5;
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);
}
/**
* 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.");
}
}
/**
* The points of each test case.
*/
private 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();
}
}
}