From 404aa3236fefa2ad6d4615018971221472e67571 Mon Sep 17 00:00:00 2001 From: David Mueller Date: Wed, 8 May 2024 10:32:03 +0200 Subject: [PATCH 1/4] no pivate and potected anymoe --- Source/Decaf.g4 | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Source/Decaf.g4 b/Source/Decaf.g4 index f40a9f8..2dde8dd 100644 --- a/Source/Decaf.g4 +++ b/Source/Decaf.g4 @@ -3,12 +3,12 @@ grammar Decaf; program: classdecl+; //class identifier{...} -classdecl: (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? 'class' Identifier OpenCurlyBracket (constuctorDecl|fieldDecl|methodDecl)* ClosedCurlyBracket; -constuctorDecl: (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; //Maybe not needed +classdecl: AccessModifierPublic? 'class' Identifier OpenCurlyBracket (constuctorDecl|fieldDecl|methodDecl)* ClosedCurlyBracket; +constuctorDecl: AccessModifierPublic? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; //Maybe not needed //Method and FieldVar -methodDecl: MainMethodDecl block | (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; -fieldDecl: (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? type Identifier Semicolon; +methodDecl: MainMethodDecl block | AccessModifierPublic? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; +fieldDecl: AccessModifierPublic? type Identifier Semicolon; //Parameters parameterList: parameter(Comma parameter)*; @@ -57,8 +57,6 @@ value: IntValue | BooleanValue | CharValue | NullValue; //Access modifier AccessModifierPublic : 'public' ; -AccessModifierPrivate : 'private'; -AccessModifierProtected : 'protected'; MainMethodDecl : 'public static void main(String[] args)'; //Types From 352531d68053275d6cfe22b6a02276b94b5c06ff Mon Sep 17 00:00:00 2001 From: Jochen Seyfried Date: Wed, 8 May 2024 10:40:44 +0200 Subject: [PATCH 2/4] Added SuperStatementExpression --- Source/TypeCheck/AbstractType.java | 2 +- .../StatementExpression/AssignStatementExpression.java | 2 +- .../StatementExpression/SuperStatementExpression.java | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Source/abstractSyntaxTree/StatementExpression/SuperStatementExpression.java diff --git a/Source/TypeCheck/AbstractType.java b/Source/TypeCheck/AbstractType.java index 068b139..ea2fd30 100644 --- a/Source/TypeCheck/AbstractType.java +++ b/Source/TypeCheck/AbstractType.java @@ -12,4 +12,4 @@ public abstract class AbstractType { public TypeCheckResult getTypeCheckResult() { return typeCheckResult; } -} +} \ No newline at end of file diff --git a/Source/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/Source/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index 11c7fb1..5538467 100644 --- a/Source/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/Source/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -38,7 +38,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi right.CodeGen(mv); if (left instanceof VarRefExpression varRef) { - //TODO: Implement the handling of a variable reference --> I need a lis of local variables + //TODO: Implement the handling of a variable reference --> I need a list of local variables // for that to determine if the variable is a local or field variable } else if (left instanceof InstVarExpression instVar) { mv.visitInsn(Opcodes.DUP_X1); diff --git a/Source/abstractSyntaxTree/StatementExpression/SuperStatementExpression.java b/Source/abstractSyntaxTree/StatementExpression/SuperStatementExpression.java new file mode 100644 index 0000000..61fa3cc --- /dev/null +++ b/Source/abstractSyntaxTree/StatementExpression/SuperStatementExpression.java @@ -0,0 +1,10 @@ +package abstractSyntaxTree.StatementExpression; + +import abstractSyntaxTree.Expression.IExpression; +import java.util.List; + +public class SuperStatementExpression extends MethodCallStatementExpression{ + public SuperStatementExpression(String methodName, List arguments) { + super(methodName, arguments); + } +} From 62c7f144bba6a80b61aeb0777404a758cc8c5117 Mon Sep 17 00:00:00 2001 From: Julian Murek Date: Wed, 8 May 2024 11:01:13 +0200 Subject: [PATCH 3/4] Added "src" directory to conform to Mavens project structure and added preliminary Tests and Testsuite. --- src/test/java/AST/TestRunner.java | 273 ++++ src/test/java/All/TestRunner.java | 1254 +++++++++++++++++ src/test/java/CodeGen/TestRunner.java | 356 +++++ src/test/java/Helper/MockGenerator.java | 965 +++++++++++++ src/test/java/Helper/ReflectLoader.java | 104 ++ src/test/java/Helper/Resources.java | 42 + src/test/java/TAST/TestRunner.java | 324 +++++ src/test/java/TestSuite.java | 10 + src/test/java/initialTest/initialTest.java | 12 + src/test/resources/Comments.java | 19 + .../resources/EmptyClassWithConstructor.java | 7 + src/test/resources/EmtpyClass.java | 1 + .../resources/FailTests/AssignWrongType.java | 11 + .../resources/FailTests/BoolAssignedInt.java | 9 + .../resources/FailTests/DivideByZero.java | 9 + .../FailTests/DuplicateFieldDeclaration.java | 6 + .../resources/FailTests/DuplicateMethod.java | 9 + .../resources/FailTests/FaultyForLoop.java | 11 + .../FailTests/MethodAccessTests.java | 18 + .../FailTests/MismatchingReturnType.java | 9 + src/test/resources/FailTests/ScopeTest.java | 23 + src/test/resources/IntegerWrapper.java | 20 + src/test/resources/Integration/Dijkstra.java | 366 +++++ src/test/resources/Integration/LOFOI.java | 23 + .../resources/Integration/LinkedList.java | 80 ++ src/test/resources/Integration/Stack.java | 55 + .../resources/Integration/StringList.java | 48 + src/test/resources/NotTested/BinaryTests.java | 31 + src/test/resources/NotTested/CharWrapper.java | 19 + src/test/resources/NotTested/ClassStress.java | 33 + src/test/resources/NotTested/Fib.java | 16 + .../NotTested/FibIntegerWrapper.java | 37 + src/test/resources/NotTested/IfTest.java | 30 + src/test/resources/NotTested/TwoClasses.java | 9 + .../NotTested/TwoClassesReferences.java | 27 + src/test/resources/NotTested/VoidMethod.java | 10 + .../resources/NotTested/VoidMethodReturn.java | 10 + src/test/resources/NotTested/WhileTest.java | 19 + src/test/resources/NotTested/ggt.java | 25 + src/test/resources/SimpleTests/AndVorOr.java | 13 + .../SimpleTests/AutoAccessModifierField.java | 7 + .../resources/SimpleTests/CharArgument.java | 15 + .../resources/SimpleTests/ClassFields.java | 9 + src/test/resources/SimpleTests/Comments.java | 21 + .../SimpleTests/ConditionalEvaluation.java | 16 + .../SimpleTests/ConstructorParams.java | 9 + .../SimpleTests/ConstructorThisDot.java | 11 + src/test/resources/SimpleTests/DecTest.java | 17 + src/test/resources/SimpleTests/DivMethod.java | 9 + .../resources/SimpleTests/EmptyClass.java | 3 + .../EmptyClassWithConstructor.java | 9 + .../SimpleTests/ExplicitNullAssign.java | 10 + .../SimpleTests/ExtendedNotTest.java | 18 + .../resources/SimpleTests/FieldWithExpr.java | 7 + src/test/resources/SimpleTests/ForTest.java | 11 + .../resources/SimpleTests/FourClasses.java | 63 + .../SimpleTests/FourClassesFieldAssign.java | 64 + .../SimpleTests/FourClassesSetter.java | 65 + .../resources/SimpleTests/GetterFunction.java | 15 + .../resources/SimpleTests/IfConstructor.java | 11 + .../SimpleTests/IfElseIfStatement.java | 15 + .../IfElseIfStatementWithOneReturn.java | 17 + .../IfElseIfStatementWithoutReturn.java | 17 + .../SimpleTests/IfElseStatement.java | 13 + .../resources/SimpleTests/IfStatement.java | 12 + .../SimpleTests/IncDecStressTest.java | 26 + src/test/resources/SimpleTests/IncTest.java | 17 + .../resources/SimpleTests/IntCompare.java | 9 + src/test/resources/SimpleTests/IntMethod.java | 9 + .../SimpleTests/KlammerVorPunkt.java | 9 + .../resources/SimpleTests/MainMethodTest.java | 9 + .../resources/SimpleTests/MethodCall.java | 15 + .../SimpleTests/MethodCallParams.java | 15 + .../SimpleTests/MethodCallStressTest.java | 34 + .../SimpleTests/MethodCallsInWhile.java | 23 + .../resources/SimpleTests/MiniLinkedList.java | 25 + .../resources/SimpleTests/MinusMethod.java | 9 + src/test/resources/SimpleTests/ModMethod.java | 7 + src/test/resources/SimpleTests/MulMethod.java | 9 + .../resources/SimpleTests/MultClasses.java | 9 + .../SimpleTests/MultClassesReference.java | 26 + .../MultipleClassesMethodCalls.java | 22 + .../SimpleTests/MultipleClassesStress.java | 24 + src/test/resources/SimpleTests/NotMethod.java | 7 + .../SimpleTests/OperatorStacking.java | 21 + .../resources/SimpleTests/OperatorTest.java | 86 ++ .../resources/SimpleTests/OverridingTest.java | 32 + .../SimpleTests/OverridingTestNull.java | 35 + .../OverridingTestNullMultiple.java | 37 + .../resources/SimpleTests/ParamMethod.java | 7 + .../resources/SimpleTests/PlusMethod.java | 9 + .../resources/SimpleTests/PunktVorStrich.java | 13 + .../SimpleTests/RealConstructor.java | 11 + .../resources/SimpleTests/RealMethod.java | 7 + src/test/resources/SimpleTests/RealWhile.java | 14 + .../resources/SimpleTests/SelfReference.java | 20 + .../SimpleTests/SystemOutPrintln.java | 9 + .../SimpleTests/SystemOutPrintlnString.java | 9 + .../SimpleTests/ThisDotMethodCall.java | 15 + .../SimpleTests/ValueAdapterTests.java | 29 + .../resources/SimpleTests/VoidMethod.java | 7 + .../resources/SimpleTests/VoidReturn.java | 9 + src/test/resources/SimpleTests/WhileTest.java | 13 + src/test/resources/basicClasses/emptyClass | 1 + .../resources/basicClasses/publicEmptyClass | 0 105 files changed, 5495 insertions(+) create mode 100644 src/test/java/AST/TestRunner.java create mode 100644 src/test/java/All/TestRunner.java create mode 100644 src/test/java/CodeGen/TestRunner.java create mode 100644 src/test/java/Helper/MockGenerator.java create mode 100644 src/test/java/Helper/ReflectLoader.java create mode 100644 src/test/java/Helper/Resources.java create mode 100644 src/test/java/TAST/TestRunner.java create mode 100644 src/test/java/TestSuite.java create mode 100644 src/test/java/initialTest/initialTest.java create mode 100644 src/test/resources/Comments.java create mode 100644 src/test/resources/EmptyClassWithConstructor.java create mode 100644 src/test/resources/EmtpyClass.java create mode 100644 src/test/resources/FailTests/AssignWrongType.java create mode 100644 src/test/resources/FailTests/BoolAssignedInt.java create mode 100644 src/test/resources/FailTests/DivideByZero.java create mode 100644 src/test/resources/FailTests/DuplicateFieldDeclaration.java create mode 100644 src/test/resources/FailTests/DuplicateMethod.java create mode 100644 src/test/resources/FailTests/FaultyForLoop.java create mode 100644 src/test/resources/FailTests/MethodAccessTests.java create mode 100644 src/test/resources/FailTests/MismatchingReturnType.java create mode 100644 src/test/resources/FailTests/ScopeTest.java create mode 100644 src/test/resources/IntegerWrapper.java create mode 100644 src/test/resources/Integration/Dijkstra.java create mode 100644 src/test/resources/Integration/LOFOI.java create mode 100644 src/test/resources/Integration/LinkedList.java create mode 100644 src/test/resources/Integration/Stack.java create mode 100644 src/test/resources/Integration/StringList.java create mode 100644 src/test/resources/NotTested/BinaryTests.java create mode 100644 src/test/resources/NotTested/CharWrapper.java create mode 100644 src/test/resources/NotTested/ClassStress.java create mode 100644 src/test/resources/NotTested/Fib.java create mode 100644 src/test/resources/NotTested/FibIntegerWrapper.java create mode 100644 src/test/resources/NotTested/IfTest.java create mode 100644 src/test/resources/NotTested/TwoClasses.java create mode 100644 src/test/resources/NotTested/TwoClassesReferences.java create mode 100644 src/test/resources/NotTested/VoidMethod.java create mode 100644 src/test/resources/NotTested/VoidMethodReturn.java create mode 100644 src/test/resources/NotTested/WhileTest.java create mode 100644 src/test/resources/NotTested/ggt.java create mode 100644 src/test/resources/SimpleTests/AndVorOr.java create mode 100644 src/test/resources/SimpleTests/AutoAccessModifierField.java create mode 100644 src/test/resources/SimpleTests/CharArgument.java create mode 100644 src/test/resources/SimpleTests/ClassFields.java create mode 100644 src/test/resources/SimpleTests/Comments.java create mode 100644 src/test/resources/SimpleTests/ConditionalEvaluation.java create mode 100644 src/test/resources/SimpleTests/ConstructorParams.java create mode 100644 src/test/resources/SimpleTests/ConstructorThisDot.java create mode 100644 src/test/resources/SimpleTests/DecTest.java create mode 100644 src/test/resources/SimpleTests/DivMethod.java create mode 100644 src/test/resources/SimpleTests/EmptyClass.java create mode 100644 src/test/resources/SimpleTests/EmptyClassWithConstructor.java create mode 100644 src/test/resources/SimpleTests/ExplicitNullAssign.java create mode 100644 src/test/resources/SimpleTests/ExtendedNotTest.java create mode 100644 src/test/resources/SimpleTests/FieldWithExpr.java create mode 100644 src/test/resources/SimpleTests/ForTest.java create mode 100644 src/test/resources/SimpleTests/FourClasses.java create mode 100644 src/test/resources/SimpleTests/FourClassesFieldAssign.java create mode 100644 src/test/resources/SimpleTests/FourClassesSetter.java create mode 100644 src/test/resources/SimpleTests/GetterFunction.java create mode 100644 src/test/resources/SimpleTests/IfConstructor.java create mode 100644 src/test/resources/SimpleTests/IfElseIfStatement.java create mode 100644 src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java create mode 100644 src/test/resources/SimpleTests/IfElseIfStatementWithoutReturn.java create mode 100644 src/test/resources/SimpleTests/IfElseStatement.java create mode 100644 src/test/resources/SimpleTests/IfStatement.java create mode 100644 src/test/resources/SimpleTests/IncDecStressTest.java create mode 100644 src/test/resources/SimpleTests/IncTest.java create mode 100644 src/test/resources/SimpleTests/IntCompare.java create mode 100644 src/test/resources/SimpleTests/IntMethod.java create mode 100644 src/test/resources/SimpleTests/KlammerVorPunkt.java create mode 100644 src/test/resources/SimpleTests/MainMethodTest.java create mode 100644 src/test/resources/SimpleTests/MethodCall.java create mode 100644 src/test/resources/SimpleTests/MethodCallParams.java create mode 100644 src/test/resources/SimpleTests/MethodCallStressTest.java create mode 100644 src/test/resources/SimpleTests/MethodCallsInWhile.java create mode 100644 src/test/resources/SimpleTests/MiniLinkedList.java create mode 100644 src/test/resources/SimpleTests/MinusMethod.java create mode 100644 src/test/resources/SimpleTests/ModMethod.java create mode 100644 src/test/resources/SimpleTests/MulMethod.java create mode 100644 src/test/resources/SimpleTests/MultClasses.java create mode 100644 src/test/resources/SimpleTests/MultClassesReference.java create mode 100644 src/test/resources/SimpleTests/MultipleClassesMethodCalls.java create mode 100644 src/test/resources/SimpleTests/MultipleClassesStress.java create mode 100644 src/test/resources/SimpleTests/NotMethod.java create mode 100644 src/test/resources/SimpleTests/OperatorStacking.java create mode 100644 src/test/resources/SimpleTests/OperatorTest.java create mode 100644 src/test/resources/SimpleTests/OverridingTest.java create mode 100644 src/test/resources/SimpleTests/OverridingTestNull.java create mode 100644 src/test/resources/SimpleTests/OverridingTestNullMultiple.java create mode 100644 src/test/resources/SimpleTests/ParamMethod.java create mode 100644 src/test/resources/SimpleTests/PlusMethod.java create mode 100644 src/test/resources/SimpleTests/PunktVorStrich.java create mode 100644 src/test/resources/SimpleTests/RealConstructor.java create mode 100644 src/test/resources/SimpleTests/RealMethod.java create mode 100644 src/test/resources/SimpleTests/RealWhile.java create mode 100644 src/test/resources/SimpleTests/SelfReference.java create mode 100644 src/test/resources/SimpleTests/SystemOutPrintln.java create mode 100644 src/test/resources/SimpleTests/SystemOutPrintlnString.java create mode 100644 src/test/resources/SimpleTests/ThisDotMethodCall.java create mode 100644 src/test/resources/SimpleTests/ValueAdapterTests.java create mode 100644 src/test/resources/SimpleTests/VoidMethod.java create mode 100644 src/test/resources/SimpleTests/VoidReturn.java create mode 100644 src/test/resources/SimpleTests/WhileTest.java create mode 100644 src/test/resources/basicClasses/emptyClass create mode 100644 src/test/resources/basicClasses/publicEmptyClass diff --git a/src/test/java/AST/TestRunner.java b/src/test/java/AST/TestRunner.java new file mode 100644 index 0000000..2b36f3f --- /dev/null +++ b/src/test/java/AST/TestRunner.java @@ -0,0 +1,273 @@ +package AST; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.InputStream; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import Helper.MockGenerator; +import Helper.Resources; +import common.AccessModifier; +import common.BaseType; +import common.Compiler; +import common.Primitives; +import common.PrintableVector; +import syntaxtree.structure.ClassDecl; +import syntaxtree.structure.ConstructorDecl; +import syntaxtree.structure.FieldDecl; +import syntaxtree.structure.Program; + +@DisplayName("Abstract Syntax Tree Generation") +public class TestRunner { + + @Test + @DisplayName("Control Test") + void controlTest() { + var first = MockGenerator.getConstructorParameterAst(); + var second = MockGenerator.getConstructorParameterAst(); + + assertEquals(first, second); + } + + @Test + @DisplayName("Empty Class") + void emptyClass() { + InputStream file = Resources.getFileAsStream("SimpleTests/EmptyClass.java"); + Program ast = Compiler.getFactory().getAstAdapter().getAst(file); + PrintableVector constructors = new PrintableVector<>(); + constructors.add(new ConstructorDecl()); + ClassDecl classDecl = new ClassDecl("EmptyClass", new PrintableVector<>(), constructors, + new PrintableVector<>()); + PrintableVector classDecls = new PrintableVector<>(); + classDecls.add(classDecl); + var generatedAst = new Program(classDecls); + + assertEquals(generatedAst, ast); + + } + + @Test + @DisplayName("EmptyClassWithConstructor") + void emptyClassWithConstructor() { + InputStream file = Resources.getFileAsStream("SimpleTests/EmptyClassWithConstructor.java"); + Program generatedAst = Compiler.getFactory().getAstAdapter().getAst(file); + + PrintableVector constructors = new PrintableVector<>(); + constructors.add(new ConstructorDecl()); + ClassDecl classDecl = new ClassDecl("EmptyClassWithConstructor", new PrintableVector<>(), constructors, + new PrintableVector<>()); + PrintableVector classDecls = new PrintableVector<>(); + classDecls.add(classDecl); + var ast = new Program(classDecls); + assertEquals(ast, generatedAst); + + } + + @Test + @DisplayName("ClassFields") + void classFields() { + Program generatedAst = Resources.getProgram("SimpleTests/ClassFields.java"); + + Program expectedAst = MockGenerator.getClassFieldsAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("ClassField without AccessModifier") + void classFieldWithoutAccessModifier() { + Program generatedAst = Resources.getProgram("SimpleTests/AutoAccessModifierField.java"); + + Program expectedAst = MockGenerator.getAutoClassFieldAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("Comments") + void commentTest() { + InputStream file = Resources.getFileAsStream("SimpleTests/Comments.java"); + Program generatedAst = Compiler.getFactory().getAstAdapter().getAst(file); + + PrintableVector constructors = new PrintableVector<>(); + constructors.add(new ConstructorDecl()); + PrintableVector fields = new PrintableVector<>(); + + FieldDecl lorem = new FieldDecl("lorem", AccessModifier.PRIVATE); + lorem.setType(new BaseType(Primitives.INT)); + fields.add(lorem); + + FieldDecl ipsum = new FieldDecl("ipsum", AccessModifier.PRIVATE); + ipsum.setType(new BaseType(Primitives.BOOL)); + fields.add(ipsum); + + ClassDecl classDecl = new ClassDecl("Comments", fields, constructors, new PrintableVector<>()); + PrintableVector classDecls = new PrintableVector<>(); + classDecls.add(classDecl); + var expectedAst = new Program(classDecls); + assertEquals(expectedAst, generatedAst); + + } + + @Test + @DisplayName("Constructor With Parameters") + void constructorWithParameters() { + Program generatedAst = Resources.getProgram("SimpleTests/ConstructorParams.java"); + Program expectedAst = MockGenerator.getConstructorParameterAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("Constructor With this. assign body") + void constructorWithThisAssignBody() { + Program generatedAst = Resources.getProgram("SimpleTests/ConstructorThisDot.java"); + Program expectedAst = MockGenerator.getConstructorThisDotAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("VoidMethod") + void voidMethod() { + Program generatedAst = Resources.getProgram("SimpleTests/VoidMethod.java"); + Program expectedAst = MockGenerator.getVoidMethodAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("RealConstructor") + void realConstructor() { + Program generatedAst = Resources.getProgram("SimpleTests/RealConstructor.java"); + Program expectedAst = MockGenerator.getRealConstructorAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("MethodCall") + void methodCall() { + Program generatedAst = Resources.getProgram("SimpleTests/MethodCall.java"); + Program expectedAst = MockGenerator.getMethodCallAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("ThisDotMethodCall") + void thisDotMethodCall() { + Program generatedAst = Resources.getProgram("SimpleTests/ThisDotMethodCall.java"); + Program expectedAst = MockGenerator.getThisDotMethodCallAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("MethodCallWithParameters") + void methodCallWithParameters() { + Program generatedAst = Resources.getProgram("SimpleTests/MethodCallParams.java"); + Program expectedAst = MockGenerator.getMethodCallWithParameterAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("GetterFunction") + void getterFunction() { + Program generatedAst = Resources.getProgram("SimpleTests/GetterFunction.java"); + Program expectedAst = MockGenerator.getGetterFunctionAst(); + + System.out.println(generatedAst); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("CharArgument") + void charArgument() { + Program generatedAst = Resources.getProgram("SimpleTests/CharArgument.java"); + Program expectedAst = MockGenerator.getCharArgumentAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("ExplicitNullAssign") + void explicitNullAssign() { + Program generatedAst = Resources.getProgram("SimpleTests/ExplicitNullAssign.java"); + Program expectedAst = MockGenerator.getExplicitNullAssignAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("SelfReference") + void selfReference() { + Program generatedAst = Resources.getProgram("SimpleTests/SelfReference.java"); + Program expectedAst = MockGenerator.getSelfReferenceAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("ValueAdapterTests") + void valueAdapterTest() { + Program generatedAst = Resources.getProgram("SimpleTests/ValueAdapterTests.java"); + Program expectedAst = MockGenerator.getValueAdapterTestAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("System.out.println Test") + void systemOutPrintlnTest() { + Program generatedAst = Resources.getProgram("SimpleTests/SystemOutPrintln.java"); + Program expectedAst = MockGenerator.getSystemOutPrintlnAst(); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("System.out.print-String Test") + void systemOutPrintStringTest() { + Program generatedAst = Resources.getProgram("SimpleTests/SystemOutPrintlnString.java"); + Program expectedAst = MockGenerator.getSystemOutPrintStringAst(); + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("MainMethodTest") + void mainMethodTest() { + Program generatedAst = Resources.getProgram("SimpleTests/MainMethodTest.java"); + Program expectedAst = MockGenerator.getMainMethodTestAst(); + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("ForTest") + void forTest() { + Program generatedAst = Resources.getProgram("SimpleTests/ForTest.java"); + Program expectedAst = MockGenerator.getForTestAst(); + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("IncTest") + void incTest() { + Program generatedAst = Resources.getProgram("SimpleTests/IncTest.java"); + Program expectedAst = MockGenerator.getIncTestAst(); + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("DecTest") + void decTest() { + Program generatedAst = Resources.getProgram("SimpleTests/DecTest.java"); + Program expectedAst = MockGenerator.getDecTestAst(); + assertEquals(expectedAst, generatedAst); + } + +} diff --git a/src/test/java/All/TestRunner.java b/src/test/java/All/TestRunner.java new file mode 100644 index 0000000..8433ea5 --- /dev/null +++ b/src/test/java/All/TestRunner.java @@ -0,0 +1,1254 @@ +package All; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import Helper.ReflectLoader; +import Helper.Resources; +import common.Compiler; +import semantic.exceptions.SemanticError; +import syntaxtree.structure.Program; + +@DisplayName("All") +public class TestRunner { + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; + + /** + * @throws Exception + */ + @Test + @DisplayName("Empty Class") + void emptyClass() throws Exception { + InputStream file = Resources.getFileAsStream("SimpleTests/EmptyClass.java"); + Program ast = Compiler.getFactory().getAstAdapter().getAst(file); + Program tast = Compiler.getFactory().getTastAdapter().getTast(ast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("EmptyClass"); + + Object o = c.getDeclaredConstructor().newInstance(); + assertEquals("EmptyClass", o.getClass().getName()); + } + + /** + * @throws Exception + */ + @Test + @DisplayName("EmptyClassWithConstructor") + void emptyClassWithConstructor() throws Exception { + InputStream file = Resources.getFileAsStream("SimpleTests/EmptyClassWithConstructor.java"); + Program ast = Compiler.getFactory().getAstAdapter().getAst(file); + Program tast = Compiler.getFactory().getTastAdapter().getTast(ast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("EmptyClassWithConstructor"); + + Object o = c.getDeclaredConstructor().newInstance(); + assertEquals("EmptyClassWithConstructor", o.getClass().getName()); + } + + @Test + @DisplayName("ClassFields - privateAccess") + void classFieldsPrivate() { + Program program = Resources.getProgram("SimpleTests/ClassFields.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + var autoAccess = loader.getField("ClassFields", "privateAccess"); + + assertEquals(Modifier.PRIVATE, autoAccess.getModifiers()); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + } + + @Test + @DisplayName("ClassFields - publicAccess") + void classFieldsPublic() { + Program program = Resources.getProgram("SimpleTests/ClassFields.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + var publicAccess = loader.getField("ClassFields", "publicAccess"); + assertEquals(Modifier.PUBLIC, publicAccess.getModifiers()); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + } + + @Test + @DisplayName("ClassFields - protectedAccess") + void classFieldsProtected() { + Program program = Resources.getProgram("SimpleTests/ClassFields.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + var protectedAccess = loader.getField("ClassFields", "protectedAccess"); + assertEquals(Modifier.PROTECTED, protectedAccess.getModifiers()); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + } + + /** + * @throws Exception + */ + @Test + @DisplayName("Comments") + void comments() throws Exception { + InputStream file = Resources.getFileAsStream("SimpleTests/Comments.java"); + Program ast = Compiler.getFactory().getAstAdapter().getAst(file); + Program tast = Compiler.getFactory().getTastAdapter().getTast(ast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("Comments"); + Field lorem = loader.getField(c.getName(), "lorem"); + assertEquals("int", lorem.getType().toString()); + } + + @Test + @DisplayName("Constructor With Parameters") + void constructorWithParameters() { + Program program = Resources.getProgram("SimpleTests/ConstructorParams.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("ConstructorParams"); + try { + Object o = c.getDeclaredConstructor(int.class).newInstance(1); + assertEquals("ConstructorParams", o.getClass().getName()); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("Constructor with this. assign body") + void constructorWithThisAssignBody() { + Program program = Resources.getProgram("SimpleTests/ConstructorThisDot.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("ConstructorThisDot"); + try { + Object o = c.getDeclaredConstructor().newInstance(); + var i = loader.getField("ConstructorThisDot", "i"); + var ivalue = i.get(o); + assertEquals(5, ivalue); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + } + + @Test + @DisplayName("VoidMethod") + void voidMethod() { + Program program = Resources.getProgram("SimpleTests/VoidMethod.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("VoidMethod"); + try { + Object o = c.getDeclaredConstructor().newInstance(); + var voidMethod = loader.getMethod("VoidMethod", "foo"); + voidMethod.invoke(o); + assertEquals("foo", voidMethod.getName()); + } catch (Exception e) { + + fail(e.getLocalizedMessage()); + } + } + + @Test + @DisplayName("RealConstructor - params&this-assigns") + void realConstructor() { + Program program = Resources.getProgram("SimpleTests/RealConstructor.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("RealConstructor"); + try { + int randomI = 2; + Object o = c.getDeclaredConstructor(int.class).newInstance(randomI); + var i = loader.getField("RealConstructor", "i"); + var ivalue = i.get(o); + assertEquals(randomI, ivalue); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + } + + @Test + @DisplayName("MethodCall") + void methodCall() { + ReflectLoader loader = new ReflectLoader("SimpleTests/MethodCall.java"); + Class c = loader.findClass("MethodCall"); + Object o = null; + int value = 1; + try { + o = c.getDeclaredConstructor().newInstance(); + var i = loader.getField("MethodCall", "i"); + int ivalue = (int) i.get(o); + assertEquals(value, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("MethodCallWithParameters") + void methodCallParams() { + ReflectLoader loader = new ReflectLoader("SimpleTests/MethodCallParams.java"); + Class c = loader.findClass("MethodCallParams"); + Object o = null; + int value = 5; + try { + o = c.getDeclaredConstructor(int.class).newInstance(value); + var i = loader.getField("MethodCallParams", "i"); + int ivalue = (int) i.get(o); + assertEquals(value, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("CharArgument") + void charArgument() { + ReflectLoader loader = new ReflectLoader("SimpleTests/CharArgument.java"); + Class clazz = loader.findClass("CharArgument"); + Object o = null; + char value = 'a'; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var cField = loader.getField("CharArgument", "c"); + char ivalue = (char) cField.get(o); + assertEquals(value, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("MultClasses") + void multClasses() { + Program program = Resources.getProgram("SimpleTests/MultClassesReference.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + if (bc.size() != 2) { + fail("Bytecode map should hold 2 Classes but got " + bc.size()); + return; + } + var loader = new ReflectLoader(bc); + int value = 5; + try { + var clazz1 = loader.findClass("MultClassesReference2"); + Object multclassref2 = null; + multclassref2 = clazz1.getDeclaredConstructor(int.class).newInstance(value); + var testMethod = loader.getMethod("MultClassesReference2", "test"); + var ivalue = testMethod.invoke(multclassref2); + assertEquals(value, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("IfStatement") + void ifStatement() { + Program program = Resources.getProgram("SimpleTests/IfStatement.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("IfStatement"); + Object o = null; + int value = -1; + int result; + if (value == 1) { + result = 10; + } else { + result = 100; + } + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("IfStatement", "foo", int.class); + var ivalue = (int) foo.invoke(o, value); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("IfElseStatement") + void ifElseStatement() { + Program program = Resources.getProgram("SimpleTests/IfElseStatement.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("IfElseStatement"); + Object o = null; + int value = -1; + int result; + if (value == 1) { + result = 10; + } else { + result = 100; + } + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("IfElseStatement", "foo", int.class); + var ivalue = (int) foo.invoke(o, value); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("IfElseIfStatement") + void ifElseIfStatement() { + Program program = Resources.getProgram("SimpleTests/IfElseIfStatement.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("IfElseIfStatement"); + Object o = null; + int value = 3; + int result; + if (value == 1) { + result = 10; + } else if (value == 2) { + result = 40000; + } else { + result = 42000; + } + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("IfElseIfStatement", "foo", int.class); + var ivalue = (int) foo.invoke(o, value); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("IfElseIfStatementWithoutReturn") + void ifElseIfStatementWithoutReturn() { + Program program = Resources.getProgram("SimpleTests/IfElseIfStatementWithoutReturn.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("IfElseIfStatementWithoutReturn"); + Object o = null; + int value = 3; + int result; + if (value == 1) { + result = 10; + } else if (value == 2) { + result = 20; + } else { + result = 30; + } + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("IfElseIfStatementWithoutReturn", "foo", int.class); + var ivalue = (int) foo.invoke(o, value); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("IfElseIfStatementWithOneReturn") + void ifElseIfStatementWithOneReturn() { + Program program = Resources.getProgram("SimpleTests/IfElseIfStatementWithOneReturn.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("IfElseIfStatementWithOneReturn"); + Object o = null; + int value = 3; + int result; + if (value == 1) { + result = 10; + } else if (value == 2) { + result = 20; + } else { + result = 30; + } + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("IfElseIfStatementWithOneReturn", "foo", int.class); + var ivalue = (int) foo.invoke(o, value); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("FourClasses") + void fourClasses() { + Program program = Resources.getProgram("SimpleTests/FourClasses.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("FourClasses"); + Object o = null; + int result = 10; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var main = loader.getMethod("FourClasses", "main", int.class); + var ivalue = (int) main.invoke(o, result); + assertEquals(result * 2, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("FourClassesFieldAssign") + void fourClassesFieldAssign() { + Program program = Resources.getProgram("SimpleTests/FourClassesFieldAssign.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("FourClassesFieldAssign"); + Object o = null; + int result = 10; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var main = loader.getMethod("FourClassesFieldAssign", "fieldAssign", int.class); + var ivalue = (int) main.invoke(o, result); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("FourClassesSetter") + void fourClassesSetter() { + Program program = Resources.getProgram("SimpleTests/FourClassesSetter.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("FourClassesSetter"); + Object o = null; + int result = 10; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var main = loader.getMethod("FourClassesSetter", "setFieldTest", int.class); + var ivalue = (int) main.invoke(o, result); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("MinusMethod") + void minusMethod() { + Program program = Resources.getProgram("SimpleTests/MinusMethod.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("MinusMethod"); + Object o = null; + int result = 10; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("MinusMethod", "foo", int.class); + var ivalue = (int) foo.invoke(o, result); + assertEquals(0, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("PlusMethod") + void plusMethod() { + Program program = Resources.getProgram("SimpleTests/PlusMethod.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("PlusMethod"); + Object o = null; + int result = 10; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("PlusMethod", "foo", int.class); + var ivalue = (int) foo.invoke(o, result); + assertEquals(20, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("MulMethod") + void mulMethod() { + Program program = Resources.getProgram("SimpleTests/MulMethod.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("MulMethod"); + Object o = null; + int result = 10; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("MulMethod", "foo", int.class); + var ivalue = (int) foo.invoke(o, result); + assertEquals(100, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("DivMethod") + void divMethod() { + Program program = Resources.getProgram("SimpleTests/DivMethod.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("DivMethod"); + Object o = null; + int result = 10; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("DivMethod", "foo", int.class); + var ivalue = (int) foo.invoke(o, result); + assertEquals(1, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("PunktVorStrich") + void punktVorStrich() { + Program program = Resources.getProgram("SimpleTests/PunktVorStrich.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("PunktVorStrich"); + Object o = null; + int a = 10; + int b = 20; + int c = 30; + int result = a + b * c; + int result2 = a * b + c; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("PunktVorStrich", "foo", int.class, int.class, int.class); + var bar = loader.getMethod("PunktVorStrich", "bar", int.class, int.class, int.class); + var ivalue = (int) foo.invoke(o, a, b, c); + assertEquals(result2, ivalue); + var ivalue2 = (int) bar.invoke(o, a, b, c); + assertEquals(result, ivalue2); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("AndVorOr") + void andVorOr() { + Program program = Resources.getProgram("SimpleTests/AndVorOr.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("AndVorOr"); + Object o = null; + boolean a = true; + boolean b = true; + boolean c = false; + boolean result = a || b && c; + boolean result2 = a || b && c || b && c; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("AndVorOr", "foo", boolean.class, boolean.class, boolean.class); + var bar = loader.getMethod("AndVorOr", "foo", boolean.class, boolean.class, boolean.class); + var ivalue = (boolean) foo.invoke(o, a, b, c); + assertEquals(result, ivalue); + ivalue = (boolean) bar.invoke(o, a, b, c); + assertEquals(result2, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("KlammerVorPunkt") + void klammerVorPunkt() { + Program program = Resources.getProgram("SimpleTests/KlammerVorPunkt.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("KlammerVorPunkt"); + Object o = null; + int a = 10; + int b = 20; + int c = 30; + int result = a * (b + c); + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("KlammerVorPunkt", "foo", int.class, int.class, int.class); + var ivalue = (int) foo.invoke(o, a, b, c); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("ModMethod") + void modMethod() { + Program program = Resources.getProgram("SimpleTests/ModMethod.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("ModMethod"); + Object o = null; + int a = 4; + int b = 260; + int result = b % a; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("ModMethod", "foo", int.class, int.class); + var ivalue = (int) foo.invoke(o, b, a); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("NotMethod") + void notMethod() { + Program program = Resources.getProgram("SimpleTests/NotMethod.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("NotMethod"); + Object o = null; + boolean a = true; + boolean result = !a; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("NotMethod", "foo", boolean.class); + var ivalue = (boolean) foo.invoke(o, a); + assertEquals(result, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("OperatorFullTest") + void operatorFullTest() { + Program program = Resources.getProgram("SimpleTests/OperatorTest.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + System.out.println(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("OperatorTest"); + Object o = null; + int x = 452; + int y = 87; + + boolean a = true; + boolean b = false; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var assign = loader.getMethod("OperatorTest", "assign", int.class).invoke(o, x); + assertEquals(x, assign); + var plus = loader.getMethod("OperatorTest", "plus", int.class, int.class).invoke(o, x, y); + assertEquals(x + y, plus); + var minus = loader.getMethod("OperatorTest", "minus", int.class, int.class).invoke(o, x, y); + assertEquals(x - y, minus); + var mult = loader.getMethod("OperatorTest", "mult", int.class, int.class).invoke(o, x, y); + assertEquals(x * y, mult); + var div = loader.getMethod("OperatorTest", "div", int.class, int.class).invoke(o, x, y); + assertEquals(x / y, div); + var mod = loader.getMethod("OperatorTest", "mod", int.class, int.class).invoke(o, x, y); + assertEquals(x % y, mod); + var gt = loader.getMethod("OperatorTest", "gt", int.class, int.class).invoke(o, x, y); + assertEquals(x > y, gt); + var lt = loader.getMethod("OperatorTest", "lt", int.class, int.class).invoke(o, x, y); + assertEquals(x < y, lt); + var gte = loader.getMethod("OperatorTest", "gte", int.class, int.class).invoke(o, x, y); + assertEquals(x >= y, gte); + var lte = loader.getMethod("OperatorTest", "lte", int.class, int.class).invoke(o, x, y); + assertEquals(x <= y, lte); + var eq = loader.getMethod("OperatorTest", "eq", int.class, int.class).invoke(o, x, y); + assertEquals(x == y, eq); + var neq = loader.getMethod("OperatorTest", "neq", int.class, int.class).invoke(o, x, y); + assertEquals(x != y, neq); + + var beq = loader.getMethod("OperatorTest", "beq", boolean.class, boolean.class).invoke(o, true, true); + assertEquals(true, beq); + var beq2 = loader.getMethod("OperatorTest", "beq", boolean.class, boolean.class).invoke(o, true, false); + assertEquals(false, beq2); + var beq3 = loader.getMethod("OperatorTest", "beq", boolean.class, boolean.class).invoke(o, false, true); + assertEquals(false, beq3); + var beq4 = loader.getMethod("OperatorTest", "beq", boolean.class, boolean.class).invoke(o, false, false); + assertEquals(true, beq4); + + var bneq = loader.getMethod("OperatorTest", "bneq", boolean.class, boolean.class).invoke(o, true, true); + assertEquals(false, bneq); + + var and = loader.getMethod("OperatorTest", "and", boolean.class, boolean.class).invoke(o, a, b); + assertEquals(a && b, and); + var and2 = loader.getMethod("OperatorTest", "and", boolean.class, boolean.class).invoke(o, a, !b); + assertEquals(a && !b, and2); + var or = loader.getMethod("OperatorTest", "or", boolean.class, boolean.class).invoke(o, a, b); + assertEquals(a || b, or); + var not = loader.getMethod("OperatorTest", "not", boolean.class).invoke(o, + a); + assertEquals(!a, not); + + var icEq = loader.getMethod("OperatorTest", "icEq", int.class, char.class).invoke(o, x, (char) x); + assertEquals(true, icEq); + + var icLt = loader.getMethod("OperatorTest", "icLt", int.class, char.class).invoke(o, x, (char) y); + assertEquals(x < (char) y, icLt); + + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("ExtendedNotTest") + void extendedNotTest() { + Program program = Resources.getProgram("SimpleTests/ExtendedNotTest.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class clazz = loader.findClass("ExtendedNotTest"); + Object o = null; + boolean a = true; + boolean b = false; + int x = 87; + int y = 452; + try { + o = clazz.getDeclaredConstructor().newInstance(); + var notequal = loader.getMethod("ExtendedNotTest", "notequal", int.class, int.class); + + assertEquals(false, notequal.invoke(o, x, x)); + assertEquals(true, notequal.invoke(o, x, y)); + + var multiple = loader.getMethod("ExtendedNotTest", "multiple", boolean.class, boolean.class); + assertEquals(!(!a || b), multiple.invoke(o, a, b)); + + var notWithAssigns = loader.getMethod("ExtendedNotTest", "notWithAssigns", boolean.class); + assertEquals(a, notWithAssigns.invoke(o, a)); + + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("SelfReference") + void SelfReference() { + Program program = Resources.getProgram("SimpleTests/SelfReference.java"); + + System.out.println(program); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + } + + @Test + @DisplayName("MultipleClassesMethodCalls") + void multipleClassesMethodCalls() { + Program program = Resources.getProgram("SimpleTests/MultipleClassesMethodCalls.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + int x = 10; + try { + Object o = loader.getConstructor("MultipleClassesMethodCalls").newInstance(); + var mainMethod = loader.getMethod("MultipleClassesMethodCalls", "main", int.class); + var returnValue = mainMethod.invoke(o, x); + assertEquals(x, returnValue); + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + + } + + @Test + @DisplayName("RealWhile") + void realWhile() { + Program program = Resources.getProgram("SimpleTests/RealWhile.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + int x = 10; + try { + Object o = loader.getConstructor("RealWhile").newInstance(); + var mainMethod = loader.getMethod("RealWhile", "foo", int.class); + var returnValue = mainMethod.invoke(o, x); + assertEquals(x * 2, returnValue); + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + + } + + @Test + @DisplayName("LinkedList") + void linkedList() { + Program program = Resources.getProgram("Integration/LinkedList.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + Object executer = loader.getConstructor("Executer").newInstance(); + var foo = loader.getMethod("Executer", "foo"); + foo.invoke(executer); + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + @Test + @DisplayName("StringLinkedList") + void stringLinkedList() { + Program program = Resources.getProgram("Integration/StringList.java"); + + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + Object o = loader.getConstructor("StringList", char.class).newInstance('H'); + var add = loader.getMethod("StringList", "add", char.class); + var print = loader.getMethod("StringList", "print"); + add.invoke(o, 'e'); + add.invoke(o, 'l'); + add.invoke(o, 'l'); + add.invoke(o, 'o'); + add.invoke(o, ' '); + add.invoke(o, 'W'); + add.invoke(o, 'o'); + add.invoke(o, 'r'); + add.invoke(o, 'l'); + add.invoke(o, 'd'); + add.invoke(o, '!'); + print.invoke(o); + System.setOut(new PrintStream(outContent)); + print.invoke(o); + var expected = new String("Hello World!").replaceAll("\\p{C}", ""); + var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expected, actual); + System.setOut(originalOut); + + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + @Test + @DisplayName("SystemOutPrintln Test") + void systemOutPrintlnTest() { + Program program = Resources.getProgram("Integration/SystemOutPrintln.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("SystemOutPrintln"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("SystemOutPrintln", "foo"); + var bar = loader.getMethod("SystemOutPrintln", "bar"); + var baz = loader.getMethod("SystemOutPrintln", "baz"); + var errortest = loader.getMethod("SystemOutPrintln", "errorTest"); + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + foo.invoke(o); + bar.invoke(o); + baz.invoke(o); + errortest.invoke(o); + var expected = new String("c\n\tcd\n\t100\n\ttrue\n\t").replaceAll("\\p{C}", ""); + ; + var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expected, actual); + var expectedErr = new String("a\n\t").replaceAll("\\p{C}", ""); + var actualErr = new String(errContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expectedErr, actualErr); + System.setOut(originalOut); + System.setErr(originalErr); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("StackTest") + void stackTest() { + Program program = Resources.getProgram("Integration/Stack.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("Executer"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("Executer", "foo"); + foo.invoke(o); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("OverridingTest") + void OverridingTest() { + Program program = Resources.getProgram("SimpleTests/OverridingTest.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("OverridingTest"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("OverridingTest", "foo", int.class); + assertEquals(1337, foo.invoke(o, 30)); + assertEquals(8, foo.invoke(o, 8)); + assertEquals(0, foo.invoke(o, 2)); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("OverridingTestNull") + void OverridingTestNull() { + Program program = Resources.getProgram("SimpleTests/OverridingTestNull.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("OverridingTestNull"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("OverridingTestNull", "foo", int.class); + assertEquals(1337, foo.invoke(o, 40)); + assertEquals(1337, foo.invoke(o, 15)); + assertEquals(8, foo.invoke(o, 8)); + assertEquals(0, foo.invoke(o, 2)); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("OverridingTestNullMultiple") + void OverridingTestNullMultiple() { + Program program = Resources.getProgram("SimpleTests/OverridingTestNullMultiple.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + System.out.println(tast); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("OverridingTestNullMultiple"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("OverridingTestNullMultiple", "foo", int.class); + assertEquals(1337, foo.invoke(o, 40)); + assertEquals(1337, foo.invoke(o, 20)); + assertEquals(1337, foo.invoke(o, 15)); + assertEquals(8, foo.invoke(o, 8)); + assertEquals(0, foo.invoke(o, 2)); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("MultipleMethodsConflictTest") + void MultipleMethodsConflictTest() { + Program program = Resources.getProgram("FailTests/MultipleMethodsConflict.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + } + + @Test + @DisplayName("LOFOI") + void lOFOITest() { + Program program = Resources.getProgram("Integration/LOFOI.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("LOFOI"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("LOFOI", "foo"); + var bar = loader.getMethod("LOFOI", "bar"); + assertEquals(20, foo.invoke(o)); + assertEquals(10, bar.invoke(o)); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("LOFOIFailTest") + void lOFOIFailTest() { + Program program = Resources.getProgram("FailTests/LOFOI.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + } + + @Test + @DisplayName("MethodCallsInWhile") + void methodCallsInWhile() { + Program program = Resources.getProgram("SimpleTests/MethodCallsInWhile.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + // Just checks if no error is thrown + } + + @Test + @DisplayName("MethodCallStressTest") + void methodCallStressTest() { + Program program = Resources.getProgram("SimpleTests/MethodCallStressTest.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + + } + + // Stream outContent = new ByteArrayOutputStream(); + // private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + // private final PrintStream originalOut = System.out; + // private final PrintStream originalErr = System.err; + + @Test + @DisplayName("DijsktraTest") + void dijsktraTest() { + Program program = Resources.getProgram("Integration/Dijkstra.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("Dijkstra"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("Dijkstra", "main"); + foo.invoke(o); + System.setOut(new PrintStream(outContent)); + foo.invoke(o); + var expected = new String( + "Shortest path from 1 to 8: 1 10 8 With a distance of 2Shortest path from 1 to 12: 1 10 8 12 With a distance of 3Shortest path from 4 to 12: 4 8 12 With a distance of 2Shortest path from 4 to 9: 4 3 5 9 With a distance of 3Shortest path from 6 to 9: 6 5 9 With a distance of 2") + .replaceAll("\\p{C}", ""); + var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expected, actual); + System.setOut(originalOut); + + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("VoidReturn") + void voidReturn() { + // private final ByteArrayOutput + Program program = Resources.getProgram("SimpleTests/VoidReturn.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + // ReflectLoader loader = new ReflectLoader(bc); + + } + + @Test + @DisplayName("OperatorStacking") + void operatorStacking() { + Program program = Resources.getProgram("SimpleTests/OperatorStacking.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("OperatorStacking"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("OperatorStacking", "foo"); + var bar = loader.getMethod("OperatorStacking", "bar"); + assertEquals(true, foo.invoke(o)); + assertEquals(1 + 2 * 3 - 4 % 6, bar.invoke(o)); + } catch (Exception e) { + fail(e.getMessage()); + } + + } + + @Test + @DisplayName("ConditionalEvaluation") + void conditionalEval() { + Program program = Resources.getProgram("SimpleTests/ConditionalEvaluation.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("ConditionalEvaluation"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("ConditionalEvaluation", "foo"); + var bar = loader.getMethod("ConditionalEvaluation", "bar"); + System.setOut(new PrintStream(outContent)); + assertEquals(true, bar.invoke(o)); + assertEquals(true, foo.invoke(o)); + var expected = new String("bar\n\tfoo\n\tfoo").replaceAll("\\p{C}", ""); + var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expected, actual); + System.setOut(originalOut); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("ForTest") + void forTest() { + // private final ByteArrayOutput + Program program = Resources.getProgram("SimpleTests/ForTest.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + // ReflectLoader loader = new ReflectLoader(bc); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("ForTest"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("ForTest", "foo"); + foo.invoke(o); + System.setOut(new PrintStream(outContent)); + foo.invoke(o); + var expected = new String("0123456789").replaceAll("\\p{C}", ""); + var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expected, actual); + System.setOut(originalOut); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("IncDecStressTest") + void incDecTest() { + Program program = Resources.getProgram("SimpleTests/IncDecStressTest.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("IncDecStressTest"); + Object o = null; + int value = 5; + try { + o = c.getDeclaredConstructor().newInstance(); + var incrementThenReturn = loader.getMethod("IncDecStressTest", "incrementThenReturn", int.class); + var returnThenIncrement = loader.getMethod("IncDecStressTest", "returnThenIncrement", int.class); + var decrementThenReturn = loader.getMethod("IncDecStressTest", "decrementThenReturn", int.class); + var returnThenDecrement = loader.getMethod("IncDecStressTest", "returnThenDecrement", int.class); + var callInline = loader.getMethod("IncDecStressTest", "callInline", int.class); + + var shouldBeIncremented = incrementThenReturn.invoke(o, value); + var shouldBeSame = returnThenIncrement.invoke(o, value); + var shouldBeDecremented = decrementThenReturn.invoke(o, value); + var shouldBeSame2 = returnThenDecrement.invoke(o, value); + var shouldBeIncremented2 = callInline.invoke(o, value); + + assertEquals(value + 1, shouldBeIncremented); + assertEquals(value, shouldBeSame); + assertEquals(value - 1, shouldBeDecremented); + assertEquals(value, shouldBeSame2); + assertEquals(value + 1, shouldBeIncremented2); + + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("Div0Test") + void div0() { + Program program = Resources.getProgram("FailTests/Div0.java"); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + Class c = loader.findClass("Div0"); + var main = c.getMethod("main", String[].class); + main.invoke(null, new Object[] { new String[] {} }); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + System.err.println(); + assertEquals("/ by zero", e.getCause().getMessage()); + return; + } + fail("Should have thrown an exception"); + + } +} diff --git a/src/test/java/CodeGen/TestRunner.java b/src/test/java/CodeGen/TestRunner.java new file mode 100644 index 0000000..eff363b --- /dev/null +++ b/src/test/java/CodeGen/TestRunner.java @@ -0,0 +1,356 @@ +package CodeGen; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import Helper.MockGenerator; +import Helper.ReflectLoader; +import Helper.Resources; +import common.Compiler; +import common.PrintableVector; +import syntaxtree.structure.ClassDecl; +import syntaxtree.structure.ConstructorDecl; +import syntaxtree.structure.Program; + +@DisplayName("Bytecode Generation") +public class TestRunner { + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; + + @Test + @DisplayName("Empty Class") + void main() { + ClassDecl emptyClass = new ClassDecl("EmptyClass", new PrintableVector<>(), new PrintableVector<>(), + new PrintableVector<>()); + PrintableVector classDecls = new PrintableVector<>(); + classDecls.add(emptyClass); + + var tast = new Program(classDecls); + + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("EmptyClass"); + Object o; + try { + o = c.getDeclaredConstructor().newInstance(); + assertEquals(o.getClass().getName(), "EmptyClass"); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | SecurityException e) { + fail(e.getLocalizedMessage()); + } + } + + @Test + @DisplayName("EmptyClassWithConstructor") + void emptyClassWithConstructor() { + PrintableVector constructors = new PrintableVector<>(); + constructors.add(new ConstructorDecl()); + ClassDecl classDecl = new ClassDecl("EmptyClassWithConstructor", new PrintableVector<>(), constructors, + new PrintableVector<>()); + PrintableVector classDecls = new PrintableVector<>(); + classDecls.add(classDecl); + var tast = new Program(classDecls); + + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("EmptyClassWithConstructor"); + Object o; + try { + o = c.getDeclaredConstructor().newInstance(); + assertEquals(o.getClass().getName(), "EmptyClassWithConstructor"); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | SecurityException e) { + fail(e.getLocalizedMessage()); + } + } + + @Test + @DisplayName("Constructor With Parameters") + void constructorWithParameters() { + Program tast = MockGenerator.getConstructorParameterTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("ConstructorParams"); + Object o = null; + try { + o = c.getDeclaredConstructor(int.class).newInstance(1); + assertEquals(o.getClass().getName(), "ConstructorParams"); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + + } + + @Test + @DisplayName("Constructor With this. assign body") + void constructorWithThisAssignBody() { + Program tast = MockGenerator.getConstructorThisDotTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("ConstructorThisDot"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var i = loader.getField("ConstructorThisDot", "i"); + var ivalue = i.get(o); + assertEquals(5, ivalue); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + + } + + @Test + @DisplayName("ClassFields - autoAccess") + void classFieldsAuto() { + Program tast = MockGenerator.getAutoClassFieldAst(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + var autoAccess = loader.getField("AutoAccessModifierField", "autoAccess"); + System.out.println(autoAccess.getModifiers()); + assertEquals(0, autoAccess.getModifiers()); + + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + + } + + @Test + @DisplayName("ClassFields - privateAccess") + void classFieldsPrivate() { + Program tast = MockGenerator.getClassFieldsTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + var autoAccess = loader.getField("ClassFields", "privateAccess"); + assertEquals(Modifier.PRIVATE, autoAccess.getModifiers()); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + + } + + @Test + @DisplayName("ClassFields - publicAccess") + void classFieldsPublic() { + Program tast = MockGenerator.getClassFieldsTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + var autoAccess = loader.getField("ClassFields", "publicAccess"); + assertEquals(Modifier.PUBLIC, autoAccess.getModifiers()); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + + } + + @Test + @DisplayName("ClassFields - protectedAccess") + void classFieldsProtected() { + Program tast = MockGenerator.getClassFieldsTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + try { + var autoAccess = loader.getField("ClassFields", "protectedAccess"); + + assertEquals(Modifier.PROTECTED, autoAccess.getModifiers()); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + + } + + @Test + @DisplayName("VoidMethod") + void voidMethod() { + Program tast = MockGenerator.getVoidMethodTast(); + + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("VoidMethod"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var m = loader.getMethod("VoidMethod", "foo"); + m.invoke(o); + assertEquals("foo", m.getName()); + } catch (Exception e) { + fail(e.getLocalizedMessage()); + } + + } + + @Test + @DisplayName("RealConstructor") + void realConstructor() { + Program tast = MockGenerator.getRealConstructorTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("RealConstructor"); + try { + int randomI = 2; + var constructor = c.getDeclaredConstructor(int.class); + Object o = constructor.newInstance(randomI); + var i = loader.getField("RealConstructor", "i"); + int ivalue = (int) i.get(o); + assertEquals(randomI, ivalue); + } catch (NoSuchFieldException e) { + fail("No such field"); + } catch (Exception e) { + fail(e.getCause()); + } + } + + @Test + @DisplayName("MethodCall") + void methodCall() { + Program tast = MockGenerator.getMethodCallTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("MethodCall"); + Object o = null; + int value = 1; + try { + o = c.getDeclaredConstructor().newInstance(); + var i = loader.getField("MethodCall", "i"); + int ivalue = (int) i.get(o); + assertEquals(value, ivalue); + } catch (Exception e) { + fail(e.getMessage()); + } + + } + + @Test + @DisplayName("MiniLinkedList") + void miniLinkedList() { + Program program = Resources.getProgram("SimpleTests/MiniLinkedList.java"); + + System.out.println(program); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + } + + @Test + @DisplayName("SystemOutPrintln Test") + void systemOutPrintlnTest() { + Program tast = MockGenerator.getSystemOutPrintlnTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("SystemOutPrintln"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var m = loader.getMethod("SystemOutPrintln", "foo"); + m.invoke(o); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("SystemOutPrintlnString Test") + void systemOutPrintlnStringTest() { + Program tast = MockGenerator.getSystemOutPrintStringTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("SystemOutPrintlnString"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var m = loader.getMethod("SystemOutPrintlnString", "foo"); + m.invoke(o); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("MainMethodTest") + void mainMethodTest() { + Program tast = MockGenerator.getMainMethodTestTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("MainMethodTest"); + try { + var m = c.getMethod("main", String[].class); + m.invoke(null, new Object[] { new String[] {} }); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("IncTest") + void incTest() { + Program tast = MockGenerator.getIncTestTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("IncTest"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("IncTest", "foo"); + var bar = loader.getMethod("IncTest", "bar"); + foo.invoke(o); + bar.invoke(o); + System.setOut(new PrintStream(outContent)); + foo.invoke(o); + var expected = new String("0123456789").replaceAll("\\p{C}", ""); + var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + outContent.reset(); + assertEquals(expected, actual); + bar.invoke(o); + actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expected, actual); + System.setOut(originalOut); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + @DisplayName("DecTest") + void decTest() { + Program tast = MockGenerator.getDecTestTast(); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + ReflectLoader loader = new ReflectLoader(bc); + Class c = loader.findClass("DecTest"); + Object o = null; + try { + o = c.getDeclaredConstructor().newInstance(); + var foo = loader.getMethod("DecTest", "foo"); + var bar = loader.getMethod("DecTest", "bar"); + foo.invoke(o); + bar.invoke(o); + System.setOut(new PrintStream(outContent)); + foo.invoke(o); + var expected = new String("10987654321").replaceAll("\\p{C}", ""); + var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + outContent.reset(); + assertEquals(expected, actual); + bar.invoke(o); + actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", ""); + assertEquals(expected, actual); + System.setOut(originalOut); + } catch (Exception e) { + fail(e.getMessage()); + } + } + +} diff --git a/src/test/java/Helper/MockGenerator.java b/src/test/java/Helper/MockGenerator.java new file mode 100644 index 0000000..7354976 --- /dev/null +++ b/src/test/java/Helper/MockGenerator.java @@ -0,0 +1,965 @@ +package Helper; + +import common.AccessModifier; +import common.BaseType; +import common.Operator; +import common.Primitives; +import common.PrintableVector; +import common.ReferenceType; +import syntaxtree.expressions.Binary; +import syntaxtree.expressions.BoolExpr; +import syntaxtree.expressions.CharExpr; +import syntaxtree.expressions.IExpression; +import syntaxtree.expressions.InstVar; +import syntaxtree.expressions.IntegerExpr; +import syntaxtree.expressions.LocalOrFieldVar; +import syntaxtree.expressions.Null; +import syntaxtree.expressions.StringExpr; +import syntaxtree.expressions.This; +import syntaxtree.statementexpression.Assign; +import syntaxtree.statementexpression.CrementStmtExpr; +import syntaxtree.statementexpression.MethodCall; +import syntaxtree.statementexpression.NewDecl; +import syntaxtree.statements.Block; +import syntaxtree.statements.ForStmt; +import syntaxtree.statements.IStatement; +import syntaxtree.statements.LocalVarDecl; +import syntaxtree.statements.ReturnStmt; +import syntaxtree.structure.ClassDecl; +import syntaxtree.structure.ConstructorDecl; +import syntaxtree.structure.FieldDecl; +import syntaxtree.structure.MainMethodDecl; +import syntaxtree.structure.MethodDecl; +import syntaxtree.structure.MethodParameter; +import syntaxtree.structure.Program; + +public abstract class MockGenerator { + + /** + * @param className + * @return Program + */ + public static Program getEmptyProgram(String className) { + + PrintableVector classes = new PrintableVector<>(); + classes.add(getEmptyClass(className)); + + return new Program(classes); + } + + /** + * @param id + * @return ClassDecl + */ + public static ClassDecl getEmptyClass(String id) { + return new ClassDecl(id, new PrintableVector<>(), new PrintableVector<>(), new PrintableVector<>()); + } + + /** + * @param expressions + * @return Block + */ + public static Block getBlock(IStatement... expressions) { + PrintableVector expressionsVector = new PrintableVector<>(); + for (IStatement expression : expressions) { + expressionsVector.add(expression); + } + return new Block(expressionsVector); + } + + /** + * @return Block + */ + public static Block getEmptyBlock() { + return new Block(); + } + + /** + * @param expressions + * @return PrintableVector + */ + public static PrintableVector getArguments(IExpression... expressions) { + PrintableVector arguments = new PrintableVector<>(); + for (IExpression expression : expressions) { + arguments.add(expression); + } + return arguments; + } + + /** + * @param constructors + * @return PrintableVector + */ + public static PrintableVector getConstructors(ConstructorDecl... constructors) { + PrintableVector cons = new PrintableVector<>(); + if (constructors.length == 0) + cons.add(new ConstructorDecl()); + for (ConstructorDecl constructor : constructors) { + cons.add(constructor); + } + return cons; + } + + /** + * @return PrintableVector + */ + public static PrintableVector getEmptyParameters() { + return new PrintableVector<>(); + } + + /** + * @param parameters + * @return PrintableVector + */ + public static PrintableVector getParameters(MethodParameter... parameters) { + PrintableVector parametersVector = new PrintableVector<>(); + for (MethodParameter parameter : parameters) { + parametersVector.add(parameter); + } + return parametersVector; + } + + /** + * @return Program + */ + public static Program getClassFieldsTast() { + return getClassFieldsAst(); + } + + /** + * @return Program + */ + public static Program getClassFieldsAst() { + Program expectedAst = getEmptyProgram("ClassFields"); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + FieldDecl privateField = new FieldDecl("privateAccess", AccessModifier.PRIVATE); + privateField.setType(new BaseType(Primitives.INT)); + FieldDecl publicField = new FieldDecl("publicAccess", AccessModifier.PUBLIC); + publicField.setType(new BaseType(Primitives.INT)); + FieldDecl protectedField = new FieldDecl("protectedAccess", AccessModifier.PROTECTED); + protectedField.setType(new BaseType(Primitives.INT)); + + PrintableVector fields = expectedAst.getClasses().firstElement().getFieldDelcarations(); + fields.add(privateField); + fields.add(publicField); + fields.add(protectedField); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getAutoClassFieldAst() { + Program expectedAst = getEmptyProgram("AutoAccessModifierField"); + + FieldDecl autoField = new FieldDecl(new BaseType(Primitives.INT), "autoAccess"); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + PrintableVector fields = expectedAst.getClasses().firstElement().getFieldDelcarations(); + fields.add(autoField); + return expectedAst; + } + + /** + * @return Program + */ + public static Program getAutoClassFieldTast() { + return getAutoClassFieldAst(); + } + + /** + * @return Program + */ + public static Program getConstructorParameterTast() { + return getConstructorParameterAst(); + } + + /** + * @return Program + */ + public static Program getConstructorParameterAst() { + Program expectedAst = getEmptyProgram("ConstructorParams"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + MethodParameter parameter = new MethodParameter(Primitives.INT, "i"); + + PrintableVector parameters = new PrintableVector<>(); + parameters.add(parameter); + + classDecl.getConstructorDeclarations() + .add(new ConstructorDecl(AccessModifier.PUBLIC, parameters, getEmptyBlock())); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getConstructorThisDotAst() { + + Program expectedAst = getEmptyProgram("ConstructorThisDot"); + + FieldDecl i = new FieldDecl("i", AccessModifier.PUBLIC); + i.setType(new BaseType(Primitives.INT)); + + PrintableVector fields = expectedAst.getClasses().firstElement().getFieldDelcarations(); + fields.add(i); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + Block block = getEmptyBlock(); + + Assign assign = new Assign(new InstVar("i", new This()), new IntegerExpr(5)); + block.getStatements().add(assign); + + classDecl.getConstructorDeclarations() + .add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block)); + System.out.println(classDecl); + return expectedAst; + + } + + /** + * @return Program + */ + public static Program getConstructorThisDotTast() { + Program expectedTast = getEmptyProgram("ConstructorThisDot"); + + FieldDecl i = new FieldDecl("i", AccessModifier.PUBLIC); + i.setType(new BaseType(Primitives.INT)); + + PrintableVector fields = expectedTast.getClasses().firstElement().getFieldDelcarations(); + fields.add(i); + + ClassDecl classDecl = expectedTast.getClasses().firstElement(); + + Block block = getEmptyBlock(); + + var thisi = new InstVar("i", new This("ConstructorThisDot")); + thisi.setType(Primitives.INT); + + Assign assign = new Assign(thisi, new IntegerExpr(5)); + assign.setType(Primitives.INT); + block.getStatements().add(assign); + + classDecl.getConstructorDeclarations() + .add(new ConstructorDecl(AccessModifier.PUBLIC, getEmptyParameters(), block)); + + return expectedTast; + } + + /** + * @return Program + */ + public static Program getVoidMethodAst() { + Program expectedAst = getEmptyProgram("VoidMethod"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + PrintableVector methods = classDecl.getMethodDeclarations(); + MethodDecl foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), + getEmptyBlock()); + methods.add(foo); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getVoidMethodTast() { + return getVoidMethodAst(); + } + + /** + * @return Program + */ + public static Program getRealMethodAst() { + Program expectedAst = getEmptyProgram("RealMethod"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + Block block = getEmptyBlock(); + ReturnStmt returnStmt = new ReturnStmt(new LocalOrFieldVar("i")); + block.getStatements().add(returnStmt); + + var parameters = getEmptyParameters(); + parameters.add(new MethodParameter(Primitives.INT, "i")); + + PrintableVector methods = classDecl.getMethodDeclarations(); + MethodDecl foo = new MethodDecl(new BaseType(Primitives.INT), "foo", parameters, block); + methods.add(foo); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getRealConstructorAst() { + Program expectedAst = getEmptyProgram("RealConstructor"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + FieldDecl i = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "i"); + + classDecl.getFieldDelcarations().add(i); + Assign assignStmt = new Assign(new InstVar(new This(), "i"), new LocalOrFieldVar("i")); + Block block = getBlock(assignStmt); + + var parameters = getParameters(new MethodParameter(Primitives.INT, "i")); + + PrintableVector constructors = classDecl.getConstructorDeclarations(); + ConstructorDecl constructor = new ConstructorDecl(AccessModifier.PUBLIC, parameters, block); + constructors.add(constructor); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getRealConstructorTast() { + String className = "RealConstructor"; + Program expectedTast = getEmptyProgram(className); + + ClassDecl classDecl = expectedTast.getClasses().firstElement(); + FieldDecl i = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "i"); + + classDecl.getFieldDelcarations().add(i); + Assign assignStmt = new Assign(new InstVar(new BaseType(Primitives.INT), new This( + className), "i"), new LocalOrFieldVar(new BaseType(Primitives.INT), "i")); + + assignStmt.setType(new BaseType(Primitives.INT)); + + Block block = getBlock(assignStmt); + + var parameters = getParameters(new MethodParameter(Primitives.INT, "i")); + + PrintableVector constructors = classDecl.getConstructorDeclarations(); + ConstructorDecl constructor = new ConstructorDecl(AccessModifier.PUBLIC, parameters, block); + constructors.add(constructor); + + return expectedTast; + } + + /** + * @return Program + */ + public static Program getMethodCallAst() { + Program expectedAst = getEmptyProgram("MethodCall"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + var fields = classDecl.getFieldDelcarations(); + fields.add(new FieldDecl(new BaseType(Primitives.INT), "i")); + PrintableVector constructors = classDecl.getConstructorDeclarations(); + + Block block = getBlock( + new Assign(new InstVar(new This(), "i"), + new MethodCall(new This(), "foo", getArguments()))); + constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block)); + + Block fooBlock = getBlock(new ReturnStmt(new IntegerExpr(1))); + PrintableVector methods = classDecl.getMethodDeclarations(); + methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo", getEmptyParameters(), fooBlock)); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getMethodCallTast() { + Program expectedTast = getEmptyProgram("MethodCall"); + + ClassDecl classDecl = expectedTast.getClasses().firstElement(); + var fields = classDecl.getFieldDelcarations(); + fields.add(new FieldDecl(new BaseType(Primitives.INT), "i")); + PrintableVector constructors = classDecl.getConstructorDeclarations(); + + Block block = getBlock( + new Assign(new BaseType( + Primitives.INT), + new InstVar(new BaseType(Primitives.INT), new This("MethodCall"), "i"), + new MethodCall(new BaseType(Primitives.INT), new This("MethodCall"), + "foo", getArguments()))); + constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block)); + + Block fooBlock = getBlock(new ReturnStmt(new BaseType(Primitives.INT), new IntegerExpr(1))); + fooBlock.setType(Primitives.INT); + PrintableVector methods = classDecl.getMethodDeclarations(); + methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo", getEmptyParameters(), fooBlock)); + + return expectedTast; + } + + /** + * @return Program + */ + public static Program getMethodCallWithParameterAst() { + Program expectedAst = getEmptyProgram("MethodCallParams"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + var fields = classDecl.getFieldDelcarations(); + fields.add(new FieldDecl(new BaseType(Primitives.INT), "i")); + PrintableVector constructors = classDecl.getConstructorDeclarations(); + + Block block = getBlock( + new Assign(new InstVar(new This(), "i"), + new MethodCall(new This(), "foo", + getArguments(new LocalOrFieldVar("i"))))); + constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, + getParameters(new MethodParameter(Primitives.INT, "i")), block)); + + Block fooBlock = getBlock(new ReturnStmt(new LocalOrFieldVar("i"))); + PrintableVector methods = classDecl.getMethodDeclarations(); + methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo", + getParameters(new MethodParameter(Primitives.INT, "i")), fooBlock)); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getCharArgumentAst() { + Program expectedAst = getEmptyProgram("CharArgument"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + var fields = classDecl.getFieldDelcarations(); + fields.add(new FieldDecl(new BaseType(Primitives.CHAR), "c")); + PrintableVector constructors = classDecl.getConstructorDeclarations(); + + Block block = getBlock( + new Assign(new InstVar(new This(), "c"), + new MethodCall(new This(), "foo", getArguments(new CharExpr('a'))))); + constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, + getParameters(), block)); + + Block fooBlock = getBlock(new ReturnStmt(new LocalOrFieldVar("c"))); + PrintableVector methods = classDecl.getMethodDeclarations(); + methods.add(new MethodDecl(new BaseType(Primitives.CHAR), "foo", + getParameters(new MethodParameter(Primitives.CHAR, "c")), fooBlock)); + + return expectedAst; + } + + /** + * @return Program + */ + public static Program getGetterFunctionAst() { + Program expectedAst = getEmptyProgram("GetterFunction"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + FieldDecl i = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "i"); + + classDecl.getFieldDelcarations().add(i); + Assign assignStmt = new Assign(new InstVar(new This(), "i"), new LocalOrFieldVar("i")); + Block block = getBlock(assignStmt); + + var parameters = getParameters(new MethodParameter(Primitives.INT, "i")); + + PrintableVector constructors = classDecl.getConstructorDeclarations(); + ConstructorDecl constructor = new ConstructorDecl(AccessModifier.PUBLIC, parameters, block); + constructors.add(constructor); + + var getI = new MethodDecl(AccessModifier.PUBLIC, new BaseType(Primitives.INT), "getI", getParameters(), + getBlock(new ReturnStmt(new InstVar(new This(), "i")))); + + classDecl.getMethodDeclarations().add(getI); + + return expectedAst; + } + + public static Program getExplicitNullAssignAst() { + Program expectedAst = getEmptyProgram("ExplicitNullAssign"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + FieldDecl i = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new ReferenceType("ExplicitNullAssign"), + "e"); + + classDecl.getFieldDelcarations().add(i); + + var test = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.VOID), "test", + getParameters(), + getBlock(new Assign(new LocalOrFieldVar("e"), new Null()))); + + classDecl.getMethodDeclarations().add(test); + + classDecl.getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getSelfReferenceAst() { + Program expectedAst = getEmptyProgram("SelfReference"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + FieldDecl i = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new ReferenceType("SelfReference"), + "selfRef"); + + classDecl.getFieldDelcarations().add(i); + + var foo = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "foo", + getParameters(), + getBlock(new ReturnStmt(new MethodCall(new This(), "baz", getArguments())))); + + var baz = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "baz", + getParameters(), + getBlock(new ReturnStmt(new IntegerExpr(10)))); + + var bar = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "bar", + getParameters(), + getBlock( + new LocalVarDecl(new ReferenceType("SelfReference"), "self", + new NewDecl("SelfReference", getArguments())), + new ReturnStmt(new MethodCall( + new InstVar(new LocalOrFieldVar("self"), "selfRef"), + "foo", + getArguments())))); + + classDecl.getMethodDeclarations().add(foo); + classDecl.getMethodDeclarations().add(baz); + classDecl.getMethodDeclarations().add(bar); + + classDecl.getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getSelfReferenceTast() { + Program expectedAst = getEmptyProgram("SelfReference"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + + FieldDecl i = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new ReferenceType("SelfReference"), + "selfRef"); + + classDecl.getFieldDelcarations().add(i); + + Block fooBlock = getBlock(new ReturnStmt(new BaseType(Primitives.INT), + new MethodCall(new BaseType(Primitives.INT), new This("SelfReference"), "baz", + getArguments()))); + fooBlock.setType(new BaseType(Primitives.INT)); + + Block bazBlock = getBlock(new ReturnStmt(new BaseType(Primitives.INT), new IntegerExpr(10))); + bazBlock.setType(new BaseType(Primitives.INT)); + + Block barBlock = getBlock( + new LocalVarDecl(new ReferenceType("SelfReference"), "self", + new NewDecl("SelfReference", getArguments())), + new ReturnStmt(new BaseType(Primitives.INT), + new MethodCall(new BaseType(Primitives.INT), + new InstVar(new ReferenceType("SelfReference"), + new LocalOrFieldVar(new ReferenceType( + "SelfReference"), + "self"), + "selfRef"), + "foo", + getArguments()))); + + barBlock.setType(new BaseType(Primitives.INT)); + + var foo = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "foo", + getParameters(), fooBlock); + + var baz = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "baz", + getParameters(), bazBlock); + + var bar = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "bar", + getParameters(), + barBlock); + + classDecl.getMethodDeclarations().add(foo); + classDecl.getMethodDeclarations().add(baz); + classDecl.getMethodDeclarations().add(bar); + + classDecl.getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getThisDotMethodCallAst() { + Program expectedAst = getEmptyProgram("ThisDotMethodCall"); + + ClassDecl classDecl = expectedAst.getClasses().firstElement(); + var fields = classDecl.getFieldDelcarations(); + fields.add(new FieldDecl(new BaseType(Primitives.INT), "i")); + PrintableVector constructors = classDecl.getConstructorDeclarations(); + + Block block = getBlock( + new Assign(new InstVar(new This(), "i"), + new MethodCall(new This(), "foo", getArguments()))); + constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block)); + + Block fooBlock = getBlock(new ReturnStmt(new IntegerExpr(1))); + PrintableVector methods = classDecl.getMethodDeclarations(); + methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo", getEmptyParameters(), fooBlock)); + + return expectedAst; + } + + public static Program getValueAdapterTestAst() { + Program expectedAst = getEmptyProgram("ValueAdapterTests"); + + Block readsTrueBlock = getBlock(new ReturnStmt(new BoolExpr(true))); + + Block readsFalseBlock = getBlock(new ReturnStmt(new BoolExpr(false))); + + Block readsTrueAndFalseBlock = getBlock(new ReturnStmt(new Binary(new BoolExpr(true), + Operator.AND, new BoolExpr(false)))); + + Block readsIntBlock = getBlock(new ReturnStmt(new IntegerExpr(1))); + + Block readsIntAndIntBlock = getBlock(new ReturnStmt(new Binary(new IntegerExpr(1), + Operator.PLUS, new IntegerExpr(1)))); + + Block readsCharBlock = getBlock(new ReturnStmt(new CharExpr('a'))); + + MethodDecl readsTrue = new MethodDecl(new BaseType(Primitives.BOOL), "readsTrue", getEmptyParameters(), + readsTrueBlock); + + MethodDecl readsFalse = new MethodDecl(new BaseType(Primitives.BOOL), "readsFalse", + getEmptyParameters(), + readsFalseBlock); + + MethodDecl readsTrueAndFalse = new MethodDecl(new BaseType(Primitives.BOOL), "readsTrueAndFalse", + getEmptyParameters(), readsTrueAndFalseBlock); + + MethodDecl readsInt = new MethodDecl(new BaseType(Primitives.INT), "readsInt", getEmptyParameters(), + readsIntBlock); + + MethodDecl readsIntAndInt = new MethodDecl(new BaseType(Primitives.INT), "readsIntAndInt", + getEmptyParameters(), + readsIntAndIntBlock); + + MethodDecl readsChar = new MethodDecl(new BaseType(Primitives.CHAR), "readsChar", getEmptyParameters(), + readsCharBlock); + + var methods = expectedAst.getClasses().firstElement().getMethodDeclarations(); + + methods.add(readsTrue); + methods.add(readsFalse); + methods.add(readsTrueAndFalse); + methods.add(readsInt); + methods.add(readsIntAndInt); + methods.add(readsChar); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + + } + + public static Program getSystemOutPrintlnTast() { + Program expectedAst = getEmptyProgram("SystemOutPrintln"); + var iv = new InstVar(new ReferenceType("java/io/PrintStream"), + new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out"); + iv.setAccessModifier(AccessModifier.PUBLIC_STATIC); + Block block = getBlock( + new MethodCall(new BaseType(Primitives.VOID), + iv, + "println", + getArguments(new IntegerExpr(1)))); + + var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getSystemOutPrintlnAst() { + Program expectedAst = getEmptyProgram("SystemOutPrintln"); + + Block block = getBlock( + new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println", + getArguments(new IntegerExpr(1)))); + + var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getSystemOutPrintStringAst() { + Program expectedAst = getEmptyProgram("SystemOutPrintlnString"); + + Block block = getBlock( + new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println", + getArguments(new StringExpr("Das ist ein String")))); + + var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getSystemOutPrintStringTast() { + Program expectedAst = getEmptyProgram("SystemOutPrintlnString"); + var iv = new InstVar(new ReferenceType("java/io/PrintStream"), + new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out"); + iv.setAccessModifier(AccessModifier.PUBLIC_STATIC); + Block block = getBlock( + new MethodCall(new BaseType(Primitives.VOID), + iv, + "println", + getArguments(new StringExpr("Das ist ein String")))); + + var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getMainMethodTestAst() { + Program expectedAst = getEmptyProgram("MainMethodTest"); + Block block = getBlock( + new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println", + getArguments(new StringExpr("maintest")))); + + var method = new MainMethodDecl(block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getMainMethodTestTast() { + Program expectedAst = getEmptyProgram("MainMethodTest"); + var iv = new InstVar(new ReferenceType("java/io/PrintStream"), + new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out"); + iv.setAccessModifier(AccessModifier.PUBLIC_STATIC); + Block block = getBlock( + new MethodCall(new BaseType(Primitives.VOID), + iv, + "println", + getArguments(new StringExpr("maintest")))); + + var method = new MainMethodDecl(block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getForTestAst() { + Program expectedAst = getEmptyProgram("ForTest"); + Block forBlock = getBlock(new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println", + getArguments(new LocalOrFieldVar("i")))); + Block block = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)), + new Binary(new LocalOrFieldVar("i"), new IntegerExpr(10), + Operator.LESS), + new Assign(new LocalOrFieldVar("i"), + new Binary(new LocalOrFieldVar("i"), new IntegerExpr(1), + Operator.PLUS)), + forBlock)); + + var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getForTestTast() { + Program expectedAst = getEmptyProgram("ForTest"); + var iv = new InstVar(new ReferenceType("java/io/PrintStream"), + new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out"); + iv.setAccessModifier(AccessModifier.PUBLIC_STATIC); + Block forBlock = getBlock(new MethodCall(new BaseType(Primitives.VOID), + iv, + "println", + getArguments(new LocalOrFieldVar(new BaseType(Primitives.INT), "i")))); + Block block = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)), + new Binary(new BaseType(Primitives.BOOL), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + new IntegerExpr(10), + Operator.LESS), + new Assign(new BaseType(Primitives.INT), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + new Binary(new BaseType(Primitives.INT), + new LocalOrFieldVar(new BaseType( + Primitives.INT), "i"), + new IntegerExpr(1), + Operator.PLUS)), + forBlock)); + var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(method); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getIncTestAst() { + Program expectedAst = getEmptyProgram("IncTest"); + Block forBlock = getBlock(new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println", + getArguments(new LocalOrFieldVar("i")))); + Block blockfoo = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)), + new Binary(new LocalOrFieldVar("i"), new IntegerExpr(10), + Operator.LESS), + new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.INCSUF), + forBlock)); + + Block blockbar = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)), + new Binary(new LocalOrFieldVar("i"), new IntegerExpr(10), + Operator.LESS), + new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.INCPRE), + forBlock)); + + var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), blockfoo); + + var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), blockbar); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo); + expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getIncTestTast() { + Program expectedAst = getEmptyProgram("IncTest"); + var iv = new InstVar(new ReferenceType("java/io/PrintStream"), + new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out"); + iv.setAccessModifier(AccessModifier.PUBLIC_STATIC); + Block forBlock = getBlock(new MethodCall(new BaseType(Primitives.VOID), + iv, + "println", + getArguments(new LocalOrFieldVar(new BaseType(Primitives.INT), "i")))); + Block fooblock = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)), + new Binary(new BaseType(Primitives.BOOL), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + new IntegerExpr(10), + Operator.LESS), + new CrementStmtExpr(new BaseType(Primitives.INT), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + Operator.INCSUF), + forBlock)); + Block barblock = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)), + new Binary(new BaseType(Primitives.BOOL), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + new IntegerExpr(10), + Operator.LESS), + new CrementStmtExpr(new BaseType(Primitives.INT), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + Operator.INCPRE), + forBlock)); + var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), fooblock); + var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), barblock); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo); + expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getDecTestAst() { + Program expectedAst = getEmptyProgram("DecTest"); + Block forBlock = getBlock(new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println", + getArguments(new LocalOrFieldVar("i")))); + Block blockfoo = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)), + new Binary(new LocalOrFieldVar("i"), new IntegerExpr(0), + Operator.GREATER), + new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.DECSUF), + forBlock)); + + Block blockbar = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)), + new Binary(new LocalOrFieldVar("i"), new IntegerExpr(0), + Operator.GREATER), + new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.DECPRE), + forBlock)); + + var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), blockfoo); + + var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), blockbar); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo); + expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + + public static Program getDecTestTast() { + Program expectedAst = getEmptyProgram("DecTest"); + var iv = new InstVar(new ReferenceType("java/io/PrintStream"), + new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out"); + iv.setAccessModifier(AccessModifier.PUBLIC_STATIC); + Block forBlock = getBlock(new MethodCall(new BaseType(Primitives.VOID), + iv, + "println", + getArguments(new LocalOrFieldVar(new BaseType(Primitives.INT), "i")))); + Block fooblock = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)), + new Binary(new BaseType(Primitives.BOOL), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + new IntegerExpr(0), + Operator.GREATER), + new CrementStmtExpr(new BaseType(Primitives.INT), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + Operator.DECSUF), + forBlock)); + Block barblock = getBlock( + new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)), + new Binary(new BaseType(Primitives.BOOL), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + new IntegerExpr(0), + Operator.GREATER), + new CrementStmtExpr(new BaseType(Primitives.INT), + new LocalOrFieldVar(new BaseType(Primitives.INT), "i"), + Operator.DECPRE), + forBlock)); + var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), fooblock); + var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), barblock); + + expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo); + expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar); + + expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl()); + + return expectedAst; + } + +} diff --git a/src/test/java/Helper/ReflectLoader.java b/src/test/java/Helper/ReflectLoader.java new file mode 100644 index 0000000..4570553 --- /dev/null +++ b/src/test/java/Helper/ReflectLoader.java @@ -0,0 +1,104 @@ +package Helper; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import common.Compiler; +import syntaxtree.structure.Program; + +public class ReflectLoader extends ClassLoader { + private byte[] byteCode; + private Map byteCodes; + private Map> classes = new HashMap<>(); + + public ReflectLoader(byte[] byteCode) { + byteCodes = new HashMap<>(); + this.byteCode = byteCode; + } + + public ReflectLoader(String fileName) { + Program program = Resources.getProgram(fileName); + Program tast = Compiler.getFactory().getTastAdapter().getTast(program); + var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast); + this.byteCodes = bc; + } + + /** + * @param fileName + * @param className + * @return Class + * @throws Exception + */ + public static Class getClass(String fileName, String className) throws Exception { + ReflectLoader loader = new ReflectLoader(fileName); + return loader.findClass(className); + } + + public ReflectLoader(Map byteCodes) { + this.byteCodes = byteCodes; + } + + /** + * @param name + * @return Class + */ + @Override + public Class findClass(String name) { + if (!byteCodes.containsKey(name)) { + if (byteCode != null) { + byteCodes.put(name, byteCode); + byteCode = null; + } else { + return null; + } + } + if (classes.containsKey(name)) { + return classes.get(name); + } else { + Class clazz = defineClass(name, byteCodes.get(name), 0, byteCodes.get(name).length); + classes.put(name, clazz); + return clazz; + } + } + + /** + * @param className + * @param method + * @param parameterTypes + * @return Method + * @throws NoSuchMethodException + */ + public Method getMethod(String className, String method, Class... parameterTypes) throws NoSuchMethodException { + Method method1 = findClass(className).getDeclaredMethod(method, parameterTypes); + method1.setAccessible(true); + return method1; + } + + /** + * @param className + * @param field + * @return Field + * @throws NoSuchFieldException + */ + public Field getField(String className, String field) throws NoSuchFieldException { + Field field1 = findClass(className).getDeclaredField(field); + field1.setAccessible(true); + return field1; + } + + /** + * @param classname + * @param parameterTyped + * @return Constructor + * @throws NoSuchMethodException + */ + public Constructor getConstructor(String classname, Class... parameterTyped) throws NoSuchMethodException { + Constructor constructor = findClass(classname).getDeclaredConstructor(parameterTyped); + constructor.setAccessible(true); + return constructor; + } + +} diff --git a/src/test/java/Helper/Resources.java b/src/test/java/Helper/Resources.java new file mode 100644 index 0000000..4ab9584 --- /dev/null +++ b/src/test/java/Helper/Resources.java @@ -0,0 +1,42 @@ +package Helper; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + +import common.Compiler; +import syntaxtree.structure.Program; + +public class Resources { + + /** + * @param fileName + * @return Program + */ + public static Program getProgram(String fileName) { + return Compiler.getFactory().getAstAdapter().getAst(getFileAsStream(fileName)); + } + + /** + * @param fileName + * @return InputStream + */ + public static InputStream getFileAsStream(String fileName) { + ClassLoader classLoader = Resources.class.getClassLoader(); + File file = new File(classLoader.getResource(fileName).getFile()); + assertNotNull(file); + try { + return new FileInputStream(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + fail(); + } + + throw new IllegalStateException(); + } + +} diff --git a/src/test/java/TAST/TestRunner.java b/src/test/java/TAST/TestRunner.java new file mode 100644 index 0000000..31deab6 --- /dev/null +++ b/src/test/java/TAST/TestRunner.java @@ -0,0 +1,324 @@ +package TAST; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.FileNotFoundException; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import Helper.MockGenerator; +import Helper.Resources; +import common.AccessModifier; +import common.BaseType; +import common.Compiler; +import common.Primitives; +import common.PrintableVector; +import semantic.exceptions.SemanticError; +import syntaxtree.structure.ClassDecl; +import syntaxtree.structure.ConstructorDecl; +import syntaxtree.structure.FieldDecl; +import syntaxtree.structure.Program; + +@DisplayName("Typed Abstract Syntax Generation") +public class TestRunner { + + /** + * @throws FileNotFoundException + */ + @Test + @DisplayName("EmptyClass") + void emptyClass() throws FileNotFoundException { + + ClassDecl emptyClass = new ClassDecl("EmptyClass", new PrintableVector<>(), new PrintableVector<>(), + new PrintableVector<>()); + PrintableVector classDecls = new PrintableVector<>(); + classDecls.add(emptyClass); + var ast = new Program(classDecls); + var tast = ast; + + var generatedTast = Compiler.getFactory().getTastAdapter().getTast(ast); + + assertEquals(tast, generatedTast); + + } + + @Test + @DisplayName("ClassFields") + void classFields() { + + Program expectedAst = MockGenerator.getEmptyProgram("ClassFields"); + + FieldDecl autoAccess = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), + "autoAccess"); + FieldDecl privateField = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "private"); + FieldDecl publicField = new FieldDecl(AccessModifier.PUBLIC, new BaseType(Primitives.INT), "public"); + FieldDecl protectedField = new FieldDecl(AccessModifier.PROTECTED, new BaseType(Primitives.INT), "protected"); + + PrintableVector fields = expectedAst.getClasses().firstElement().getFieldDelcarations(); + fields.add(autoAccess); + fields.add(privateField); + fields.add(publicField); + fields.add(protectedField); + + var generatedAst = Compiler.getFactory().getTastAdapter().getTast(expectedAst); + + assertEquals(expectedAst, generatedAst); + } + + @Test + @DisplayName("ClassField without AccessModifier") + void classFieldWithoutAccessModifier() { + + Program expectedTast = MockGenerator.getAutoClassFieldTast(); + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(expectedTast); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("EmptyClassWithConstructor") + void emptyClassWithConstructor() { + PrintableVector constructors = new PrintableVector<>(); + constructors.add(new ConstructorDecl()); + ClassDecl classDecl = new ClassDecl("EmptyClassWithConstructor", new PrintableVector<>(), constructors, + new PrintableVector<>()); + PrintableVector classDecls = new PrintableVector<>(); + classDecls.add(classDecl); + var ast = new Program(classDecls); + + var generatedTast = Compiler.getFactory().getTastAdapter().getTast(ast); + var tast = ast; + assertEquals(tast, generatedTast); + + } + + @Test + @DisplayName("Constructor With Parameters") + void constructorWithParameters() { + Program generatedTast = Compiler.getFactory().getTastAdapter() + .getTast(MockGenerator.getConstructorParameterAst()); + Program expectedTast = MockGenerator.getConstructorParameterTast(); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("Constructor With this. assign body") + void constructorWithThisAssignBody() { + Program generatedTast = Compiler.getFactory().getTastAdapter() + .getTast(MockGenerator.getConstructorThisDotAst()); + Program expectedTast = MockGenerator.getConstructorThisDotTast(); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("VoidMethod") + void voidMethod() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getVoidMethodAst()); + + Program expectedTast = MockGenerator.getVoidMethodTast(); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("RealConstructor") + void realConstructor() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getRealConstructorAst()); + + Program expectedTast = MockGenerator.getRealConstructorTast(); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("MethodCall") + void methodCall() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getMethodCallAst()); + + Program expectedTast = MockGenerator.getMethodCallTast(); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("MultipleFields") + @Tag("expectfail") + void multipleFields() { + Program program = Resources.getProgram("FailTests/MultiFieldDecl.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("MismatchingReturnType") + @Tag("expectfail") + void mismatchingReturnType() { + Program program = Resources.getProgram("FailTests/MismatchingReturnType.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("WhileTest") + void whileTest() { + Program program = Resources.getProgram("SimpleTests/WhileTest.java"); + Compiler.getFactory().getTastAdapter().getTast(program); + + } + + @Test + @DisplayName("WhileFailTest") + void whileFailTest() { + Program program = Resources.getProgram("FailTests/WhileBool.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("ScopeFailTest") + void scopeFailTest() { + Program program = Resources.getProgram("FailTests/ScopeTest.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("AssignFailTest") + void assignFailTest() { + Program program = Resources.getProgram("FailTests/AssignWrongType.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("LocalVarDeclaration with wrong init-Type") + void localVarDeclInitFail() { + Program program = Resources.getProgram("FailTests/LocalVarWrongInit.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("ExplicitNullAssign") + void explicitNullAssign() { + Program program = Resources.getProgram("SimpleTests/ExplicitNullAssign.java"); + Compiler.getFactory().getTastAdapter().getTast(program); + } + + @Test + @DisplayName("SelfReference") + void selfReference() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getSelfReferenceAst()); + + Program expectedTast = MockGenerator.getSelfReferenceTast(); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("SystemOutPrintln-Test") + void systemOutPrintlnTest() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getSystemOutPrintlnAst()); + Program expectedTast = MockGenerator.getSystemOutPrintlnTast(); + + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("FieldAccessTests") + void fieldAccessTests() { + Program program = Resources.getProgram("FailTests/FieldAccessTests.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("MethodAccessTests") + void methodAccessTests() { + Program program = Resources.getProgram("FailTests/MethodAccessTests.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("DuplicateMethod") + void duplicateMethod() { + Program program = Resources.getProgram("FailTests/DuplicateMethod.java"); + System.err.print(assertThrows( + SemanticError.class, + () -> Compiler.getFactory().getTastAdapter().getTast(program), + "Expected SemanticError to be thrown").getMessage()); + + } + + @Test + @DisplayName("SystemOutPrintln-String-Test") + void systemOutPrintlnStringTest() { + Program generatedTast = Compiler.getFactory().getTastAdapter() + .getTast(MockGenerator.getSystemOutPrintStringAst()); + Program expectedTast = MockGenerator.getSystemOutPrintStringTast(); + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("MainMethodTest") + void mainMethodTest() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getMainMethodTestAst()); + Program expectedTast = MockGenerator.getMainMethodTestTast(); + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("ForLoopTest") + void forLoopTest() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getForTestAst()); + Program expectedTast = MockGenerator.getForTestTast(); + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("IncTest") + void incTest() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getIncTestAst()); + Program expectedTast = MockGenerator.getIncTestTast(); + assertEquals(expectedTast, generatedTast); + } + + @Test + @DisplayName("DecTest") + void decTest() { + Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getDecTestAst()); + Program expectedTast = MockGenerator.getDecTestTast(); + assertEquals(expectedTast, generatedTast); + } + +} diff --git a/src/test/java/TestSuite.java b/src/test/java/TestSuite.java new file mode 100644 index 0000000..82821d8 --- /dev/null +++ b/src/test/java/TestSuite.java @@ -0,0 +1,10 @@ +import org.junit.platform.suite.api.SelectClasses; +import org.junit.platform.suite.api.Suite; +import org.junit.platform.suite.api.SuiteDisplayName; + +@SelectClasses({AST.TestRunner.class, TAST.TestRunner.class, CodeGen.TestRunner.class, All.TestRunner.class}) + +@Suite +@SuiteDisplayName("House of Compiler") +public class TestSuite { +} diff --git a/src/test/java/initialTest/initialTest.java b/src/test/java/initialTest/initialTest.java new file mode 100644 index 0000000..8883cf2 --- /dev/null +++ b/src/test/java/initialTest/initialTest.java @@ -0,0 +1,12 @@ +package initialTest; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class initialTest { + + @Test + public void testSomething() { + //MyClass myClass = new MyClass(); + assertEquals(1, 1); + } +} diff --git a/src/test/resources/Comments.java b/src/test/resources/Comments.java new file mode 100644 index 0000000..18610ef --- /dev/null +++ b/src/test/resources/Comments.java @@ -0,0 +1,19 @@ +class Comments{ + + private int lorem; + + // One Line Comment!!!!! IGNORE THAT + + private boolean ipsum; + + /* + MULT-LINE COMMENT! IGNORE THIS + */ + + /** + * + * Should be ignored + * + */ + +} \ No newline at end of file diff --git a/src/test/resources/EmptyClassWithConstructor.java b/src/test/resources/EmptyClassWithConstructor.java new file mode 100644 index 0000000..257b084 --- /dev/null +++ b/src/test/resources/EmptyClassWithConstructor.java @@ -0,0 +1,7 @@ +class EmptyClassWithConstructor{ + + public EmptyClassWithConstructor(){ + + } + +} \ No newline at end of file diff --git a/src/test/resources/EmtpyClass.java b/src/test/resources/EmtpyClass.java new file mode 100644 index 0000000..d8a3350 --- /dev/null +++ b/src/test/resources/EmtpyClass.java @@ -0,0 +1 @@ +class EmptyClass{} \ No newline at end of file diff --git a/src/test/resources/FailTests/AssignWrongType.java b/src/test/resources/FailTests/AssignWrongType.java new file mode 100644 index 0000000..e9dfd23 --- /dev/null +++ b/src/test/resources/FailTests/AssignWrongType.java @@ -0,0 +1,11 @@ +package FailTests; + +class AssignWrongType { + + int test() { + int x = 1; + x = true; + return x; + } + +} diff --git a/src/test/resources/FailTests/BoolAssignedInt.java b/src/test/resources/FailTests/BoolAssignedInt.java new file mode 100644 index 0000000..6e022e4 --- /dev/null +++ b/src/test/resources/FailTests/BoolAssignedInt.java @@ -0,0 +1,9 @@ +package FailTests; + +class BoolAssignedInt { + + void foo() { + boolean bool = 10; + } + +} diff --git a/src/test/resources/FailTests/DivideByZero.java b/src/test/resources/FailTests/DivideByZero.java new file mode 100644 index 0000000..04cdeef --- /dev/null +++ b/src/test/resources/FailTests/DivideByZero.java @@ -0,0 +1,9 @@ +package FailTests; + +class DivideByZero { + + public static void main(String[] args) { + int a = 1 / 0; + } + +} diff --git a/src/test/resources/FailTests/DuplicateFieldDeclaration.java b/src/test/resources/FailTests/DuplicateFieldDeclaration.java new file mode 100644 index 0000000..20daca4 --- /dev/null +++ b/src/test/resources/FailTests/DuplicateFieldDeclaration.java @@ -0,0 +1,6 @@ +package FailTests; + +class DuplicateFieldDeclaration { + char i; + int i; +} diff --git a/src/test/resources/FailTests/DuplicateMethod.java b/src/test/resources/FailTests/DuplicateMethod.java new file mode 100644 index 0000000..5ca1f3e --- /dev/null +++ b/src/test/resources/FailTests/DuplicateMethod.java @@ -0,0 +1,9 @@ +package FailTests; + +class DuplicateMethod { + + public void test() { } + + public void test() { } + +} diff --git a/src/test/resources/FailTests/FaultyForLoop.java b/src/test/resources/FailTests/FaultyForLoop.java new file mode 100644 index 0000000..17d5486 --- /dev/null +++ b/src/test/resources/FailTests/FaultyForLoop.java @@ -0,0 +1,11 @@ +package FailTests; + +class FaultyForLoop { + int foo() { + int x; + for (x == true; ;) { + return 4324; + } + + } +} \ No newline at end of file diff --git a/src/test/resources/FailTests/MethodAccessTests.java b/src/test/resources/FailTests/MethodAccessTests.java new file mode 100644 index 0000000..6bba4fa --- /dev/null +++ b/src/test/resources/FailTests/MethodAccessTests.java @@ -0,0 +1,18 @@ +package FailTests; + +class MethodAccessTests { + + void MethodAccesTestMethod() { + DontTouchMe clazz = new DontTouchMe(); + DontTouchMeMethod(); + } + +} + +class DontTouchMe { + + public void DontTouchMeMethod() { + + } + +} diff --git a/src/test/resources/FailTests/MismatchingReturnType.java b/src/test/resources/FailTests/MismatchingReturnType.java new file mode 100644 index 0000000..25bbb80 --- /dev/null +++ b/src/test/resources/FailTests/MismatchingReturnType.java @@ -0,0 +1,9 @@ +package FailTests; + +class MismatchingReturnType { + + char foo(){ + return 69; + } + +} diff --git a/src/test/resources/FailTests/ScopeTest.java b/src/test/resources/FailTests/ScopeTest.java new file mode 100644 index 0000000..7f59de1 --- /dev/null +++ b/src/test/resources/FailTests/ScopeTest.java @@ -0,0 +1,23 @@ +package FailTests; + +class ScopeTest { + + int returnDoesntExist() { + int x = 1; + { + int y = 1; + } + return y; + } + + int varInMethodDoesntExist() { + int x = 1; + { + int y = 1; + int x = 2; + } + int y = 1; + return y; + } + +} diff --git a/src/test/resources/IntegerWrapper.java b/src/test/resources/IntegerWrapper.java new file mode 100644 index 0000000..b6c213f --- /dev/null +++ b/src/test/resources/IntegerWrapper.java @@ -0,0 +1,20 @@ +class IntegerWrapper{ + + public int i; + + public IntegerWrapper(){ + this.i = 0; + } + + public IntegerWrapper(int i) { + this.i = i; + } + + public IntegerWrapper add(IntegerWrapper j){ return new IntegerWrapper(j.i+this.i);} + + public IntegerWrapper sub(IntegerWrapper j){ return new IntegerWrapper(j.i-this.i);} + + public boolean equals(IntegerWrapper j){return j.i == this.i;} + + +} \ No newline at end of file diff --git a/src/test/resources/Integration/Dijkstra.java b/src/test/resources/Integration/Dijkstra.java new file mode 100644 index 0000000..2844c29 --- /dev/null +++ b/src/test/resources/Integration/Dijkstra.java @@ -0,0 +1,366 @@ +package Integration; + +class Dijkstra { + + void main() { + Vertex v1 = new Vertex(1); + Graph g = new Graph(v1); + Vertex v2 = new Vertex(2); + Vertex v3 = new Vertex(3); + Vertex v4 = new Vertex(4); + Vertex v5 = new Vertex(5); + Vertex v6 = new Vertex(6); + Vertex v7 = new Vertex(7); + Vertex v8 = new Vertex(8); + Vertex v9 = new Vertex(9); + Vertex v10 = new Vertex(10); + Vertex v11 = new Vertex(11); + Vertex v12 = new Vertex(12); + + g.addVertex(v2); + g.addVertex(v4); + g.addVertex(v3); + g.addVertex(v5); + g.addVertex(v6); + g.addVertex(v7); + g.addVertex(v8); + g.addVertex(v9); + g.addVertex(v10); + g.addVertex(v11); + g.addVertex(v12); + + g.addEdge(v1, v2); // A-B + g.addEdge(v1, v5); // A-E + g.addEdge(v1, v10); // A-J + + g.addEdge(v2, v3); // B + g.addEdge(v2, v7); + + g.addEdge(v3, v4); // C + g.addEdge(v3, v5); + + g.addEdge(v4, v8); // D + + g.addEdge(v5, v6); // E + g.addEdge(v5, v9); + + g.addEdge(v6, v7); // F + + g.addEdge(v7, v8); // G + + g.addEdge(v8, v12); // H + g.addEdge(v8, v10); + + g.addEdge(v9, v10); // I + + g.addEdge(v10, v11); // J + + g.addEdge(v11, v12); // K + + g.getShortestPath(v1, v8); + g.getShortestPath(v1, v12); + g.getShortestPath(v4, v12); + g.getShortestPath(v4, v9); + g.getShortestPath(v6, v9); + + } + +} + +class Graph { + + private VertexSet vertexList; + + public Graph(VertexSet vertexList) { + this.vertexList = vertexList; + } + + public Graph(Vertex v1) { + this.vertexList = new VertexSet(v1); + } + + public VertexSet getVertexSet() { + return vertexList; + } + + public void addVertex(Vertex vertex) { + vertexList.add(vertex); + } + + public void addEdge(Vertex vertex1, Vertex vertex2) { + vertex1.addAdjancey(vertex2); + vertex2.addAdjancey(vertex1); + } + + public void getShortestPath(Vertex source, Vertex destination) { + VertexSet calcList = vertexList.copy(); + calcList.get(source.id).setDistance(0); + calcList.get(source.id).setPrevious(null); + Vertex current = calcList.get(source.id); + + // Fill the list with the distances + while (current != null) { + current.setVisited(true); + // Search for every adjacent vertex + VertexSet currentAdjanceyList = current.getAdjanceyList(); + if (currentAdjanceyList != null) { + for (int i = 0; i < currentAdjanceyList.size(); i = i + 1) { + Vertex adjancey = currentAdjanceyList.getFromIndex(i); + adjancey = calcList.get(adjancey.id); + if ((adjancey != null) && !adjancey.isVisited()) { + int distance = current.getDistance() + 1; + if (distance < adjancey.getDistance()) { + adjancey.setDistance(distance); + adjancey.setPrevious(current); + } + } + + } + } else { + } + current = calcList.getNextSmallestUnvisited(); + } + Vertex previous = calcList.get(destination.id); + if (previous == null) { + + return; + } + IntStack path = new IntStack(previous.id); + previous = previous.getPrevious(); + while (previous != null) { + path.push(previous.id); + previous = previous.getPrevious(); + } + +} + +class Vertex { + + public int id; + public VertexSet adjanceyList; + public int distance; + public Vertex previous; + public boolean visited; + + public Vertex(int id) { + this.id = id; + } + + public void setDistance(int distance) { + this.distance = distance; + } + + public void setPrevious(Vertex previous) { + this.previous = previous; + } + + public void setVisited(boolean visited) { + this.visited = visited; + } + + public int getDistance() { + return distance; + } + + public VertexSet getAdjanceyList() { + return adjanceyList; + } + + public Vertex getPrevious() { + return previous; + } + + public boolean isVisited() { + return visited; + } + + public Vertex(Vertex vertex) { + this.id = vertex.id; + this.adjanceyList = vertex.adjanceyList; + this.distance = 10000; // No infinity so... + this.previous = null; + this.visited = false; + } + + public void addAdjancey(Vertex v) { + if (adjanceyList == null) { + adjanceyList = new VertexSet(v); + } else { + adjanceyList.add(v); + } + } + + public Vertex copy() { + return new Vertex(this); + } + + public boolean isDirect(Vertex v) { + return adjanceyList.contains(v); + } + +} + +class VertexSet { + + private Vertex vertex; + private VertexSet next; + + public VertexSet(Vertex vertex) { + this.vertex = vertex; + this.next = null; + } + + public int size() { + int size = 1; + VertexSet v = next; + while (v != null) { + size = size + 1; + v = v.next; + } + return size; + } + + public VertexSet(VertexSet vertexList) { + this.vertex = vertexList.vertex.copy(); + if (vertexList.next != null) { + this.next = vertexList.next.copy(); + } else { + this.next = null; + } + } + + public Vertex getNextSmallestUnvisited() { + VertexSet v = this.getUnvisited(); + if (v == null) { + return null; + } else { + return v.getSmallestDistance(); + } + } + + public Vertex getSmallestDistance() { + Vertex smallest = vertex; + VertexSet v = next; + while (v != null) { + if (v.vertex.getDistance() < smallest.getDistance()) { + smallest = v.vertex; + } + v = v.next; + } + return smallest; + } + + public VertexSet getUnvisited() { + VertexSet unvisited = null; + + VertexSet current = this; + while (current != null) { + if (!current.vertex.isVisited()) { + if (unvisited == null) { + unvisited = new VertexSet(current.vertex); + } else { + unvisited.add(current.vertex); + } + } + current = current.next; + } + + return unvisited; + } + + public Vertex getFromIndex(int i) { + if (i == 0) { + return vertex; + } else { + return next.getFromIndex(i - 1); + } + } + + public void remove(Vertex v) { + if (vertex.id == v.id) { + vertex = next.vertex; + next = next.next; + } else { + if (next != null) { + next.remove(v); + } + } + } + + public boolean hasUnvisited() { + if (next != null) { + return next.hasUnvisited(); + } else { + return vertex.isVisited(); + } + } + + public VertexSet copy() { + return new VertexSet(this); + } + + public boolean contains(Vertex v) { + if (vertex.id == v.id) { + return true; + } + if (next == null) { + return false; + } + return next.contains(v); + } + + public void add(Vertex vertex) { + if (this.next == null) { + this.next = new VertexSet(vertex); + } else { + this.next.add(vertex); + } + } + + public Vertex get(int index) { + if ((vertex == null)) { + return null; + } else if (vertex.id == index) { + return this.vertex; + } else if (next != null) { + return this.next.get(index); + } else { + return null; + } + } + +} + +class IntStack { + + public int c; + public IntStack next; + public int size; + + public IntStack(int c) { + this.size = 1; + this.c = c; + this.next = null; + } + + public void push(int c) { + size = size + 1; + if (this.next == null) { + this.next = new IntStack(c); + } else { + this.next.push(c); + } + } + + public int pop() { + size = size - 1; + if (this.next.next == null) { + int temp = this.next.c; + this.next = null; + return temp; + } else { + return this.next.pop(); + } + } + +} \ No newline at end of file diff --git a/src/test/resources/Integration/LOFOI.java b/src/test/resources/Integration/LOFOI.java new file mode 100644 index 0000000..fb77773 --- /dev/null +++ b/src/test/resources/Integration/LOFOI.java @@ -0,0 +1,23 @@ +package Integration; + +// Local or Field or Imported var Test +class LOFOI { + + // This test is to determine wheter the order of finding the vars is correct + + int System; + + public LOFOI() { + System = 10; + } + + public int foo() { + int System = 20; + return System; + } + + public int bar() { + return System; + } + +} diff --git a/src/test/resources/Integration/LinkedList.java b/src/test/resources/Integration/LinkedList.java new file mode 100644 index 0000000..7289e9b --- /dev/null +++ b/src/test/resources/Integration/LinkedList.java @@ -0,0 +1,80 @@ +package Integration; + +class LinkedList { + + public int value; + public LinkedList next; + public int size; + + public LinkedList(int value) { + this.value = value; + size = 1; + } + + public int get() { + return value; + } + + public void set(int value) { + this.value = value; + } + + public LinkedList getNode(int i) { + if (i == 0) { + return this; + } else { + return this.next.getNode(i - 1); + } + } + + public void swap(int i, int j) { + int temp = getNode(i).get(); + getNode(i).set(getNode(j).get()); + getNode(j).set(temp); + } + + public void add(int value) { + size = size + 1; + if (this.next == null) { + this.next = new LinkedList(value); + + } else { + this.next.add(value); + } + } + +} + +class Executer { + + void foo() { + LinkedList list = new LinkedList(3); + list.add(2); + list.add(7); + list.add(5); + list.add(1); + list.add(8); + list.add(6); + list.add(4); + } + + LinkedList sort(LinkedList list) { + + int i = 0; + int j = 0; + while (i < list.size - 1) { + j = 0; + while (j < list.size - 1) { + if (list.getNode(j).get() > list.getNode(j + 1).get()) { + list.swap(j, j + 1); + } + j = j + 1; + } + i = i + 1; + } + + return list; + + } + +} \ No newline at end of file diff --git a/src/test/resources/Integration/Stack.java b/src/test/resources/Integration/Stack.java new file mode 100644 index 0000000..cb3ba1c --- /dev/null +++ b/src/test/resources/Integration/Stack.java @@ -0,0 +1,55 @@ +package Integration; + +class Stack { + + private int value; + private Stack next; + public int size; + + public Stack(int value) { + this.value = value; + size = 1; + } + + public int peek(int i) { + if (i == 0) { + return this.value; + } else { + return this.next.peek(i - 1); + } + } + + public void push(int value) { + size = size + 1; + if (this.next == null) { + this.next = new Stack(value); + + } else { + this.next.push(value); + } + } + + public int pop() { + size = size - 1; + if (this.next == null) { + return this.value; + } else { + return this.next.pop(); + } + } + +} + +class Executer { + + void foo() { + Stack stack = new Stack(1); + stack.push(2); + stack.push(3); + stack.push(4); + stack.push(5); + stack.push(6); + + } + +} \ No newline at end of file diff --git a/src/test/resources/Integration/StringList.java b/src/test/resources/Integration/StringList.java new file mode 100644 index 0000000..2c08950 --- /dev/null +++ b/src/test/resources/Integration/StringList.java @@ -0,0 +1,48 @@ +package Integration; + +class StringList { + + private char value; + private StringList next; + + public StringList() { + } + + public StringList(char c) { + this.value = c; + } + + public char get(int i) { + if (i == 0) { + return this.value; + } else { + return next.get(i - 1); + } + } + + public void add(char value) { + if (next == null) { + next = new StringList(value); + } else { + next.add(value); + } + } + + public int length() { + if (this.next == null) { + return 1; + } else { + return 1 + next.length(); + } + } + + public void print() { + System.out.print(this.value); + if (this.next != null) { + next.print(); + } else { + System.out.println(); + } + } + +} diff --git a/src/test/resources/NotTested/BinaryTests.java b/src/test/resources/NotTested/BinaryTests.java new file mode 100644 index 0000000..57a30c5 --- /dev/null +++ b/src/test/resources/NotTested/BinaryTests.java @@ -0,0 +1,31 @@ +package NotTested; + +class BinaryTests{ + + private int a,b,c; + + public int pvs() { + return a+b*c; + } + + public int klammern() { + return a * (b+c); + } + + public int modulo(){ + return a%b; + } + + public boolean avo() { + return a || b && c; + } + + public boolean binary() { + return a == b && !c; + } + + public int endBoss() { + return (a%c) + b / (a * a) * a + a - (c-b/a) && b-c; + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/CharWrapper.java b/src/test/resources/NotTested/CharWrapper.java new file mode 100644 index 0000000..a18da95 --- /dev/null +++ b/src/test/resources/NotTested/CharWrapper.java @@ -0,0 +1,19 @@ +package NotTested; + +class CharWrapper{ + + private char c; + + public CharWrapper(char c){ + this.c = c; + } + + public void setChar(char c){ + this.c = c; + } + + public char getChar(){ + return this.c; + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/ClassStress.java b/src/test/resources/NotTested/ClassStress.java new file mode 100644 index 0000000..5d189b4 --- /dev/null +++ b/src/test/resources/NotTested/ClassStress.java @@ -0,0 +1,33 @@ +package NotTested; + +class ClassStress{ + + private int x = 100; + + public ClassStress(int x){ + this.x = x*2%2+x/10 % 100*x; + } + +} + +class ClassStress2{ + + private int x = 2; + private boolean b = true; + private ClassStress cd = new ClassStress(3); + + public ClassStress2(int x, ClassStress cs, boolean b){ + this.x = x; + this.b = b; + this.cs = cs + } + + public ClassStress2(int x){ + this.x = x; + } + + public foo(){ + return new ClassStress2(1, new ClassStress(this.cd.x), this.b); + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/Fib.java b/src/test/resources/NotTested/Fib.java new file mode 100644 index 0000000..6b82c69 --- /dev/null +++ b/src/test/resources/NotTested/Fib.java @@ -0,0 +1,16 @@ +package NotTested; + +public class Fibonacci +{ + public int fibonacci(int n) + { + if (n <= 1) + { + return n; + } + else + { + return fibonacci(n-1) + fibonacci(n-2); + } + } +} \ No newline at end of file diff --git a/src/test/resources/NotTested/FibIntegerWrapper.java b/src/test/resources/NotTested/FibIntegerWrapper.java new file mode 100644 index 0000000..52c7ad8 --- /dev/null +++ b/src/test/resources/NotTested/FibIntegerWrapper.java @@ -0,0 +1,37 @@ +package NotTested; + +class IntegerWrapper{ + + public int i; + + public IntegerWrapper(int i){ + this.i = i; + } + + public IntegerWrapper(){ + this.i = 0; + } + + public IntegerWrapper Add(IntegerWrapper j){ return new IntegerWrapper(j.i+this.i)} + + public IntegerWrapper Sub(IntegerWrapper j){ return new IntegerWrapper(j.i-this.i)} + + public boolean equals(IntegerWrapper j){return j.i == this.i} + + +} + +class Fibonacci +{ + public IntegerWrapper fibonacci(IntegerWrapper n) + { + if (n.LessThan(new IntegerWrapper(2))) + { + return n; + } + else + { + return fibonacci(n.Sub(new IntegerWrapper(1))).Add(fibonacci(n.Sub(new IntegerWrapper(2)))); + } + } +} \ No newline at end of file diff --git a/src/test/resources/NotTested/IfTest.java b/src/test/resources/NotTested/IfTest.java new file mode 100644 index 0000000..053a1a8 --- /dev/null +++ b/src/test/resources/NotTested/IfTest.java @@ -0,0 +1,30 @@ +package NotTested; + +class IfTest{ + + public int ifTest(){ + if(true){ + return 1; + } + return 0; + } + + public int ifElseTest(){ + if(true){ + return 1; + }else{ + return 0; + } + } + + public int ifElseIfTest(){ + if(false){ + return 0; + }else if(true){ + return 1; + }else{ + return 0; + } + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/TwoClasses.java b/src/test/resources/NotTested/TwoClasses.java new file mode 100644 index 0000000..a3c7b3e --- /dev/null +++ b/src/test/resources/NotTested/TwoClasses.java @@ -0,0 +1,9 @@ +package NotTested; + +class TwoClasses{ + +} + +class TwoClasses{ + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/TwoClassesReferences.java b/src/test/resources/NotTested/TwoClassesReferences.java new file mode 100644 index 0000000..3055aba --- /dev/null +++ b/src/test/resources/NotTested/TwoClassesReferences.java @@ -0,0 +1,27 @@ +package NotTested; + +class TwoClassesFilled{ + + private int i; + public int a = 4; + + public TwoClassesFilled(int i){ + this.i = i; + } + +} + + +class TwoClassesFilled2{ + + private TwoClassesFilled a; + + public TwoClassesFilled2(int a){ + this.a = new TwoClassesFilled(a); + } + + public int get(){ + return this.a.a; + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/VoidMethod.java b/src/test/resources/NotTested/VoidMethod.java new file mode 100644 index 0000000..884726a --- /dev/null +++ b/src/test/resources/NotTested/VoidMethod.java @@ -0,0 +1,10 @@ +package NotTested; + +class VoidMethod{ + + + public void test(){ + + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/VoidMethodReturn.java b/src/test/resources/NotTested/VoidMethodReturn.java new file mode 100644 index 0000000..5c6f9d1 --- /dev/null +++ b/src/test/resources/NotTested/VoidMethodReturn.java @@ -0,0 +1,10 @@ +package NotTested; + +class VoidMethodReturn{ + + + public void test(){ + return; + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/WhileTest.java b/src/test/resources/NotTested/WhileTest.java new file mode 100644 index 0000000..dbb8b94 --- /dev/null +++ b/src/test/resources/NotTested/WhileTest.java @@ -0,0 +1,19 @@ +package NotTested; + +class WhileTest{ + + public void infinite(){ + while(true){} + } + + public int ten(){ + int i = 0; + int counter = 10; + while(counter>0){ + i = i+1; + counter = counter - 1; + } + return i; + } + +} \ No newline at end of file diff --git a/src/test/resources/NotTested/ggt.java b/src/test/resources/NotTested/ggt.java new file mode 100644 index 0000000..30847d0 --- /dev/null +++ b/src/test/resources/NotTested/ggt.java @@ -0,0 +1,25 @@ +package NotTested; + +class ggt{ + + private int a; + private int b; + + public ggt(int a, int b){ + this.a = a; + this.b = b; + } + + public int calculate(){ + int c; + int a = this.a; + int b = this.b; + while ( b != 0) { + c = a % b; + a = b; + b = c; + } + return a; + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/AndVorOr.java b/src/test/resources/SimpleTests/AndVorOr.java new file mode 100644 index 0000000..af4cb86 --- /dev/null +++ b/src/test/resources/SimpleTests/AndVorOr.java @@ -0,0 +1,13 @@ +package SimpleTests; + +class AndVorOr { + + boolean foo(boolean a, boolean b, boolean c) { + return a && b || c && b; + } + + boolean bar(boolean a, boolean b, boolean c) { + return a || b && c || b && c; + } + +} diff --git a/src/test/resources/SimpleTests/AutoAccessModifierField.java b/src/test/resources/SimpleTests/AutoAccessModifierField.java new file mode 100644 index 0000000..3acd3d3 --- /dev/null +++ b/src/test/resources/SimpleTests/AutoAccessModifierField.java @@ -0,0 +1,7 @@ +package SimpleTests; + +class AutoAccessModifierField { + + int autoAccess; + +} diff --git a/src/test/resources/SimpleTests/CharArgument.java b/src/test/resources/SimpleTests/CharArgument.java new file mode 100644 index 0000000..3cdaf65 --- /dev/null +++ b/src/test/resources/SimpleTests/CharArgument.java @@ -0,0 +1,15 @@ +package SimpleTests; + +class CharArgument { + + char c; + + public CharArgument() { + this.c = foo('a'); + } + + char foo(char c) { + return c; + } + +} diff --git a/src/test/resources/SimpleTests/ClassFields.java b/src/test/resources/SimpleTests/ClassFields.java new file mode 100644 index 0000000..85df092 --- /dev/null +++ b/src/test/resources/SimpleTests/ClassFields.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class ClassFields { + + private int privateAccess; + public int publicAccess; + protected int protectedAccess; + +} diff --git a/src/test/resources/SimpleTests/Comments.java b/src/test/resources/SimpleTests/Comments.java new file mode 100644 index 0000000..af041b0 --- /dev/null +++ b/src/test/resources/SimpleTests/Comments.java @@ -0,0 +1,21 @@ +package SimpleTests; + +class Comments{ + + private int lorem; + + // One Line Comment!!!!! IGNORE THAT + + private boolean ipsum; + + /* + MULT-LINE COMMENT! IGNORE THIS + */ + + /** + * + * Should be ignored + * + */ + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/ConditionalEvaluation.java b/src/test/resources/SimpleTests/ConditionalEvaluation.java new file mode 100644 index 0000000..2d38734 --- /dev/null +++ b/src/test/resources/SimpleTests/ConditionalEvaluation.java @@ -0,0 +1,16 @@ +package SimpleTests; + +class ConditionalEvaluation { + + boolean foo() { + System.out.println("foo"); + return true || bar(); + } + + boolean bar() { + System.out.println("bar"); + + return true && foo(); + } + +} diff --git a/src/test/resources/SimpleTests/ConstructorParams.java b/src/test/resources/SimpleTests/ConstructorParams.java new file mode 100644 index 0000000..519ab50 --- /dev/null +++ b/src/test/resources/SimpleTests/ConstructorParams.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class ConstructorParams { + + public ConstructorParams(int i) { + + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/ConstructorThisDot.java b/src/test/resources/SimpleTests/ConstructorThisDot.java new file mode 100644 index 0000000..f9a4b26 --- /dev/null +++ b/src/test/resources/SimpleTests/ConstructorThisDot.java @@ -0,0 +1,11 @@ +package SimpleTests; + +class ConstructorThisDot{ + + public int i; + + public ConstructorThisDot(){ + this.i = 5; + } + +} diff --git a/src/test/resources/SimpleTests/DecTest.java b/src/test/resources/SimpleTests/DecTest.java new file mode 100644 index 0000000..d589d41 --- /dev/null +++ b/src/test/resources/SimpleTests/DecTest.java @@ -0,0 +1,17 @@ +package SimpleTests; + +class DecTest { + + void foo() { + for (int i = 10; i > 0; i--) { + System.out.println(i); + } + } + + void bar() { + for (int i = 10; i > 0; --i) { + System.out.println(i); + } + } + +} diff --git a/src/test/resources/SimpleTests/DivMethod.java b/src/test/resources/SimpleTests/DivMethod.java new file mode 100644 index 0000000..80007ed --- /dev/null +++ b/src/test/resources/SimpleTests/DivMethod.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class DivMethod { + + int foo(int i){ + return i/i; + } + +} diff --git a/src/test/resources/SimpleTests/EmptyClass.java b/src/test/resources/SimpleTests/EmptyClass.java new file mode 100644 index 0000000..4bae0fa --- /dev/null +++ b/src/test/resources/SimpleTests/EmptyClass.java @@ -0,0 +1,3 @@ +package SimpleTests; + +class EmptyClass{} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/EmptyClassWithConstructor.java b/src/test/resources/SimpleTests/EmptyClassWithConstructor.java new file mode 100644 index 0000000..9d54fa8 --- /dev/null +++ b/src/test/resources/SimpleTests/EmptyClassWithConstructor.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class EmptyClassWithConstructor{ + + public EmptyClassWithConstructor(){ + + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/ExplicitNullAssign.java b/src/test/resources/SimpleTests/ExplicitNullAssign.java new file mode 100644 index 0000000..c56eca7 --- /dev/null +++ b/src/test/resources/SimpleTests/ExplicitNullAssign.java @@ -0,0 +1,10 @@ +package SimpleTests; + +class ExplicitNullAssign { + + ExplicitNullAssign e; + + void test() { + e = null; + } +} diff --git a/src/test/resources/SimpleTests/ExtendedNotTest.java b/src/test/resources/SimpleTests/ExtendedNotTest.java new file mode 100644 index 0000000..23fc8f2 --- /dev/null +++ b/src/test/resources/SimpleTests/ExtendedNotTest.java @@ -0,0 +1,18 @@ +package SimpleTests; + +class ExtendedNotTest { + + boolean notequal(int a, int b) { + return !(a == b); + } + + boolean multiple(boolean a, boolean b) { + return !(!a || b); + } + + boolean notWithAssigns(boolean a) { + boolean b = !a; + return !b; + } + +} diff --git a/src/test/resources/SimpleTests/FieldWithExpr.java b/src/test/resources/SimpleTests/FieldWithExpr.java new file mode 100644 index 0000000..e87be59 --- /dev/null +++ b/src/test/resources/SimpleTests/FieldWithExpr.java @@ -0,0 +1,7 @@ +package SimpleTests; + +class FieldWithExpr { + + public int x = 5; + +} diff --git a/src/test/resources/SimpleTests/ForTest.java b/src/test/resources/SimpleTests/ForTest.java new file mode 100644 index 0000000..87ba856 --- /dev/null +++ b/src/test/resources/SimpleTests/ForTest.java @@ -0,0 +1,11 @@ +package SimpleTests; + +class ForTest { + + void foo() { + for (int i = 0; i < 10; i = i + 1) { + System.out.println(i); + } + } + +} diff --git a/src/test/resources/SimpleTests/FourClasses.java b/src/test/resources/SimpleTests/FourClasses.java new file mode 100644 index 0000000..7cb84e0 --- /dev/null +++ b/src/test/resources/SimpleTests/FourClasses.java @@ -0,0 +1,63 @@ +package SimpleTests; + +class FourClasses { + + public int main(int i) { + Test t = new Test(i); + Test2 t2 = new Test2(t.y); + return t2.test.test3.getX(); + } + +} + +class Test { + private int x; + public int y; + public Test3 test3; + + public Test(int i) { + this.x = i; + this.y = 10; + this.test3 = new Test3(i * 2); + } + + public Test3 getTest3() { + return this.test3; + } + + public int getX() { + return this.x; + } +} + +class Test2 { + public Test test; + + public Test2(int i) { + this.test = new Test(i); + } + +} + +class Test3 { + public int x; + + private int y; + + public Test3(int i) { + this.x = i; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public void setY(int y) { + this.y = y; + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/FourClassesFieldAssign.java b/src/test/resources/SimpleTests/FourClassesFieldAssign.java new file mode 100644 index 0000000..e3b6e7f --- /dev/null +++ b/src/test/resources/SimpleTests/FourClassesFieldAssign.java @@ -0,0 +1,64 @@ +package SimpleTests; + +class FourClassesFieldAssign { + + public int fieldAssign(int i) { + Test t = new Test(i); + Test2 t2 = new Test2(t.y); + t2.test.test3.x = i; + return t2.test.test3.getX(); + } + +} + +class Test { + private int x; + public int y; + public Test3 test3; + + public Test(int i) { + this.x = i; + this.y = 10; + this.test3 = new Test3(i * 2); + } + + public Test3 getTest3() { + return this.test3; + } + + public int getX() { + return this.x; + } +} + +class Test2 { + public Test test; + + public Test2(int i) { + this.test = new Test(i); + } + +} + +class Test3 { + public int x; + + private int y; + + public Test3(int i) { + this.x = i; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public void setY(int y) { + this.y = y; + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/FourClassesSetter.java b/src/test/resources/SimpleTests/FourClassesSetter.java new file mode 100644 index 0000000..57909eb --- /dev/null +++ b/src/test/resources/SimpleTests/FourClassesSetter.java @@ -0,0 +1,65 @@ +package SimpleTests; + +class FourClassesSetter { + + public int setFieldTest(int i) { + Test t = new Test(i); + Test2 t2 = new Test2(t.y); + t2.test.test3.setY(i); + + return t2.test.test3.getY(); + } + +} + +class Test { + private int x; + public int y; + public Test3 test3; + + public Test(int i) { + this.x = i; + this.y = 10; + this.test3 = new Test3(i * 2); + } + + public Test3 getTest3() { + return this.test3; + } + + public int getX() { + return this.x; + } +} + +class Test2 { + public Test test; + + public Test2(int i) { + this.test = new Test(i); + } + +} + +class Test3 { + public int x; + + private int y; + + public Test3(int i) { + this.x = i; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public void setY(int y) { + this.y = y; + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/GetterFunction.java b/src/test/resources/SimpleTests/GetterFunction.java new file mode 100644 index 0000000..c501713 --- /dev/null +++ b/src/test/resources/SimpleTests/GetterFunction.java @@ -0,0 +1,15 @@ +package SimpleTests; + +class GetterFunction { + + private int i; + + public GetterFunction(int i) { + this.i = i; + } + + public int getI() { + return this.i; + } + +} diff --git a/src/test/resources/SimpleTests/IfConstructor.java b/src/test/resources/SimpleTests/IfConstructor.java new file mode 100644 index 0000000..dff598a --- /dev/null +++ b/src/test/resources/SimpleTests/IfConstructor.java @@ -0,0 +1,11 @@ +package SimpleTests; + +class IfConstructor { + + int i; + + public IfConstructor() { + i = 0; + } + +} diff --git a/src/test/resources/SimpleTests/IfElseIfStatement.java b/src/test/resources/SimpleTests/IfElseIfStatement.java new file mode 100644 index 0000000..66921a1 --- /dev/null +++ b/src/test/resources/SimpleTests/IfElseIfStatement.java @@ -0,0 +1,15 @@ +package SimpleTests; + +class IfElseIfStatement { + + int foo(int i) { + if (i == 1) { + return 10; + } else if (i == 2) { + return 40000; + } else { + return 42000; + } + } + +} diff --git a/src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java b/src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java new file mode 100644 index 0000000..a1ab5ce --- /dev/null +++ b/src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java @@ -0,0 +1,17 @@ +package SimpleTests; + +class IfElseIfStatementWithOneReturn { + + int foo(int i) { + int result = 0; + if (i == 1) { + result = 10; + } else if (i == 2) { + return 20; + } else { + result = 30; + } + return result; + } + +} diff --git a/src/test/resources/SimpleTests/IfElseIfStatementWithoutReturn.java b/src/test/resources/SimpleTests/IfElseIfStatementWithoutReturn.java new file mode 100644 index 0000000..846da0c --- /dev/null +++ b/src/test/resources/SimpleTests/IfElseIfStatementWithoutReturn.java @@ -0,0 +1,17 @@ +package SimpleTests; + +class IfElseIfStatementWithoutReturn { + + int foo(int i) { + int result; + if (i == 1) { + result = 10; + } else if (i == 2) { + result = 20; + } else { + result = 30; + } + return result; + } + +} diff --git a/src/test/resources/SimpleTests/IfElseStatement.java b/src/test/resources/SimpleTests/IfElseStatement.java new file mode 100644 index 0000000..0678843 --- /dev/null +++ b/src/test/resources/SimpleTests/IfElseStatement.java @@ -0,0 +1,13 @@ +package SimpleTests; + +class IfElseStatement { + + int foo(int i) { + if (i == 1) { + return 10; + } else { + return 100; + } + } + +} diff --git a/src/test/resources/SimpleTests/IfStatement.java b/src/test/resources/SimpleTests/IfStatement.java new file mode 100644 index 0000000..a13b32d --- /dev/null +++ b/src/test/resources/SimpleTests/IfStatement.java @@ -0,0 +1,12 @@ +package SimpleTests; + +class IfStatement { + + int foo(int i) { + if (i == 1) { + return 10; + } + return 100; + } + +} diff --git a/src/test/resources/SimpleTests/IncDecStressTest.java b/src/test/resources/SimpleTests/IncDecStressTest.java new file mode 100644 index 0000000..39a4354 --- /dev/null +++ b/src/test/resources/SimpleTests/IncDecStressTest.java @@ -0,0 +1,26 @@ +package SimpleTests; + +class IncDecStressTest { + + int incrementThenReturn(int i) { + return ++i; + } + + int returnThenIncrement(int i) { + return i++; + } + + int decrementThenReturn(int i) { + return --i; + } + + int returnThenDecrement(int i) { + return i--; + } + + int callInline(int i) { + i++; + return i; + } + +} diff --git a/src/test/resources/SimpleTests/IncTest.java b/src/test/resources/SimpleTests/IncTest.java new file mode 100644 index 0000000..711b53f --- /dev/null +++ b/src/test/resources/SimpleTests/IncTest.java @@ -0,0 +1,17 @@ +package SimpleTests; + +class IncTest { + + void foo() { + for (int i = 0; i < 10; i++) { + System.out.println(i); + } + } + + void bar() { + for (int i = 0; i < 10; ++i) { + System.out.println(i); + } + } + +} diff --git a/src/test/resources/SimpleTests/IntCompare.java b/src/test/resources/SimpleTests/IntCompare.java new file mode 100644 index 0000000..7f2e1c1 --- /dev/null +++ b/src/test/resources/SimpleTests/IntCompare.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class IntCompare { + + boolean eq(int a, int b) { + return a == b; + } + +} diff --git a/src/test/resources/SimpleTests/IntMethod.java b/src/test/resources/SimpleTests/IntMethod.java new file mode 100644 index 0000000..95a7ce7 --- /dev/null +++ b/src/test/resources/SimpleTests/IntMethod.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class IntMethod { + + int foo(){ + return 1; + } + +} diff --git a/src/test/resources/SimpleTests/KlammerVorPunkt.java b/src/test/resources/SimpleTests/KlammerVorPunkt.java new file mode 100644 index 0000000..4165f26 --- /dev/null +++ b/src/test/resources/SimpleTests/KlammerVorPunkt.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class KlammerVorPunkt { + + int foo(int a, int b, int c) { + return (b + c) * a; + } + +} diff --git a/src/test/resources/SimpleTests/MainMethodTest.java b/src/test/resources/SimpleTests/MainMethodTest.java new file mode 100644 index 0000000..892f7b9 --- /dev/null +++ b/src/test/resources/SimpleTests/MainMethodTest.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class MainMethodTest { + + public static void main(String[] args) { + System.out.println("maintest"); + } + +} diff --git a/src/test/resources/SimpleTests/MethodCall.java b/src/test/resources/SimpleTests/MethodCall.java new file mode 100644 index 0000000..f70d253 --- /dev/null +++ b/src/test/resources/SimpleTests/MethodCall.java @@ -0,0 +1,15 @@ +package SimpleTests; + +class MethodCall { + + int i; + + public MethodCall() { + this.i = foo(); + } + + int foo() { + return 1; + } + +} diff --git a/src/test/resources/SimpleTests/MethodCallParams.java b/src/test/resources/SimpleTests/MethodCallParams.java new file mode 100644 index 0000000..6d63a35 --- /dev/null +++ b/src/test/resources/SimpleTests/MethodCallParams.java @@ -0,0 +1,15 @@ +package SimpleTests; + +class MethodCallParams { + + int i; + + public MethodCallParams(int i) { + this.i = foo(i); + } + + int foo(int i) { + return i; + } + +} diff --git a/src/test/resources/SimpleTests/MethodCallStressTest.java b/src/test/resources/SimpleTests/MethodCallStressTest.java new file mode 100644 index 0000000..c2cdbe8 --- /dev/null +++ b/src/test/resources/SimpleTests/MethodCallStressTest.java @@ -0,0 +1,34 @@ +package SimpleTests; + +class MethodCallStressTest { + + void foo() { + int i = baz(); + while (i < 10) { + baz(); + bar(); + i = i + baz(); + } + + i = baz() - baz(); + i = baz() + baz(); + + if (boo()) { + i = baz(); + } else { + i = baz(); + } + } + + void bar() { + } + + int baz() { + return 1; + } + + boolean boo() { + return true; + } + +} diff --git a/src/test/resources/SimpleTests/MethodCallsInWhile.java b/src/test/resources/SimpleTests/MethodCallsInWhile.java new file mode 100644 index 0000000..458b1c7 --- /dev/null +++ b/src/test/resources/SimpleTests/MethodCallsInWhile.java @@ -0,0 +1,23 @@ +package SimpleTests; + +class MethodCallsInWhile { + + void main() { + int i = 0; + Test t = new Test(); + t.foo(); + while (i < 10) { + i = i + t.foo(); + t.foo(); // <-- This is causing the error + } + + } + +} + +class Test { + + int foo() { + return 1; + } +} diff --git a/src/test/resources/SimpleTests/MiniLinkedList.java b/src/test/resources/SimpleTests/MiniLinkedList.java new file mode 100644 index 0000000..2c3e08e --- /dev/null +++ b/src/test/resources/SimpleTests/MiniLinkedList.java @@ -0,0 +1,25 @@ +package SimpleTests; + +class MiniLinkedList { + + int main() { + LinkedList list = new LinkedList(1); + return 1; + } + +} + +class LinkedList { + + int x; + private LinkedList next; + + public LinkedList(int x) { + this.x = x; + } + + void add(int x) { + next = new LinkedList(x); + } + +} diff --git a/src/test/resources/SimpleTests/MinusMethod.java b/src/test/resources/SimpleTests/MinusMethod.java new file mode 100644 index 0000000..5c99fe5 --- /dev/null +++ b/src/test/resources/SimpleTests/MinusMethod.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class MinusMethod { + + int foo(int i){ + return i-i; + } + +} diff --git a/src/test/resources/SimpleTests/ModMethod.java b/src/test/resources/SimpleTests/ModMethod.java new file mode 100644 index 0000000..5c81d23 --- /dev/null +++ b/src/test/resources/SimpleTests/ModMethod.java @@ -0,0 +1,7 @@ +package SimpleTests; + +class ModMethod { + int foo(int i, int j) { + return i % j; + } +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/MulMethod.java b/src/test/resources/SimpleTests/MulMethod.java new file mode 100644 index 0000000..9d03138 --- /dev/null +++ b/src/test/resources/SimpleTests/MulMethod.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class MulMethod { + + int foo(int i){ + return i*i; + } + +} diff --git a/src/test/resources/SimpleTests/MultClasses.java b/src/test/resources/SimpleTests/MultClasses.java new file mode 100644 index 0000000..8a35cf4 --- /dev/null +++ b/src/test/resources/SimpleTests/MultClasses.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class MultClasses1 { + +} + +class MultClasses2 { + +} diff --git a/src/test/resources/SimpleTests/MultClassesReference.java b/src/test/resources/SimpleTests/MultClassesReference.java new file mode 100644 index 0000000..1b3d3ef --- /dev/null +++ b/src/test/resources/SimpleTests/MultClassesReference.java @@ -0,0 +1,26 @@ +package SimpleTests; + +class MultClassesReference1 { + + public int i; + + public MultClassesReference1(int i){ + this.i = i; + } + + +} + +class MultClassesReference2 { + + public MultClassesReference1 bar; + + public MultClassesReference2(int i){ + this.bar = new MultClassesReference1(i); + } + + int test(){ + return this.bar.i; + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/MultipleClassesMethodCalls.java b/src/test/resources/SimpleTests/MultipleClassesMethodCalls.java new file mode 100644 index 0000000..609159e --- /dev/null +++ b/src/test/resources/SimpleTests/MultipleClassesMethodCalls.java @@ -0,0 +1,22 @@ +package SimpleTests; + +class MultipleClassesMethodCalls { + + public int main(int i) { + Test t = new Test(i); + return t.getX(); + } + +} + +class Test { + public int x; + + public Test(int i) { + this.x = i; + } + + public int getX() { + return this.x; + } +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/MultipleClassesStress.java b/src/test/resources/SimpleTests/MultipleClassesStress.java new file mode 100644 index 0000000..a742183 --- /dev/null +++ b/src/test/resources/SimpleTests/MultipleClassesStress.java @@ -0,0 +1,24 @@ +package SimpleTests; + +class MultipleClassesStress { + + public int main(int i) { + Test t = new Test(i); + return t.getX() + t.y; + } + +} + +class Test { + private int x; + public int y; + + public Test(int i) { + this.x = i; + this.y = 10; + } + + public int getX() { + return this.x; + } +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/NotMethod.java b/src/test/resources/SimpleTests/NotMethod.java new file mode 100644 index 0000000..da989bc --- /dev/null +++ b/src/test/resources/SimpleTests/NotMethod.java @@ -0,0 +1,7 @@ +package SimpleTests; + +class NotMethod { + boolean foo(boolean i) { + return !i; + } +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/OperatorStacking.java b/src/test/resources/SimpleTests/OperatorStacking.java new file mode 100644 index 0000000..43a0123 --- /dev/null +++ b/src/test/resources/SimpleTests/OperatorStacking.java @@ -0,0 +1,21 @@ +package SimpleTests; + +class OperatorStacking { + + boolean foo() { + return true && false || true && true; // Should return true + } + + int bar() { + return 1 + 2 * 3 - 4 % 6; // Should return 3 + } + // 1 + 6 - 4 % 6 + // 7 - 4 % 6 + // 3 % 6 = 3 + + // 1 + 6 - 4 % 6 + // 1 + 6 - 4 + // 7-4 + // 3 + +} diff --git a/src/test/resources/SimpleTests/OperatorTest.java b/src/test/resources/SimpleTests/OperatorTest.java new file mode 100644 index 0000000..4d9e6e5 --- /dev/null +++ b/src/test/resources/SimpleTests/OperatorTest.java @@ -0,0 +1,86 @@ +package SimpleTests; + +class OperatorTest { + + public OperatorTest() { + } + + public int assign(int i) { + int returnValue; + returnValue = i; + return returnValue; + } + + public int plus(int x, int y) { + return x + y; + } + + public int minus(int x, int y) { + return x - y; + } + + public int mult(int x, int y) { + return x * y; + } + + public int div(int x, int y) { + return x / y; + } + + public int mod(int x, int y) { + return x % y; + } + + public boolean gt(int x, int y) { + return x > y; + } + + public boolean lt(int x, int y) { + return x < y; + } + + public boolean lte(int x, int y) { + return x <= y; + } + + public boolean gte(int x, int y) { + return x >= y; + } + + public boolean eq(int x, int y) { + return x == y; + } + + public boolean neq(int x, int y) { + return x != y; + } + + public boolean beq(boolean x, boolean y) { + return x == y; + } + + public boolean bneq(boolean x, boolean y) { + return x != y; + } + + public boolean and(boolean x, boolean y) { + return x && y; + } + + public boolean or(boolean x, boolean y) { + return x || y; + } + + public boolean not(boolean x) { + return !x; + } + + public boolean icEq(int x, char y) { + return x == y; + } + + public boolean icLt(int x, char y) { + return x < y; + } + +} \ No newline at end of file diff --git a/src/test/resources/SimpleTests/OverridingTest.java b/src/test/resources/SimpleTests/OverridingTest.java new file mode 100644 index 0000000..658de52 --- /dev/null +++ b/src/test/resources/SimpleTests/OverridingTest.java @@ -0,0 +1,32 @@ +package SimpleTests; + +class OverridingTest { + + // Test is used to determine wheter the Right method with the right types is + // called + + public int foo(int i) { + if (i < 5) { + return test(); + } else if (i < 10) { + return test(i); + } else { + return test(this); + } + } + + public int test() { + System.out.println(0); + return 0; + } + + public int test(int i) { + System.out.println(i); + return i; + } + + public int test(OverridingTest o) { + return 1337; + } + +} diff --git a/src/test/resources/SimpleTests/OverridingTestNull.java b/src/test/resources/SimpleTests/OverridingTestNull.java new file mode 100644 index 0000000..39b0df1 --- /dev/null +++ b/src/test/resources/SimpleTests/OverridingTestNull.java @@ -0,0 +1,35 @@ +package SimpleTests; + +class OverridingTestNull { + + // Test is used to determine wheter the Right method with the right types is + // called + + public int foo(int i) { + if (i < 5) { + return test(); + } else if (i < 10) { + return test(i); + } else if (i < 20) { + return test(this); + } else { + return test(null); + } + } + + public int test() { + System.out.println(0); + return 0; + } + + public int test(int i) { + System.out.println(i); + return i; + } + + public int test(OverridingTestNull o) { + System.out.println(1337); + return 1337; + } + +} diff --git a/src/test/resources/SimpleTests/OverridingTestNullMultiple.java b/src/test/resources/SimpleTests/OverridingTestNullMultiple.java new file mode 100644 index 0000000..99dd3d1 --- /dev/null +++ b/src/test/resources/SimpleTests/OverridingTestNullMultiple.java @@ -0,0 +1,37 @@ +package SimpleTests; + +class OverridingTestNullMultiple { + + // Test is used to determine wheter the Right method with the right types is + // called + + public int foo(int i) { + if (i < 5) { + return test(); + } else if (i < 10) { + return test(i); + } else if (i < 20) { + return test(this, null); + } else if (i < 30) { + return test(null, null); + } else { + return test(this, this); + } + } + + public int test() { + System.out.println(0); + return 0; + } + + public int test(int i) { + System.out.println(i); + return i; + } + + public int test(OverridingTestNullMultiple o, OverridingTestNullMultiple b) { + System.out.println(1337); + return 1337; + } + +} diff --git a/src/test/resources/SimpleTests/ParamMethod.java b/src/test/resources/SimpleTests/ParamMethod.java new file mode 100644 index 0000000..fe19181 --- /dev/null +++ b/src/test/resources/SimpleTests/ParamMethod.java @@ -0,0 +1,7 @@ +package SimpleTests; + +class ParamMethod { + + void foo(int i){} + +} diff --git a/src/test/resources/SimpleTests/PlusMethod.java b/src/test/resources/SimpleTests/PlusMethod.java new file mode 100644 index 0000000..3dc849c --- /dev/null +++ b/src/test/resources/SimpleTests/PlusMethod.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class PlusMethod { + + int foo(int i){ + return i+i; + } + +} diff --git a/src/test/resources/SimpleTests/PunktVorStrich.java b/src/test/resources/SimpleTests/PunktVorStrich.java new file mode 100644 index 0000000..42643d6 --- /dev/null +++ b/src/test/resources/SimpleTests/PunktVorStrich.java @@ -0,0 +1,13 @@ +package SimpleTests; + +class PunktVorStrich { + + int foo(int a, int b, int c) { + return a * b + c; + } + + int bar(int a, int b, int c) { + return a + b * c; + } + +} diff --git a/src/test/resources/SimpleTests/RealConstructor.java b/src/test/resources/SimpleTests/RealConstructor.java new file mode 100644 index 0000000..3bb9571 --- /dev/null +++ b/src/test/resources/SimpleTests/RealConstructor.java @@ -0,0 +1,11 @@ +package SimpleTests; + +class RealConstructor { + + private int i; + + public RealConstructor(int i){ + this.i = i; + } + +} diff --git a/src/test/resources/SimpleTests/RealMethod.java b/src/test/resources/SimpleTests/RealMethod.java new file mode 100644 index 0000000..1b39ece --- /dev/null +++ b/src/test/resources/SimpleTests/RealMethod.java @@ -0,0 +1,7 @@ +package SimpleTests; + +class RealMethod { + int foo(int i){ + return i; + } +} diff --git a/src/test/resources/SimpleTests/RealWhile.java b/src/test/resources/SimpleTests/RealWhile.java new file mode 100644 index 0000000..b81a5e2 --- /dev/null +++ b/src/test/resources/SimpleTests/RealWhile.java @@ -0,0 +1,14 @@ +package SimpleTests; + +class RealWhile { + + int foo(int i) { + int result = 0; + while (i > 0) { + result = result + 2; + i = i - 1; + } + return result; + } + +} diff --git a/src/test/resources/SimpleTests/SelfReference.java b/src/test/resources/SimpleTests/SelfReference.java new file mode 100644 index 0000000..4f2c922 --- /dev/null +++ b/src/test/resources/SimpleTests/SelfReference.java @@ -0,0 +1,20 @@ +package SimpleTests; + +class SelfReference { + + SelfReference selfRef; + + int foo() { + return this.baz(); + } + + int baz() { + return 10; + } + + int bar() { + SelfReference self = new SelfReference(); + return self.selfRef.foo(); + } + +} diff --git a/src/test/resources/SimpleTests/SystemOutPrintln.java b/src/test/resources/SimpleTests/SystemOutPrintln.java new file mode 100644 index 0000000..39273fd --- /dev/null +++ b/src/test/resources/SimpleTests/SystemOutPrintln.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class SystemOutPrintln { + + void foo() { + System.out.println(1); + } + +} diff --git a/src/test/resources/SimpleTests/SystemOutPrintlnString.java b/src/test/resources/SimpleTests/SystemOutPrintlnString.java new file mode 100644 index 0000000..7fdb8d2 --- /dev/null +++ b/src/test/resources/SimpleTests/SystemOutPrintlnString.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class SystemOutPrintlnString { + + void foo() { + System.out.println("Das ist ein String"); + } + +} diff --git a/src/test/resources/SimpleTests/ThisDotMethodCall.java b/src/test/resources/SimpleTests/ThisDotMethodCall.java new file mode 100644 index 0000000..1171ff2 --- /dev/null +++ b/src/test/resources/SimpleTests/ThisDotMethodCall.java @@ -0,0 +1,15 @@ +package SimpleTests; + +class ThisDotMethodCall { + + int i; + + public ThisDotMethodCall() { + this.i = this.foo(); + } + + int foo() { + return 1; + } + +} diff --git a/src/test/resources/SimpleTests/ValueAdapterTests.java b/src/test/resources/SimpleTests/ValueAdapterTests.java new file mode 100644 index 0000000..d0a67d1 --- /dev/null +++ b/src/test/resources/SimpleTests/ValueAdapterTests.java @@ -0,0 +1,29 @@ +package SimpleTests; + +class ValueAdapterTests { + + boolean readsTrue() { + return true; + } + + boolean readsFalse() { + return false; + } + + boolean readsTrueAndFalse() { + return true && false; + } + + int readsInt() { + return 1; + } + + int readsIntAndInt() { + return 1 + 1; + } + + char readsChar() { + return 'a'; + } + +} diff --git a/src/test/resources/SimpleTests/VoidMethod.java b/src/test/resources/SimpleTests/VoidMethod.java new file mode 100644 index 0000000..4635a60 --- /dev/null +++ b/src/test/resources/SimpleTests/VoidMethod.java @@ -0,0 +1,7 @@ +package SimpleTests; + +class VoidMethod { + + void foo(){} + +} diff --git a/src/test/resources/SimpleTests/VoidReturn.java b/src/test/resources/SimpleTests/VoidReturn.java new file mode 100644 index 0000000..2e53b08 --- /dev/null +++ b/src/test/resources/SimpleTests/VoidReturn.java @@ -0,0 +1,9 @@ +package SimpleTests; + +class VoidReturn { + + void foo() { + return; + } + +} diff --git a/src/test/resources/SimpleTests/WhileTest.java b/src/test/resources/SimpleTests/WhileTest.java new file mode 100644 index 0000000..0c366da --- /dev/null +++ b/src/test/resources/SimpleTests/WhileTest.java @@ -0,0 +1,13 @@ +package SimpleTests; + +class WhileTest { + + + int foo(){ + while(true){ + return 1; + } + } + + +} diff --git a/src/test/resources/basicClasses/emptyClass b/src/test/resources/basicClasses/emptyClass new file mode 100644 index 0000000..0a60526 --- /dev/null +++ b/src/test/resources/basicClasses/emptyClass @@ -0,0 +1 @@ +class emptyClass {} \ No newline at end of file diff --git a/src/test/resources/basicClasses/publicEmptyClass b/src/test/resources/basicClasses/publicEmptyClass new file mode 100644 index 0000000..e69de29 From c185b56065ce3c9415f32837a1803011721cd358 Mon Sep 17 00:00:00 2001 From: i22022 Date: Wed, 8 May 2024 08:59:02 +0000 Subject: [PATCH 4/4] Readme --- Readme.MD | 1 + 1 file changed, 1 insertion(+) create mode 100644 Readme.MD diff --git a/Readme.MD b/Readme.MD new file mode 100644 index 0000000..9d0061b --- /dev/null +++ b/Readme.MD @@ -0,0 +1 @@ +Please move your stuff from "Source" to "src".