added tests for i++ etc

This commit is contained in:
JonathanFleischmann 2024-07-02 22:49:23 +02:00
parent cc674625b0
commit 93279743f2
15 changed files with 675 additions and 6 deletions

View File

@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
import static HelpClasses.TestFileTester.run; import static HelpClasses.TestFileTester.run;
class E2ETests { class AllE2ETests {
// @Test // @Test
// void testPublicClass() { // void testPublicClass() {

View File

@ -54,6 +54,10 @@ public class BytecodeTestUtil {
return clazz.getDeclaredConstructors()[index].getParameterCount(); return clazz.getDeclaredConstructors()[index].getParameterCount();
} }
public int getConstructorParameterCount(Class<?>[] parameterTypes) throws Exception {
return clazz.getDeclaredConstructor(parameterTypes).getParameterCount();
}
public List<String> getConstructorParameterTypes(int index) { public List<String> getConstructorParameterTypes(int index) {
List<String> parameterTypes = new ArrayList<>(); List<String> parameterTypes = new ArrayList<>();
for (Class<?> parameterType : clazz.getDeclaredConstructors()[index].getParameterTypes()) { for (Class<?> parameterType : clazz.getDeclaredConstructors()[index].getParameterTypes()) {

View File

@ -27,14 +27,22 @@ public class ByteCode_ClassObjects {
@Test @Test
public void testConstructor1() { public void testConstructor1() {
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(0)); Assertions.assertTrue(util.getConstructorNames().contains("ClassObjects"));
Assertions.assertEquals(0, util.getConstructorParameterCount(0)); try {
Assertions.assertEquals(0, util.getConstructorParameterCount(new Class[]{}));
} catch (Exception e) {
Assertions.fail();
}
} }
@Test @Test
public void testConstructor2() { public void testConstructor2() {
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(1)); Assertions.assertTrue(util.getConstructorNames().contains("ClassObjects"));
Assertions.assertEquals(1, util.getConstructorParameterCount(1)); try {
Assertions.assertEquals(1, util.getConstructorParameterCount(new Class[]{int.class}));
} catch (Exception e) {
Assertions.fail();
}
} }

View File

@ -67,19 +67,21 @@ public class ByteCode_For {
@Test @Test
public void testFieldCount() { public void testFieldCount() {
Assertions.assertEquals(2, util.getFieldCount()); Assertions.assertEquals(3, util.getFieldCount());
} }
@Test @Test
public void testFieldNames() { public void testFieldNames() {
Assertions.assertTrue(util.getFieldNames().contains("repetitionsFirstFor")); Assertions.assertTrue(util.getFieldNames().contains("repetitionsFirstFor"));
Assertions.assertTrue(util.getFieldNames().contains("repetitionsSecondFor")); Assertions.assertTrue(util.getFieldNames().contains("repetitionsSecondFor"));
Assertions.assertTrue(util.getFieldNames().contains("repetitionsThirdFor"));
} }
@Test @Test
public void testFieldTypes() { public void testFieldTypes() {
Assertions.assertEquals("int", util.getFieldTypes().get(0)); Assertions.assertEquals("int", util.getFieldTypes().get(0));
Assertions.assertEquals("int", util.getFieldTypes().get(1)); Assertions.assertEquals("int", util.getFieldTypes().get(1));
Assertions.assertEquals("int", util.getFieldTypes().get(2));
} }
@Test @Test
@ -87,6 +89,7 @@ public class ByteCode_For {
try { try {
Assertions.assertEquals(0, util.getFieldValue("repetitionsFirstFor")); Assertions.assertEquals(0, util.getFieldValue("repetitionsFirstFor"));
Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor")); Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor"));
Assertions.assertEquals(0, util.getFieldValue("repetitionsThirdFor"));
} catch (Exception e) { } catch (Exception e) {
Assertions.fail(); Assertions.fail();
} }
@ -99,6 +102,7 @@ public class ByteCode_For {
Assertions.assertEquals(instance.getClass().getName(), "For"); Assertions.assertEquals(instance.getClass().getName(), "For");
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsFirstFor")); Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsFirstFor"));
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsSecondFor")); Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsSecondFor"));
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsThirdFor"));
} catch (Exception e) { } catch (Exception e) {
Assertions.fail(); Assertions.fail();
} }
@ -109,9 +113,11 @@ public class ByteCode_For {
try { try {
Assertions.assertEquals(0, util.getFieldValue("repetitionsFirstFor")); Assertions.assertEquals(0, util.getFieldValue("repetitionsFirstFor"));
Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor")); Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor"));
Assertions.assertEquals(0, util.getFieldValue("repetitionsThirdFor"));
util.invokeMethod("testFor", new Class<?>[]{}, new Object[]{}); util.invokeMethod("testFor", new Class<?>[]{}, new Object[]{});
Assertions.assertEquals(45, util.getFieldValue("repetitionsFirstFor")); Assertions.assertEquals(45, util.getFieldValue("repetitionsFirstFor"));
Assertions.assertEquals(45, util.getFieldValue("repetitionsSecondFor")); Assertions.assertEquals(45, util.getFieldValue("repetitionsSecondFor"));
Assertions.assertEquals(10, util.getFieldValue("repetitionsThirdFor"));
} catch (Exception e) { } catch (Exception e) {
Assertions.fail(); Assertions.fail();
} }

