Homework 2

This commit is contained in:
Carlos Galindo 2020-01-15 22:30:09 +01:00
parent 12f678a924
commit bf60a078d7
Signed by: kauron
GPG key ID: 83E68706DEE119A3
64 changed files with 4786 additions and 1185 deletions

View file

@ -1,20 +1,21 @@
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.ir.Ast.ClassDecl;
import cd.util.FileUtil;
@ -26,13 +27,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 + "]");
@ -47,9 +52,6 @@ abstract public class AbstractTestAgainstFrozenReference {
List<ClassDecl> astRoots = testParser();
if (astRoots != null) {
{
testCodeGenerator(astRoots);
}
}
} catch (org.junit.ComparisonFailure cf) {
throw cf;
@ -78,7 +80,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();
@ -94,13 +96,24 @@ abstract public class AbstractTestAgainstFrozenReference {
private static String referenceVersion() {
{
return "CD_HW_CODEGEN_SIMPLE_SOL";
return "CD_HW_PARSER_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;
@ -110,8 +123,11 @@ abstract public class AbstractTestAgainstFrozenReference {
parserOut = AstDump.toString(astRoots);
} catch (ParseFailure pf) {
{
// For this assignment, we expect all tests to parse!
throw pf;
// Parse errors are ok too.
main.debug("");
main.debug("");
main.debug("%s", pf.toString());
parserOut = PARSE_FAILURE;
}
}
@ -121,64 +137,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");
}
/**
* 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 = findExecRef();
// 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 String findExecRef() throws IOException {
// Check for a .ref file
if (execreffile.exists() && execreffile.lastModified() > file.lastModified()) {
return FileUtil.read(execreffile);
}
throw new RuntimeException("ERROR: could not find execution .ref");
}
private void assertEquals(String phase, String exp, String act_) {
String act = act_.replace("\r\n", "\n"); // for windows machines
@ -186,16 +144,6 @@ abstract public class AbstractTestAgainstFrozenReference {
warnAboutDiff(phase, exp, act);
}
}
/**
* 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 {

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,9 @@
package cd.test;
package cd;
import cd.util.FileUtil;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.io.File;
import java.io.StringWriter;
@ -6,14 +11,6 @@ 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 {
@ -31,8 +28,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/HW1");
public static final File testDir = new File("javali_tests");
public static final File testDir = new File("javali_tests/HW2_nop90");
// public static final File testDir = new File("javali_tests");
@Parameters(name="{index}:{0}")
public static Collection<Object[]> testFiles() {
@ -64,6 +61,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();