Files
Testate/WS2425/Ersatztestat/Tests.java
T
2025-07-21 16:03:34 +02:00

366 lines
12 KiB
Java

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
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 {
public static void main(String[] args) {
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());
}
@Test(name = "Student - Interface - 10")
public void testStudentInterface() {
int points = 0;
if (implementsInterface(Student.class, Person.class)) {
points += 10;
} else {
TracingFramework.printError("Student does not implement Person.");
}
addPoints(points);
}
@Test(name = "Student - Variables - 10")
public void testStudentVariables() {
int points = 0;
if (hasVariableOfType(Student.class, "name", String.class)) {
points += 3;
if (hasPrivateVariable(Student.class, "name")) {
points += 2;
} else {
TracingFramework.printError("Student.name should be private.");
}
} else {
TracingFramework.printError("Student should contain String field name.");
}
if (hasVariableOfType(Student.class, "id", long.class)) {
points += 3;
if (hasPrivateVariable(Student.class, "id")) {
points += 2;
} else {
TracingFramework.printError("Student.id should be private.");
}
} else {
TracingFramework.printError("Student should contain long field id.");
}
addPoints(points);
}
@Test(name = "Student - Getters - 10")
public void testStudentGetters() {
int points = 0;
Student s = new Student("Max Mustermann", 42);
if (s.getId() == 42) {
points += 5;
} else {
TracingFramework.printError("Student.getId() does not return the value passed into the constructor.");
}
if ("Max Mustermann".equals(s.getName())) {
points += 5;
} else {
TracingFramework.printError("Student.getName() does not return the value passed into the constructor.");
}
addPoints(points);
}
@Test(name = "Student - Equals - 10")
public void testStudentEquals() {
int points = 0;
Student a = new Student("a", 1);
Student b = new Student("b", 2);
Student c = new Student("c", 1);
if (a.equals(a) && a.equals(c) && !a.equals(b)) {
points += 7;
} else {
TracingFramework.printError("Student.equals() does not work right.");
}
try {
if (!a.equals("hello")) {
points += 3;
} else {
TracingFramework.printError("Student.equals() does not work for string.");
}
} catch (Exception e) {
TracingFramework
.printError("Student.equals() throws an exception for string: " + e.getClass().getSimpleName());
}
addPoints(points);
}
@Test(name = "Operation - average - 10")
public void testOperationAverage() {
int points = 0;
double[] values = new double[] { 1, 2, 3, 4, 5 };
double expect = 3;
double actual = Operation.average(values);
if (expect == actual) {
points += 10;
} else {
TracingFramework.printError("average([1, 2, 3, 4, 5]) should be 3 but was " + actual);
}
addPoints(points);
}
@Test(name = "Operation - average - null - 5")
public void testOperationAverageNull() {
int points = 0;
try {
Operation.average(null);
TracingFramework.printError("average(null) should throw IllegalArgumentException but did not.");
} catch (IllegalArgumentException e) {
points += 5;
} catch (Exception e) {
TracingFramework.printError(
"average(null) should throw IllegalArgumentException but threw: " + e.getClass().getSimpleName());
}
addPoints(points);
}
@Test(name = "Operation - average - empty - 5")
public void testOperationAverageEmpty() {
int points = 0;
try {
Operation.average(new double[] {});
TracingFramework.printError("average(new double[]{}) should throw IllegalArgumentException but did not.");
} catch (IllegalArgumentException e) {
points += 5;
} catch (Exception e) {
TracingFramework.printError(
"average(new double[]{}) should throw IllegalArgumentException but threw: "
+ e.getClass().getSimpleName());
}
addPoints(points);
}
@Test(name = "LinkedList - size - 10")
public void testLinkedListSize() {
int points = 0;
LinkedList list = new LinkedList() { // Added to not penalise students having only implemented size
public void prepend(String value) {
try {
Field f = LinkedList.class.getDeclaredField("head");
f.setAccessible(true);
ListItem head = (ListItem) f.get(this);
ListItem item = new ListItem(value);
item.setNext(head);
f.set(this, item);
} catch (Exception e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < 10; i++) {
int size = list.size();
if (size == i) {
points += 1;
} else {
TracingFramework.printError("Size should be " + i + " but was " + size + ".");
}
list.prepend("xx");
}
addPoints(points);
}
@Test(name = "LinkedList - prepend - 10")
public void testLinkedListPrepend() {
int points = 0;
LinkedList list = new LinkedList() { // Added to not penalise students having only implemented prepend
public int size() {
try {
Field f = LinkedList.class.getDeclaredField("head");
f.setAccessible(true);
ListItem current = (ListItem) f.get(this);
int size = 0;
while (current != null) {
current = current.getNext();
size += 1;
}
return size;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
};
for (int i = 0; i < 10; i++) {
int size = list.size();
if (size == i) {
points += 1;
} else {
TracingFramework.printError("Size should be " + i + " but was " + size + ".");
}
list.prepend("xx");
}
addPoints(points);
}
@Test(name = "LinkedList - get - 10")
public void testLinkedListGet() {
int points = 0;
LinkedList list = new LinkedList() { // Added to not penalise students having only implemented get
public void prepend(String value) {
try {
Field f = LinkedList.class.getDeclaredField("head");
f.setAccessible(true);
ListItem head = (ListItem) f.get(this);
ListItem item = new ListItem(value);
item.setNext(head);
f.set(this, item);
} catch (Exception e) {
e.printStackTrace();
}
}
};
for (int i = 9; i >= 0; i--) {
list.prepend("test" + i);
}
for (int i = 0; i < 10; i++) {
try {
String s = list.get(i);
if (s == null || !s.equals("test" + i)) {
TracingFramework.printError("Get is not test" + i + " but " + s);
} else {
points += 1;
}
} catch (Exception e) {
TracingFramework.printError("Get throws exception: " + e.getClass().getSimpleName());
}
}
addPoints(points);
}
@Test(name = "LinkedList - get - Exceptions - 10")
public void testLinkedListGetExceptions() {
int points = 0;
LinkedList list = new LinkedList();
try {
list.get(-1);
TracingFramework.printError("Get -1 does not throw an exception.");
} catch (IllegalArgumentException e) {
points += 5;
} catch (Exception e) {
TracingFramework.printError("Get -1 does not throw the right exception: " + e.getClass().getSimpleName());
}
try {
list.get(10);
TracingFramework.printError("Get 10 does not throw an exception.");
} catch (IllegalArgumentException e) {
points += 5;
} catch (Exception e) {
TracingFramework.printError("Get 10 does not throw the right exception: " + e.getClass().getSimpleName());
}
addPoints(points);
}
/**
* 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);
}
public static boolean implementsInterface(Class<?> clazz, Class<?> interfaceClass) {
Class<?>[] interfaces = clazz.getInterfaces();
return Arrays.stream(interfaces).anyMatch(interfaceClass::equals);
}
public static boolean hasVariableOfType(Class<?> clazz, String fieldName, Class<?> fieldType) {
try {
Field field = clazz.getDeclaredField(fieldName);
return field.getType().equals(fieldType);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static boolean hasPrivateVariable(Class<?> clazz, String fieldName) {
try {
Field field = clazz.getDeclaredField(fieldName);
return Modifier.isPrivate(field.getModifiers());
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}