View File

@ -0,0 +1,127 @@
package E2ETests.Features;
import E2ETests.BytecodeTestUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
public class ByteCode_InDeCrement {
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/E2EFeatures/InDeCrement.java"), "InDeCrement");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("InDeCrement", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("InDeCrement", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testMethodCount() {
Assertions.assertEquals(4, util.getMethodCount());
}
@Test
public void testMethodNames() {
Assertions.assertTrue(util.getMethodNames().contains("PP"));
Assertions.assertTrue(util.getMethodNames().contains("MM"));
Assertions.assertTrue(util.getMethodNames().contains("PE"));
Assertions.assertTrue(util.getMethodNames().contains("ME"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("int", util.getMethodReturnType("PP", new Class<?>[]{int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("MM", new Class<?>[]{int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("PE", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("ME", new Class<?>[]{int.class, int.class}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(1, util.getMethodParameterCount("PP", new Class<?>[]{int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("MM", new Class<?>[]{int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("PE", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("ME", new Class<?>[]{int.class, int.class}));
} 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(), "InDeCrement");
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testPP() {
try {
Assertions.assertEquals(13, util.invokeMethod("PP", new Class<?>[]{int.class}, 12));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMM() {
try {
Assertions.assertEquals(8, util.invokeMethod("MM", new Class<?>[]{int.class}, 9));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testPE() {
try {
Assertions.assertEquals(15, util.invokeMethod("PE", new Class<?>[]{int.class, int.class}, 10, 5));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testME() {
try {
Assertions.assertEquals(7, util.invokeMethod("ME", new Class<?>[]{int.class, int.class}, 9, 2));
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -105,6 +105,48 @@ public class AST_For {
new Block( new Block(
List.of() List.of()
) )
),
new Declaration(
"k",
Type.INT
),
new For(
new Assignment(
new FieldVarAccess(
false,
null,
"k"
),
new IntLiteral(0)
),
new Binary(
new FieldVarAccess(
false,
null,
"k"
),
Operator.LT,
new IntLiteral(10)
),
new Assignment(
new FieldVarAccess(
false,
null,
"k"
),
new Binary(
new FieldVarAccess(
false,
null,
"k"
),
Operator.ADD,
new IntLiteral(1)
)
),
new Block(
List.of()
)
) )
) )
) )

View File

@ -0,0 +1,154 @@
package FeatureTestResources.AST;
import de.maishai.ast.Operator;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.List;
public class AST_InDeCrement {
public static Program get() {
return new Program(
List.of(
new Class(
"InDeCrement",
null,
List.of(),
List.of(),
List.of(
new Constructor(
"InDeCrement",
List.of(),
new Block(
List.of(
new Declaration(
"a",
Type.INT
),
new Assignment(
new FieldVarAccess(
false,
null,
"a"
),
new IntLiteral(5)
),
new Assignment(
new FieldVarAccess(
false,
null,
"a"
),
new Binary(
new FieldVarAccess(
false,
null,
"a"
),
Operator.ADD,
new IntLiteral(1)
)
),
new Assignment(
new FieldVarAccess(
false,
null,
"a"
),
new Binary(
new FieldVarAccess(
false,
null,
"a"
),
Operator.ADD,
new IntLiteral(1)
)
),
new Assignment(
new FieldVarAccess(
false,
null,
"a"
),
new Binary(
new FieldVarAccess(
false,
null,
"a"
),
Operator.SUB,
new IntLiteral(1)
)
),
new Declaration(
"b",
Type.INT
),
new Assignment(
new FieldVarAccess(
false,
null,
"b"
),
new IntLiteral(7)
),
new Assignment(
new FieldVarAccess(
false,
null,
"b"
),
new Binary(
new FieldVarAccess(
false,
null,
"b"
),
Operator.ADD,
new IntLiteral(1)
)
),
new Assignment(
new FieldVarAccess(
false,
null,
"b"
),
new Binary(
new FieldVarAccess(
false,
null,
"b"
),
Operator.SUB,
new IntLiteral(1)
)
),
new Assignment(
new FieldVarAccess(
false,
null,
"b"
),
new Binary(
new FieldVarAccess(
false,
null,
"b"
),
Operator.SUB,
new IntLiteral(1)
)
)
)
)
)
)
)
)
);
}
}

View File

@ -28,6 +28,10 @@ public class TypedAST_For {
new TypedLocalVariable( new TypedLocalVariable(
"j", "j",
Type.INT Type.INT
),
new TypedLocalVariable(
"k",
Type.INT
) )
), ),
List.of( List.of(
@ -146,6 +150,64 @@ public class TypedAST_For {
Type.VOID Type.VOID
), ),
Type.VOID Type.VOID
),
new TypedFor(
new TypedAssignment(
new TypedIntLiteral(
0,
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"k",
Type.INT
),
Type.INT
),
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"k",
Type.INT
),
Operator.LT,
new TypedIntLiteral(
10,
Type.INT
),
Type.BOOL
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"k",
Type.INT
),
Operator.ADD,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"k",
Type.INT
),
Type.INT
),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID
) )
) )
) )

View File

@ -0,0 +1,214 @@
package FeatureTestResources.TypedAST;
import de.maishai.ast.Operator;
import de.maishai.typedast.Type;
import de.maishai.typedast.typedclass.*;
import java.util.List;
public class TypedAST_InDeCrement {
public static TypedProgram get() {
return new TypedProgram(
List.of(
new TypedClass(
"InDeCrement",
List.of(),
List.of(),
List.of(
new TypedConstructor(
"InDeCrement",
List.of(),
new TypedBlock(
List.of(
new TypedLocalVariable(
"a",
Type.INT
),
new TypedLocalVariable(
"b",
Type.INT
)
),
List.of(
new TypedAssignment(
new TypedIntLiteral(
5,
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.ADD,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.ADD,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.SUB,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedIntLiteral(
7,
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Operator.ADD,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Operator.SUB,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Operator.SUB,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
)
),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("InDeCrement")
)
),
null
);
}
}

View File

@ -87,6 +87,12 @@ class ScannerParserTests {
assertEquals(AST_If.get(), resultAst); assertEquals(AST_If.get(), resultAst);
} }
@Test
void testInDeCrement() {
Program resultAst = Compiler.generateASTFromFiles(List.of("src/test/testFiles/ASTandTypedASTFeatures/InDeCrement.java"));
assertEquals(AST_InDeCrement.get(), resultAst);
}
@Test @Test
void testLogicExpr() { void testLogicExpr() {
Program resultAst = Compiler.generateASTFromFiles(List.of("src/test/testFiles/ASTandTypedASTFeatures/LogicExpr.java")); Program resultAst = Compiler.generateASTFromFiles(List.of("src/test/testFiles/ASTandTypedASTFeatures/LogicExpr.java"));

View File

@ -87,6 +87,12 @@ class TypingTests {
assertEquals(TypedAST_If.get(), resultTypedAst); assertEquals(TypedAST_If.get(), resultTypedAst);
} }
@Test
void testInDeCrement() {
TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AST_InDeCrement.get());
assertEquals(TypedAST_InDeCrement.get(), resultTypedAst);
}
@Test @Test
void testLogicExpr() { void testLogicExpr() {
TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AST_LogicExpr.get()); TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AST_LogicExpr.get());

View File

@ -5,5 +5,7 @@ public class For {
int j; int j;
for (j = 0; j < 10; j += 1) { for (j = 0; j < 10; j += 1) {
} }
for (int k = 0; k < 10; k++) {
}
} }
} }

View File

@ -0,0 +1,12 @@
public class InDeCrement {
public InDeCrement() {
int a = 5;
a++;
a++;
a--;
int b = 7;
b += 1;
b -= 1;
b -= 1;
}
}

View File

@ -1,6 +1,7 @@
public class For { public class For {
int repetitionsFirstFor; int repetitionsFirstFor;
int repetitionsSecondFor; int repetitionsSecondFor;
int repetitionsThirdFor;
public void testFor() { public void testFor() {
this.repetitionsFirstFor = 0; this.repetitionsFirstFor = 0;
@ -12,5 +13,9 @@ public class For {
for (j = 0; j < 10; j += 1) { for (j = 0; j < 10; j += 1) {
this.repetitionsSecondFor = this.repetitionsSecondFor + j; this.repetitionsSecondFor = this.repetitionsSecondFor + j;
} }
this.repetitionsThirdFor = 0;
for (int k = 0; k < 5; k++) {
this.repetitionsThirdFor = this.repetitionsThirdFor + k;
}
} }
} }

View File

@ -0,0 +1,21 @@
public class InDeCrement {
public int PP(int a) {
a++;
return a;
}
public int MM(int a) {
a--;
return a;
}
public int PE(int a, int b) {
a += b ;
return a;
}
public int ME(int a, int b) {
a -= b;
return a;
}
}