Homework 3
This commit is contained in:
parent
bf60a078d7
commit
0afc86ceeb
129 changed files with 3163 additions and 4316 deletions
|
@ -1,4 +1,4 @@
|
|||
package cd;
|
||||
package cd.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
|
@ -7,16 +7,14 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.DisableOnDebug;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.rules.Timeout;
|
||||
|
||||
import cd.Config;
|
||||
import cd.Main;
|
||||
import cd.frontend.parser.ParseFailure;
|
||||
import cd.frontend.semantic.SemanticFailure;
|
||||
import cd.ir.Ast.ClassDecl;
|
||||
import cd.util.FileUtil;
|
||||
import cd.util.debug.AstDump;
|
||||
|
@ -27,17 +25,13 @@ abstract public class AbstractTestAgainstFrozenReference {
|
|||
public static final String PARSE_FAILURE = "ParseFailure";
|
||||
|
||||
public File file, sfile, binfile, infile;
|
||||
public File parserreffile, semanticreffile, execreffile, cfgreffile, rdreffile,
|
||||
nnreffile, optreffile;
|
||||
public File parserreffile, semanticreffile, execreffile, cfgreffile, optreffile;
|
||||
public File errfile;
|
||||
public Main main;
|
||||
|
||||
public static int counter = 0;
|
||||
|
||||
@Rule
|
||||
public TestRule timeout = new DisableOnDebug(new Timeout(15, TimeUnit.SECONDS));
|
||||
|
||||
@Test
|
||||
@Test(timeout=10000)
|
||||
public void test() throws Throwable {
|
||||
System.err.println("[" + counter++ + " = " + file + "]");
|
||||
|
||||
|
@ -52,6 +46,9 @@ abstract public class AbstractTestAgainstFrozenReference {
|
|||
|
||||
List<ClassDecl> astRoots = testParser();
|
||||
if (astRoots != null) {
|
||||
{
|
||||
boolean passedSemanticAnalysis = testSemanticAnalyzer(astRoots);
|
||||
}
|
||||
}
|
||||
} catch (org.junit.ComparisonFailure cf) {
|
||||
throw cf;
|
||||
|
@ -80,7 +77,7 @@ abstract public class AbstractTestAgainstFrozenReference {
|
|||
|
||||
ProcessBuilder pb = new ProcessBuilder(
|
||||
javaExe, "-Dcd.meta_hidden.Version=" + referenceVersion(),
|
||||
"-cp", "lib/frozenReferenceObf.jar" + colon + " lib/junit-4.12.jar" + colon + "lib/antlr-4.7.1-complete.jar",
|
||||
"-cp", "lib/frozenReferenceObf.jar" + colon + " lib/junit-4.12.jar" + colon + "lib/antlr-4.4-complete.jar",
|
||||
"cd.FrozenReferenceMain", file.getAbsolutePath());
|
||||
|
||||
Process proc = pb.start();
|
||||
|
@ -96,24 +93,13 @@ abstract public class AbstractTestAgainstFrozenReference {
|
|||
|
||||
private static String referenceVersion() {
|
||||
{
|
||||
return "CD_HW_PARSER_SOL";
|
||||
return "CD_HW_SEMANTIC_SOL";
|
||||
}
|
||||
}
|
||||
|
||||
private String tryReadRefFile(File fileToFind) {
|
||||
if (fileToFind.exists() && fileToFind.lastModified() >= file.lastModified()) {
|
||||
try {
|
||||
return FileUtil.read(fileToFind);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("ERROR: could not read file " + fileToFind.getPath());
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("ERROR: could not find file " + fileToFind.getPath());
|
||||
}
|
||||
|
||||
/** Run the parser and compare the output against the reference results */
|
||||
private List<ClassDecl> testParser() throws Exception {
|
||||
String parserRef = tryReadRefFile(parserreffile);
|
||||
String parserRef = findParserRef();
|
||||
List<ClassDecl> astRoots = null;
|
||||
String parserOut;
|
||||
|
||||
|
@ -132,11 +118,68 @@ abstract public class AbstractTestAgainstFrozenReference {
|
|||
}
|
||||
|
||||
{
|
||||
assertEquals("parser", parserRef, parserOut);
|
||||
// Now that the 2nd assignment is over, we don't
|
||||
// do a detailed comparison of the AST, just check
|
||||
// whether the parse succeeded or failed.
|
||||
if (parserOut.equals(PARSE_FAILURE) || parserRef.equals(PARSE_FAILURE))
|
||||
assertEquals("parser", parserRef, parserOut);
|
||||
}
|
||||
return astRoots;
|
||||
}
|
||||
|
||||
private String findParserRef() throws IOException {
|
||||
// Check for a .ref file
|
||||
if (parserreffile.exists() && parserreffile.lastModified() >= file.lastModified()) {
|
||||
return FileUtil.read(parserreffile);
|
||||
}
|
||||
throw new RuntimeException("ERROR: could not find parser .ref");
|
||||
}
|
||||
|
||||
private boolean testSemanticAnalyzer(List<ClassDecl> astRoots)
|
||||
throws IOException {
|
||||
String semanticRef = findSemanticRef();
|
||||
|
||||
boolean passed;
|
||||
String result;
|
||||
try {
|
||||
main.semanticCheck(astRoots);
|
||||
result = SEMANTIC_OK;
|
||||
passed = true;
|
||||
} catch (SemanticFailure sf) {
|
||||
result = sf.cause.name();
|
||||
main.debug("Error message: %s", sf.getLocalizedMessage());
|
||||
passed = false;
|
||||
}
|
||||
|
||||
assertEquals("semantic", semanticRef, result);
|
||||
return passed;
|
||||
}
|
||||
|
||||
private String findSemanticRef() throws IOException {
|
||||
// Semantic ref file is a little different. It consists
|
||||
// of 2 lines, but only the first line is significant.
|
||||
// The second one contains additional information that we log
|
||||
// to the debug file.
|
||||
|
||||
// Read in the result
|
||||
String res;
|
||||
if (semanticreffile.exists() && semanticreffile.lastModified() > file.lastModified())
|
||||
res = FileUtil.read(semanticreffile);
|
||||
else
|
||||
throw new RuntimeException("ERROR: could not find semantic .ref");
|
||||
|
||||
// Extract the first line: there should always be multiple lines,
|
||||
// but someone may have tinkered with the file or something
|
||||
if (res.contains("\n")) {
|
||||
int newline = res.indexOf("\n");
|
||||
String info = res.substring(newline + 1);
|
||||
if (!info.equals("") && !info.equals("\n"))
|
||||
main.debug("Error message from reference is: %s", info);
|
||||
return res.substring(0, newline); // 1st line
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
private void assertEquals(String phase, String exp, String act_) {
|
||||
String act = act_.replace("\r\n", "\n"); // for windows machines
|
||||
|
@ -144,7 +187,7 @@ abstract public class AbstractTestAgainstFrozenReference {
|
|||
warnAboutDiff(phase, exp, act);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void warnAboutDiff(String phase, String exp, String act) {
|
||||
try {
|
||||
try (PrintStream err = new PrintStream(errfile)) {
|
|
@ -1,4 +1,4 @@
|
|||
package cd;
|
||||
package cd.test;
|
||||
|
||||
// NOTE: This code was adapted from the code found at
|
||||
// http://www.cs.princeton.edu/introcs/96optimization/Diff.java.html
|
|
@ -1,9 +1,4 @@
|
|||
package cd;
|
||||
|
||||
import cd.util.FileUtil;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
package cd.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.StringWriter;
|
||||
|
@ -11,6 +6,14 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import cd.Config;
|
||||
import cd.Main;
|
||||
import cd.util.FileUtil;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class TestSamplePrograms extends AbstractTestAgainstFrozenReference {
|
||||
|
||||
|
@ -28,7 +31,7 @@ public class TestSamplePrograms extends AbstractTestAgainstFrozenReference {
|
|||
* particular directory, use sth. like:
|
||||
* {@code testDir = new File("javali_tests/HW2/")}.
|
||||
*/
|
||||
public static final File testDir = new File("javali_tests/HW2_nop90");
|
||||
public static final File testDir = new File("javali_tests/HW3_nop90");
|
||||
// public static final File testDir = new File("javali_tests");
|
||||
|
||||
@Parameters(name="{index}:{0}")
|
||||
|
@ -61,8 +64,6 @@ public class TestSamplePrograms extends AbstractTestAgainstFrozenReference {
|
|||
this.semanticreffile = new File(file.getPath() + ".semantic.ref");
|
||||
this.execreffile = new File(file.getPath() + ".exec.ref");
|
||||
this.cfgreffile = new File(file.getPath() + ".cfg.dot.ref");
|
||||
this.rdreffile = new File(file.getPath() + ".rd.ref");
|
||||
this.nnreffile = new File(file.getPath() + ".nn.ref");
|
||||
this.optreffile = new File(file.getPath() + ".opt.ref");
|
||||
this.errfile = new File(String.format("%s.err", file.getPath()));
|
||||
this.main = new Main();
|
Loading…
Add table
Add a link
Reference in a new issue