added some e2e tests

This commit is contained in:
JonathanFleischmann 2024-06-28 21:31:01 +02:00
parent 88061275b8
commit 133e7824ba
5 changed files with 379 additions and 741 deletions

View File

@ -8,11 +8,14 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BytecodeTestUtil {
private Class<?> clazz;
private Object instance;
private final Class<?> clazz;
private final Object instance;
private static final Logger LOGGER = Logger.getLogger(BytecodeTestUtil.class.getName());
public BytecodeTestUtil(List<String> sourceFilePaths, String className) throws Exception {
byte[] resultBytecode = Compiler.generateByteCodeArrayFromFiles(sourceFilePaths).get(0);
@ -23,8 +26,14 @@ public class BytecodeTestUtil {
return defineClass(name, resultBytecode, 0, resultBytecode.length);
}
};
clazz = classLoader.loadClass(className);
this.instance = clazz.getDeclaredConstructor().newInstance();
try {
clazz = classLoader.loadClass(className);
this.instance = clazz.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException | SecurityException | NoSuchMethodException e) {
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
}
}
@ -76,15 +85,25 @@ public class BytecodeTestUtil {
}
public int getMethodParameterCount(String methodName, Class<?>... params) throws Exception {
return clazz.getMethod(methodName, params).getParameterCount();
try {
return clazz.getMethod(methodName, params).getParameterCount();
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
}
}
public List<String> getMethodParameterTypes(String methodName, Class<?>... params) throws Exception {
List<String> parameterTypes = new ArrayList<>();
for (Class<?> parameterType : clazz.getMethod(methodName, params).getParameterTypes()) {
parameterTypes.add(parameterType.getSimpleName());
public List<String> getMethodParameterTypes(String methodName, Class<?>... params) {
try {
List<String> parameterTypes = new ArrayList<>();
for (Class<?> parameterType : clazz.getMethod(methodName, params).getParameterTypes()) {
parameterTypes.add(parameterType.getSimpleName());
}
return parameterTypes;
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
}
return parameterTypes;
}
public Object invokeMethod(String methodName, Class<?>[] parameterTypes, Object... args) throws Exception {
@ -92,7 +111,7 @@ public class BytecodeTestUtil {
Method method = clazz.getMethod(methodName, parameterTypes);
return method.invoke(instance, args);
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e.getTargetException());
}
}
@ -123,7 +142,7 @@ public class BytecodeTestUtil {
try {
return clazz.getField(fieldName).get(instance);
} catch (IllegalAccessException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
}
}

View File

@ -3,212 +3,161 @@ 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_MethodCall {
public static TypedProgram get() {
return new TypedProgram(
List.of(
new TypedClass(
"MethodCall",
List.of(),
List.of(
new TypedMethod(
"methodCall",
Type.INT,
List.of(),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"method",
Type.INT
),
List.of(),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"methodCall1",
Type.INT,
List.of(),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"method1",
Type.INT
),
List.of(
new TypedIntLiteral(
1,
Type.INT
)
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"methodCall2",
Type.INT,
List.of(),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"method2",
Type.INT
),
List.of(
new TypedIntLiteral(
1,
Type.INT
),
new TypedIntLiteral(
2,
Type.INT
)
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"method",
Type.INT,
List.of(),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedIntLiteral(
0,
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"method1",
Type.INT,
List.of(
new TypedParameter(
"x",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedFieldVarAccess(
false,
null,
"x",
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"method2",
Type.INT,
List.of(
new TypedParameter(
"x",
Type.INT
),
new TypedParameter(
"y",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"x",
Type.INT
),
Operator.ADD,
new TypedFieldVarAccess(
false,
null,
"y",
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
)
),
List.of(
new TypedConstructor(
"MethodCall",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("MethodCall")
)
),
null
);
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/MethodCall.java"), "MethodCall");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("MethodCall", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("MethodCall", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodCount() {
Assertions.assertEquals(6, util.getMethodCount());
}
@Test
public void testMethodNames() {
Assertions.assertTrue(util.getMethodNames().contains("methodCall"));
Assertions.assertTrue(util.getMethodNames().contains("methodCall1"));
Assertions.assertTrue(util.getMethodNames().contains("methodCall2"));
Assertions.assertTrue(util.getMethodNames().contains("method"));
Assertions.assertTrue(util.getMethodNames().contains("method1"));
Assertions.assertTrue(util.getMethodNames().contains("method2"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("int", util.getMethodReturnType("methodCall", new Class<?>[]{}));
Assertions.assertEquals("int", util.getMethodReturnType("methodCall1", new Class<?>[]{}));
Assertions.assertEquals("int", util.getMethodReturnType("methodCall2", new Class<?>[]{}));
Assertions.assertEquals("int", util.getMethodReturnType("method", new Class<?>[]{}));
Assertions.assertEquals("int", util.getMethodReturnType("method1", new Class<?>[]{int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("method2", new Class<?>[]{int.class, int.class}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(0, util.getMethodParameterCount("methodCall", new Class<?>[]{}));
Assertions.assertEquals(0, util.getMethodParameterCount("methodCall1", new Class<?>[]{}));
Assertions.assertEquals(0, util.getMethodParameterCount("methodCall2", new Class<?>[]{}));
Assertions.assertEquals(0, util.getMethodParameterCount("method", new Class<?>[]{}));
Assertions.assertEquals(1, util.getMethodParameterCount("method1", new Class<?>[]{int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("method2", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("method1", new Class<?>[]{int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("method2", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("method2", new Class<?>[]{int.class, int.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(), "MethodCall");
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethod() {
try {
Object returnValue = util.invokeMethod("method", new Class<?>[]{}, null);
Assertions.assertEquals(0, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethod1() {
try {
Object returnValue = util.invokeMethod("method1", new Class<?>[]{int.class}, new Object[]{3});
Assertions.assertEquals(3, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethod2() {
try {
Object returnValue = util.invokeMethod("method2", new Class<?>[]{int.class, int.class}, new Object[]{3, 4});
Assertions.assertEquals(7, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodCall() {
try {
Object returnValue = util.invokeMethod("methodCall", new Class<?>[]{}, null);
Assertions.assertEquals(0, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodCall1() {
try {
Object returnValue = util.invokeMethod("methodCall1", new Class<?>[]{}, null);
Assertions.assertEquals(1, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodCall2() {
try {
Object returnValue = util.invokeMethod("methodCall2", new Class<?>[]{}, null);
Assertions.assertEquals(3, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -6,93 +6,5 @@ import de.maishai.typedast.typedclass.*;
import java.util.List;
public class ByteCode_MultipleClasses {
public static TypedProgram get() {
return new TypedProgram(
List.of(
new TypedClass(
"MultipleClasses",
List.of(
new TypedDeclaration(
"anotherClass",
Type.REFERENCE("AnotherClass")
)
),
List.of(),
List.of(
new TypedConstructor(
"MultipleClasses",
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedAssignment(
new TypedNew(
Type.REFERENCE("AnotherClass"),
List.of()
),
new TypedFieldVarAccess(
true,
null,
"anotherClass",
Type.REFERENCE("AnotherClass")
),
Type.REFERENCE("AnotherClass")
)
),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("MultipleClasses")
),
new TypedClass(
"AnotherClass",
List.of(
new TypedDeclaration(
"multipleClasses",
Type.REFERENCE("MultipleClasses")
)
),
List.of(),
List.of(
new TypedConstructor(
"AnotherClass",
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedAssignment(
new TypedNew(
Type.REFERENCE("MultipleClasses"),
List.of()
),
new TypedFieldVarAccess(
true,
null,
"multipleClasses",
Type.REFERENCE("MultipleClasses")
),
Type.REFERENCE("MultipleClasses")
)
),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("AnotherClass")
)
),
null
);
}
// TODO: implement with multiple Files
}

View File

@ -1,446 +1,200 @@
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_Operators {
public static TypedProgram get() {
return new TypedProgram(
List.of(
new TypedClass(
"Operators",
List.of(),
List.of(
new TypedMethod(
"add",
Type.INT,
List.of(
new TypedParameter(
"a",
Type.INT
),
new TypedParameter(
"b",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.ADD,
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"sub",
Type.INT,
List.of(
new TypedParameter(
"a",
Type.INT
),
new TypedParameter(
"b",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.SUB,
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"mul",
Type.INT,
List.of(
new TypedParameter(
"a",
Type.INT
),
new TypedParameter(
"b",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.MUL,
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"div",
Type.INT,
List.of(
new TypedParameter(
"a",
Type.INT
),
new TypedParameter(
"b",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.DIV,
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"mod",
Type.INT,
List.of(
new TypedParameter(
"a",
Type.INT
),
new TypedParameter(
"b",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.MOD,
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"brackets",
Type.INT,
List.of(
new TypedParameter(
"a",
Type.INT
),
new TypedParameter(
"b",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Operator.ADD,
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
),
Type.INT
),
Operator.MUL,
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"callOperatedMethods",
Type.INT,
List.of(
new TypedParameter(
"a",
Type.INT
),
new TypedParameter(
"b",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedReturn(
new TypedBinary(
new TypedBinary(
new TypedBinary(
new TypedBinary(
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"add",
Type.INT
),
List.of(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
)
),
Type.INT
),
Operator.ADD,
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"sub",
Type.INT
),
List.of(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
)
),
Type.INT
),
Type.INT
),
Operator.ADD,
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"mul",
Type.INT
),
List.of(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
)
),
Type.INT
),
Type.INT
),
Operator.ADD,
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"div",
Type.INT
),
List.of(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
)
),
Type.INT
),
Type.INT
),
Operator.ADD,
new TypedMethodCall(
new TypedFieldVarAccess(
false,
null,
"mod",
Type.INT
),
List.of(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"b",
Type.INT
)
),
Type.INT
),
Type.INT
),
Type.INT
)
),
Type.INT
)
)
),
List.of(
new TypedConstructor(
"Operators",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("Operators")
)
),
null
);
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Operators.java"), "Operators");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("Operators", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("Operators", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodCount() {
Assertions.assertEquals(8, util.getMethodCount());
}
@Test
public void testMethodNames() {
Assertions.assertTrue(util.getMethodNames().contains("add"));
Assertions.assertTrue(util.getMethodNames().contains("sub"));
Assertions.assertTrue(util.getMethodNames().contains("mul"));
Assertions.assertTrue(util.getMethodNames().contains("div"));
Assertions.assertTrue(util.getMethodNames().contains("mod"));
Assertions.assertTrue(util.getMethodNames().contains("dotBeforeLine"));
Assertions.assertTrue(util.getMethodNames().contains("brackets"));
Assertions.assertTrue(util.getMethodNames().contains("callOperatedMethods"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("int", util.getMethodReturnType("add", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("sub", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("mul", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("div", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("mod", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("dotBeforeLine", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("brackets", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodReturnType("callOperatedMethods", new Class<?>[]{int.class, int.class}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(2, util.getMethodParameterCount("add", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("sub", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("mul", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("div", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("mod", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("dotBeforeLine", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("brackets", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(2, util.getMethodParameterCount("callOperatedMethods", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("add", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("add", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals("int", util.getMethodParameterTypes("sub", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("sub", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals("int", util.getMethodParameterTypes("mul", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("mul", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals("int", util.getMethodParameterTypes("div", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("div", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals("int", util.getMethodParameterTypes("mod", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("mod", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals("int", util.getMethodParameterTypes("dotBeforeLine", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("dotBeforeLine", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals("int", util.getMethodParameterTypes("brackets", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("brackets", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals("int", util.getMethodParameterTypes("callOperatedMethods", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("callOperatedMethods", new Class<?>[]{int.class, int.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(), "Operators");
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testAdd() {
try {
Object result = util.invokeMethod("add", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(8, result);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testSub() {
try {
Object result = util.invokeMethod("sub", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(2, result);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMul() {
try {
Object result = util.invokeMethod("mul", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(15, result);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testDiv() {
try {
Object result = util.invokeMethod("div", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(1, result);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMod() {
try {
Object result = util.invokeMethod("mod", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(2, result);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testDotBeforeLine() {
try {
Object result = util.invokeMethod("dotBeforeLine", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(20, result);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testBrackets() {
try {
Object result = util.invokeMethod("brackets", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(40, result);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testCallOperatedMethods() {
try {
Object result = util.invokeMethod("callOperatedMethods", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Assertions.assertEquals(28, result);
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -19,6 +19,10 @@ public class Operators {
return a % b;
}
public int dotBeforeLine(int a, int b) {
return a + b * a - b / a;
}
public int brackets(int a, int b) {
return (a + b) * a;
}