322 lines
8.3 KiB
Java
322 lines
8.3 KiB
Java
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
|
|
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 = "List - addLast - size - 15")
|
|
public void testListAdd() {
|
|
int points = 0;
|
|
|
|
List list = new List();
|
|
int size = list.size();
|
|
if (size != 0) {
|
|
TracingFramework.printError("A new list should have size 0 but was " + size);
|
|
} else {
|
|
points += 3;
|
|
}
|
|
|
|
try {
|
|
list.addLast("A");
|
|
size = list.size();
|
|
if (size != 1) {
|
|
TracingFramework.printError("add(\"A\") should add one element to the list");
|
|
} else {
|
|
points += 3;
|
|
}
|
|
} catch (Exception e) {
|
|
TracingFramework.printError("add(\"A\") should not throw an Exception. " + e.getClass().getName());
|
|
}
|
|
|
|
try {
|
|
list.addLast(null);
|
|
size = list.size();
|
|
if (size != 1) {
|
|
TracingFramework.printError("add(null) should not change the list");
|
|
} else {
|
|
points += 3;
|
|
}
|
|
} catch (Exception e) {
|
|
TracingFramework.printError("add(null) should not throw an Exception. " + e.getClass().getName());
|
|
}
|
|
|
|
try {
|
|
list.addLast("B");
|
|
size = list.size();
|
|
if (size != 2) {
|
|
TracingFramework.printError("add(\"B\") should add one element to the list");
|
|
} else {
|
|
points += 3;
|
|
}
|
|
} catch (Exception e) {
|
|
TracingFramework.printError("add(\"B\") should not throw an Exception. " + e.getClass().getName());
|
|
}
|
|
|
|
try {
|
|
list.addLast("");
|
|
size = list.size();
|
|
if (size != 2) {
|
|
TracingFramework.printError("add(\"\") should not add elements to the list");
|
|
} else {
|
|
points += 3;
|
|
}
|
|
} catch (Exception e) {
|
|
TracingFramework.printError("add(\"\") should not throw an Exception. " + e.getClass().getName());
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "List - removeLast - 5")
|
|
public void testListRemoveLastEmpty() {
|
|
int points = 0;
|
|
|
|
List list = new List();
|
|
try {
|
|
String value = list.removeLast();
|
|
if (value == null) {
|
|
points += 5;
|
|
} else {
|
|
points += 2;
|
|
TracingFramework
|
|
.printError("removeLast from empty list should return null, but was: \"" + value + "\"");
|
|
}
|
|
} catch (Exception e) {
|
|
TracingFramework
|
|
.printError("removeLast from empty list should not throw an Exception. " + e.getClass().getName());
|
|
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "List - addLast - removeLast - size - 30")
|
|
public void testList() {
|
|
int points = 0;
|
|
List list = new List();
|
|
list.addLast("A");
|
|
list.addLast("B");
|
|
list.addLast("C");
|
|
String value = list.removeLast();
|
|
int size = list.size();
|
|
if (size == 2) {
|
|
points += 5;
|
|
} else {
|
|
TracingFramework.printError("A list with 2 elements should have size 2 but was " + size);
|
|
}
|
|
if ("C".equals(value)) {
|
|
points += 5;
|
|
} else {
|
|
TracingFramework.printError("Remove last of A,B,C should return C but was " + value);
|
|
}
|
|
value = list.removeLast();
|
|
size = list.size();
|
|
if (size == 1) {
|
|
points += 5;
|
|
} else {
|
|
TracingFramework.printError("A list with 1 element should have size 1 but was " + size);
|
|
}
|
|
if ("B".equals(value)) {
|
|
points += 5;
|
|
} else {
|
|
TracingFramework.printError("Remove last of A,B should return B but was " + value);
|
|
}
|
|
value = list.removeLast();
|
|
size = list.size();
|
|
if (size == 0) {
|
|
points += 5;
|
|
} else {
|
|
TracingFramework.printError("A list with 0 elements should have size 0 but was " + size);
|
|
}
|
|
if ("A".equals(value)) {
|
|
points += 5;
|
|
} else {
|
|
TracingFramework.printError("Remove last of A should return A but was " + value);
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Tree - traverse")
|
|
public void testTreeTraverse() {
|
|
int points = 0;
|
|
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)) {
|
|
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 pre-order (point deduction) but complete.");
|
|
} else {
|
|
TracingFramework.printError("Traversal " + Arrays.toString(actual) + " is incomplete (no points).");
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Tree - count")
|
|
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 += 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 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();
|
|
}
|
|
}
|
|
}
|