Test update

This commit is contained in:
Julian Murek 2024-07-03 13:41:55 +02:00
parent f6358867f0
commit 888534955a
60 changed files with 283 additions and 116 deletions

BIN
CharArgument.class Normal file

Binary file not shown.

BIN
ClassFields.class Normal file

Binary file not shown.

BIN
ClassOne.class Normal file

Binary file not shown.

BIN
ClassTwo.class Normal file

Binary file not shown.

BIN
ConstructorParams.class Normal file

Binary file not shown.

BIN
ConstructorThisDot.class Normal file

Binary file not shown.

BIN
DivMethod.class Normal file

Binary file not shown.

BIN
FieldVar.class Normal file

Binary file not shown.

BIN
FieldWithExpr.class Normal file

Binary file not shown.

BIN
GetterFunction.class Normal file

Binary file not shown.

BIN
IfElseIfStatement.class Normal file

Binary file not shown.

Binary file not shown.

BIN
classWithMain.class Normal file

Binary file not shown.

BIN
getI.class Normal file

Binary file not shown.

View File

@ -1,3 +1,4 @@
/*
class FourClasses {
public int notmain(int i) {
@ -10,4 +11,4 @@ class FourClasses {
}
}
}*/

View File

@ -29,6 +29,49 @@ public class Program implements Node {
this.classes = classes;
}
public TypeCheckResult typeCheckWithoutMain() throws TypeCheckException {
this.typeContext = new HashMap<>();
this.methodContext = new HashMap<>();
for(RefType oneClass : classes){
// build type context
HashMap<String, String> classVars = new HashMap<>();
for (FieldDecl fieldDecl: oneClass.fieldDecls){
classVars.put(fieldDecl.identifier, fieldDecl.type);
}
typeContext.put(oneClass.name, classVars);
// build method context
HashMap<String, HashMap<String, ParameterList>> identifierAndMethod = new HashMap<>();
for (MethodDecl methodDecl : oneClass.methodDecls){
if(methodDecl.returnType == null) continue;
HashMap<String, ParameterList> returnTypeAndParameter = new HashMap<>();
returnTypeAndParameter.put(methodDecl.returnType, methodDecl.parameters);
identifierAndMethod.put(methodDecl.name, returnTypeAndParameter);
}
methodContext.put(oneClass.name, identifierAndMethod);
}
int mainCounter = 0;
// check if main exists
for(RefType oneClass : classes){
if(oneClass.hasMain)
mainCounter++;
}
if(mainCounter != 1) {
//throw new TypeCheckException("There is not 1 Main method.");
}
// typecheck each class
TypeCheckResult result = new TypeCheckResult();
for(RefType oneClass : classes){
oneClass.typeCheck(methodContext, typeContext);
}
result.type = "program";
return result;
}
public TypeCheckResult typeCheck() throws TypeCheckException {
this.typeContext = new HashMap<>();

View File

@ -58,7 +58,7 @@ public class ByteCodeTester {
if(generateAST){
try {
abstractSyntaxTree.typeCheck();
abstractSyntaxTree.typeCheckWithoutMain();
} catch (Exception e){
System.out.println("Le Exception in le type-check");
//fail();
@ -86,6 +86,7 @@ public class ByteCodeTester {
} catch (ClassNotFoundException e) {
e.printStackTrace();
fail();
}
}
@ -125,10 +126,10 @@ public class ByteCodeTester {
Program abstractSyntaxTree = (Program) generator.visit(tree);
try {
abstractSyntaxTree.typeCheck();
abstractSyntaxTree.typeCheckWithoutMain();
} catch (Exception e){
System.out.println("Le Exception in le type-check");
//fail();
fail();
}
try {

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;
import static ByteCode.CompareByteCodeSyntax.compareMethod;
@ -54,12 +55,16 @@ public class CompareByteCodeBehaviour {
try {
this.sortMethods(class1, class2);
// Create instances
Constructor<?> constructor1 = class1.getDeclaredConstructor();
Constructor<?> constructor2 = class2.getDeclaredConstructor();
Constructor<?>[] constructors1 = class1.getDeclaredConstructors();
Constructor<?>[] constructors2 = class2.getDeclaredConstructors();
Constructor<?> constructor1 = constructors1[0];
Constructor<?> constructor2 = constructors2[0];
constructor1.setAccessible(true);
constructor2.setAccessible(true);
Object obj1 = constructor1.newInstance();
Object obj2 = constructor2.newInstance();
Object[] constructorArgs1 = getDefaultArguments(constructor1.getParameters());
Object[] constructorArgs2 = getDefaultArguments(constructor2.getParameters());
Object obj1 = constructor1.newInstance(constructorArgs1);
Object obj2 = constructor2.newInstance(constructorArgs2);
// Get methods
Method[] methods1 = new Method[this.methodArray1.size()];
@ -96,8 +101,9 @@ public class CompareByteCodeBehaviour {
}
} catch (InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
InvocationTargetException e) {
e.printStackTrace();
return false;
}
return true;
}
@ -110,10 +116,35 @@ public class CompareByteCodeBehaviour {
inputs[i] = 1; // example value
} else if (parameterTypes[i] == String.class) {
inputs[i] = "test"; // example value
} else if (parameterTypes[i] == char.class) {
inputs[i] = 'a';
} else if (parameterTypes[i] == boolean.class) {
inputs[i] = false;
}
// Add more cases as needed for different parameter types
}
return inputs;
}
private Object[] getDefaultArguments(Parameter[] parameters) {
Object[] defaultArgs = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
Class<?> paramType = parameters[i].getType();
if (paramType.isPrimitive()) {
if (paramType == boolean.class) {
defaultArgs[i] = false;
} else if (paramType == int.class) {
defaultArgs[i] = 1;
} else if (paramType == char.class) {
defaultArgs[i] = '\u0000';
} else if (paramType == String.class) {
defaultArgs[i] = "baguette";
}
} else {
defaultArgs[i] = null; // Use null for non-primitive types
}
}
return defaultArgs;
}
}

View File

@ -42,6 +42,122 @@ public class TestAll {
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testCharArgument() {
String classPath = "src/test/resources/SimpleTests/CharArgument.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "CharArgument";
String javacode = "src/test/resources/SimpleTests/CharArgument.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testClassFields() {
String classPath = "src/test/resources/SimpleTests/ClassFields.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ClassFields";
String javacode = "src/test/resources/SimpleTests/ClassFields.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testConstructorParams() {
String classPath = "src/test/resources/SimpleTests/ConstructorParams.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ConstructorParams";
String javacode = "src/test/resources/SimpleTests/ConstructorParams.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testConstructorThisDot() {
String classPath = "src/test/resources/SimpleTests/ConstructorThisDot.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ConstructorThisDot";
String javacode = "src/test/resources/SimpleTests/ConstructorThisDot.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testDivMethod() {
String classPath = "src/test/resources/SimpleTests/DivMethod.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "DivMethod";
String javacode = "src/test/resources/SimpleTests/DivMethod.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
// We dont do null assignments
/*
@Test
public void testExplicitNullAssign() {
String classPath = "src/test/resources/SimpleTests/ExplicitNullAssign.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ExplicitNullAssign";
String javacode = "src/test/resources/SimpleTests/ExplicitNullAssign.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
*/
@Test
public void testFieldVar() {
String classPath = "src/test/resources/SimpleTests/FieldVar.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "FieldVar";
String javacode = "src/test/resources/SimpleTests/FieldVar.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testFieldWithExpr() {
String classPath = "src/test/resources/SimpleTests/FieldWithExpr.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "FieldWithExpr";
String javacode = "src/test/resources/SimpleTests/FieldWithExpr.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testGetterFunction() {
String classPath = "src/test/resources/SimpleTests/GetterFunction.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "GetterFunction";
String javacode = "src/test/resources/SimpleTests/GetterFunction.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testIfElseIfStatement() {
String classPath = "src/test/resources/SimpleTests/IfElseIfStatement.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "IfElseIfStatement";
String javacode = "src/test/resources/SimpleTests/IfElseIfStatement.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testIfElseIfStatementWithOneReturn() {
String classPath = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "IfElseIfStatementWithOneReturn";
String javacode = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testTwoClasses() {
String jarPath = "src/test/resources/basicClasses/TwoClasses.jar";
@ -51,6 +167,7 @@ public class TestAll {
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
}
@Test
public void testFourClasses() {
String jarPath = "src/test/resources/basicClasses/FourClasses.jar";
@ -60,6 +177,15 @@ public class TestAll {
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
}
@Test
public void testDijkstra() {
String jarPath = "src/test/resources/Integration/Dijkstra.jar";
//Program ast = ClassWithMainASTTyped.getProgram();
String[] classNames = {"FourClasses", "Test", "Test2", "Test3"};
String javacode = "src/test/resources/Integration/Dijkstra.java";
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
}

View File

@ -67,5 +67,10 @@ public class TestAll {
testLexer.testTokens("SimpleTests/FieldVar.java", "SimpleTests/FieldVar.tokens");
}
@Test
public void testIfElseIfStatementWithOneReturn() throws Exception {
testLexer.testTokens("SimpleTests/IfElseIfStatementWithOneReturn.java", "SimpleTests/IfElseIfStatementWithOneReturn.tokens");
}
}

View File

@ -8,10 +8,22 @@ import static org.junit.Assert.assertEquals;
public class TypeChecker {
// Method to test if the TypeCheck returns the expected Result
static public void assertTypeCheckResultNoMain(boolean testForMain, Program programmToBeTested, Program correctAST, boolean expectedResult) {
assertTypeCheckResult(false, programmToBeTested, correctAST, expectedResult);
}
static public void assertTypeCheckResult(Program programmToBeTested, Program correctAST, boolean expectedResult) {
assertTypeCheckResult(true, programmToBeTested, correctAST, expectedResult);
}
static public void assertTypeCheckResult(boolean testForMain, Program programmToBeTested, Program correctAST, boolean expectedResult) {
boolean actualResult;
try {
TypeCheckResult typeCheckResult = programmToBeTested.typeCheck();
if(testForMain){
programmToBeTested.typeCheck();
} else {
programmToBeTested.typeCheckWithoutMain();
}
actualResult = true;
} catch (Exception e) {
actualResult = false;

View File

@ -1,5 +1,3 @@
package Integration;
class Dijkstra {
void main() {
@ -29,33 +27,33 @@ class Dijkstra {
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(v1, v2);
g.addEdge(v1, v5);
g.addEdge(v1, v10);
g.addEdge(v2, v3); // B
g.addEdge(v2, v3);
g.addEdge(v2, v7);
g.addEdge(v3, v4); // C
g.addEdge(v3, v4);
g.addEdge(v3, v5);
g.addEdge(v4, v8); // D
g.addEdge(v4, v8);
g.addEdge(v5, v6); // E
g.addEdge(v5, v6);
g.addEdge(v5, v9);
g.addEdge(v6, v7); // F
g.addEdge(v6, v7);
g.addEdge(v7, v8); // G
g.addEdge(v7, v8);
g.addEdge(v8, v12); // H
g.addEdge(v8, v12);
g.addEdge(v8, v10);
g.addEdge(v9, v10); // I
g.addEdge(v9, v10);
g.addEdge(v10, v11); // J
g.addEdge(v10, v11);
g.addEdge(v11, v12); // K
g.addEdge(v11, v12);
g.getShortestPath(v1, v8);
g.getShortestPath(v1, v12);
@ -69,7 +67,7 @@ class Dijkstra {
class Graph {
private VertexSet vertexList;
public VertexSet vertexList;
public Graph(VertexSet vertexList) {
this.vertexList = vertexList;
@ -98,23 +96,25 @@ class Graph {
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) {
int i = 0;
int i = 0;
while (i < currentAdjanceyList.size()) {
Vertex adjancey = currentAdjanceyList.getFromIndex(i);
adjancey = calcList.get(adjancey.id);
if ((adjancey != null) && !adjancey.isVisited()) {
if (adjancey != null) {
if (!adjancey.isVisited()) {
int distance = current.getDistance() + 1;
if (distance < adjancey.getDistance()) {
adjancey.setDistance(distance);
adjancey.setPrevious(current);
}
}
}
i = i + 1;
}
} else {
}
@ -132,8 +132,8 @@ class Graph {
previous = previous.getPrevious();
}
}
}
class Vertex {
public int id;
@ -177,7 +177,7 @@ class Vertex {
public Vertex(Vertex vertex) {
this.id = vertex.id;
this.adjanceyList = vertex.adjanceyList;
this.distance = 10000; // No infinity so...
this.distance = 10000;
this.previous = null;
this.visited = false;
}
@ -202,8 +202,8 @@ class Vertex {
class VertexSet {
private Vertex vertex;
private VertexSet next;
public Vertex vertex;
public VertexSet next;
public VertexSet(Vertex vertex) {
this.vertex = vertex;
@ -318,7 +318,7 @@ class VertexSet {
}
public Vertex get(int index) {
if ((vertex == null)) {
if (vertex == null) {
return null;
} else if (vertex.id == index) {
return this.vertex;

Binary file not shown.

Binary file not shown.

View File

@ -1,9 +1,7 @@
package SimpleTests;
class ClassFields {
private int privateAccess;
public int privateAccess;
public int publicAccess;
protected int protectedAccess;
public int protectedAccess;
}

Binary file not shown.

View File

@ -1,5 +1,3 @@
package SimpleTests;
class ConstructorParams {
public ConstructorParams(int i) {

View File

@ -1,5 +1,3 @@
package SimpleTests;
class ConstructorThisDot{
public int i;

View File

@ -1,17 +0,0 @@
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);
}
}
}

Binary file not shown.

View File

@ -1,5 +1,3 @@
package SimpleTests;
class DivMethod {
int foo(int i){

View File

@ -1,3 +0,0 @@
package SimpleTests;
class EmptyClass{}

View File

@ -1,5 +1,3 @@
package SimpleTests;
class ExplicitNullAssign {
ExplicitNullAssign e;

View File

@ -1,18 +0,0 @@
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;
}
}

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,3 @@
package SimpleTests;
class FieldWithExpr {
public int x = 5;

View File

@ -1,11 +0,0 @@
package SimpleTests;
class ForTest {
void foo() {
for (int i = 0; i < 10; i = i + 1) {
System.out.println(i);
}
}
}

Binary file not shown.

View File

@ -1,11 +1,9 @@
package SimpleTests;
class GetterFunction {
private int i;
public int i;
public GetterFunction(int i) {
this.i = i;
public GetterFunction(int number) {
this.i = number;
}
public int getI() {

Binary file not shown.

View File

@ -1,8 +1,6 @@
package SimpleTests;
class IfElseIfStatement {
int foo(int i) {
public int foo(int i) {
if (i == 1) {
return 10;
} else if (i == 2) {

View File

@ -1,5 +1,3 @@
package SimpleTests;
class IfElseIfStatementWithOneReturn {
int foo(int i) {

View File

@ -0,0 +1,14 @@
Class: "class"
Identifier: "IfElseIfStatementWithOneReturn"
OpenCurlyBracket: "{"
Int: "int"
Identifier: "foo"
OpenRoundBracket: "("
Int: "int"
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
Int: "int"
Identifier: "result"
Assign: "="
IntValue: "0"
Semicolon: ";"

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,13 +1,14 @@
class FourClasses {
public int main(int i) {
public int method(int i) {
Test t = new Test(i);
Test2 t2 = new Test2(t.y);
return t2.test.test3.getX();
}
public static void main(String[] args){
FourClasses fourClasses = new FourClasses();
fourClasses.method(1);
}
}

Binary file not shown.

Binary file not shown.