Test update
This commit is contained in:
parent
41d5cd428c
commit
3c863aafde
@ -46,13 +46,14 @@ public class testAll {
|
||||
testAst(ast, pathToCode);
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
public void testDivideByZero(){
|
||||
Program ast = AssignWrongTypeAST.getProgram();
|
||||
String pathToCode = "failTests/DivideByzero.java";
|
||||
testAst(ast, pathToCode);
|
||||
}
|
||||
|
||||
*/
|
||||
@Test
|
||||
public void testcharArgument(){
|
||||
Program ast = CharArgumentAST.getProgram();
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ASTs;
|
||||
|
||||
import Typecheck.TypingHelper;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
@ -15,6 +16,7 @@ import abstractSyntaxTree.Statement.ReturnStatement;
|
||||
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class AssignWrongTypeASTTyped {
|
||||
@ -54,6 +56,22 @@ public class AssignWrongTypeASTTyped {
|
||||
List<RefType> refTypeList = new ArrayList<>();
|
||||
refTypeList.add(assignWrongType);
|
||||
Program program = new Program(refTypeList);
|
||||
|
||||
addTyping(program);
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
public static void addTyping(Program program){
|
||||
//Type Context
|
||||
TypingHelper.addTypeContext(program, "AssignWrongType", new HashMap<>());
|
||||
|
||||
|
||||
//Method Context
|
||||
HashMap<String, ParameterList> method0 = new HashMap<>();
|
||||
method0.put("int", new ParameterList(new ArrayList<>()));
|
||||
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
|
||||
methods.put("test", method0);
|
||||
TypingHelper.addMethodContext(program, "AssignWrongType", methods);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ASTs;
|
||||
|
||||
import Typecheck.TypingHelper;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
@ -14,6 +15,7 @@ import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
|
||||
import abstractSyntaxTree.StatementExpression.MethodCallStatementExpression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class CharArgumentASTTyped {
|
||||
@ -70,4 +72,16 @@ public class CharArgumentASTTyped {
|
||||
|
||||
return methodDecl;
|
||||
}
|
||||
|
||||
public static void addTyping(Program program){
|
||||
// TypeContext
|
||||
TypingHelper.addTypeContext(program, "EmptyClassWithConstructor", new HashMap<>());
|
||||
|
||||
// MethodContext
|
||||
HashMap<String, ParameterList> method0 = new HashMap<>();
|
||||
//method0.put("void", new ParameterList(new ArrayList<>()));
|
||||
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
|
||||
//methods.put("main", method0);
|
||||
TypingHelper.addMethodContext(program, "EmptyClassWithConstructor", methods);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ASTs;
|
||||
|
||||
import Typecheck.TypingHelper;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
@ -9,6 +10,7 @@ import abstractSyntaxTree.Program;
|
||||
import abstractSyntaxTree.Statement.BlockStatement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class ClassWithMainASTTyped {
|
||||
@ -16,7 +18,7 @@ public class ClassWithMainASTTyped {
|
||||
List<RefType> refTypeList = new ArrayList<>();
|
||||
refTypeList.add(getRefType());
|
||||
Program program = new Program(refTypeList);
|
||||
|
||||
addTyping(program);
|
||||
return program;
|
||||
}
|
||||
public static RefType getRefType(){
|
||||
@ -37,4 +39,13 @@ public class ClassWithMainASTTyped {
|
||||
|
||||
return methodDecl;
|
||||
}
|
||||
|
||||
public static void addTyping(Program program){
|
||||
TypingHelper.addTypeContext(program, "classWithMain", new HashMap<>());
|
||||
HashMap<String, ParameterList> method0 = new HashMap<>();
|
||||
method0.put("void", new ParameterList(new ArrayList<>()));
|
||||
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
|
||||
methods.put("main", method0);
|
||||
TypingHelper.addMethodContext(program, "classWithMain", methods);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ASTs;
|
||||
|
||||
import Typecheck.TypingHelper;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
@ -9,6 +10,7 @@ import abstractSyntaxTree.Statement.BlockStatement;
|
||||
import abstractSyntaxTree.Statement.IStatement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class EmptyClassWithConstructorASTTyped {
|
||||
@ -17,7 +19,7 @@ public class EmptyClassWithConstructorASTTyped {
|
||||
|
||||
ParameterList emptyParameterList = new ParameterList(new ArrayList<>());
|
||||
List<IStatement> emptyIStatementList = new ArrayList<>();
|
||||
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, null);
|
||||
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, "void");
|
||||
MethodDecl constructor = new MethodDecl("EmptyClassWithConstructor", null, "EmptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
|
||||
List<MethodDecl> methodDeclList = new ArrayList<>();
|
||||
methodDeclList.add(constructor);
|
||||
@ -26,6 +28,19 @@ public class EmptyClassWithConstructorASTTyped {
|
||||
classes.add(emptyClass);
|
||||
|
||||
Program program = new Program(classes);
|
||||
addTyping(program);
|
||||
return program;
|
||||
}
|
||||
|
||||
public static void addTyping(Program program){
|
||||
// TypeContext
|
||||
TypingHelper.addTypeContext(program, "EmptyClassWithConstructor", new HashMap<>());
|
||||
|
||||
// MethodContext
|
||||
HashMap<String, ParameterList> method0 = new HashMap<>();
|
||||
//method0.put("void", new ParameterList(new ArrayList<>()));
|
||||
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
|
||||
//methods.put("main", method0);
|
||||
TypingHelper.addMethodContext(program, "EmptyClassWithConstructor", methods);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package ASTs;
|
||||
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import Typecheck.TypingHelper;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
@ -13,6 +15,7 @@ import abstractSyntaxTree.Statement.*;
|
||||
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class FakultaetASTTyped {
|
||||
@ -27,7 +30,7 @@ public class FakultaetASTTyped {
|
||||
RefType fakultaet = new RefType(name, fieldDeclList, methodDeclList, true);
|
||||
classes.add(fakultaet);
|
||||
Program program = new Program(classes);
|
||||
|
||||
addTyping(program);
|
||||
return program;
|
||||
}
|
||||
|
||||
@ -37,7 +40,7 @@ public class FakultaetASTTyped {
|
||||
ParameterList parameterList = new ParameterList(new ArrayList<>());
|
||||
String returnType = "void";
|
||||
List<IStatement> iStmtList = new ArrayList<>();
|
||||
BlockStatement blockStatement = new BlockStatement(iStmtList, null);
|
||||
BlockStatement blockStatement = new BlockStatement(iStmtList, "void");
|
||||
return new MethodDecl(classThatContainsMethod, returnType, name, parameterList, blockStatement);
|
||||
}
|
||||
|
||||
@ -62,10 +65,13 @@ public class FakultaetASTTyped {
|
||||
BinaryExpression binExpr = new BinaryExpression("<", binExprIdentifier, binExprIntConst);
|
||||
|
||||
IntConstantExpression return0Expr0 = new IntConstantExpression(1);
|
||||
TypeCheckResult typeCheckResult1 = new TypeCheckResult();
|
||||
typeCheckResult1.type = "int";
|
||||
return0Expr0.setTypeCheckResult(typeCheckResult1);
|
||||
ReturnStatement returnStatement0 = new ReturnStatement(return0Expr0);
|
||||
List<IStatement> iStmtList1 = new ArrayList<>();
|
||||
iStmtList1.add(returnStatement0);
|
||||
BlockStatement ifStmt = new BlockStatement(iStmtList1, null);
|
||||
BlockStatement ifStmt = new BlockStatement(iStmtList1, "int");
|
||||
IfStatement ifStatement = new IfStatement(binExpr, ifStmt);
|
||||
|
||||
iStmtList0.add(ifStatement);
|
||||
@ -88,7 +94,7 @@ public class FakultaetASTTyped {
|
||||
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", new LocalVarIdentifier("factorial"), whileBinExpr);
|
||||
List<IStatement> whileBlockStmts = new ArrayList<>();
|
||||
whileBlockStmts.add(assignStatementExpression0);
|
||||
BlockStatement whileBlock = new BlockStatement( whileBlockStmts, null);
|
||||
BlockStatement whileBlock = new BlockStatement( whileBlockStmts, "void");
|
||||
|
||||
WhileStatement whileStatement0 = new WhileStatement(whileCondition, whileBlock);
|
||||
|
||||
@ -96,10 +102,35 @@ public class FakultaetASTTyped {
|
||||
|
||||
// Return Statement
|
||||
LocalVarIdentifier returnIdentifier = new LocalVarIdentifier("factorial");
|
||||
TypeCheckResult typeCheckResult0 = new TypeCheckResult();
|
||||
typeCheckResult0.type = "int";
|
||||
returnIdentifier.setTypeCheckResult(typeCheckResult0);
|
||||
ReturnStatement returnStatement = new ReturnStatement(returnIdentifier);
|
||||
|
||||
iStmtList0.add(returnStatement);
|
||||
BlockStatement blockStatement = new BlockStatement(iStmtList0, null);
|
||||
BlockStatement blockStatement = new BlockStatement(iStmtList0, "int");
|
||||
return new MethodDecl(classThatContainsMethod, returnType, name, parameterListObj, blockStatement);
|
||||
}
|
||||
|
||||
public static void addTyping(Program program){
|
||||
// TypeContext
|
||||
TypingHelper.addTypeContext(program, "Fakultaet", new HashMap<>());
|
||||
|
||||
// MethodContext
|
||||
//main Method
|
||||
HashMap<String, ParameterList> method1 = new HashMap<>();
|
||||
method1.put("void", new ParameterList(new ArrayList<>()));
|
||||
//fak Method
|
||||
HashMap<String, ParameterList> method0 = new HashMap<>();
|
||||
List<Parameter> parameterList0 = new ArrayList<>(){{
|
||||
add(new Parameter("int", "number"));
|
||||
}};
|
||||
ParameterList parameterListObj0 = new ParameterList(parameterList0);
|
||||
method0.put("int", parameterListObj0);
|
||||
|
||||
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
|
||||
methods.put("fak", method0);
|
||||
methods.put("main", method1);
|
||||
TypingHelper.addMethodContext(program, "Fakultaet", methods);
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,12 @@ package ASTs;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import abstractSyntaxTree.Program;
|
||||
import Typecheck.TypingHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class emptyClassASTTyped {
|
||||
@ -15,7 +18,15 @@ public class emptyClassASTTyped {
|
||||
RefType emptyClass = new RefType("emptyClass", emptyFieldDeclList, emptyMethodDeclList, false);
|
||||
List<RefType> classes = new ArrayList<>();
|
||||
classes.add(emptyClass);
|
||||
Program program = new Program(classes);
|
||||
addTyping(program);
|
||||
return program;
|
||||
}
|
||||
|
||||
return (new Program(classes));
|
||||
public static void addTyping(Program program) {
|
||||
HashMap<String, String> fields = new HashMap<>();
|
||||
//fields.put();
|
||||
TypingHelper.addTypeContext(program, "emptyClass", fields);
|
||||
TypingHelper.addMethodContext(program, "emptyClass", new HashMap<>());
|
||||
}
|
||||
}
|
||||
|
168
src/test/java/ByteCode/ByteCodeTester.java
Normal file
168
src/test/java/ByteCode/ByteCodeTester.java
Normal file
@ -0,0 +1,168 @@
|
||||
package ByteCode;
|
||||
|
||||
import abstractSyntaxTree.Program;
|
||||
import astGenerator.ASTGenerator;
|
||||
import gen.DecafLexer;
|
||||
import gen.DecafParser;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class ByteCodeTester {
|
||||
CompareByteCodeBehaviour byteCodeBehaviourComparer;
|
||||
public ByteCodeTester(){
|
||||
byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
|
||||
}
|
||||
|
||||
public void testByteCodeFromAst(String correctClassFilePath, Program abstractSyntaxTree, String className) {
|
||||
testClassFile(false, className, abstractSyntaxTree, correctClassFilePath, null);
|
||||
}
|
||||
|
||||
public void testClassFileFromScratch(String correctClassFilePath, String fileToComparePath, String className) {
|
||||
testClassFile(true, className, null, correctClassFilePath, fileToComparePath);
|
||||
}
|
||||
public void testClassFile(boolean generateAST, String className, Program abstractSyntaxTree,
|
||||
String correctClassFilePath, String fileToComparePath) {
|
||||
|
||||
if(generateAST){
|
||||
String content = "";
|
||||
try {
|
||||
System.out.println("Classpath: " + Path.of(fileToComparePath));
|
||||
content = Files.readString(Path.of(fileToComparePath));
|
||||
} catch (IOException e) {
|
||||
System.out.println("File not found!");
|
||||
fail();
|
||||
}
|
||||
|
||||
CharStream codeCharStream = CharStreams.fromString(content);
|
||||
DecafLexer lexer = new DecafLexer(codeCharStream);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
|
||||
tokens.fill();
|
||||
|
||||
DecafParser parser = new DecafParser(tokens);
|
||||
|
||||
ParseTree tree = parser.program();
|
||||
|
||||
ASTGenerator generator = new ASTGenerator();
|
||||
abstractSyntaxTree = (Program) generator.visit(tree);
|
||||
}
|
||||
|
||||
if(generateAST){
|
||||
try {
|
||||
abstractSyntaxTree.typeCheck();
|
||||
} catch (Exception e){
|
||||
System.out.println("Le Exception in le type-check");
|
||||
//fail();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
abstractSyntaxTree.codeGen();
|
||||
} catch (Exception e){
|
||||
System.out.println("Le Exception in le codegen");
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
|
||||
try {
|
||||
ClassFileLoader classLoader1 = new ClassFileLoader(correctClassFilePath);
|
||||
ClassFileLoader classLoader2 = new ClassFileLoader(className+ ".class");
|
||||
|
||||
Class<?> class1 = classLoader1.findClass(className);
|
||||
Class<?> class2 = classLoader2.findClass(className);
|
||||
|
||||
assertTrue(CompareByteCodeSyntax.haveSameBehavior(class1, class2));
|
||||
assertTrue(byteCodeBehaviourComparer.compareMethodBehaviour(class1, class2));
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void compareJarFilesFromAST(Program abstractSyntaxTree, String jarPath, String[] classNames){
|
||||
try {
|
||||
abstractSyntaxTree.codeGen();
|
||||
} catch (Exception e){
|
||||
System.out.println("Le Exception in le codegen");
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
|
||||
compareJarFiles(jarPath, "output.jar", classNames);
|
||||
}
|
||||
|
||||
public void compareJarFilesFromScratch(String javaCodePath, String jarPath, String[] classNames){
|
||||
String content = "";
|
||||
try {
|
||||
System.out.println("Classpath: " + Path.of(javaCodePath));
|
||||
content = Files.readString(Path.of(javaCodePath));
|
||||
} catch (IOException e) {
|
||||
System.out.println("File not found!");
|
||||
fail();
|
||||
}
|
||||
|
||||
CharStream codeCharStream = CharStreams.fromString(content);
|
||||
DecafLexer lexer = new DecafLexer(codeCharStream);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
|
||||
tokens.fill();
|
||||
|
||||
DecafParser parser = new DecafParser(tokens);
|
||||
|
||||
ParseTree tree = parser.program();
|
||||
|
||||
ASTGenerator generator = new ASTGenerator();
|
||||
Program abstractSyntaxTree = (Program) generator.visit(tree);
|
||||
|
||||
try {
|
||||
abstractSyntaxTree.typeCheck();
|
||||
} catch (Exception e){
|
||||
System.out.println("Le Exception in le type-check");
|
||||
//fail();
|
||||
}
|
||||
|
||||
try {
|
||||
abstractSyntaxTree.codeGen();
|
||||
} catch (Exception e){
|
||||
System.out.println("Le Exception in le codegen");
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
|
||||
compareJarFiles(jarPath, "output.jar", classNames);
|
||||
|
||||
}
|
||||
|
||||
public void compareJarFiles(String jarPath1, String jarPath2, String[] classNames) {
|
||||
try {
|
||||
JarFileLoader loader1 = new JarFileLoader(jarPath1);
|
||||
JarFileLoader loader2 = new JarFileLoader(jarPath2);
|
||||
CompareByteCodeBehaviour comparator = new CompareByteCodeBehaviour();
|
||||
|
||||
for (String className : classNames) {
|
||||
Class<?> class1 = loader1.loadClass(className);
|
||||
Class<?> class2 = loader2.loadClass(className);
|
||||
|
||||
boolean comparisonResult0 = CompareByteCodeSyntax.haveSameBehavior(class1, class2);
|
||||
System.out.println("Syntax Comparison result for " + className + ": " + comparisonResult0);
|
||||
|
||||
boolean comparisonResult1 = comparator.compareMethodBehaviour(class1, class2);
|
||||
System.out.println("Behaviour Comparison result for " + className + ": " + comparisonResult1);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,8 @@ public class JarFileLoader extends ClassLoader {
|
||||
byte[] classData = loadClassDataFromJar(classFileName);
|
||||
return defineClass(name, classData, 0, classData.length);
|
||||
} catch (IOException e) {
|
||||
throw new ClassNotFoundException("Class not found", e);
|
||||
e.printStackTrace();
|
||||
throw new ClassNotFoundException("Class not found: " + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,6 +32,7 @@ public class JarFileLoader extends ClassLoader {
|
||||
try (JarFile jarFile = new JarFile(new File(jarFilePath))) {
|
||||
JarEntry entry = jarFile.getJarEntry(classFileName);
|
||||
if (entry == null) {
|
||||
System.err.println("Class file " + classFileName + " not found in jar file.");
|
||||
throw new IOException("Class file " + classFileName + " not found in jar file.");
|
||||
}
|
||||
|
||||
|
@ -1,133 +1,66 @@
|
||||
package ByteCode;
|
||||
|
||||
import abstractSyntaxTree.Program;
|
||||
import astGenerator.ASTGenerator;
|
||||
import gen.DecafLexer;
|
||||
import gen.DecafParser;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import ASTs.emptyClassAST;
|
||||
import ASTs.*;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
||||
public class TestAll {
|
||||
CompareByteCodeBehaviour byteCodeBehaviourComparer;
|
||||
public TestAll(){
|
||||
byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
|
||||
}
|
||||
|
||||
public void testByteCodeFromAst(String correctClassFilePath, Program abstractSyntaxTree, String className) {
|
||||
testByteCode(false, className, abstractSyntaxTree, correctClassFilePath, null);
|
||||
}
|
||||
|
||||
public void testByteCodeFromScratch(String correctClassFilePath, String fileToComparePath, String className) {
|
||||
testByteCode(true, className, null, correctClassFilePath, fileToComparePath);
|
||||
}
|
||||
public void testByteCode(boolean generateAST, String className, Program abstractSyntaxTree,
|
||||
String correctClassFilePath, String fileToComparePath) {
|
||||
|
||||
if(generateAST){
|
||||
String content = "";
|
||||
try {
|
||||
System.out.println("Classpath: " + Path.of(fileToComparePath));
|
||||
content = Files.readString(Path.of(fileToComparePath));
|
||||
} catch (IOException e) {
|
||||
System.out.println("File not found!");
|
||||
fail();
|
||||
}
|
||||
|
||||
CharStream codeCharStream = CharStreams.fromString(content);
|
||||
DecafLexer lexer = new DecafLexer(codeCharStream);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
|
||||
tokens.fill();
|
||||
|
||||
DecafParser parser = new DecafParser(tokens);
|
||||
|
||||
ParseTree tree = parser.program();
|
||||
|
||||
ASTGenerator generator = new ASTGenerator();
|
||||
abstractSyntaxTree = (Program) generator.visit(tree);
|
||||
}
|
||||
|
||||
try {
|
||||
abstractSyntaxTree.typeCheck();
|
||||
} catch (Exception e){
|
||||
System.out.println("Le Exception in le type-check");
|
||||
//fail();
|
||||
}
|
||||
|
||||
try {
|
||||
abstractSyntaxTree.codeGen();
|
||||
} catch (Exception e){
|
||||
System.out.println("Le Exception in le codegen");
|
||||
fail();
|
||||
}
|
||||
|
||||
try {
|
||||
ClassFileLoader classLoader1 = new ClassFileLoader(correctClassFilePath);
|
||||
ClassFileLoader classLoader2 = new ClassFileLoader(className+ ".class");
|
||||
|
||||
Class<?> class1 = classLoader1.findClass(className);
|
||||
Class<?> class2 = classLoader2.findClass(className);
|
||||
|
||||
assertTrue(CompareByteCodeSyntax.haveSameBehavior(class1, class2));
|
||||
assertTrue(byteCodeBehaviourComparer.compareMethodBehaviour(class1, class2));
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
ByteCodeTester byteCodeTester;
|
||||
public TestAll(){byteCodeTester = new ByteCodeTester();}
|
||||
@Test
|
||||
public void testEmptyClass() {
|
||||
String classPath = "src/test/resources/basicClasses/emptyClass.class";
|
||||
Program ast = emptyClassAST.getEmptyProgramm();
|
||||
Program ast = emptyClassASTTyped.getEmptyProgramm();
|
||||
String className = "emptyClass";
|
||||
String javacode = "src/test/resources/basicClasses/emptyClass.java";
|
||||
testByteCodeFromAst(classPath, ast ,className);
|
||||
//testByteCodeFromScratch(classPath, javacode, className);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFourClasses() {
|
||||
String classPath = "src/test/resources/basicClasses/FourClasses.class";
|
||||
Program ast = emptyClassAST.getEmptyProgramm();
|
||||
String className = "emptyClass";
|
||||
String javacode = "src/test/resources/basicClasses/FourClasses.java";
|
||||
//testByteCodeFromAst(classPath, ast ,className);
|
||||
testByteCodeFromScratch(classPath, javacode, className);
|
||||
//byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
|
||||
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFakultaet() {
|
||||
String classPath = "src/test/resources/basicClasses/Fakultaet.class";
|
||||
Program ast = emptyClassAST.getEmptyProgramm();
|
||||
Program ast = FakultaetASTTyped.getProgram();
|
||||
String className = "Fakultaet";
|
||||
String javacode = "src/test/resources/basicClasses/Fakultaet.java";
|
||||
//testByteCodeFromAst(classPath, ast ,className);
|
||||
testByteCodeFromScratch(classPath, javacode, className);
|
||||
//byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
|
||||
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithMain() {
|
||||
String classPath = "src/test/resources/basicClasses/classWithMain.class";
|
||||
Program ast = emptyClassAST.getEmptyProgramm();
|
||||
Program ast = ClassWithMainASTTyped.getProgram();
|
||||
String className = "classWithMain";
|
||||
String javacode = "src/test/resources/basicClasses/classWithMain.java";
|
||||
//testByteCodeFromAst(classPath, ast ,className);
|
||||
testByteCodeFromScratch(classPath, javacode, className);
|
||||
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoClasses() {
|
||||
String jarPath = "src/test/resources/basicClasses/TwoClasses.jar";
|
||||
//Program ast = ClassWithMainASTTyped.getProgram();
|
||||
String[] classNames = {"ClassOne", "ClassTwo"};
|
||||
String javacode = "src/test/resources/basicClasses/TwoClasses.java";
|
||||
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFourClasses() {
|
||||
String jarPath = "src/test/resources/basicClasses/FourClasses.jar";
|
||||
//Program ast = ClassWithMainASTTyped.getProgram();
|
||||
String[] classNames = {"FourClasses", "Test", "Test2", "Test3"};
|
||||
String javacode = "src/test/resources/basicClasses/FourClasses.java";
|
||||
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -95,16 +95,17 @@ public class TestAll {
|
||||
|
||||
@Test
|
||||
public void testFakultaet(){
|
||||
Program assignWrongType = FakultaetAST.getProgram();
|
||||
Program correctAST = FakultaetAST.getProgram();
|
||||
Program fakultaet = FakultaetAST.getProgram();
|
||||
Program correctAST = FakultaetASTTyped.getProgram();
|
||||
boolean expectedResult = true;
|
||||
testTypeCheck(assignWrongType, correctAST, expectedResult);
|
||||
testTypeCheck(fakultaet, correctAST, expectedResult);
|
||||
}
|
||||
|
||||
|
||||
public void testTypeCheckWM(Program abstractSyntaxTree, Program correctAST, boolean expectedResult){
|
||||
abstractSyntaxTree.classes.add(ClassWithMainASTTyped.getRefType());
|
||||
correctAST.classes.add(ClassWithMainASTTyped.getRefType());
|
||||
ClassWithMainASTTyped.addTyping(correctAST);
|
||||
testTypeCheck(abstractSyntaxTree, correctAST, expectedResult);
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,13 @@ public class TypeChecker {
|
||||
} catch (Exception e) {
|
||||
actualResult = false;
|
||||
}
|
||||
|
||||
assertEquals(programmToBeTested.typeContext, correctAST.typeContext);
|
||||
assertEquals(programmToBeTested.methodContext, correctAST.methodContext);
|
||||
assertEquals(expectedResult, actualResult);
|
||||
assertEquals(programmToBeTested, correctAST);
|
||||
if(expectedResult){
|
||||
assertEquals(programmToBeTested, correctAST);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
22
src/test/java/Typecheck/TypingHelper.java
Normal file
22
src/test/java/Typecheck/TypingHelper.java
Normal file
@ -0,0 +1,22 @@
|
||||
package Typecheck;
|
||||
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import abstractSyntaxTree.Program;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TypingHelper {
|
||||
public static void addTypeContext(Program program, String className, HashMap<String, String> fields) {
|
||||
if (program.typeContext == null) {
|
||||
program.typeContext = new HashMap<>();
|
||||
}
|
||||
program.typeContext.put(className, fields);
|
||||
}
|
||||
|
||||
public static void addMethodContext(Program program, String className, HashMap<String, HashMap<String, ParameterList>> methods) {
|
||||
if (program.methodContext == null) {
|
||||
program.methodContext = new HashMap<>();
|
||||
}
|
||||
program.methodContext.put(className, methods);
|
||||
}
|
||||
}
|
@ -6,6 +6,10 @@ class FourClasses {
|
||||
return t2.test.test3.getX();
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Test {
|
||||
@ -19,11 +23,11 @@ class Test {
|
||||
this.test3 = new Test3(i * 2);
|
||||
}
|
||||
|
||||
public Test3 getTest3(int not) {
|
||||
public Test3 getTest3() {
|
||||
return this.test3;
|
||||
}
|
||||
|
||||
public int getX(int not) {
|
||||
public int getX() {
|
||||
return this.x;
|
||||
}
|
||||
}
|
||||
@ -46,11 +50,11 @@ class Test3 {
|
||||
this.x = i;
|
||||
}
|
||||
|
||||
public int getX(int not) {
|
||||
public int getX() {
|
||||
return this.x;
|
||||
}
|
||||
|
||||
public int getY(int not) {
|
||||
public int getY() {
|
||||
return this.y;
|
||||
}
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
17
src/test/resources/basicClasses/TwoClasses.java
Normal file
17
src/test/resources/basicClasses/TwoClasses.java
Normal file
@ -0,0 +1,17 @@
|
||||
class ClassOne{
|
||||
public int classOneFunction(){
|
||||
ClassTwo classTwo = new ClassTwo();
|
||||
int returnValue = classTwo.classTwoFunction();
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ClassTwo{
|
||||
public int classTwoFunction(){
|
||||
return 2;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user