Commit 9868d441 authored by Jon Amos Fehling's avatar Jon Amos Fehling
Browse files

Merge branch 'development' of git.scc.kit.edu:WS-17-18/assignment-tests into development

parents c413de9c a2a73f96
...@@ -23,7 +23,7 @@ public final class Terminal { ...@@ -23,7 +23,7 @@ public final class Terminal {
* If enabled Terminal creates a file with TestPairs of the current run * If enabled Terminal creates a file with TestPairs of the current run
*/ */
public static boolean isTestCreation = false; public static boolean isTestCreation = false;
private static boolean useErrorMessagesInTestCreation = true; public static boolean useMyErrorMessagesInTestCreation = true;
private static StringBuilder testPairFile = new StringBuilder(); private static StringBuilder testPairFile = new StringBuilder();
/** /**
...@@ -42,11 +42,6 @@ public final class Terminal { ...@@ -42,11 +42,6 @@ public final class Terminal {
*/ */
private static final BufferedReader IN = new BufferedReader(new InputStreamReader(System.in)); private static final BufferedReader IN = new BufferedReader(new InputStreamReader(System.in));
/**
* this boolean determines whether the user already outputted something for that readLine call
*/
public static boolean concatOutput = true;
/** /**
* Private constructor to avoid object generation. * Private constructor to avoid object generation.
* *
...@@ -71,15 +66,15 @@ public final class Terminal { ...@@ -71,15 +66,15 @@ public final class Terminal {
String out = "Error, " + message; String out = "Error, " + message;
if (isTest) { if (isTest) {
if (concatOutput) { String current = OUT_TEST.pollLast();
OUT_TEST.add(OUT_TEST.pollLast() + "\n" + out); if (current == null || current.isEmpty()) {
} else {
OUT_TEST.add(out); OUT_TEST.add(out);
concatOutput = true; } else {
OUT_TEST.add(current + "\n" + out);
} }
} else if (isTestCreation) { } else if (isTestCreation) {
System.err.println(out); System.err.println(out);
if (useErrorMessagesInTestCreation) { if (useMyErrorMessagesInTestCreation) {
testPairFile.append(out); testPairFile.append(out);
testPairFile.append("\n"); testPairFile.append("\n");
} else { } else {
...@@ -101,12 +96,8 @@ public final class Terminal { ...@@ -101,12 +96,8 @@ public final class Terminal {
*/ */
public static void printLine(final Object object) { public static void printLine(final Object object) {
if (isTest) { if (isTest) {
if (concatOutput) { String current = OUT_TEST.pollLast();
OUT_TEST.add(OUT_TEST.pollLast() + "\n" + object); OUT_TEST.add((current.isEmpty() ? "" : current + "\n") + object);
} else {
OUT_TEST.add(object.toString());
concatOutput = true;
}
} else if (isTestCreation) { } else if (isTestCreation) {
System.out.println(object); System.out.println(object);
testPairFile.append(String.valueOf(object)).append("\n"); testPairFile.append(String.valueOf(object)).append("\n");
...@@ -157,12 +148,9 @@ public final class Terminal { ...@@ -157,12 +148,9 @@ public final class Terminal {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
else { else {
if (!concatOutput) { //If the program outputted nothing we still have at least an empty line
//The program outputted nothing. To support this in tests, we append an empty line OUT_TEST.add("");
OUT_TEST.add("");
} else {
concatOutput = false;
}
assert !IN_TEST.isEmpty() assert !IN_TEST.isEmpty()
: "Either the executed test doesn't has quit as last input\n" : "Either the executed test doesn't has quit as last input\n"
+ "or your quitting mechanism doesn't work properly.\n" + "or your quitting mechanism doesn't work properly.\n"
...@@ -248,7 +236,7 @@ public final class Terminal { ...@@ -248,7 +236,7 @@ public final class Terminal {
* @param doNotUseMyErrorMessages tells the program to use 'Error, ...' instead of the error-msges for io-files * @param doNotUseMyErrorMessages tells the program to use 'Error, ...' instead of the error-msges for io-files
*/ */
public static void startTestCreation(String[] args, boolean doNotUseMyErrorMessages) { public static void startTestCreation(String[] args, boolean doNotUseMyErrorMessages) {
useErrorMessagesInTestCreation = !doNotUseMyErrorMessages; useMyErrorMessagesInTestCreation = !doNotUseMyErrorMessages;
startTestCreation(args); startTestCreation(args);
} }
} }
...@@ -47,7 +47,6 @@ public class Assignment1TestBase extends TestBase { ...@@ -47,7 +47,6 @@ public class Assignment1TestBase extends TestBase {
* @param acceptable specify whether these args are ok or not * @param acceptable specify whether these args are ok or not
*/ */
protected void testArgs(String args[], boolean acceptable) { protected void testArgs(String args[], boolean acceptable) {
Terminal.concatOutput = false;
Terminal.IN_TEST.add("quit"); Terminal.IN_TEST.add("quit");
StringBuilder strb = new StringBuilder(); StringBuilder strb = new StringBuilder();
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
......
package final_assignment.task_1;
import edu.kit.informatik.Terminal;
/**
* Run this to manually create a .io-file
*
* @author Alexander Sommer
* @since 06.03.2018
*/
public class ManualTestCreation {
public static void main(String[] args) {
/*
If you want to submit tests, it would be useful to give a reason
why your program outputs an error for others to comprehend.
If you don't want that exactly the error-messages of your program are used in the test,
you can still find and replace them afterwards (CTRL + R).
*/
Terminal.useMyErrorMessagesInTestCreation = true;
Terminal.startTestCreation(args);
DummyMain.main(args);
Terminal.saveInputOutputPairs();
}
}
package final_assignment.task_2; package final_assignment.task_2;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import org.junit.jupiter.api.AfterAll;
import test.TestBase; import test.TestBase;
import test.TestPair; import test.TestPair;
import java.io.File;
/** /**
* This class links the DummyMain of this task and the TestBase * This class links the DummyMain of this task and the TestBase
* *
...@@ -30,7 +33,40 @@ public class Assignment2TestBase extends TestBase { ...@@ -30,7 +33,40 @@ public class Assignment2TestBase extends TestBase {
testUsingPairs(testPairs, () -> DummyMain.main(args)); testUsingPairs(testPairs, () -> DummyMain.main(args));
} }
protected String getDataFolderDirectory() { protected static String getDataFolderDirectory() {
return "src/final_assignment/task_2/tests/data/"; return "src/final_assignment/task_2/tests/data/";
} }
private static int testClassCount = 0;
protected static int getTestClassCount() {
if (testClassCount == 0) {
final File folder = new File(getDataFolderDirectory() + "../");
File[] files = folder.listFiles();
if (files == null) {
return 0;
}
for (final File fileEntry : files) {
if (fileEntry.getName().contains(".java")) {
testClassCount++;
}
}
}
return testClassCount;
}
private static int runCount = 0;
@AfterAll
public static void finish() {
if (++runCount >= getTestClassCount()) {
int sum = numSucceeded + numFailed;
double percentage = (numSucceeded * 100.) / sum;
System.out.printf("You mastered %.2f %% of the %d tests!%n", percentage, sum);
}
}
} }
...@@ -10,6 +10,13 @@ import edu.kit.informatik.Terminal; ...@@ -10,6 +10,13 @@ import edu.kit.informatik.Terminal;
*/ */
public class ManualTestCreation { public class ManualTestCreation {
public static void main(String[] args) { public static void main(String[] args) {
/*
If you want to submit tests, it would be useful to give a reason
why your program outputs an error for others to comprehend.
If you don't want that exactly the error-messages of your program are used in the test,
you can still find and replace them afterwards (CTRL + R).
*/
Terminal.useMyErrorMessagesInTestCreation = true;
Terminal.startTestCreation(args); Terminal.startTestCreation(args);
DummyMain.main(args); DummyMain.main(args);
Terminal.saveInputOutputPairs(); Terminal.saveInputOutputPairs();
......
package test; package test;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import java.io.*; import java.io.*;
import java.net.NetworkInterface; import java.net.NetworkInterface;
...@@ -15,7 +12,6 @@ import java.nio.file.Paths; ...@@ -15,7 +12,6 @@ import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -48,7 +44,8 @@ public abstract class TestBase { ...@@ -48,7 +44,8 @@ public abstract class TestBase {
private long startTime; private long startTime;
private static String userId; private static String userId;
public static final String IO_FILE_COMMAND_LINE_ARGS_PREFIX = "! "; public static final String IO_FILE_COMMAND_LINE_ARGS_PREFIX = "! ";
protected static int numFailed = 0;
protected static int numSucceeded = 0;
/** /**
* Initializes testing * Initializes testing
...@@ -104,7 +101,6 @@ public abstract class TestBase { ...@@ -104,7 +101,6 @@ public abstract class TestBase {
*/ */
@BeforeEach @BeforeEach
protected void initTest(TestInfo testInfo) { protected void initTest(TestInfo testInfo) {
Terminal.concatOutput = true;
alwaysShowLog = false; alwaysShowLog = false;
isPerformanceTest = false; isPerformanceTest = false;
testPassed = false; testPassed = false;
...@@ -161,6 +157,12 @@ public abstract class TestBase { ...@@ -161,6 +157,12 @@ public abstract class TestBase {
*/ */
@AfterEach @AfterEach
protected void cleanUp(TestInfo testInfo) { protected void cleanUp(TestInfo testInfo) {
if (testPassed) {
numSucceeded++;
} else {
numFailed++;
}
if (isPerformanceTest && testPassed) { if (isPerformanceTest && testPassed) {
long endTime = System.nanoTime(); long endTime = System.nanoTime();
double runTime = ((double) (endTime - startTime)) / 1000000.0; double runTime = ((double) (endTime - startTime)) / 1000000.0;
...@@ -193,7 +195,7 @@ public abstract class TestBase { ...@@ -193,7 +195,7 @@ public abstract class TestBase {
} }
} }
if (showAllProgramOutput && !Terminal.OUT_TEST.isEmpty()) { if (showAllProgramOutput && !testPassed && !Terminal.OUT_TEST.isEmpty()) {
System.out.println("The output after that was:"); System.out.println("The output after that was:");
while (!Terminal.OUT_TEST.isEmpty()) { while (!Terminal.OUT_TEST.isEmpty()) {
...@@ -214,7 +216,6 @@ public abstract class TestBase { ...@@ -214,7 +216,6 @@ public abstract class TestBase {
if (alwaysShowLog) if (alwaysShowLog)
printLog(); printLog();
logData.clear(); logData.clear();
Terminal.concatOutput = true;
} }
...@@ -293,7 +294,6 @@ public abstract class TestBase { ...@@ -293,7 +294,6 @@ public abstract class TestBase {
* @param testedMethod is a reference to a tested program * @param testedMethod is a reference to a tested program
*/ */
protected void testUsingPairs(TestPair[] testPairs, Runnable testedMethod) { protected void testUsingPairs(TestPair[] testPairs, Runnable testedMethod) {
Terminal.concatOutput = true;
log.info("Testing using " + testPairs.length + " pairs...\n"); log.info("Testing using " + testPairs.length + " pairs...\n");
for (TestPair testPair : testPairs) { for (TestPair testPair : testPairs) {
...@@ -308,7 +308,7 @@ public abstract class TestBase { ...@@ -308,7 +308,7 @@ public abstract class TestBase {
String output = popProgramOutput(); String output = popProgramOutput();
assert output != null : "got no output"; // This should never happen, since this is handled by the Terminal assert output != null : "got no output, this should never happen, since this is handled by the Terminal";
switch (testPair.getType()) { switch (testPair.getType()) {
case CHECK_EQUALS: case CHECK_EQUALS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment