Test update

This commit is contained in:
Julian Murek 2024-07-02 12:57:44 +02:00
parent 38aeaa657d
commit e46cede8d5
40 changed files with 817 additions and 166 deletions

2
.idea/misc.xml generated
View File

@ -24,7 +24,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/target" /> <output url="file://$PROJECT_DIR$/target" />
</component> </component>
</project> </project>

BIN
Fakultaet.class Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -72,7 +72,8 @@ public class FieldDecl extends AbstractType implements Node {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
FieldDecl fieldDecl = (FieldDecl) o; FieldDecl fieldDecl = (FieldDecl) o;
boolean result = Objects.equals(type, fieldDecl.type) boolean result = Objects.equals(type, fieldDecl.type)
&& Objects.equals(identifier, fieldDecl.identifier); && Objects.equals(identifier, fieldDecl.identifier)
&& Objects.equals(expression, fieldDecl.expression);
System.out.println("In FieldDecl: " + result); System.out.println("In FieldDecl: " + result);
return result; return result;

View File

@ -129,9 +129,9 @@ public class BlockStatement extends AbstractType implements IStatement {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
BlockStatement blockStatement = (BlockStatement) o; BlockStatement blockStatement = (BlockStatement) o;
boolean result = (Objects.equals(localVars, blockStatement.localVars) boolean result = Objects.equals(returnType, blockStatement.returnType)
&& Objects.equals(returnType, blockStatement.returnType) && Objects.equals(statements, blockStatement.statements
&& Objects.equals(statements, blockStatement.statements) /*&& (Objects.equals(localVars, blockStatement.localVars)*/
); );
System.out.println("In ParameterList: " + result); System.out.println("In ParameterList: " + result);

View File

@ -8,9 +8,16 @@ public class testAll {
AstComparer astComparer; AstComparer astComparer;
public testAll() public testAll()
{ {
this.astComparer = new AstComparer("test/resources"); this.astComparer = new AstComparer("src/test/resources");
} }
@Test
public void testClassWithMain(){
Program ast = ClassWithMainAST.getProgram();
String pathToCode = "basicClasses/classWithMain.java";
testAst(ast, pathToCode);
}
@Test @Test
public void testEmptyClass(){ public void testEmptyClass(){
Program ast = emptyClassAST.getEmptyProgramm(); Program ast = emptyClassAST.getEmptyProgramm();
@ -38,6 +45,21 @@ public class testAll {
String pathToCode = "failTests/AssignWrongType.java"; String pathToCode = "failTests/AssignWrongType.java";
testAst(ast, pathToCode); 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();
String pathToCode = "SimpleTests/CharArgument.java";
testAst(ast, pathToCode);
}
public void testAst(Program ast, String pathToCode) public void testAst(Program ast, String pathToCode)
{ {

View File

@ -0,0 +1,59 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BooleanConstantExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.LocalVarDecl;
import abstractSyntaxTree.Statement.ReturnStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class AssignWrongTypeASTTyped {
public static Program getProgram(){
// TestMethod
//Expressions
// 0
IntConstantExpression intConstantExpression0 = new IntConstantExpression(1);
LocalVarDecl localVarDecl = new LocalVarDecl("int", "x", intConstantExpression0);
//1
LocalVarIdentifier localVarIdentifier0 = new LocalVarIdentifier("x");
BooleanConstantExpression booleanConstantExpression0 = new BooleanConstantExpression(true);
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", localVarIdentifier0, booleanConstantExpression0);
//2
ReturnStatement returnStatement0 = new ReturnStatement(localVarIdentifier0);
String classThatContainsMethod = "AssignWrongType";
String nameMethod0 = "test";
String returnType = "int";
ParameterList parameterList = new ParameterList(new ArrayList<>());
List<IStatement> iStatementList = new ArrayList<>();
iStatementList.add(localVarDecl);
iStatementList.add(assignStatementExpression0);
iStatementList.add(returnStatement0);
BlockStatement codeBlock = new BlockStatement(iStatementList, null);
MethodDecl test = new MethodDecl(classThatContainsMethod, returnType, nameMethod0, parameterList, codeBlock);
String name = "AssignWrongType";
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(test);
boolean hasMain = false;
RefType assignWrongType = new RefType(name, fieldDeclList, methodDeclList, hasMain);
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(assignWrongType);
Program program = new Program(refTypeList);
return program;
}
}

View File

@ -0,0 +1,73 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.*;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.ReturnStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import abstractSyntaxTree.StatementExpression.MethodCallStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class CharArgumentAST {
public static Program getProgram() {
List<RefType> classes = new ArrayList<>();
classes.add(getRefType());
Program program = new Program(classes);
return program;
}
public static RefType getRefType() {
FieldDecl fieldDecl0 = new FieldDecl("char", "c", null);
List<FieldDecl> fieldDeclList = new ArrayList<>();
fieldDeclList.add(fieldDecl0);
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
methodDeclList.add(method1());
RefType charArgumentClass = new RefType("CharArgument", fieldDeclList, methodDeclList, false);
return charArgumentClass;
}
public static MethodDecl method0(){
SubReceiver subReceiver0 =new SubReceiver(true);
List<SubReceiver> subReceiverList0 = new ArrayList<>();
subReceiverList0.add(subReceiver0);
InstVarExpression instVarExpression0 = new InstVarExpression(subReceiverList0, new ArrayList<>(), "c");
CharConstantExpression charConstantExpression0 = new CharConstantExpression('a');
List<IExpression> argumentList0 = new ArrayList<>();
argumentList0.add(charConstantExpression0);
MethodCallStatementExpression methodCallStatementExpression0 = new MethodCallStatementExpression("foo", null, new ArrayList<>(), argumentList0);
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", instVarExpression0,methodCallStatementExpression0);
List<IStatement> iStatementList0 = new ArrayList<>();
iStatementList0.add(assignStatementExpression0);
BlockStatement blockStatement0 = new BlockStatement(iStatementList0, null);
MethodDecl methodDecl0 =new MethodDecl("CharArgument", null,
"CharArgument", new ParameterList(new ArrayList<>()), blockStatement0);
return methodDecl0;
}
public static MethodDecl method1(){
ReturnStatement returnStatement0 =new ReturnStatement(new LocalVarIdentifier("c"));
List<IStatement> iStatementList0 =new ArrayList<>();
iStatementList0.add(returnStatement0);
BlockStatement blockStatement0 = new BlockStatement(iStatementList0, null);
Parameter parameter00 = new Parameter("char", "c");
List<Parameter> parameterList0 = new ArrayList<>();
parameterList0.add(parameter00);
ParameterList parameterListObj0 = new ParameterList(parameterList0);
MethodDecl methodDecl = new MethodDecl("CharArgument", "char", "foo",
parameterListObj0, blockStatement0);
return methodDecl;
}
}

View File

@ -0,0 +1,73 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.*;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.ReturnStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import abstractSyntaxTree.StatementExpression.MethodCallStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class CharArgumentASTTyped {
public static Program getProgram() {
List<RefType> classes = new ArrayList<>();
classes.add(getRefType());
Program program = new Program(classes);
return program;
}
public static RefType getRefType() {
FieldDecl fieldDecl0 = new FieldDecl("char", "c", null);
List<FieldDecl> fieldDeclList = new ArrayList<>();
fieldDeclList.add(fieldDecl0);
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
methodDeclList.add(method1());
RefType charArgumentClass = new RefType("CharArgument", fieldDeclList, methodDeclList, false);
return charArgumentClass;
}
public static MethodDecl method0(){
SubReceiver subReceiver0 =new SubReceiver(true);
List<SubReceiver> subReceiverList0 = new ArrayList<>();
subReceiverList0.add(subReceiver0);
InstVarExpression instVarExpression0 = new InstVarExpression(subReceiverList0, new ArrayList<>(), "c");
CharConstantExpression charConstantExpression0 = new CharConstantExpression('a');
List<IExpression> argumentList0 = new ArrayList<>();
argumentList0.add(charConstantExpression0);
MethodCallStatementExpression methodCallStatementExpression0 = new MethodCallStatementExpression("foo", null, new ArrayList<>(), argumentList0);
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", instVarExpression0,methodCallStatementExpression0);
List<IStatement> iStatementList0 = new ArrayList<>();
iStatementList0.add(assignStatementExpression0);
BlockStatement blockStatement0 = new BlockStatement(iStatementList0, null);
MethodDecl methodDecl0 =new MethodDecl("CharArgument", null,
"CharArgument", new ParameterList(new ArrayList<>()), blockStatement0);
return methodDecl0;
}
public static MethodDecl method1(){
ReturnStatement returnStatement0 =new ReturnStatement(new LocalVarIdentifier("c"));
List<IStatement> iStatementList0 =new ArrayList<>();
iStatementList0.add(returnStatement0);
BlockStatement blockStatement0 = new BlockStatement(iStatementList0, null);
Parameter parameter00 = new Parameter("char", "c");
List<Parameter> parameterList0 = new ArrayList<>();
parameterList0.add(parameter00);
ParameterList parameterListObj0 = new ParameterList(parameterList0);
MethodDecl methodDecl = new MethodDecl("CharArgument", "char", "foo",
parameterListObj0, blockStatement0);
return methodDecl;
}
}

View File

@ -0,0 +1,41 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import java.util.ArrayList;
import java.util.List;
public class ClassWithMainAST {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
return program;
}
public static RefType getRefType(){
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType classWithMain = new RefType("classWithMain", fieldDeclList, methodDeclList, true);
return classWithMain;
}
public static MethodDecl method0(){
BlockStatement blockStatement = new BlockStatement(new ArrayList<>(), null);
List<Parameter> parameterList = new ArrayList<>();
ParameterList parameterListObj = new ParameterList(parameterList);
MethodDecl methodDecl =new MethodDecl("classWithMain", "void", "main",
parameterListObj, blockStatement);
return methodDecl;
}
}

View File

@ -0,0 +1,40 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import java.util.ArrayList;
import java.util.List;
public class ClassWithMainASTTyped {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
return program;
}
public static RefType getRefType(){
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType classWithMain = new RefType("classWithMain", fieldDeclList, methodDeclList, true);
return classWithMain;
}
public static MethodDecl method0(){
BlockStatement blockStatement = new BlockStatement(new ArrayList<>(), "void");
List<Parameter> parameterList = new ArrayList<>();
ParameterList parameterListObj = new ParameterList(parameterList);
MethodDecl methodDecl =new MethodDecl("classWithMain", "void", "main",
parameterListObj, blockStatement);
return methodDecl;
}
}

View File

@ -0,0 +1,30 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Statement.BlockStatement;
import java.util.ArrayList;
import java.util.List;
public class DivideByZeroAST {
/*
public static RefType getRefType(){
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
RefType refType = new RefType("DivideByZero", fieldDeclList, methodDeclList, false);
return refType;
}
public static MethodDecl method0(){
BlockStatement blockStatement = new BlockStatement();
List<Parameter> parameterList = new ArrayList<>();
ParameterList parameterListObj = new ParameterList(parameterList);
MethodDecl methodDecl = new MethodDecl("DivideByZero", "int", "test",
);
}*/
}

View File

@ -0,0 +1,4 @@
package ASTs;
public class DivideByZeroASTTyped {
}

View File

@ -0,0 +1,31 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import java.util.ArrayList;
import java.util.List;
public class EmptyClassWithConstructorASTTyped {
public static Program getProgram() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
ParameterList emptyParameterList = new ParameterList(new ArrayList<>());
List<IStatement> emptyIStatementList = new ArrayList<>();
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, null);
MethodDecl constructor = new MethodDecl("EmptyClassWithConstructor", null, "EmptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(constructor);
RefType emptyClass = new RefType("EmptyClassWithConstructor", fieldDeclList, methodDeclList, false);
List<RefType> classes = new ArrayList<>();
classes.add(emptyClass);
Program program = new Program(classes);
return program;
}
}

View File

@ -0,0 +1,105 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BinaryExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.*;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class FakultaetASTTyped {
public static Program getProgram() {
String name = "Fakultaet";
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
methodDeclList.add(method1());
List<RefType> classes = new ArrayList<>();
RefType fakultaet = new RefType(name, fieldDeclList, methodDeclList, true);
classes.add(fakultaet);
Program program = new Program(classes);
return program;
}
private static MethodDecl method0() {
String classThatContainsMethod = "Fakultaet";
String name = "main";
ParameterList parameterList = new ParameterList(new ArrayList<>());
String returnType = "void";
List<IStatement> iStmtList = new ArrayList<>();
BlockStatement blockStatement = new BlockStatement(iStmtList, null);
return new MethodDecl(classThatContainsMethod, returnType, name, parameterList, blockStatement);
}
private static MethodDecl method1() {
String classThatContainsMethod = "Fakultaet";
String name = "fak";
// Parameters
Parameter param0 = new Parameter("int", "number");
List<Parameter> paramList = new ArrayList<>() {{
add(param0);
}};
ParameterList parameterListObj = new ParameterList(paramList);
String returnType = "int";
// Block Statement
List<IStatement> iStmtList0 = new ArrayList<>();
// If Statament
//condition
LocalVarIdentifier binExprIdentifier = new LocalVarIdentifier("number");
IntConstantExpression binExprIntConst = new IntConstantExpression(0);
BinaryExpression binExpr = new BinaryExpression("<", binExprIdentifier, binExprIntConst);
IntConstantExpression return0Expr0 = new IntConstantExpression(1);
ReturnStatement returnStatement0 = new ReturnStatement(return0Expr0);
List<IStatement> iStmtList1 = new ArrayList<>();
iStmtList1.add(returnStatement0);
BlockStatement ifStmt = new BlockStatement(iStmtList1, null);
IfStatement ifStatement = new IfStatement(binExpr, ifStmt);
iStmtList0.add(ifStatement);
// Expression 1
IntConstantExpression intConst0 = new IntConstantExpression(1);
LocalVarDecl localVarExpr0 = new LocalVarDecl("int", "factorial", intConst0);
iStmtList0.add(localVarExpr0);
// Expression 2
IntConstantExpression intConst1 = new IntConstantExpression(0);
LocalVarDecl localVarExpr1 = new LocalVarDecl("int", "i", intConst1);
iStmtList0.add(localVarExpr1);
//While Statement
BinaryExpression whileCondition = new BinaryExpression("<", new LocalVarIdentifier("i"), new LocalVarIdentifier("number"));
BinaryExpression whileBinExpr = new BinaryExpression("*", new LocalVarIdentifier("factorial"), new LocalVarIdentifier("i"));
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", new LocalVarIdentifier("factorial"), whileBinExpr);
List<IStatement> whileBlockStmts = new ArrayList<>();
whileBlockStmts.add(assignStatementExpression0);
BlockStatement whileBlock = new BlockStatement( whileBlockStmts, null);
WhileStatement whileStatement0 = new WhileStatement(whileCondition, whileBlock);
iStmtList0.add(whileStatement0);
// Return Statement
LocalVarIdentifier returnIdentifier = new LocalVarIdentifier("factorial");
ReturnStatement returnStatement = new ReturnStatement(returnIdentifier);
iStmtList0.add(returnStatement);
BlockStatement blockStatement = new BlockStatement(iStmtList0, null);
return new MethodDecl(classThatContainsMethod, returnType, name, parameterListObj, blockStatement);
}
}

View File

@ -0,0 +1,21 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Program;
import java.util.ArrayList;
import java.util.List;
public class emptyClassASTTyped {
public static Program getEmptyProgramm() {
List<FieldDecl> emptyFieldDeclList = new ArrayList<>();
List<MethodDecl> emptyMethodDeclList = new ArrayList<>();
RefType emptyClass = new RefType("emptyClass", emptyFieldDeclList, emptyMethodDeclList, false);
List<RefType> classes = new ArrayList<>();
classes.add(emptyClass);
return (new Program(classes));
}
}

View File

@ -83,6 +83,12 @@ public class CompareByteCodeBehaviour {
Object result1 = method1.invoke(obj1, testInputs); Object result1 = method1.invoke(obj1, testInputs);
Object result2 = method2.invoke(obj2, testInputs); Object result2 = method2.invoke(obj2, testInputs);
if( (result1 == null) && (result2 == null)) {
return true;
}
if(( result1 == null) || (result2 == null) ) {
return false;
}
if (!result1.equals(result2)) { if (!result1.equals(result2)) {
return false; return false;

View File

@ -26,55 +26,40 @@ public class TestAll {
byteCodeBehaviourComparer = new CompareByteCodeBehaviour(); byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
} }
public void testByteCodeFromAst(String classPath, Program abstractSyntaxTree, String className) { public void testByteCodeFromAst(String correctClassFilePath, Program abstractSyntaxTree, String className) {
testByteCode(false, className, abstractSyntaxTree, correctClassFilePath, null);
try {
abstractSyntaxTree.codeGen();
} catch (Exception e){
System.out.println("Le Exception in le codegen");
}
try {
ClassFileLoader classLoader1 = new ClassFileLoader(classPath);
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 testByteCodeFromScratch(String correctClassFilePath, String fileToComparePath, String className) { 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) {
String content = ""; if(generateAST){
try { String content = "";
System.out.println("Classpath: " + Path.of(fileToComparePath)); try {
content = Files.readString(Path.of(fileToComparePath)); System.out.println("Classpath: " + Path.of(fileToComparePath));
} catch (IOException e) { content = Files.readString(Path.of(fileToComparePath));
System.out.println("File not found!"); } catch (IOException e) {
fail(); 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);
} }
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 { try {
abstractSyntaxTree.typeCheck(); abstractSyntaxTree.typeCheck();
} catch (Exception e){ } catch (Exception e){
@ -128,7 +113,7 @@ public class TestAll {
public void testFakultaet() { public void testFakultaet() {
String classPath = "src/test/resources/basicClasses/Fakultaet.class"; String classPath = "src/test/resources/basicClasses/Fakultaet.class";
Program ast = emptyClassAST.getEmptyProgramm(); Program ast = emptyClassAST.getEmptyProgramm();
String className = "Fakultät"; String className = "Fakultaet";
String javacode = "src/test/resources/basicClasses/Fakultaet.java"; String javacode = "src/test/resources/basicClasses/Fakultaet.java";
//testByteCodeFromAst(classPath, ast ,className); //testByteCodeFromAst(classPath, ast ,className);
testByteCodeFromScratch(classPath, javacode, className); testByteCodeFromScratch(classPath, javacode, className);

View File

@ -31,4 +31,41 @@ public class TestAll {
public void testPublicEmptyClass() throws Exception { public void testPublicEmptyClass() throws Exception {
testLexer.testTokens("basicClasses/PublicEmptyClass.java", "basicClasses/PublicEmptyClass.tokens"); testLexer.testTokens("basicClasses/PublicEmptyClass.java", "basicClasses/PublicEmptyClass.tokens");
} }
@Test
public void testFakultaet() throws Exception {
testLexer.testTokens("basicClasses/Fakultaet.java", "basicClasses/Fakultaet.tokens");
}
@Test
public void testAssignWrongType() throws Exception {
testLexer.testTokens("FailTests/AssignWrongType.java", "FailTests/AssignWrongType.tokens");
}
@Test
public void testBoolAssignedInt() throws Exception {
testLexer.testTokens("FailTests/BoolAssignedInt.java", "FailTests/BoolAssignedInt.tokens");
}
@Test
public void testDivideByZero() throws Exception {
testLexer.testTokens("FailTests/DivideByZero.java", "FailTests/DivideByZero.tokens");
}
@Test
public void testDuplicateFieldDeclaration() throws Exception {
testLexer.testTokens("FailTests/DuplicateFieldDeclaration.java", "FailTests/DuplicateFieldDeclaration.tokens");
}
@Test
public void testDuplicateMethod() throws Exception {
testLexer.testTokens("FailTests/DuplicateMethod.java", "FailTests/DuplicateMethod.tokens");
}
@Test
public void testFieldVar() throws Exception {
testLexer.testTokens("SimpleTests/FieldVar.java", "SimpleTests/FieldVar.tokens");
}
} }

View File

@ -19,43 +19,98 @@ import static org.junit.Assert.fail;
public class TestAll { public class TestAll {
@Test
public void testClassWithMain() {
Program emptyClassAst = ClassWithMainAST.getProgram();
Program correctAST = ClassWithMainASTTyped.getProgram();
boolean expectedResult = true;
testTypeCheck(emptyClassAst, correctAST, expectedResult);
}
@Test @Test
public void testEmptyClass() { public void testEmptyClass() {
Program emptyClassAst = emptyClassAST.getEmptyProgramm(); Program emptyClassAst = emptyClassAST.getEmptyProgramm();
testTypeCheck(emptyClassAst, false); Program correctAST = emptyClassASTTyped.getEmptyProgramm();
boolean expectedResult = false;
testTypeCheck(emptyClassAst, correctAST, expectedResult);
}
@Test
public void testEmptyClassWM() {
Program emptyClassAst = emptyClassAST.getEmptyProgramm();
Program correctAST = emptyClassASTTyped.getEmptyProgramm();
boolean expectedResult = true;
testTypeCheckWM(emptyClassAst, correctAST, expectedResult);
} }
@Test @Test
public void testEmptyClassWithConstructor(){ public void testEmptyClassWithConstructor(){
Program abstractSyntaxTree = EmptyClassWithConstructorAST.getProgram(); Program abstractSyntaxTree = EmptyClassWithConstructorAST.getProgram();
Program correctAST = EmptyClassWithConstructorASTTyped.getProgram();
boolean expectedResult = false;
testTypeCheck(abstractSyntaxTree, correctAST, expectedResult);
}
@Test
public void testEmptyClassWithConstructorWM(){
Program abstractSyntaxTree = EmptyClassWithConstructorAST.getProgram();
Program correctAST = EmptyClassWithConstructorASTTyped.getProgram();
boolean expectedResult = true;
testTypeCheckWM(abstractSyntaxTree, correctAST, expectedResult);
}
/*
@Test
public void testFourClasses(){
Program abstractSyntaxTree = getAst("src/test/resources/basicClasses/FourClasses.java");
boolean expectedResult = false; boolean expectedResult = false;
testTypeCheck(abstractSyntaxTree, expectedResult); testTypeCheck(abstractSyntaxTree, expectedResult);
} }
@Test @Test
public void testFourClasses(){ public void testFourClassesWM(){
Program abstractSyntaxTree = getAst("test/resources/basicClasses/FourClasses.java"); Program abstractSyntaxTree = getAst("src/test/resources/basicClasses/FourClasses.java");
boolean expectedResult = true; boolean expectedResult = true;
testTypeCheck(abstractSyntaxTree, expectedResult); testTypeCheckWM(abstractSyntaxTree, expectedResult);
} }
*/
@Test @Test
public void testAssignWrongType(){ public void testAssignWrongType(){
Program assignWrongType = AssignWrongTypeAST.getProgram(); Program assignWrongType = AssignWrongTypeAST.getProgram();
testTypeCheck(assignWrongType, false); Program correctAST = AssignWrongTypeASTTyped.getProgram();
boolean expectedResult = false;
testTypeCheck(assignWrongType, correctAST,expectedResult);
}
@Test
public void testAssignWrongTypeWM(){
Program assignWrongType = AssignWrongTypeAST.getProgram();
Program correctAST = AssignWrongTypeASTTyped.getProgram();
boolean expectedResult = false;
testTypeCheckWM(assignWrongType, correctAST, expectedResult);
} }
@Test @Test
public void testFakultaet(){ public void testFakultaet(){
Program assignWrongType = FakultaetAST.getProgram(); Program assignWrongType = FakultaetAST.getProgram();
testTypeCheck(assignWrongType, true); Program correctAST = FakultaetAST.getProgram();
boolean expectedResult = true;
testTypeCheck(assignWrongType, correctAST, expectedResult);
} }
public void testTypeCheck(Program abstractSyntaxTree, boolean expectedResult){ public void testTypeCheckWM(Program abstractSyntaxTree, Program correctAST, boolean expectedResult){
abstractSyntaxTree.classes.add(ClassWithMainASTTyped.getRefType());
correctAST.classes.add(ClassWithMainASTTyped.getRefType());
testTypeCheck(abstractSyntaxTree, correctAST, expectedResult);
}
public void testTypeCheck(Program abstractSyntaxTree, Program correctAST, boolean expectedResult){
try { try {
TypeChecker.assertTypeCheckResult(abstractSyntaxTree, expectedResult); TypeChecker.assertTypeCheckResult(abstractSyntaxTree , correctAST,expectedResult);
} catch (Exception e) { } catch (Exception e) {
fail(); fail();
} }

View File

@ -1,22 +1,14 @@
package Typecheck; package Typecheck;
import ASTs.ClassWithMainAST;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Program; import abstractSyntaxTree.Program;
import gen.DecafLexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class TypeChecker { public class TypeChecker {
// Method to test if the TypeCheck returns the expected Result // Method to test if the TypeCheck returns the expected Result
static public void assertTypeCheckResult(Program programmToBeTested, boolean expectedResult) throws Exception { static public void assertTypeCheckResult(Program programmToBeTested, Program correctAST, boolean expectedResult) {
boolean actualResult; boolean actualResult;
try { try {
TypeCheckResult typeCheckResult = programmToBeTested.typeCheck(); TypeCheckResult typeCheckResult = programmToBeTested.typeCheck();
@ -26,5 +18,7 @@ public class TypeChecker {
} }
assertEquals(expectedResult, actualResult); assertEquals(expectedResult, actualResult);
assertEquals(programmToBeTested, correctAST);
} }
} }

View File

@ -1,7 +0,0 @@
class EmptyClassWithConstructor{
public EmptyClassWithConstructor(){
}
}

View File

@ -1 +0,0 @@
class EmptyClass{}

View File

@ -1,22 +1,23 @@
Class Class: "class"
Identifier Identifier: "AssignWrongType"
OpenCurlyBracket OpenCurlyBracket: "{"
Int Int: "int"
Identifier Identifier: "test"
OpenRoundBracket OpenRoundBracket: "("
ClosedRoundBracket ClosedRoundBracket: ")"
OpenCurlyBracket OpenCurlyBracket: "{"
Int Int: "int"
Identifier Identifier: "x"
Assign Assign: "="
IntValue IntValue: "1"
Semicolon Semicolon: ";"
Identifier Identifier: "x"
Assign Assign: "="
Identifier BooleanValue: "true"
Semicolon Semicolon: ";"
Return Return: "return"
Identifier Identifier: "x"
Semicolon Semicolon: ";"
ClosedCurlyBracket ClosedCurlyBracket: "}"
ClosedCurlyBracket ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -1,7 +1,7 @@
class BoolAssignedInt { class BoolAssignedInt {
void foo() { void foo() {
bool myBool = 10; boolean myBool = 10;
} }
} }

View File

@ -1,15 +1,16 @@
Class Class: "class"
Identifier Identifier: "BoolAssignedInt"
OpenCurlyBracket OpenCurlyBracket: "{"
Void Void: "void"
Identifier Identifier: "foo"
OpenRoundBracket OpenRoundBracket: "("
ClosedRoundBracket ClosedRoundBracket: ")"
OpenCurlyBracket OpenCurlyBracket: "{"
Boolean Boolean: "boolean"
Identifier Identifier: "myBool"
Assign Assign: "="
Int IntValue: "10"
Semicolon Semicolon: ";"
ClosedCurlyBracket ClosedCurlyBracket: "}"
ClosedCurlyBracket ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -1,14 +1,15 @@
Class Class: "class"
Identifier Identifier: "DivideByZero"
OpenCurlyBracket OpenCurlyBracket: "{"
MainMethodDecl MainMethodDecl: "public static void main(String[] args)"
OpenCurlyBracket OpenCurlyBracket: "{"
Int Int: "int"
Identifier Identifier: "a"
Assign Assign: "="
IntValue IntValue: "1"
DotOperator DotOperator: "/"
IntValue IntValue: "0"
Semicolon Semicolon: ";"
ClosedCurlyBracket ClosedCurlyBracket: "}"
ClosedCurlyBracket ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -1,10 +1,11 @@
Class Class: "class"
Identifier Identifier: "DuplicateFieldDeclaration"
OpenCurlyBracket OpenCurlyBracket: "{"
Char Char: "char"
Identifier Identifier: "i"
Semicolon Semicolon: ";"
Int Int: "int"
Identifier Identifier: "i"
Semicolon Semicolon: ";"
ClosedCurlyBracket ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -1,4 +1,4 @@
Class DuplicateMethod { class DuplicateMethod {
public void test() { } public void test() { }

View File

@ -1,18 +1,19 @@
Class Class: "class"
Identifier Identifier: "DuplicateMethod"
OpenCurlyBrackets OpenCurlyBracket: "{"
AccessModifier AccessModifierPublic: "public"
Void Void: "void"
Identifier Identifier: "test"
OpenRoundBrackets OpenRoundBracket: "("
ClosedRoundBrackets ClosedRoundBracket: ")"
OpenCurlyBrackets OpenCurlyBracket: "{"
ClosedCurlyBrackets ClosedCurlyBracket: "}"
AccessModifier AccessModifierPublic: "public"
Void Void: "void"
Identifier Identifier: "test"
OpenRoundBrackets OpenRoundBracket: "("
ClosedRoundBrackets ClosedRoundBracket: ")"
OpenCurlyBrackets OpenCurlyBracket: "{"
ClosedCurlyBrackets ClosedCurlyBracket: "}"
ClosedCurlyBrackets ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -1,7 +0,0 @@
package SimpleTests;
class AutoAccessModifierField {
int autoAccess;
}

View File

@ -1,5 +1,3 @@
package SimpleTests;
class CharArgument { class CharArgument {
char c; char c;

View File

@ -0,0 +1,18 @@
Class: "class"
Identifier: "CharArgument"
OpenCurlyBracket: "{"
Char: "char"
Identifier: "c"
Semicolon: ";"
AccessModifierPublic: "public"
Identifier: "CharArgument"
OpenRoundBracket: "("
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
This: "this"
Dot: "."
Identifier: "c"
Assign: "="
Identifier: "foo"
OpenRoundBracket: "("
w

View File

@ -0,0 +1,5 @@
class FieldVar {
int field;
}

View File

@ -0,0 +1,8 @@
Class: "class"
Identifier: "FieldVar"
OpenCurlyBracket: "{"
Int: "int"
Identifier: "field"
Semicolon: ";"
ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -0,0 +1,55 @@
Class: "class"
Identifier: "Fakultaet"
OpenCurlyBracket: "{"
AccessModifierPublic: "public"
Int: "int"
Identifier: "fak"
OpenRoundBracket: "("
Int: "int"
Identifier: "number"
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
If: "if"
OpenRoundBracket: "("
Identifier: "number"
ComparisonOperator: "<"
IntValue: "0"
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
Return: "return"
IntValue: "1"
Semicolon: ";"
ClosedCurlyBracket: "}"
Int: "int"
Identifier: "factorial"
Assign: "="
IntValue: "1"
Semicolon: ";"
Int: "int"
Identifier: "i"
Assign: "="
IntValue: "0"
Semicolon: ";"
While: "while"
OpenRoundBracket: "("
Identifier: "i"
ComparisonOperator: "<"
Identifier: "number"
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
Identifier: "factorial"
Assign: "="
Identifier: "factorial"
DotOperator: "*"
Identifier: "i"
Semicolon: ";"
ClosedCurlyBracket: "}"
Return: "return"
Identifier: "factorial"
Semicolon: ";"
ClosedCurlyBracket: "}"
MainMethodDecl: "public static void main(String[] args)"
OpenCurlyBracket: "{"
ClosedCurlyBracket: "}"
ClosedCurlyBracket: "}"
EOF: "<EOF>"