Finalized Testat 1.

This commit is contained in:
2025-09-04 12:47:27 +02:00
parent ef03336ecd
commit c9355b3a08
10 changed files with 353 additions and 285 deletions
+3 -3
View File
@@ -25,15 +25,15 @@ public class Tests {
if (info == null)
return 0;
String whiteSpaceMessed = "\s*" + firstName + "\s*" + surname + "\s*\:\s*" + title + "\s*\(\s*"
+ yearOfPublication + "\s*\)\s*";
String whiteSpaceMessed = "\\s*" + firstName + "\\s*" + surname + "\\s*:\\s*" + title + "\\s*\\(\\s*"
+ yearOfPublication + "\\s*\\)\\s*";
if (info.matches(whiteSpaceMessed)) {
TracingFramework.printError("Die Ausgabe hat eine fehlerhafte Anzahl von whitespaces");
return 39;
}
String rightOrderWithoutSpecialChars = ".*" + firstName + ".*" + surname + ".*" + title + ".*"
+ yearOfPublication + ".*\).*";
+ yearOfPublication + ".*\\).*";
if (info.matches(rightOrderWithoutSpecialChars)) {
TracingFramework.printError("Die Ausgabe gibt die gewollten Eigenschaften nur in der richtigen Reihenfolge aus.");
return 30;
-1
View File
@@ -1,4 +1,3 @@
/**
* A class representing an item in a fridge.
*/
+39 -28
View File
@@ -7,47 +7,58 @@ public class Main {
* sondern nur einige, die Ihnen helfen sollen, Ihre Lösungen zu überprüfen.
*/
public static void main(String[] args) {
System.out.println("Aufgabe 1");
System.out.println("-----");
System.out.println("Eingabearray: [1, 2, 3, 4, 5]");
System.out.println("Grenzwert: 2");
System.out.println("Erwartete Ausgabe: 4.0");
System.out.println("Ihre Ausgabe: " + Testat1A.calculateMeanAbove(new int[] { 1, 2, 3, 4, 5 }, 2));
System.out.println();
System.out.println("Eingabearray: [-10, 0, 10, 20, 4, -7, 4]");
System.out.println("Grenzwert: 0");
System.out.println("Erwartete Ausgabe: 9.5");
System.out.println("Ihre Ausgabe: " + Testat1A.calculateMeanAbove(new int[] { -10, 0, 10, 20, 4, -7, 4 }, 0));
System.out.println();
System.out.println("Eingabearray: null");
System.out.println("Grenzwert: 5");
System.out.println("Erwartete Ausgabe: 0.0");
System.out.println("Ihre Ausgabe: " + Testat1A.calculateMeanAbove(null, 5));
System.out.println("=========================================");
System.out.println("Aufgabe 1 - Arrays");
System.out.println("=========================================");
System.out.println();
System.out.println("Input: [1, 2, 3, 4, 5]");
System.out.println("Threshold: 2");
System.out.println("Expected Output: 4.0");
System.out.println("Your Output: " + Testat1A.calculateMeanAbove(new int[] { 1, 2, 3, 4, 5 }, 2));
System.out.println();
System.out.println("Input: [-10, 0, 10, 20, 4, -7, 4]");
System.out.println("Threshold: 0");
System.out.println("Expected Output: 9.5");
System.out.println("Your Output: " + Testat1A.calculateMeanAbove(new int[] { -10, 0, 10, 20, 4, -7, 4 }, 0));
System.out.println();
System.out.println("Input: null");
System.out.println("Threshold: 5");
System.out.println("Expected Output: 0.0");
System.out.println("Your Output: " + Testat1A.calculateMeanAbove(null, 5));
System.out.println();
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 2 - 2d-Arrays");
System.out.println("=========================================");
System.out.println();
System.out.println("Aufgabe 2");
System.out.println("-----");
int[][] array = { { 1, 2, 3 }, { 4, 5 }, { 6 }, null };
double[] means = Testat1A.calculateRowMean(array);
System.out.println("Eingabearray: [[1, 2, 3], [4, 5], [6], null]");
System.out.println("Erwartete Ausgabe: [2.0, 4.5, 6.0, 0.0]");
System.out.println("Ihre Ausgabe: " + Arrays.toString(means));
System.out.println("Input: [[1, 2, 3], [4, 5], [6], null]");
System.out.println("Expected Output: [2.0, 4.5, 6.0, 0.0]");
System.out.println("Your Output: " + Arrays.toString(means));
System.out.println();
array = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
means = Testat1A.calculateRowMean(array);
System.out.println("Eingabearray: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]");
System.out.println("Erwartete Ausgabe: [2.0, 5.0, 8.0]");
System.out.println("Ihre Ausgabe: " + Arrays.toString(means));
System.out.println("Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]");
System.out.println("Expected Output: [2.0, 5.0, 8.0]");
System.out.println("Your Output: " + Arrays.toString(means));
System.out.println();
array = new int[][] { { 1, 2 }, { 1, 2 }, { 1, 2 }, {} };
means = Testat1A.calculateRowMean(array);
System.out.println("Eingabearray: [[1, 2], [1, 2], [1, 2], []]");
System.out.println("Erwartete Ausgabe: [1.5, 1.5, 1.5, 0.0]");
System.out.println("Ihre Ausgabe: " + Arrays.toString(means));
System.out.println("Input: [[1, 2], [1, 2], [1, 2], []]");
System.out.println("Expected Output: [1.5, 1.5, 1.5, 0.0]");
System.out.println("Your Output: " + Arrays.toString(means));
System.out.println();
System.out.println("Aufgabe 3");
System.out.println("-----");
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 3 - Gegenstände");
System.out.println("=========================================");
System.out.println();
// create items
FridgeItem gouda = new Cheese(500.5, "Gouda", "yellow");
+124 -150
View File
@@ -3,49 +3,19 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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) {
Class<Tests> clazz = Tests.class;
Tests test = new Tests();
System.out.println(String.format("Tests: %s", test.getClass().getName()));
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.points.size();
try {
method.invoke(test);
if (test.points.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.points.get(test.points.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()));
}
private static final String TESTAT_NAME = "Testat 1A";
// Aufgabe 1 - 25 Punkte
@@ -82,8 +52,9 @@ public class Tests {
public void testCalculateMeanAbove(int[] arr, int threshold, double expected, int points) {
double result = Testat1A.calculateMeanAbove(arr, threshold);
if (result != expected) {
TracingFramework.printError(String.format("FAIL: Expected %f for %s with threshold %d, but got %f",
TracingFramework.printError(String.format("Expected %f for %s with threshold %d, but got %f",
expected, Arrays.toString(arr), threshold, result));
addPoints(0);
} else {
addPoints(points);
}
@@ -134,30 +105,31 @@ public class Tests {
testCalculateRowMean(arr, new double[] { 2.0, 5.0, 8.0 }, 7);
}
public void testCalculateRowMean(int[][] arr, double[] expected, int points) {
double[] result = Testat1A.calculateRowMean(arr);
private void test2DArrayOperation(int[][] arr, double[] expected, int points, Function<int[][], double[]> func) {
double[] result = func.apply(arr);
if (Arrays.equals(result, expected)) {
addPoints(points);
} else {
TracingFramework.printError(String.format("FAIL: Expected %s for %s, but got %s",
TracingFramework.printError(String.format("Expected %s for %s, but got %s",
Arrays.toString(expected), Arrays.deepToString(arr), Arrays.toString(result)));
addPoints(0);
}
}
public void testCalculateRowMean(int[][] arr, double[] expected, int points) {
test2DArrayOperation(arr, expected, points, Testat1A::calculateRowMean);
}
// Aufgabe 3 - 50 Punkte
class TestItem extends FridgeItem {
public TestItem(double weight, String name, String color) {
super(weight, name, color);
}
}
@Test(name = "Aufgabe 3a - private fields - 6 points")
public void testA3PrivateFields() {
int points = 6;
points -= testField(FridgeItem.class, "weight", double.class, 2);
points -= testField(FridgeItem.class, "name", String.class, 2);
points -= testField(FridgeItem.class, "color", String.class, 2);
addPoints(points);
}
@@ -168,7 +140,7 @@ public class Tests {
double weight = 42.314159;
String name = "Foo";
String color = "Red";
FridgeItem item = new TestItem(weight, name, color);
FridgeItem item = new FridgeItem(weight, name, color);
points -= checkConstructorField(FridgeItem.class, "weight", double.class, item, weight, 2);
points -= checkConstructorField(FridgeItem.class, "name", String.class, item, name, 2);
@@ -180,33 +152,33 @@ public class Tests {
@Test(name = "Aufgabe 3c - weightful interface - 8 points")
public void testA3WeightfulInterface() {
int points = 8;
Class<?> clazz = FridgeItem.class;
if (!implementsInterface(clazz, Weightful.class)) {
points -= 4;
TracingFramework
.printError(String.format("FAIL: Class '%s' does not implement interface '%s'. Deducting 4 points.",
.printError(String.format("Class '%s' does not implement interface '%s'. Deducting 4 points.",
clazz.getName(), Weightful.class.getName()));
}
Optional<Method> method = findMethod(FridgeItem.class, "getWeight", double.class);
if (method.isEmpty()) {
Method method = findMethod(FridgeItem.class, "getWeight", double.class);
if (method == null) {
points -= 4;
TracingFramework
.printError(String.format("FAIL: Method 'getWeight' not found in class '%s'. Deducting 4 points.",
.printError(String.format("Method 'getWeight' not found in class '%s'. Deducting 4 points.",
clazz.getName()));
} else {
Method getWeightMethod = method.get();
double weight = 42.314159;
FridgeItem item = new TestItem(weight, "Foo", "Red");
FridgeItem item = new FridgeItem(weight, "Foo", "Red");
try {
double result = (double) getWeightMethod.invoke(item);
double result = (double) method.invoke(item);
if (result != weight) {
TracingFramework.printError(String.format("FAIL: Expected getWeight() to return %f, but got %f",
TracingFramework.printError(String.format("Expected getWeight() to return %f, but got %f",
weight, result));
points = 0;
}
} catch (IllegalAccessException | InvocationTargetException e) {
TracingFramework
.printError(String.format("FAIL: Could not invoke getWeight() on item of class '%s'",
.printError(String.format("Could not invoke getWeight() on item of class '%s'",
item.getClass().getName()));
points = 0;
}
@@ -220,15 +192,15 @@ public class Tests {
int points = 10;
if (!overridesMethod(FridgeItem.class, "toString")) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not override method 'toString'",
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
FridgeItem.class.getName()));
points = 0;
} else {
FridgeItem item = new TestItem(42.314159, "Foo", "Red");
FridgeItem item = new FridgeItem(42.314159, "Foo", "Red");
String expected = "Foo (Red 42.314159 g)";
points -= testToString(item, expected, 5);
FridgeItem item2 = new TestItem(100.0, "Milk", "White");
FridgeItem item2 = new FridgeItem(100.0, "Milk", "White");
String expected2 = "Milk (White 100.0 g)";
points -= testToString(item2, expected2, 5);
}
@@ -241,13 +213,13 @@ public class Tests {
int points = 5;
if (!extendsClass(Cheese.class, FridgeItem.class)) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not extend class '%s'",
TracingFramework.printError(String.format("Class '%s' does not extend class '%s'",
Cheese.class.getName(), FridgeItem.class.getName()));
points -= 5;
} else {
Cheese cheese = createInstance(Cheese.class, 42.0, "Test Cheese", "Yellow");
if (cheese == null) {
TracingFramework.printError(String.format("FAIL: Could not create instance of class '%s'",
TracingFramework.printError(String.format("Could not create instance of class '%s'",
Cheese.class.getName()));
points = 0;
}
@@ -261,44 +233,15 @@ public class Tests {
int points = 5;
if (!extendsClass(Yoghurt.class, FridgeItem.class)) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not extend class '%s'",
TracingFramework.printError(String.format("Class '%s' does not extend class '%s'",
Yoghurt.class.getName(), FridgeItem.class.getName()));
points -= 5;
} else {
Yoghurt yoghurt = createInstance(Yoghurt.class, 42.0, "Test Yoghurt", "White");
if (yoghurt == null) {
TracingFramework.printError(String.format("FAIL: could not create instance of class '%s'",
TracingFramework.printError(String.format("could not create instance of class '%s'",
Yoghurt.class.getName()));
points = 0;
} else {
List<String> expectedTraceExcerpt = List.of("TraceableItem", "FridgeItem", "Yoghurt");
Method method = getMethod(Yoghurt.class, "getTrace");
if (method == null) {
TracingFramework.printError(String.format("FAIL: Method 'getTrace' not found in class '%s'",
Yoghurt.class.getName()));
points -= 5;
} else {
try {
Object rawTrace = method.invoke(yoghurt);
List<?> rawList = (List<?>) rawTrace;
List<String> classTrace = rawList.stream()
.filter(StackTraceElement.class::isInstance)
.map(StackTraceElement.class::cast)
.map(StackTraceElement::getClassName)
.collect(Collectors.toList());
int index = Collections.indexOfSubList(classTrace, expectedTraceExcerpt);
if (index == -1) {
TracingFramework.printError(
String.format(
"FAIL: Yoghurt does not call super constructor. Creation class trace: %s",
classTrace));
points -= 2;
}
} catch (IllegalAccessException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@@ -308,14 +251,15 @@ public class Tests {
@Test(name = "Aufgabe 3g - Cheese overrides toString - 5 points")
public void testA3CheeseOverridesToString() {
int points = 5;
if (!overridesMethod(Cheese.class, "toString")) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not override method 'toString'",
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
Cheese.class.getName()));
points = 0;
} else {
Cheese cheese = createInstance(Cheese.class, 100.0, "Cheddar", "Yellow");
if (cheese == null) {
TracingFramework.printError(String.format("FAIL: Could not create instance of class '%s'",
TracingFramework.printError(String.format("Could not create instance of class '%s'",
Cheese.class.getName()));
points = 0;
} else {
@@ -323,20 +267,22 @@ public class Tests {
points -= testToString(cheese, expected, 5);
}
}
addPoints(points);
}
@Test(name = "Aufgabe 3g - Yoghurt overrides toString - 5 points")
public void testA3YoghurtOverridesToString() {
int points = 5;
if (!overridesMethod(Yoghurt.class, "toString")) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not override method 'toString'",
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
Yoghurt.class.getName()));
points = 0;
} else {
Yoghurt yoghurt = createInstance(Yoghurt.class, 200.0, "Greek Yoghurt", "White");
if (yoghurt == null) {
TracingFramework.printError(String.format("FAIL: Could not create instance of class '%s'",
TracingFramework.printError(String.format("Could not create instance of class '%s'",
Yoghurt.class.getName()));
points = 0;
} else {
@@ -344,9 +290,20 @@ public class Tests {
points -= testToString(yoghurt, expected, 5);
}
}
addPoints(points);
}
/**
* Creates a new instance of the specified class with the given constructor
* arguments.
*
* @param <T> The type of the class to instantiate.
* @param clazz The class to instantiate.
* @param args The constructor arguments.
* @return A new instance of the specified class, or null if instantiation
* failed.
*/
public static <T> T createInstance(Class<T> clazz, Object... args) {
try {
Class<?>[] argTypes = Arrays.stream(args).map(Object::getClass).map(c -> {
@@ -355,7 +312,6 @@ public class Tests {
} else if (c.getName().equals("java.lang.Double")) {
return double.class;
}
return c;
}).toArray(Class<?>[]::new);
Constructor<T> constructor = clazz.getDeclaredConstructor(argTypes);
@@ -376,17 +332,27 @@ public class Tests {
}
}
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?> returnType,
/**
* Finds a method with the given name, return type, and parameter types in the
* specified class or its superclasses.
*
* @param clazz The class to search in.
* @param methodName The name of the method to find.
* @param returnType The return type of the method.
* @param parameterTypes The parameter types of the method.
* @return The method if found, null otherwise.
*/
public static Method findMethod(Class<?> clazz, String methodName, Class<?> returnType,
Class<?>... parameterTypes) {
try {
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
if (method.getReturnType().equals(returnType)) {
return Optional.of(method);
return method;
}
} catch (NoSuchMethodException e) {
// Method not found
e.printStackTrace();
}
return Optional.empty();
return null;
}
/**
@@ -415,17 +381,17 @@ public class Tests {
private static int testToString(Object obj, String expected, int maxPoints) {
String result = obj.toString();
if (result == null) {
TracingFramework.printError(String.format("FAIL: toString() returned null for object of class '%s'",
TracingFramework.printError(String.format("toString() returned null for object of class '%s'",
obj.getClass().getName()));
return maxPoints;
} else if (!result.equals(expected)) {
if (result.replaceAll("\\s+", " ").trim().equals(expected.replaceAll("\\s+", " ").trim())) {
TracingFramework.printError(String.format(
"FAIL: toString() returned '%s' for object of class '%s', but expected '%s' (ignoring whitespace)",
"toString() returned '%s' for object of class '%s', but expected '%s' (ignoring whitespace)",
result, obj.getClass().getName(), expected));
return maxPoints / 2;
} else {
TracingFramework.printError(String.format("FAIL: Expected toString() to return '%s', but got '%s'",
TracingFramework.printError(String.format("Expected toString() to return '%s', but got '%s'",
expected, result));
return maxPoints;
}
@@ -433,23 +399,6 @@ public class Tests {
return 0;
}
/**
* Returns the method with the given name and parameter types from the
* specified class or its superclasses.
*
* @param clazz The class to check.
* @param methodName The name of the method to find.
* @param parameterTypes The parameter types of the method to find.
* @return The method if found, null otherwise.
*/
private static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
try {
return clazz.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
return null;
}
}
/**
* Returns whether the given class overrides the specified method.
*
@@ -483,14 +432,14 @@ public class Tests {
int maxPoints) {
Field field = getField(clazz, fieldName, type);
if (field == null) {
TracingFramework.printError(String.format("FAIL: Field '%s' of type %s not found in class '%s'", fieldName,
TracingFramework.printError(String.format("Field '%s' of type %s not found in class '%s'", fieldName,
type.getName(), clazz.getName()));
return maxPoints;
}
T value = readField(field, type, instance);
if (!Objects.equals(value, expected)) {
TracingFramework.printError(String.format("FAIL: Expected field '%s' to be %s, but got %s", fieldName,
TracingFramework.printError(String.format("Expected field '%s' to be %s, but got %s", fieldName,
expected, value));
return maxPoints;
}
@@ -512,12 +461,12 @@ public class Tests {
Field field = getField(clazz, fieldName, type);
if (field == null) {
TracingFramework
.printError(String.format("FAIL: Field '%s' of type %s not found in class '%s'", fieldName,
.printError(String.format("Field '%s' of type %s not found in class '%s'", fieldName,
type.getName(), clazz.getName()));
return maxPoints;
} else if (!isPrivateField(field)) {
TracingFramework.printError(
String.format("FAIL: Field '%s' in class '%s' is not private", fieldName, clazz.getName()));
String.format("Field '%s' in class '%s' is not private", fieldName, clazz.getName()));
return maxPoints / 2;
}
return 0;
@@ -595,24 +544,6 @@ public class Tests {
}
}
/**
* Returns true if the given class has a constructor with the specified
* parameter types.
*
* @param clazz The class to check.
* @param parameterTypes The parameter types of the constructor.
* @return True if the class has a constructor with the specified parameter
* types, false otherwise.
*/
public boolean hasConstructor(Class<?> clazz, Class<?>... parameterTypes) {
try {
clazz.getDeclaredConstructor(parameterTypes);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Returns true if the given class implements the specified interface.
*
@@ -627,31 +558,74 @@ public class Tests {
/**
* The points of each test case.
*/
private List<Integer> points = new ArrayList<>();
private List<Integer> testPoints = new ArrayList<>();
/**
* This method will be called by jack after all
* test have been executed to get the resulting
* points.
* 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) {
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.
* 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);
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();
}
}
}
+67 -1
View File
@@ -1,3 +1,5 @@
import java.util.Arrays;
public class Main {
/**
* Die folgende main-Methode dient Ihnen zur Hilfe, um Ihre Lösungen zutesten.
@@ -5,13 +7,77 @@ public class Main {
* sondern nur einige, die Ihnen helfen sollen, Ihre Lösungen zu überprüfen.
*/
public static void main(String[] args) {
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 1 - Arrays");
System.out.println("=========================================");
System.out.println();
System.out.println("Input: [1, 2, 3, 4, 5]");
System.out.println("Threshold: 2");
System.out.println("Expected Output: 4.0");
System.out.println("Your output: " + Testat1B.calculateMeanBelow(new int[] { 1, 2, 3, 4, 5 }, 2));
System.out.println();
System.out.println("Input: [-10, 0, 10, 20, 4, -7, 4]");
System.out.println("Threshold: 0");
System.out.println("Expected Output: -8.5");
System.out.println("Your output: " + Testat1B.calculateMeanBelow(new int[] { -10, 0, 10, 20, 4, -7, 4 }, 0));
System.out.println();
System.out.println("Input: null");
System.out.println("Threshold: 5");
System.out.println("Expected Output: 0.0");
System.out.println("Your output: " + Testat1B.calculateMeanBelow(null, 5));
System.out.println();
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 2 - 2d-Arrays");
System.out.println("=========================================");
System.out.println();
int[][] array = { { 1, 2, 3 }, { 4, 5 }, { 6 }, null };
double[] means = Testat1B.calculateColumnMean(array);
System.out.println("Input: [[1, 2, 3], [4, 5], [6], null]");
System.out.println("Expected Output: [2.0, 4.5, 6.0, 0.0]");
System.out.println("Your output: " + Arrays.toString(means));
System.out.println();
array = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
means = Testat1B.calculateColumnMean(array);
System.out.println("Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]");
System.out.println("Expected Output: [2.0, 5.0, 8.0]");
System.out.println("Your output: " + Arrays.toString(means));
System.out.println();
array = new int[][] { { 1, 2 }, { 1, 2 }, { 1, 2 }, {} };
means = Testat1B.calculateColumnMean(array);
System.out.println("Input: [[1, 2], [1, 2], [1, 2], []]");
System.out.println("Expected Output: [1.5, 1.5, 1.5, 0.0]");
System.out.println("Your output: " + Arrays.toString(means));
System.out.println();
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 3 - Gegenstände");
System.out.println("=========================================");
System.out.println();
// create items
MediaItem longMovie = new Movie(241, "The Lord of the Rings", 5);
MediaItem shortMovie = new Movie(109, "Deadpool", 42);
MediaItem shortSeries = new Series(1230, "SpongeBob", 7);
MediaItem longSeries = new Series(2930, "Breaking Bad", 4);
// toString - Aufgabe 1
// Lasting
System.out.println("Expected (Movie.getDuration()): 241");
System.out.println("Your code (Movie.getDuration()): " + longMovie.getDuration());
System.out.println("Expected (Movie.getDuration()): 109");
System.out.println("Your code (Movie.getDuration()): " + shortMovie.getDuration());
System.out.println("Expected (Series.getDuration()): 1230");
System.out.println("Your code (Series.getDuration()): " + shortSeries.getDuration());
System.out.println("Expected (Series.getDuration()): 2930");
System.out.println("Your code (Series.getDuration()): " + longSeries.getDuration());
System.out.println();
// toString
System.out.println("Expected (Movie.toString()): Movie: The Lord of the Rings (5 241 min)");
System.out.println("Your code (Movie.toString()): " + longMovie.toString());
System.out.println();
+7 -7
View File
@@ -1,3 +1,6 @@
/**
* A class representing an item in a media library.
*/
public class MediaItem { // TODO: change
/**
@@ -5,28 +8,26 @@ public class MediaItem { // TODO: change
*/
// TODO: add variable
/**
* The title of the item.
*/
// TODO: add variable
/**
* The views of the item.
*/
// TODO: add variable
/**
* Creates a media library item.
* Creates a media library item with the specified title, duration and views.
*
* @param title The title of the item.
* @param duration The duration of the item.
* @param views The views of the item.
*/
public MediaItem(int duration, String title, int views) {
// TODO: assign member variables
}
/**
@@ -47,7 +48,6 @@ public class MediaItem { // TODO: change
return views;
}
// TODO: toString
}
}
-2
View File
@@ -8,9 +8,7 @@ public class Movie { // TODO: change
* @param views The views.
*/
// TODO: constructor
// TODO: toString
}
-2
View File
@@ -9,8 +9,6 @@ public class Series { // TODO: change
*/
// TODO: constructor
// TODO: toString
}
+1
View File
@@ -1,4 +1,5 @@
public class Testat1B {
/**
* Returns the average of all values in the array that are below the given
* threshold.
+112 -91
View File
@@ -5,48 +5,16 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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) {
Class<Tests> clazz = Tests.class;
Tests test = new Tests();
System.out.println(String.format("Tests: %s", test.getClass().getName()));
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.points.size();
try {
method.invoke(test);
if (test.points.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.points.get(test.points.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()));
}
private static final String TESTAT_NAME = "Testat 1B";
// Aufgabe 1 - 25 Punkte
@@ -83,8 +51,9 @@ public class Tests {
public void testCalculateMeanBelow(int[] arr, int threshold, double expected, int points) {
double result = Testat1B.calculateMeanBelow(arr, threshold);
if (result != expected) {
TracingFramework.printError(String.format("FAIL: Expected %f for %s with threshold %d, but got %f",
TracingFramework.printError(String.format("Expected %f for %s with threshold %d, but got %f",
expected, Arrays.toString(arr), threshold, result));
addPoints(0);
} else {
addPoints(points);
}
@@ -140,8 +109,9 @@ public class Tests {
if (Arrays.equals(result, expected)) {
addPoints(points);
} else {
TracingFramework.printError(String.format("FAIL: Expected %s for %s, but got %s",
TracingFramework.printError(String.format("Expected %s for %s, but got %s",
Arrays.toString(expected), Arrays.deepToString(arr), Arrays.toString(result)));
addPoints(0);
}
}
@@ -150,9 +120,11 @@ public class Tests {
@Test(name = "Aufgabe 3a - private fields - 6 points")
public void testA3PrivateFields() {
int points = 6;
points -= testField(MediaItem.class, "duration", int.class, 2);
points -= testField(MediaItem.class, "title", String.class, 2);
points -= testField(MediaItem.class, "views", int.class, 2);
addPoints(points);
}
@@ -174,33 +146,34 @@ public class Tests {
@Test(name = "Aufgabe 3c - lasting interface - 8 points")
public void testA3LastingInterface() {
int points = 8;
Class<?> clazz = MediaItem.class;
if (!implementsInterface(clazz, Lasting.class)) {
points -= 4;
TracingFramework
.printError(String.format("FAIL: Class '%s' does not implement interface '%s'. Deducting 4 points.",
.printError(String.format("Class '%s' does not implement interface '%s'. Deducting 4 points.",
clazz.getName(), Lasting.class.getName()));
}
Optional<Method> method = findMethod(MediaItem.class, "getDuration", int.class);
if (method.isEmpty()) {
Method method = findMethod(MediaItem.class, "getDuration", int.class);
if (method == null) {
points -= 4;
TracingFramework
.printError(String.format("FAIL: Method 'getDuration' not found in class '%s'. Deducting 4 points.",
.printError(String.format("Method 'getDuration' not found in class '%s'. Deducting 4 points.",
clazz.getName()));
} else {
Method getDurationMethod = method.get();
int duration = 42;
MediaItem item = new MediaItem(duration, "Foo", 42);
try {
int result = (int) getDurationMethod.invoke(item);
int result = (int) method.invoke(item);
if (result != duration) {
TracingFramework.printError(String.format("FAIL: Expected getDuration() to return %d, but got %d",
TracingFramework.printError(String.format("Expected getDuration() to return %d, but got %d",
duration, result));
points = 0;
}
} catch (IllegalAccessException | InvocationTargetException e) {
TracingFramework
.printError(String.format("FAIL: Could not invoke getDuration() on item of class '%s'",
.printError(String.format("Could not invoke getDuration() on item of class '%s'",
item.getClass().getName()));
points = 0;
}
@@ -214,7 +187,7 @@ public class Tests {
int points = 10;
if (!overridesMethod(MediaItem.class, "toString")) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not override method 'toString'",
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
MediaItem.class.getName()));
points = 0;
} else {
@@ -235,13 +208,13 @@ public class Tests {
int points = 5;
if (!extendsClass(Series.class, MediaItem.class)) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not extend class '%s'",
TracingFramework.printError(String.format("Class '%s' does not extend class '%s'",
Series.class.getName(), MediaItem.class.getName()));
points -= 5;
} else {
Series series = createInstance(Series.class, 42, "Test Series", 42);
if (series == null) {
TracingFramework.printError(String.format("FAIL: Could not create instance of class '%s'",
TracingFramework.printError(String.format("Could not create instance of class '%s'",
Series.class.getName()));
points = 0;
}
@@ -255,13 +228,13 @@ public class Tests {
int points = 5;
if (!extendsClass(Movie.class, MediaItem.class)) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not extend class '%s'",
TracingFramework.printError(String.format("Class '%s' does not extend class '%s'",
Movie.class.getName(), MediaItem.class.getName()));
points -= 5;
} else {
Movie movie = createInstance(Movie.class, 42, "Test Movie", 42);
if (movie == null) {
TracingFramework.printError(String.format("FAIL: could not create instance of class '%s'",
TracingFramework.printError(String.format("could not create instance of class '%s'",
Movie.class.getName()));
points = 0;
}
@@ -273,14 +246,15 @@ public class Tests {
@Test(name = "Aufgabe 3g - Series overrides toString - 5 points")
public void testA3SeriesOverridesToString() {
int points = 5;
if (!overridesMethod(Series.class, "toString")) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not override method 'toString'",
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
Series.class.getName()));
points = 0;
} else {
Series series = createInstance(Series.class, 42, "Test Series", 42);
if (series == null) {
TracingFramework.printError(String.format("FAIL: Could not create instance of class '%s'",
TracingFramework.printError(String.format("Could not create instance of class '%s'",
Series.class.getName()));
points = 0;
} else {
@@ -288,20 +262,22 @@ public class Tests {
points -= testToString(series, expected, 5);
}
}
addPoints(points);
}
@Test(name = "Aufgabe 3g - Movie overrides toString - 5 points")
public void testA3MovieOverridesToString() {
int points = 5;
if (!overridesMethod(Movie.class, "toString")) {
TracingFramework.printError(String.format("FAIL: Class '%s' does not override method 'toString'",
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
Movie.class.getName()));
points = 0;
} else {
Movie movie = createInstance(Movie.class, 42, "Test Movie", 42);
if (movie == null) {
TracingFramework.printError(String.format("FAIL: Could not create instance of class '%s'",
TracingFramework.printError(String.format("Could not create instance of class '%s'",
Movie.class.getName()));
points = 0;
} else {
@@ -309,9 +285,20 @@ public class Tests {
points -= testToString(movie, expected, 5);
}
}
addPoints(points);
}
/**
* Creates a new instance of the specified class with the given constructor
* arguments.
*
* @param <T> The type of the class to instantiate.
* @param clazz The class to instantiate.
* @param args The constructor arguments.
* @return A new instance of the specified class, or null if instantiation
* failed.
*/
public static <T> T createInstance(Class<T> clazz, Object... args) {
try {
Class<?>[] argTypes = Arrays.stream(args).map(Object::getClass).map(c -> {
@@ -320,7 +307,6 @@ public class Tests {
} else if (c.getName().equals("java.lang.Double")) {
return double.class;
}
return c;
}).toArray(Class<?>[]::new);
Constructor<T> constructor = clazz.getDeclaredConstructor(argTypes);
@@ -341,17 +327,27 @@ public class Tests {
}
}
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?> returnType,
/**
* Finds a method with the given name, return type, and parameter types in the
* specified class or its superclasses.
*
* @param clazz The class to search in.
* @param methodName The name of the method to find.
* @param returnType The return type of the method.
* @param parameterTypes The parameter types of the method.
* @return The method if found, null otherwise.
*/
public static Method findMethod(Class<?> clazz, String methodName, Class<?> returnType,
Class<?>... parameterTypes) {
try {
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
if (method.getReturnType().equals(returnType)) {
return Optional.of(method);
return method;
}
} catch (NoSuchMethodException e) {
// Method not found
e.printStackTrace();
}
return Optional.empty();
return null;
}
/**
@@ -380,17 +376,17 @@ public class Tests {
private static int testToString(Object obj, String expected, int maxPoints) {
String result = obj.toString();
if (result == null) {
TracingFramework.printError(String.format("FAIL: toString() returned null for object of class '%s'",
TracingFramework.printError(String.format("toString() returned null for object of class '%s'",
obj.getClass().getName()));
return maxPoints;
} else if (!result.equals(expected)) {
if (result.replaceAll("\\s+", " ").trim().equals(expected.replaceAll("\\s+", " ").trim())) {
TracingFramework.printError(String.format(
"FAIL: toString() returned '%s' for object of class '%s', but expected '%s' (ignoring whitespace)",
"toString() returned '%s' for object of class '%s', but expected '%s' (ignoring whitespace)",
result, obj.getClass().getName(), expected));
return maxPoints / 2;
} else {
TracingFramework.printError(String.format("FAIL: Expected toString() to return '%s', but got '%s'",
TracingFramework.printError(String.format("Expected toString() to return '%s', but got '%s'",
expected, result));
return maxPoints;
}
@@ -431,14 +427,14 @@ public class Tests {
int maxPoints) {
Field field = getField(clazz, fieldName, type);
if (field == null) {
TracingFramework.printError(String.format("FAIL: Field '%s' of type %s not found in class '%s'", fieldName,
TracingFramework.printError(String.format("Field '%s' of type %s not found in class '%s'", fieldName,
type.getName(), clazz.getName()));
return maxPoints;
}
T value = readField(field, type, instance);
if (!Objects.equals(value, expected)) {
TracingFramework.printError(String.format("FAIL: Expected field '%s' to be %s, but got %s", fieldName,
TracingFramework.printError(String.format("Expected field '%s' to be %s, but got %s", fieldName,
expected, value));
return maxPoints;
}
@@ -460,12 +456,12 @@ public class Tests {
Field field = getField(clazz, fieldName, type);
if (field == null) {
TracingFramework
.printError(String.format("FAIL: Field '%s' of type %s not found in class '%s'", fieldName,
.printError(String.format("Field '%s' of type %s not found in class '%s'", fieldName,
type.getName(), clazz.getName()));
return maxPoints;
} else if (!isPrivateField(field)) {
TracingFramework.printError(
String.format("FAIL: Field '%s' in class '%s' is not private", fieldName, clazz.getName()));
String.format("Field '%s' in class '%s' is not private", fieldName, clazz.getName()));
return maxPoints / 2;
}
return 0;
@@ -543,24 +539,6 @@ public class Tests {
}
}
/**
* Returns true if the given class has a constructor with the specified
* parameter types.
*
* @param clazz The class to check.
* @param parameterTypes The parameter types of the constructor.
* @return True if the class has a constructor with the specified parameter
* types, false otherwise.
*/
public boolean hasConstructor(Class<?> clazz, Class<?>... parameterTypes) {
try {
clazz.getDeclaredConstructor(parameterTypes);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Returns true if the given class implements the specified interface.
*
@@ -575,31 +553,74 @@ public class Tests {
/**
* The points of each test case.
*/
private List<Integer> points = new ArrayList<>();
private List<Integer> testPoints = new ArrayList<>();
/**
* This method will be called by jack after all
* test have been executed to get the resulting
* points.
* 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) {
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.
* 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);
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();
}
}
}