Homework 4

This commit is contained in:
Carlos Galindo 2020-01-15 22:34:57 +01:00
parent 0afc86ceeb
commit 72cc3206c4
Signed by: kauron
GPG key ID: 83E68706DEE119A3
125 changed files with 4200 additions and 1636 deletions

View file

@ -1,18 +1,23 @@
package cd.test;
package cd;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
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.backend.codegen.AssemblyFailedException;
import cd.frontend.parser.ParseFailure;
import cd.frontend.semantic.SemanticFailure;
import cd.ir.Ast.ClassDecl;
@ -25,13 +30,17 @@ abstract public class AbstractTestAgainstFrozenReference {
public static final String PARSE_FAILURE = "ParseFailure";
public File file, sfile, binfile, infile;
public File parserreffile, semanticreffile, execreffile, cfgreffile, optreffile;
public File parserreffile, semanticreffile, execreffile, cfgreffile, rdreffile,
nnreffile, optreffile;
public File errfile;
public Main main;
public static int counter = 0;
@Test(timeout=10000)
@Rule
public TestRule timeout = new DisableOnDebug(new Timeout(15, TimeUnit.SECONDS));
@Test
public void test() throws Throwable {
System.err.println("[" + counter++ + " = " + file + "]");
@ -48,6 +57,12 @@ abstract public class AbstractTestAgainstFrozenReference {
if (astRoots != null) {
{
boolean passedSemanticAnalysis = testSemanticAnalyzer(astRoots);
{
if (passedSemanticAnalysis) {
testCodeGenerator(astRoots);
}
}
}
}
} catch (org.junit.ComparisonFailure cf) {
@ -77,7 +92,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.4-complete.jar",
"-cp", "lib/frozenReferenceObf.jar" + colon + " lib/junit-4.12.jar" + colon + "lib/antlr-4.7.1-complete.jar",
"cd.FrozenReferenceMain", file.getAbsolutePath());
Process proc = pb.start();
@ -93,13 +108,24 @@ abstract public class AbstractTestAgainstFrozenReference {
private static String referenceVersion() {
{
return "CD_HW_SEMANTIC_SOL";
return "CD_HW_CODEGEN_FULL_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 = findParserRef();
String parserRef = tryReadRefFile(parserreffile);
List<ClassDecl> astRoots = null;
String parserOut;
@ -127,13 +153,6 @@ abstract public class AbstractTestAgainstFrozenReference {
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 {
@ -161,12 +180,7 @@ abstract public class AbstractTestAgainstFrozenReference {
// 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");
String res = tryReadRefFile(semanticreffile);
// Extract the first line: there should always be multiple lines,
// but someone may have tinkered with the file or something
@ -181,6 +195,48 @@ abstract public class AbstractTestAgainstFrozenReference {
}
}
/**
* Run the code generator, assemble the resulting .s file, and (if the output
* is well-defined) compare against the expected output.
*/
private void testCodeGenerator(List<ClassDecl> astRoots)
throws IOException {
// Determine the input and expected output.
String inFile = (infile.exists() ? FileUtil.read(infile) : "");
String execRef = tryReadRefFile(execreffile);
// Run the code generator:
try (FileWriter fw = new FileWriter(this.sfile)) {
main.generateCode(astRoots, fw);
}
// At this point, we have generated a .s file and we have to compile
// it to a binary file. We need to call out to GCC or something
// to do this.
String asmOutput = FileUtil.runCommand(
Config.ASM_DIR,
Config.ASM,
new String[] { binfile.getAbsolutePath(),
sfile.getAbsolutePath() }, null, false);
// To check if gcc succeeded, check if the binary file exists.
// We could use the return code instead, but this seems more
// portable to other compilers / make systems.
if (!binfile.exists())
throw new AssemblyFailedException(asmOutput);
// Execute the binary file, providing input if relevant, and
// capturing the output. Check the error code so see if the
// code signaled dynamic errors.
String execOut = FileUtil.runCommand(new File("."),
new String[] { binfile.getAbsolutePath() }, new String[] {},
inFile, true);
// Compute the output to what we expected to see.
if (!execRef.equals(execOut))
assertEqualOutput("exec", execRef, execOut);
}
private void assertEquals(String phase, String exp, String act_) {
String act = act_.replace("\r\n", "\n"); // for windows machines
if (!exp.equals(act)) {
@ -188,6 +244,16 @@ abstract public class AbstractTestAgainstFrozenReference {
}
}
/**
* Compare the output of two executions
*/
private void assertEqualOutput(String phase, String exp, String act_) {
String act = act_.replace("\r\n", "\n"); // for windows machines
if (!exp.equals(act)) {
warnAboutDiff(phase, exp, act);
}
}
private void warnAboutDiff(String phase, String exp, String act) {
try {
try (PrintStream err = new PrintStream(errfile)) {

View file

@ -1,4 +1,4 @@
package cd.test;
package cd;
// NOTE: This code was adapted from the code found at
// http://www.cs.princeton.edu/introcs/96optimization/Diff.java.html

View file

@ -1,4 +1,4 @@
package cd.test;
package cd;
import java.io.File;
import java.io.StringWriter;
@ -10,8 +10,6 @@ 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)
@ -31,8 +29,8 @@ 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/HW3_nop90");
// public static final File testDir = new File("javali_tests");
// public static final File testDir = new File("javali_tests/HW1");
public static final File testDir = new File("javali_tests");
@Parameters(name="{index}:{0}")
public static Collection<Object[]> testFiles() {
@ -64,6 +62,8 @@ 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();