Commit b3377a9e authored by uhflp's avatar uhflp Committed by ungpk
Browse files

Daily release 02.03.2018

parent b9b380fe
...@@ -45,7 +45,7 @@ public final class Terminal { ...@@ -45,7 +45,7 @@ public final class Terminal {
/** /**
* this boolean determines whether the user already outputted something for that readLine call * this boolean determines whether the user already outputted something for that readLine call
*/ */
private static boolean concatOutput = false; public static boolean concatOutput = true;
/** /**
* Private constructor to avoid object generation. * Private constructor to avoid object generation.
...@@ -157,7 +157,12 @@ public final class Terminal { ...@@ -157,7 +157,12 @@ public final class Terminal {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
else { else {
concatOutput = false; if (!concatOutput) {
//The program outputted nothing. To support this in tests, we append an empty line
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"
......
...@@ -47,6 +47,7 @@ public class Assignment1TestBase extends TestBase { ...@@ -47,6 +47,7 @@ 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++) {
...@@ -59,12 +60,12 @@ public class Assignment1TestBase extends TestBase { ...@@ -59,12 +60,12 @@ public class Assignment1TestBase extends TestBase {
} }
log.info("args[]: '" + strb.toString() + "'"); log.info("args[]: '" + strb.toString() + "'");
DummyMain.main(args); DummyMain.main(args);
String output = Terminal.OUT_TEST.poll();
if (acceptable) { if (acceptable) {
if (Terminal.OUT_TEST.poll() != null) { if (output != null && !output.equals("")) {
failAndLog("test should have succeeded"); failAndLog("test should have succeeded");
} }
} else { } else {
String output = Terminal.OUT_TEST.poll();
if (output == null || !output.startsWith("Error")) { if (output == null || !output.startsWith("Error")) {
failAndLog("test should have failed"); failAndLog("test should have failed");
} }
......
package final_assignment.task_1.tests; package final_assignment.task_1.tests;
import final_assignment.task_1.Assignment1TestBase; import final_assignment.task_1.Assignment1TestBase;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -16,6 +15,7 @@ public class MassiveCornerCases extends Assignment1TestBase { ...@@ -16,6 +15,7 @@ public class MassiveCornerCases extends Assignment1TestBase {
for (final File fileEntry : folder.listFiles()) { for (final File fileEntry : folder.listFiles()) {
log.info("\nFile: " + fileEntry.getPath()); log.info("\nFile: " + fileEntry.getPath());
testWithIOFile(fileEntry.getPath()); testWithIOFile(fileEntry.getPath());
clearData();
} }
} }
} }
...@@ -15,6 +15,7 @@ public class PerformanceTests extends Assignment1TestBase { ...@@ -15,6 +15,7 @@ public class PerformanceTests extends Assignment1TestBase {
String args[] = {"torus", "20", "2"}; String args[] = {"torus", "20", "2"};
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
testUsingPairs(pairs, args); testUsingPairs(pairs, args);
clearData();
} }
} }
} }
...@@ -3,62 +3,57 @@ package final_assignment.task_2; ...@@ -3,62 +3,57 @@ package final_assignment.task_2;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import test.TestPair; import test.TestPair;
import java.io.FileNotFoundException; import java.io.*;
import java.io.PrintWriter;
import java.util.Random; import java.util.Random;
import java.util.Vector; import java.util.Vector;
public class CaseCreator { public class CaseCreator {
private static StringBuilder testPairfile; private StringBuilder testPairFile;
private static final Random random = new Random(); private final Random random = new Random();
private static long rgenseed = System.currentTimeMillis(); private long rgenseed = System.currentTimeMillis();
private final int nCountries = 20;
private final int nSports = 20;
private final int nAthletes = 21;
private final int nErrors = 100;
private static final int nCountries = 20;
private static final int nSports = 4;
private static final int nAthletes = 2300; //nAthletes*nSports<10000
private static final int nCompetitions = 200000;
public static void main(String args[]) { public static void main(String args[]) {
Terminal.isTest = true; Terminal.isTest = true;
for (int i = 0; i < 1; i++) { for (int i = 0; i < 1; i++) {
rgenseed = System.currentTimeMillis(); CaseCreator caseCreator = new CaseCreator();
random.setSeed(rgenseed); caseCreator.rgenseed = System.currentTimeMillis();
createTest(); caseCreator.random.setSeed(caseCreator.rgenseed);
caseCreator.createTest();
} }
} }
private static void createTest() { private void createTest() {
System.out.println("Creating input..."); final RandomPicker randomPicker = new RandomPicker(random);
testPairfile = new StringBuilder(); System.out.printf("Creating input for seed %d...%n", rgenseed);
testPairFile = new StringBuilder();
Terminal.IN_TEST.clear(); Terminal.IN_TEST.clear();
Terminal.OUT_TEST.clear(); Terminal.OUT_TEST.clear();
Vector<TestPair> pairs = new Vector<TestPair>(); Vector<TestPair> pairs = new Vector<TestPair>();
pairs.add(new TestPair("add-admin Vorname;Nachname;tester;123123123", "OK")); pairs.add(new TestPair("add-admin vorname;nachname;tester;123123123", "OK"));
pairs.add(new TestPair("login-admin tester;123123123", "OK")); pairs.add(new TestPair("login-admin tester;123123123", "OK"));
Vector<String> ids = new Vector<>(); Vector<String> ids = new Vector<>();
int idCounter = 0;
Vector<String> countries = new Vector<>(); Vector<String> countries = new Vector<>();
for (int i = 0; i < nCountries; i++) { for (int i = 0; i < nCountries; i++) {
String country = new String(); String country = randomPicker.pickCountry();
do {
country = randomString("abcdefghijklmnobqrstuvwxyz", 10);
} while (countries.contains(country));
countries.add(country);
do { countries.add(country.split(" ", 2)[1].toLowerCase());
country = randomString("abcdefghijklmnobqrstuvwxyz", 3);
} while (countries.contains(country));
countries.add(country);
String id = new String(); countries.add(country.split(" ", 2)[0].toLowerCase());
do {
id = randomString("123456789", 3); String id = String.format("%03d", ++idCounter);
} while (ids.contains(id));
ids.add(id); ids.add(id);
pairs.add(new TestPair("add-ioc-code " + id + ";" + countries.elementAt(2 * i + 1) + ";" pairs.add(new TestPair("add-ioc-code " + id + ";" + countries.elementAt(2 * i + 1) + ";"
...@@ -70,52 +65,64 @@ public class CaseCreator { ...@@ -70,52 +65,64 @@ public class CaseCreator {
Vector<String> sports = new Vector<>(); Vector<String> sports = new Vector<>();
for (int i = 0; i < nSports; i++) { for (int i = 0; i < nSports; i++) {
String country = new String(); sports.add(randomPicker.pickSport());
do {
country = randomString("abcdefghijklmnobqrstuvwxyz", 6);
} while (sports.contains(country));
sports.add(country);
do {
country = randomString("abcdefghijklmnobqrstuvwxyz", 6);
} while (sports.contains(country));
sports.add(country);
pairs.add(new TestPair("add-olympic-sport " + sports.elementAt(2 * i) + ";" pairs.add(new TestPair("add-olympic-sport " + sports.elementAt(i) + ";"
+ sports.elementAt(2 * i + 1), "OK")); + sports.elementAt(i), "OK"));
} }
Vector<Vector<String>> athletes = new Vector<>(); Vector<Vector<String>> athletes = new Vector<>();
idCounter = 0;
for (int s = 0; s < nSports; s++) { for (int s = 0; s < nSports; s++) {
athletes.add(new Vector<>()); athletes.add(new Vector<>());
for (int a = 0; a < nAthletes; a++) { for (int a = 0; a < nAthletes; a++) {
String id = new String(); String id = String.format("%04d", ++idCounter);
do {
id = randomString("0123456789", 4);
} while (ids.contains(id) || id.equals("0000"));
ids.add(id); ids.add(id);
String country = countries.elementAt(2 * random.nextInt(nCountries)); String country = countries.elementAt(2 * random.nextInt(nCountries));
athletes.elementAt(s).add(id); athletes.elementAt(s).add(id);
athletes.elementAt(s).add(country); athletes.elementAt(s).add(country);
pairs.add(new TestPair("add-athlete " + id + ";" + randomString("abcdefghijklmnobqrstuvwxyz ", 10) + ";" + randomString("abcdefghijklmnobqrstuvwxyz ", 10) + ";" + country + ";" pairs.add(new TestPair("add-athlete "
+ sports.elementAt(2 * s) + ";" + sports.elementAt(2 * s + 1), "OK")); + id + ";" + randomPicker.pickName()
+ ";" + randomPicker.pickName() + ";" + country + ";"
+ sports.elementAt(s) + ";" + sports.elementAt(s), "OK"));
} }
} }
for (int s = 0; s < nSports; s++) { for (int s = 0; s < nSports; s++) {
for (int c = 0; c < nCompetitions; c++) { for (int c = 0; c < nAthletes; c++) {
for (int y = 1926; y <= 2018; y += 4) {
int indexAthlete = 2 * c;
int medal = random.nextInt(3);
String medalString = "";
if (medal == 0) medalString = "0;1;0";
if (medal == 1) medalString = "1;0;0";
if (medal == 2) medalString = "0;0;1";
if (medal == 3) medalString = "0;0;0";
pairs.add(new TestPair("add-competition " + athletes.elementAt(s).elementAt(indexAthlete) + ";" + y + ";"
+ athletes.elementAt(s).elementAt(indexAthlete + 1) + ";"
+ sports.elementAt(s) + ";" + sports.elementAt(s) + ";" + medalString, "OK"));
}
}
}
for (int s = 0; s < nSports; s++) {
for (int f = 0; f < nErrors; f++) {
int indexAthlete = 2 * random.nextInt(nAthletes); int indexAthlete = 2 * random.nextInt(nAthletes);
int medal = random.nextInt(3); int medal = random.nextInt(3);
String medalString = new String(); String medalString = "";
if (medal == 0) medalString = "0;1;0"; if (medal == 0) medalString = "0;1;0";
if (medal == 1) medalString = "1;0;0"; if (medal == 1) medalString = "1;0;0";
if (medal == 2) medalString = "0;0;1"; if (medal == 2) medalString = "0;0;1";
if (medal == 3) medalString = "0;0;0";
pairs.add(new TestPair("add-competition " + athletes.elementAt(s).elementAt(indexAthlete) + ";" + (1926 + random.nextInt(24) * 4) + ";" pairs.add(new TestPair("add-competition " + athletes.elementAt(s).elementAt(indexAthlete) + ";" + (1926 + random.nextInt(24) * 4) + ";"
+ athletes.elementAt(s).elementAt(indexAthlete + 1) + ";" + athletes.elementAt(s).elementAt(indexAthlete + 1) + ";"
+ sports.elementAt(2 * s) + ";" + sports.elementAt(2 * s + 1) + ";" + medalString, "OK")); + sports.elementAt(s) + ";" + sports.elementAt(s) + ";" + medalString, "OK"));
} }
} }
...@@ -123,28 +130,38 @@ public class CaseCreator { ...@@ -123,28 +130,38 @@ public class CaseCreator {
for (int s = 0; s < nSports; s++) { for (int s = 0; s < nSports; s++) {
pairs.add(new TestPair("summary-athletes " + sports.elementAt(2 * s + 1), "")); pairs.add(new TestPair("summary-athletes " + sports.elementAt(s), ""));
} }
pairs.add(new TestPair("olympic-medal-table", "")); pairs.add(new TestPair("olympic-medal-table", ""));
System.out.println("Creating output..."); ids.clear();
sports.clear();
countries.clear();
athletes.clear();
System.out.println(String.format("Creating output for %d pairs...", pairs.size()));
testUsingPairs(pairs.toArray(new TestPair[pairs.size()]), () -> DummyMain.main(null)); testUsingPairs(pairs.toArray(new TestPair[pairs.size()]), () -> DummyMain.main(null));
System.out.println("Saving..."); System.out.println("Saving...");
saveInputOutputPairs(); saveInputOutputPairs();
} }
private static void testUsingPairs(TestPair[] testPairs, Runnable testedMethod) { private void testUsingPairs(TestPair[] testPairs, Runnable testedMethod) {
for (TestPair testPair : testPairs) { for (TestPair testPair : testPairs) {
Terminal.IN_TEST.add(testPair.getInput()); Terminal.IN_TEST.add(testPair.getInput());
} }
Terminal.IN_TEST.add("quit"); Terminal.IN_TEST.add("quit");
long startTime = System.nanoTime();
testedMethod.run(); testedMethod.run();
long endTime = System.nanoTime();
System.out.println(String.format("Program run in %f ms.", (endTime - startTime) / 1000000.0));
for (TestPair testPair : testPairs) { for (TestPair testPair : testPairs) {
testPairfile.append("> " + testPair.getInput() + "\n"); testPairFile.append("> ");
testPairFile.append(testPair.getInput());
testPairFile.append("\n");
String partOutput; String partOutput;
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
...@@ -161,26 +178,18 @@ public class CaseCreator { ...@@ -161,26 +178,18 @@ public class CaseCreator {
} }
String output = stringBuilder.toString(); String output = stringBuilder.toString();
testPairfile.append(output + "\n"); testPairFile.append(output);
testPairFile.append("\n");
} }
} }
public static void saveInputOutputPairs() { public void saveInputOutputPairs() {
String path = "testPairs_" + new java.util.Date().getTime() + ".io"; String path = "testPairs_" + new java.util.Date().getTime() + ".io";
try (PrintWriter out = new PrintWriter(path)) { try (PrintWriter out = new PrintWriter(path)) {
out.print("Automatically created test with seed " + rgenseed + "\n" + testPairfile.toString()); out.print("Automatically created test with seed " + rgenseed + "\n" + testPairFile.toString());
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.println("Unable to open file " + path + " for saving test pairs."); System.out.println("Unable to open file " + path + " for saving test pairs.");
} }
} }
public static String randomString(String chars, int length) {
StringBuilder strb = new StringBuilder();
int range = chars.length();
for (int i = 0; i < length; i++) {
strb.append(chars.charAt(random.nextInt(range)));
}
return strb.toString();
}
} }
\ No newline at end of file
package final_assignment.task_2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* @author Alexander Sommer
* @since 01.03.2018
*/
public class RandomPicker {
private static final String COUNTRY_FILE = "src/final_assignment/task_2/sample_data/countries.txt";
private static final String NAME_FILE = "src/final_assignment/task_2/sample_data/names.txt";
private static final String SPORTS_FILE = "src/final_assignment/task_2/sample_data/sports.txt";
private final List<String> countries = new LinkedList<>();
private final List<String> names = new LinkedList<>();
private final List<String> sports = new LinkedList<>();
private final Random random;
public RandomPicker(Random random) {
this.random = random;
loadFileIntoList(COUNTRY_FILE, countries);
loadFileIntoList(NAME_FILE, names);
loadFileIntoList(SPORTS_FILE,sports);
}
private void loadFileIntoList(String file, List<String> list) {
assert Files.exists(Paths.get(file)) : "Missing File: " + file;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#"))
continue;
list.add(line);
}
} catch (IOException e) {
assert false : e.getMessage();
}
}
public String pickCountry() {
assert !countries.isEmpty() : "Not enough countries";
return countries.remove(random.nextInt(countries.size()));
}
public String pickName() {
return names.get(random.nextInt(names.size()));
}
public String pickSport() {
assert !sports.isEmpty() : "Not enough sports";
return sports.remove(random.nextInt(sports.size()));
}
}
# Taken from wikipedia and formatted
AFG Afghanistan
ALB Albania
ALG Algeria
AND Andorra
ANG Angola
ANT Antigua and Barbuda
ARG Argentina
ARM Armenia
ARU Aruba
ASA American Samoa
AUS Australia
AUT Austria
AZE Azerbaijan
BAH Bahamas
BAN Bangladesh
BAR Barbados
BDI Burundi
BEL Belgium
BEN Benin
BER Bermuda
BHU Bhutan
BIH Bosnia and Herzegovina
BIZ Belize
BLR Belarus
BOL Bolivia
BOT Botswana
BRA Brazil
BRN Bahrain
BRU Brunei
BUL Bulgaria
BUR Burkina Faso
CAF Central African Republic
CAM Cambodia
CAN Canada
CAY Cayman Islands
CGO Congo
CHA Chad
CHI Chile
CHN China
CIV Ivory Coast
CMR Cameroon
COD Democratic Republic of the Congo
COK Cook Islands
COL Colombia
COM Comoros
CPV Cape Verde
CRC Costa Rica
CRO Croatia
CUB Cuba
CYP Cyprus
CZE Czech Republic
DEN Denmark
DJI Djibouti
DMA Dominica
DOM Dominican Republic
ECU Ecuador
EGY Egypt
ERI Eritrea
ESA El Salvador
ESP Spain
EST Estonia
ETH Ethiopia
FIJ Fiji
FIN Finland
FRA France
FSM Federated States of Micronesia
GAB Gabon
GAM The Gambia
GBR Great Britain
GBS GuineaBissau
GEO Georgia
GEQ Equatorial Guinea
GER Germany
GHA Ghana
GRE Greece
GRN Grenada
GUA Guatemala
GUI Guinea
GUM Guam
GUY Guyana
HAI Haiti
HKG Hong Kong
HON Honduras
HUN Hungary
INA Indonesia
IND India
IRI Iran
IRL Ireland
IRQ Iraq
ISL Iceland
ISR Israel
ISV Virgin Islands