Test update

This commit is contained in:
Julian Murek 2024-07-03 17:32:49 +02:00
parent 6948797001
commit 1dd405a00f
16 changed files with 233 additions and 72 deletions

Binary file not shown.

View File

@ -15,7 +15,7 @@ import java.util.Objects;
public class BinaryExpression extends AbstractType implements IExpression{
public String operator;
public IExpression left;
public IExpression left; // This needs TypeCheckResult
public IExpression right;
public BinaryExpression(String operator, IExpression left, IExpression right) {

View File

@ -15,7 +15,7 @@ import java.util.List;
import java.util.Objects;
public class ReturnStatement extends AbstractType implements IStatement{
IExpression expression;
IExpression expression; // Needs typeCheckResult
public String thisClass;
public ReturnStatement(IExpression expression) {

View File

@ -18,7 +18,7 @@ import java.util.*;
public class MethodCallStatementExpression extends AbstractType implements IExpression, IStatement {
String methodName;
List<IExpression> arguments;
Receiver receiver;
Receiver receiver; // Braucht typecheckResult
List<ReceivingMethod> receivingMethods;
public String thisClass;

View File

@ -19,7 +19,7 @@ import java.util.List;
public class NewStatementExpression extends AbstractType implements IExpression, IStatement {
private String className;
private List<IExpression> arguments;
private List<IExpression> arguments; //These need to have a TypeCheckResult
public NewStatementExpression(String className, List<IExpression> arguments) {
this.className = className;

View File

@ -46,14 +46,14 @@ public class testAll {
testAst(ast, pathToCode);
}
/*
@Test
public void testDivideByZero(){
Program ast = AssignWrongTypeAST.getProgram();
Program ast = DivideByZeroAST.getProgram();
String pathToCode = "failTests/DivideByzero.java";
testAst(ast, pathToCode);
}
*/
@Test
public void testcharArgument(){
Program ast = CharArgumentAST.getProgram();

View File

@ -3,28 +3,48 @@ package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BinaryExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.LocalVarDecl;
import java.util.ArrayList;
import java.util.List;
public class DivideByZeroAST {
/*
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<>();
RefType refType = new RefType("DivideByZero", fieldDeclList, methodDeclList, false);
methodDeclList.add(method0());
RefType refType = new RefType("DivideByZero", fieldDeclList, methodDeclList, true);
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",
BinaryExpression binaryExpression00 = new BinaryExpression("/", new IntConstantExpression(1), new IntConstantExpression(0));
LocalVarDecl localVarDecl00 = new LocalVarDecl("int", "a", binaryExpression00);
List<IStatement> iStatementList = new ArrayList<>();
iStatementList.add(localVarDecl00);
BlockStatement blockStatement = new BlockStatement(iStatementList, null);
MethodDecl methodDecl = new MethodDecl("DivideByZero", "void", "main",
new ParameterList(new ArrayList<>()), blockStatement
);
}*/
return methodDecl;
}
}

View File

@ -1,4 +1,69 @@
package ASTs;
import TypeCheck.TypeCheckResult;
import Typecheck.TypingHelper;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BinaryExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.LocalVarDecl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DivideByZeroASTTyped {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
addTyping(program);
return program;
}
public static RefType getRefType(){
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType refType = new RefType("DivideByZero", fieldDeclList, methodDeclList, true);
return refType;
}
public static MethodDecl method0(){
IntConstantExpression intConstantExpression0 = new IntConstantExpression(1);
intConstantExpression0.setTypeCheckResult(new TypeCheckResult("int"));
BinaryExpression binaryExpression00 = new BinaryExpression("/", intConstantExpression0, new IntConstantExpression(0));
LocalVarDecl localVarDecl00 = new LocalVarDecl("int", "a", binaryExpression00);
List<IStatement> iStatementList = new ArrayList<>();
iStatementList.add(localVarDecl00);
BlockStatement blockStatement = new BlockStatement(iStatementList, "void");
MethodDecl methodDecl = new MethodDecl("DivideByZero", "void", "main",
new ParameterList(new ArrayList<>()), blockStatement
);
return methodDecl;
}
public static void addTyping(Program program) {
//Type Context
TypingHelper.addTypeContext(program, "DivideByZero", new HashMap<>());
//Method Context
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, "DivideByZero", methods);
}
}

View File

@ -76,18 +76,24 @@ public class FakultaetAST {
iStmtList0.add(localVarExpr0);
// Expression 2
IntConstantExpression intConst1 = new IntConstantExpression(0);
IntConstantExpression intConst1 = new IntConstantExpression(1);
LocalVarDecl localVarExpr1 = new LocalVarDecl("int", "i", intConst1);
iStmtList0.add(localVarExpr1);
//While Statement
BinaryExpression whileCondition = new BinaryExpression("<", new LocalVarIdentifier("i"), new LocalVarIdentifier("number"));
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<>();
LocalVarIdentifier identifierI = new LocalVarIdentifier("i");
BinaryExpression rightAssign1 = new BinaryExpression("+", identifierI, new IntConstantExpression(1));
AssignStatementExpression assignStatementExpression1 = new AssignStatementExpression("=", identifierI, rightAssign1);
whileBlockStmts.add(assignStatementExpression0);
whileBlockStmts.add(assignStatementExpression1);
BlockStatement whileBlock = new BlockStatement( whileBlockStmts, null);
WhileStatement whileStatement0 = new WhileStatement(whileCondition, whileBlock);

View File

@ -61,8 +61,14 @@ public class FakultaetASTTyped {
// If Statament
//condition
LocalVarIdentifier binExprIdentifier = new LocalVarIdentifier("number");
TypeCheckResult typeCheckResultbinExprIdentifier = new TypeCheckResult();
typeCheckResultbinExprIdentifier.type = "int";
binExprIdentifier.setTypeCheckResult(typeCheckResultbinExprIdentifier);
IntConstantExpression binExprIntConst = new IntConstantExpression(0);
BinaryExpression binExpr = new BinaryExpression("<", binExprIdentifier, binExprIntConst);
TypeCheckResult typeCheckResultbinExpr = new TypeCheckResult();
typeCheckResultbinExpr.type = "boolean";
binExpr.setTypeCheckResult(typeCheckResultbinExpr);
IntConstantExpression return0Expr0 = new IntConstantExpression(1);
TypeCheckResult typeCheckResult1 = new TypeCheckResult();
@ -88,12 +94,19 @@ public class FakultaetASTTyped {
iStmtList0.add(localVarExpr1);
//While Statement
BinaryExpression whileCondition = new BinaryExpression("<", new LocalVarIdentifier("i"), new LocalVarIdentifier("number"));
BinaryExpression whileCondition = new BinaryExpression("<=", new LocalVarIdentifier("i"), new LocalVarIdentifier("number"));
whileCondition.setTypeCheckResult(new TypeCheckResult("void"));
BinaryExpression whileBinExpr = new BinaryExpression("*", new LocalVarIdentifier("factorial"), new LocalVarIdentifier("i"));
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", new LocalVarIdentifier("factorial"), whileBinExpr);
List<IStatement> whileBlockStmts = new ArrayList<>();
LocalVarIdentifier identifierI = new LocalVarIdentifier("i");
BinaryExpression rightAssign1 = new BinaryExpression("+", identifierI, new IntConstantExpression(1));
AssignStatementExpression assignStatementExpression1 = new AssignStatementExpression("=", identifierI, rightAssign1);
whileBlockStmts.add(assignStatementExpression0);
whileBlockStmts.add(assignStatementExpression1);
BlockStatement whileBlock = new BlockStatement( whileBlockStmts, "void");
WhileStatement whileStatement0 = new WhileStatement(whileCondition, whileBlock);

View File

@ -23,49 +23,32 @@ public class ByteCodeTester {
}
public void testByteCodeFromAst(String correctClassFilePath, Program abstractSyntaxTree, String className) {
testClassFile(false, className, abstractSyntaxTree, correctClassFilePath, null);
typeCheckWithoutmain(abstractSyntaxTree);
generateCode(abstractSyntaxTree);
testAST(correctClassFilePath, className);
}
public void testByteCodeFromTypedAst(String correctClassFilePath, Program abstractSyntaxTree, String className) {
generateCode(abstractSyntaxTree);
testAST(correctClassFilePath, className);
}
public void testClassFileFromScratch(String correctClassFilePath, String fileToComparePath, String className) {
testClassFile(true, className, null, correctClassFilePath, fileToComparePath);
Program abstractSyntaxTree = generateAST(fileToComparePath);
typeCheckWithoutmain(abstractSyntaxTree);
generateCode(abstractSyntaxTree);
testAST(correctClassFilePath, className);
}
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);
private void typeCheckWithoutmain(Program abstractSyntaxTree){
try {
abstractSyntaxTree.typeCheckWithoutMain();
} catch (Exception e){
System.out.println("Le Exception in le type-check");
}
}
if(generateAST){
try {
abstractSyntaxTree.typeCheckWithoutMain();
} catch (Exception e){
System.out.println("Le Exception in le type-check");
//fail();
}
}
private void generateCode(Program abstractSyntaxTree){
try {
abstractSyntaxTree.codeGen();
} catch (Exception e){
@ -73,7 +56,9 @@ public class ByteCodeTester {
e.printStackTrace();
fail();
}
}
private void testAST(String correctClassFilePath, String className) {
try {
ClassFileLoader classLoader1 = new ClassFileLoader(correctClassFilePath);
ClassFileLoader classLoader2 = new ClassFileLoader(className+ ".class");
@ -89,6 +74,31 @@ public class ByteCodeTester {
fail();
}
}
private Program generateAST(String pathToJavaFile){
String content = "";
try {
System.out.println("Classpath: " + Path.of(pathToJavaFile));
content = Files.readString(Path.of(pathToJavaFile));
} 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);
return abstractSyntaxTree;
}
public void compareJarFilesFromAST(Program abstractSyntaxTree, String jarPath, String[] classNames){
try {

View File

@ -38,6 +38,14 @@ public class CompareByteCodeBehaviour {
Method[] methods1 = class1.getDeclaredMethods();
Method[] methods2 = class2.getDeclaredMethods();
/*
for(int i = 0; i < methods1.length; i++) {
System.out.println("Method1[" + i + "]: " + methods1[i]);
System.out.println("Method2[" + i + "]: " + methods2[i]);
}
*/
for (Method method1 : methods1) {
for (Method method2 : methods2) {
if (compareMethod(method1, method2)) {
@ -49,6 +57,12 @@ public class CompareByteCodeBehaviour {
}
}
}
/*
for(int i = 0; i < this.methodArray1.size(); i++){
System.out.println("Method1[" + i + "]: " + methodArray1.get(i));
System.out.println("Method2[" + i + "]: " + methodArray2.get(i));
}
*/
}
public boolean compareMethodBehaviour(Class<?> class1, Class<?> class2){
@ -72,12 +86,23 @@ public class CompareByteCodeBehaviour {
Method[] methods2 = new Method[this.methodArray2.size()];
methods2 = this.methodArray2.toArray(methods2);
/*
for(int i = 0; i < methods1.length; i++) {
System.out.println("MethodArray1 [" + i + "]: " + methods1[i]);
System.out.println("MethodArray2 [" + i + "]: " + methods2[i]);
}
*/
System.out.println("length: " + methods1.length);
// Compare methods
for (int i = 0; i < methods1.length; i++) {
System.out.println("i: " + i);
Method method1 = methods1[i];
Method method2 = methods2[i];
System.out.println(method1);
System.out.println(method2);
method1.setAccessible(true);
method2.setAccessible(true);
@ -88,16 +113,19 @@ public class CompareByteCodeBehaviour {
Object result1 = method1.invoke(obj1, testInputs);
Object result2 = method2.invoke(obj2, testInputs);
if( (result1 == null) && (result2 == null)) {
return true;
}
if(( result1 == null) || (result2 == null) ) {
return false;
System.out.println("Result method 1: " + result1);
System.out.println("Result method 2: " + result2);
if( !((result1 == null) && (result2 == null))) {
if(( result1 == null) || (result2 == null) ) {
return false;
}
if (!result1.equals(result2)) {
return false;
}
}
if (!result1.equals(result2)) {
return false;
}
}
} catch (InstantiationException | IllegalAccessException |

View File

@ -18,18 +18,28 @@ public class TestAll {
Program ast = emptyClassASTTyped.getEmptyProgramm();
String className = "emptyClass";
String javacode = "src/test/resources/basicClasses/emptyClass.java";
//byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
byteCodeTester.testByteCodeFromTypedAst(classPath, ast ,className);
//byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testEmptyClassWithConstructor() {
String classPath = "src/test/resources/basicClasses/EmptyClassWithConstructor.class";
Program ast = EmptyClassWithConstructorASTTyped.getProgram();
String className = "EmptyClassWithConstructor";
String javacode = "src/test/resources/basicClasses/emptyClass.java";
byteCodeTester.testByteCodeFromTypedAst(classPath, ast ,className);
//byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testFakultaet() {
String classPath = "src/test/resources/basicClasses/Fakultaet.class";
Program ast = FakultaetASTTyped.getProgram();
Program ast = FakultaetAST.getProgram();
String className = "Fakultaet";
String javacode = "src/test/resources/basicClasses/Fakultaet.java";
//byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
//byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
@ -38,17 +48,17 @@ public class TestAll {
Program ast = ClassWithMainASTTyped.getProgram();
String className = "classWithMain";
String javacode = "src/test/resources/basicClasses/classWithMain.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
byteCodeTester.testByteCodeFromTypedAst(classPath, ast ,className);
//byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testCharArgument() {
String classPath = "src/test/resources/SimpleTests/CharArgument.class";
//Program ast = ClassWithMainASTTyped.getProgram();
Program ast = CharArgumentAST.getProgram();
String className = "CharArgument";
String javacode = "src/test/resources/SimpleTests/CharArgument.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}

View File

@ -101,6 +101,14 @@ public class TestAll {
testTypeCheck(fakultaet, correctAST, expectedResult);
}
@Test
public void testDivideByZero(){
Program fakultaet = DivideByZeroAST.getProgram();
Program correctAST = DivideByZeroASTTyped.getProgram();
boolean expectedResult = true;
testTypeCheck(fakultaet, correctAST, expectedResult);
}
public void testTypeCheckWM(Program abstractSyntaxTree, Program correctAST, boolean expectedResult){
abstractSyntaxTree.classes.add(ClassWithMainASTTyped.getRefType());

View File

@ -4,9 +4,10 @@ class Fakultaet {
return 1;
}
int factorial = 1;
int i = 0;
while(i < number){
int i = 1;
while(i <= number){
factorial = factorial * i;
i = i + 1;
}
return factorial;