From 3e2368d13cffe97506f1fbcbbe95837534e3647c Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Thu, 27 Jun 2024 22:05:22 +0200 Subject: [PATCH] added some e2e tests for CodeGenFiles with reflections --- .../CodeGen/BytecodeTestUtil.java | 42 +- .../CodeGen/Features/ByteCode_Break.java | 1 + .../CodeGen/Features/ByteCode_Field.java | 103 +- .../CodeGen/Features/ByteCode_For.java | 276 ++--- .../CodeGen/Features/ByteCode_If.java | 200 ++-- .../CodeGen/Features/ByteCode_LogicExpr.java | 962 ++++++++---------- src/test/testFiles/CodeGenFeatures/For.java | 17 +- 7 files changed, 747 insertions(+), 854 deletions(-) diff --git a/src/test/java/testResources/CodeGen/BytecodeTestUtil.java b/src/test/java/testResources/CodeGen/BytecodeTestUtil.java index 498d687..9c9c0d2 100644 --- a/src/test/java/testResources/CodeGen/BytecodeTestUtil.java +++ b/src/test/java/testResources/CodeGen/BytecodeTestUtil.java @@ -120,27 +120,18 @@ public class BytecodeTestUtil { } public Object getFieldValue(String fieldName) throws Exception { - return clazz.getField(fieldName).get(instance); + try { + return clazz.getField(fieldName).get(instance); + } catch (IllegalAccessException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } } public void setFieldValue(String fieldName, Object value) throws Exception { clazz.getField(fieldName).set(instance, value); } - - - public Object getFieldValueAfterMethodInvocation(String fieldName, String methodName, Class[] parameterTypes, Object... args) throws Exception { - Object instance = clazz.getDeclaredConstructor().newInstance(); - - // Methode aufrufen - Method method = clazz.getMethod(methodName, parameterTypes); - method.invoke(instance, args); - - // Wert des Feldes nach dem Methodenaufruf abrufen - return clazz.getField(fieldName).get(instance); - } - - public Object getFieldValueOfObject(Object obj, String fieldName) { try { Field field = obj.getClass().getField(fieldName); @@ -153,25 +144,4 @@ public class BytecodeTestUtil { return null; } } - - - - private Class[] toClassArray(Object... args) { - return java.util.Arrays.stream(args) - .map(Object::getClass) - .toArray(Class[]::new); - } - - - - public Object getValueOfFieldInObject(Object instance, String fieldName) { - try { - Field field = instance.getClass().getDeclaredField(fieldName); - field.setAccessible(true); - return field.get(instance); - } catch (NoSuchFieldException | IllegalAccessException e) { - e.printStackTrace(); - return null; - } - } } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java index f7e6cfe..79de113 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java @@ -64,6 +64,7 @@ public class ByteCode_Break { @Test public void testMethodParameters() { try { + Assertions.assertEquals(1, util.getMethodParameterCount("breakMethod", new Class[]{boolean.class})); Assertions.assertEquals("boolean", util.getMethodParameterTypes("breakMethod", new Class[]{boolean.class}).get(0)); } catch (Exception e) { Assertions.fail(); diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java index 1a60241..01b184e 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java @@ -62,13 +62,98 @@ public class ByteCode_Field { } } -// @Test -// public void testFieldCount() { -// Assertions.assertEquals(2, util.getFieldCount()); -// } -// -// @Test -// public void testFieldCount() { -// Assertions.assertEquals(2, util.getFieldCount()); -// } + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(0, util.getMethodParameterCount("fieldAccess", new Class[]{})); + Assertions.assertEquals(1, util.getMethodParameterCount("setX", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("setX", new Class[]{int.class}).get(0)); + Assertions.assertEquals(1, util.getMethodParameterCount("setC", new Class[]{char.class})); + Assertions.assertEquals("char", util.getMethodParameterTypes("setC", new Class[]{char.class}).get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(2, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertTrue(util.getFieldNames().contains("x")); + Assertions.assertTrue(util.getFieldNames().contains("c")); + } + + @Test + public void testFieldTypes() { + try { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + Assertions.assertEquals("char", util.getFieldTypes().get(1)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertEquals('\u0000', util.getFieldValue("c")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Field"); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "x")); + Assertions.assertEquals('\u0000', util.getFieldValueOfObject(instance, "c")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeFieldAccess() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertEquals('\u0000', util.getFieldValue("c")); + util.invokeMethod("fieldAccess", new Class[]{}, new Object[]{}); + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertEquals('a', util.getFieldValue("c")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSetXWith5() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertEquals('\u0000', util.getFieldValue("c")); + util.invokeMethod("setX", new Class[]{int.class}, new Object[]{5}); + Assertions.assertEquals(5, util.getFieldValue("x")); + Assertions.assertEquals('\u0000', util.getFieldValue("c")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSetCWithH() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertEquals('\u0000', util.getFieldValue("c")); + util.invokeMethod("setC", new Class[]{char.class}, new Object[]{'h'}); + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertEquals('h', util.getFieldValue("c")); + } catch (Exception e) { + Assertions.fail(); + } + } } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_For.java b/src/test/java/testResources/CodeGen/Features/ByteCode_For.java index 6670d6d..3c53cf2 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_For.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_For.java @@ -3,174 +3,120 @@ package testResources.CodeGen.Features; import de.maishai.ast.Operator; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_For { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "For", - List.of(), - List.of( - new TypedMethod( - "testFor", - Type.VOID, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "i", - Type.INT - ), - new TypedLocalVariable( - "j", - Type.INT - ) - ), - List.of( - new TypedFor( - new TypedAssignment( - new TypedIntLiteral( - 0, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ), - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Operator.LT, - new TypedIntLiteral( - 10, - Type.INT - ), - Type.BOOL - ), - new TypedAssignment( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Operator.ADD, - new TypedIntLiteral( - 1, - Type.INT - ), - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID - ), - new TypedFor( - new TypedAssignment( - new TypedIntLiteral( - 0, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "j", - Type.INT - ), - Type.INT - ), - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "j", - Type.INT - ), - Operator.LT, - new TypedIntLiteral( - 10, - Type.INT - ), - Type.BOOL - ), - new TypedAssignment( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "j", - Type.INT - ), - Operator.ADD, - new TypedIntLiteral( - 1, - Type.INT - ), - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "j", - Type.INT - ), - Type.INT - ), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID - ) - ) - ) - ) - ), - List.of( - new TypedConstructor( - "For", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - null, - Type.REFERENCE("For") - ) - ), - null - ); + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/For.java"), "For"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("For", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("For", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(1, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("testFor")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("void", util.getMethodReturnType("testFor", new Class[]{})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(0, util.getMethodParameterCount("testFor")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(2, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertTrue(util.getFieldNames().contains("repetitionsFirstFor")); + Assertions.assertTrue(util.getFieldNames().contains("repetitionsSecondFor")); + } + + @Test + public void testFieldTypes() { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + Assertions.assertEquals("int", util.getFieldTypes().get(1)); + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("repetitionsFirstFor")); + Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "For"); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsFirstFor")); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsSecondFor")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethod() { + try { + Assertions.assertEquals(0, util.getFieldValue("repetitionsFirstFor")); + Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor")); + util.invokeMethod("testFor", new Class[]{}, new Object[]{}); + Assertions.assertEquals(45, util.getFieldValue("repetitionsFirstFor")); + Assertions.assertEquals(45, util.getFieldValue("repetitionsSecondFor")); + } catch (Exception e) { + Assertions.fail(); + } } } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_If.java b/src/test/java/testResources/CodeGen/Features/ByteCode_If.java index 3733c16..bc11677 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_If.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_If.java @@ -2,85 +2,133 @@ package testResources.CodeGen.Features; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_If { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "If", - List.of(), - List.of( - new TypedMethod( - "ifMethod", - Type.VOID, - List.of(), - List.of(), - new TypedBlock( - List.of(), - List.of( - new TypedIfElse( - new TypedBoolLiteral( - false, - Type.BOOL - ), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - new TypedBlock( - List.of(), - List.of( - new TypedIfElse( - new TypedBoolLiteral( - true, - Type.BOOL - ), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID - ) - ), - Type.VOID - ), - Type.VOID - ) - ), - Type.VOID - ) - ) - ), - List.of( - new TypedConstructor( - "If", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - null, - Type.REFERENCE("If") - ) - ), - null - ); + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/If.java"), "If"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("If", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("If", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(1, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("ifMethod")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("int", + util.getMethodReturnType("ifMethod", new Class[]{boolean.class, boolean.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(2, + util.getMethodParameterCount("ifMethod", new Class[]{boolean.class, boolean.class})); + Assertions.assertEquals("boolean", + util.getMethodParameterTypes("ifMethod", new Class[]{boolean.class, boolean.class}).get(0)); + Assertions.assertEquals("boolean", + util.getMethodParameterTypes("ifMethod", new Class[]{boolean.class, boolean.class}).get(1)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(0, util.getFieldCount()); + } + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "If"); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeIfMethodWithTrueTrue() { + try { + int returnValue = (int) util.invokeMethod("ifMethod", + new Class[]{boolean.class, boolean.class}, true, true); + Assertions.assertEquals(1, returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeIfMethodWithTrueFalse() { + try { + int returnValue = (int) util.invokeMethod("ifMethod", + new Class[]{boolean.class, boolean.class}, true, false); + Assertions.assertEquals(1, returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeIfMethodWithFalseTrue() { + try { + int returnValue = (int) util.invokeMethod("ifMethod", + new Class[]{boolean.class, boolean.class}, false, true); + Assertions.assertEquals(2, returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeIfMethodWithFalseFalse() { + try { + int returnValue = (int) util.invokeMethod("ifMethod", + new Class[]{boolean.class, boolean.class}, false, false); + Assertions.assertEquals(3, returnValue); + } catch (Exception e) { + Assertions.fail(); + } } } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java b/src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java index be4dcdc..26aae59 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java @@ -3,566 +3,414 @@ package testResources.CodeGen.Features; import de.maishai.ast.Operator; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_LogicExpr { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "LogicExpr", - List.of(), - List.of( - new TypedMethod( - "test", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.INT - ), - new TypedLocalVariable( - "y", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 10, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Type.INT - ), - new TypedAssignment( - new TypedIntLiteral( - 20, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.INT - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Operator.LT, - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "test2", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.INT - ), - new TypedLocalVariable( - "y", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 10, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Type.INT - ), - new TypedAssignment( - new TypedIntLiteral( - 20, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.INT - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Operator.GT, - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "test3", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.INT - ), - new TypedLocalVariable( - "y", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 10, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Type.INT - ), - new TypedAssignment( - new TypedIntLiteral( - 20, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.INT - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Operator.EQ, - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "test4", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.INT - ), - new TypedLocalVariable( - "y", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 10, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Type.INT - ), - new TypedAssignment( - new TypedIntLiteral( - 20, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.INT - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Operator.NE, - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "test5", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.INT - ), - new TypedLocalVariable( - "y", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 10, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Type.INT - ), - new TypedAssignment( - new TypedIntLiteral( - 20, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.INT - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Operator.LE, - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "test6", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.INT - ), - new TypedLocalVariable( - "y", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 10, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Type.INT - ), - new TypedAssignment( - new TypedIntLiteral( - 20, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.INT - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Operator.GE, - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "test7", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.BOOL - ), - new TypedLocalVariable( - "y", - Type.BOOL - ) - ), - List.of( - new TypedAssignment( - new TypedBoolLiteral( - true, - Type.BOOL - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.BOOL - ), - Type.BOOL - ), - new TypedAssignment( - new TypedBoolLiteral( - false, - Type.BOOL - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.BOOL - ), - Type.BOOL - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.BOOL - ), - Operator.AND, - new TypedFieldVarAccess( - false, - null, - "y", - Type.BOOL - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "test8", - Type.BOOL, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "x", - Type.BOOL - ), - new TypedLocalVariable( - "y", - Type.BOOL - ) - ), - List.of( - new TypedAssignment( - new TypedBoolLiteral( - true, - Type.BOOL - ), - new TypedFieldVarAccess( - false, - null, - "x", - Type.BOOL - ), - Type.BOOL - ), - new TypedAssignment( - new TypedBoolLiteral( - false, - Type.BOOL - ), - new TypedFieldVarAccess( - false, - null, - "y", - Type.BOOL - ), - Type.BOOL - ), - new TypedReturn( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.BOOL - ), - Operator.OR, - new TypedFieldVarAccess( - false, - null, - "y", - Type.BOOL - ), - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ) - ), - List.of( - new TypedConstructor( - "LogicExpr", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - null, - Type.REFERENCE("LogicExpr") - ) - ), - null - ); + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/LogicExpr.java"), "LogicExpr"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("LogicExpr", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("LogicExpr", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(8, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("testSmaller")); + Assertions.assertTrue(util.getMethodNames().contains("testLarger")); + Assertions.assertTrue(util.getMethodNames().contains("testEqual")); + Assertions.assertTrue(util.getMethodNames().contains("testNotEqual")); + Assertions.assertTrue(util.getMethodNames().contains("testSmallerEqual")); + Assertions.assertTrue(util.getMethodNames().contains("testLargerEqual")); + Assertions.assertTrue(util.getMethodNames().contains("testAND")); + Assertions.assertTrue(util.getMethodNames().contains("testOR")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals(1, util.getMethodParameterCount("testSmaller", new Class[]{int.class, int.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testSmaller", new Class[]{int.class, int.class})); + Assertions.assertEquals(1, util.getMethodParameterCount("testLarger", new Class[]{int.class, int.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testLarger", new Class[]{int.class, int.class})); + Assertions.assertEquals(1, util.getMethodParameterCount("testEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals(1, util.getMethodParameterCount("testNotEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testNotEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals(1, util.getMethodParameterCount("testSmallerEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testSmallerEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals(1, util.getMethodParameterCount("testLargerEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testLargerEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals(1, util.getMethodParameterCount("testAND", new Class[]{boolean.class, boolean.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testAND", new Class[]{boolean.class, boolean.class})); + Assertions.assertEquals(1, util.getMethodParameterCount("testOR", new Class[]{boolean.class, boolean.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("testOR", new Class[]{boolean.class, boolean.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(2, util.getMethodParameterCount("testSmaller", new Class[]{int.class, int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("testSmaller", new Class[]{int.class, int.class}).get(0)); + Assertions.assertEquals("int", util.getMethodParameterTypes("testSmaller", new Class[]{int.class, int.class}).get(1)); + Assertions.assertEquals(2, util.getMethodParameterCount("testLarger", new Class[]{int.class, int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("testLarger", new Class[]{int.class, int.class}).get(0)); + Assertions.assertEquals("int", util.getMethodParameterTypes("testLarger", new Class[]{int.class, int.class}).get(1)); + Assertions.assertEquals(2, util.getMethodParameterCount("testEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("testEqual", new Class[]{int.class, int.class}).get(0)); + Assertions.assertEquals("int", util.getMethodParameterTypes("testEqual", new Class[]{int.class, int.class}).get(1)); + Assertions.assertEquals(2, util.getMethodParameterCount("testNotEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("testNotEqual", new Class[]{int.class, int.class}).get(0)); + Assertions.assertEquals("int", util.getMethodParameterTypes("testNotEqual", new Class[]{int.class, int.class}).get(1)); + Assertions.assertEquals(2, util.getMethodParameterCount("testSmallerEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("testSmallerEqual", new Class[]{int.class, int.class}).get(0)); + Assertions.assertEquals("int", util.getMethodParameterTypes("testSmallerEqual", new Class[]{int.class, int.class}).get(1)); + Assertions.assertEquals(2, util.getMethodParameterCount("testLargerEqual", new Class[]{int.class, int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("testLargerEqual", new Class[]{int.class, int.class}).get(0)); + Assertions.assertEquals("int", util.getMethodParameterTypes("testLargerEqual", new Class[]{int.class, int.class}).get(1)); + Assertions.assertEquals(2, util.getMethodParameterCount("testAND", new Class[]{boolean.class, boolean.class})); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("testAND", new Class[]{boolean.class, boolean.class}).get(0)); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("testAND", new Class[]{boolean.class, boolean.class}).get(1)); + Assertions.assertEquals(2, util.getMethodParameterCount("testOR", new Class[]{boolean.class, boolean.class})); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("testOR", new Class[]{boolean.class, boolean.class}).get(0)); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("testOR", new Class[]{boolean.class, boolean.class}).get(1)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(0, util.getFieldCount()); + } + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "LogicExpr"); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSmallerWith4And5() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testSmaller", new Class[]{int.class, int.class}, new Object[]{4, 5}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSmallerWith5And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testSmaller", new Class[]{int.class, int.class}, new Object[]{5, 4}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSmallerWith4And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testSmaller", new Class[]{int.class, int.class}, new Object[]{4, 4}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeLargerWith4And5() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testLarger", new Class[]{int.class, int.class}, new Object[]{4, 5}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeLargerWith5And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testLarger", new Class[]{int.class, int.class}, new Object[]{5, 4}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeLargerWith4And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testLarger", new Class[]{int.class, int.class}, new Object[]{4, 4}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeEqualWith4And5() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testEqual", new Class[]{int.class, int.class}, new Object[]{4, 5}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeEqualWith5And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testEqual", new Class[]{int.class, int.class}, new Object[]{5, 4}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeEqualWith4And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testEqual", new Class[]{int.class, int.class}, new Object[]{4, 4}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeNotEqualWith4And5() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testNotEqual", new Class[]{int.class, int.class}, new Object[]{4, 5}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeNotEqualWith5And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testNotEqual", new Class[]{int.class, int.class}, new Object[]{5, 4}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeNotEqualWith4And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testNotEqual", new Class[]{int.class, int.class}, new Object[]{4, 4}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSmallerEqualWith4And5() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testSmallerEqual", new Class[]{int.class, int.class}, new Object[]{4, 5}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSmallerEqualWith5And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testSmallerEqual", new Class[]{int.class, int.class}, new Object[]{5, 4}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeSmallerEqualWith4And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testSmallerEqual", new Class[]{int.class, int.class}, new Object[]{4, 4}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeLargerEqualWith4And5() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testLargerEqual", new Class[]{int.class, int.class}, new Object[]{4, 5}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeLargerEqualWith5And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testLargerEqual", new Class[]{int.class, int.class}, new Object[]{5, 4}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeLargerEqualWith4And4() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testLargerEqual", new Class[]{int.class, int.class}, new Object[]{4, 4}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeANDWithTrueAndTrue() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testAND", new Class[]{boolean.class, boolean.class}, new Object[]{true, true}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeANDWithTrueAndFalse() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testAND", new Class[]{boolean.class, boolean.class}, new Object[]{true, false}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeANDWithFalseAndTrue() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testAND", new Class[]{boolean.class, boolean.class}, new Object[]{false, true}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeANDWithFalseAndFalse() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testAND", new Class[]{boolean.class, boolean.class}, new Object[]{false, false}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeORWithTrueAndTrue() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testOR", new Class[]{boolean.class, boolean.class}, new Object[]{true, true}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeORWithTrueAndFalse() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testOR", new Class[]{boolean.class, boolean.class}, new Object[]{true, false}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeORWithFalseAndTrue() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testOR", new Class[]{boolean.class, boolean.class}, new Object[]{false, true}); + Assertions.assertTrue(returnValue); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeORWithFalseAndFalse() { + try { + boolean returnValue = (boolean) util.invokeMethod( + "testOR", new Class[]{boolean.class, boolean.class}, new Object[]{false, false}); + Assertions.assertFalse(returnValue); + } catch (Exception e) { + Assertions.fail(); + } } } \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/For.java b/src/test/testFiles/CodeGenFeatures/For.java index 012eb0b..498c440 100644 --- a/src/test/testFiles/CodeGenFeatures/For.java +++ b/src/test/testFiles/CodeGenFeatures/For.java @@ -1,21 +1,16 @@ public class For { - int repititionsFirtsFor; - int repititionsSecondFor; - - public For() { - this.repititionsFirtsFor = 0; - this.repititionsSecondFor = 0; - } + int repetitionsFirstFor; + int repetitionsSecondFor; public void testFor() { - this.repititionsFirtsFor = 0; + this.repetitionsFirstFor = 0; for (int i = 0; i < 10; i = i+1) { - this.repititionsFirtsFor = this.repititionsFirtsFor + i; + this.repetitionsFirstFor = this.repetitionsFirstFor + i; } - this.repititionsSecondFor = 0; + this.repetitionsSecondFor = 0; int j; for (j = 0; j < 10; j += 1) { - this.repititionsSecondFor = this.repititionsSecondFor + j; + this.repetitionsSecondFor = this.repetitionsSecondFor + j; } } } \ No newline at end of file