LDC2_W fuer Long verwendet. Tests bereinigt.

This commit is contained in:
Fayez Abu Alia 2018-08-01 14:03:23 +02:00
parent 5029cca0f1
commit deb632050a
11 changed files with 39 additions and 109 deletions

View File

@ -340,7 +340,7 @@ public class BytecodeGenMethod implements StatementVisitor {
}
private void doVisitRelOpInsn(Operator op, String typeOfBinary, Label branchLabel, Label endLabel) {
System.out.println("TypeOfBinary: " + typeOfBinary);
switch (typeOfBinary) {
case "java/lang/Long":
mv.visitInsn(Opcodes.LCMP);
@ -970,6 +970,7 @@ public class BytecodeGenMethod implements StatementVisitor {
public void visit(Literal literal) {
Object value = literal.value;
String typeOfLiteral = getResolvedType(literal.getType());
System.out.println("typeOfLiteral :=> "+ typeOfLiteral);
// Der Wert des Literals wird auf den Stack geladen und
// geboxt, wenn es nötig ist.
loadValue(typeOfLiteral, value, false);
@ -1118,8 +1119,10 @@ public class BytecodeGenMethod implements StatementVisitor {
}
private void visitLongLiteral(Long value, boolean isLong) {
if (value < Math.pow(2, 15) || (value >= -Math.pow(2, 15)) && value < -128) {
visitShortLiteral(value.shortValue(), isLong);
if (value == 0) {
mv.visitInsn(Opcodes.LCONST_0);
} else if (value == 1) {
mv.visitInsn(Opcodes.LCONST_1);
} else {
mv.visitLdcInsn(value);
}

View File

@ -3,6 +3,7 @@ package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
@ -33,8 +34,10 @@ public class BinaryTest {
}
@Test
public void test() {
fail("Not yet implemented");
public void test() throws Exception {
Method m2 = classToTest.getDeclaredMethod("m2", Integer.class,Integer.class);
Integer res = (Integer) m2.invoke(instanceOfClass, 2,3);
assertEquals(6, res);
}
}

View File

@ -33,12 +33,12 @@ public class FacTest {
classToTest = loader.loadClass("Fac");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
public void testInteger() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method getFac = classToTest.getDeclaredMethod("getFac", Integer.class);
Double result = (Double) getFac.invoke(instanceOfClass,3);
assertEquals(result, 6.0);
Integer result = (Integer) getFac.invoke(instanceOfClass,3);
assertEquals(result, 6);
}
}

View File

@ -16,26 +16,15 @@ public class GenTest {
private static String path;
private static File fileToTest;
private static JavaTXCompiler compiler;
private static ClassLoader loader;
private static Class<?> classToTest;
private static String pathToClassFile;
private static Object instanceOfClass;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
@Test
public void generateBC() throws Exception {
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Gen.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
compiler.generateBytecode(pathToClassFile);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("Gen");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void test() {
fail("Not yet implemented");
}
}

View File

@ -1,41 +0,0 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import org.junit.BeforeClass;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
public class StaticTest {
private static String path;
private static File fileToTest;
private static JavaTXCompiler compiler;
private static ClassLoader loader;
private static Class<?> classToTest;
private static String pathToClassFile;
private static Object instanceOfClass;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/StaticM.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
compiler.generateBytecode(pathToClassFile);
loader = new URLClassLoader(new URL[] {new URL("file://" + pathToClassFile)});
classToTest = loader.loadClass("StaticM");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void test() {
fail("Not yet implemented");
}
}

View File

@ -17,53 +17,15 @@ public class Tph3Test {
private static String path;
private static File fileToTest;
private static JavaTXCompiler compiler;
private static ClassLoader loader;
private static Class<?> classToTest;
private static String pathToClassFile;
private static Object instanceOfClass;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
@Test
public void generateBC() throws Exception {
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Tph3.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
compiler.generateBytecode(pathToClassFile);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("Tph3");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void test1() throws Exception {
Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class);
Object result = m.invoke(instanceOfClass, 1,2);
assertEquals(1,result);
}
@Test
public void test2() throws Exception {
Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class);
Object result = m.invoke(instanceOfClass, "sss",2);
assertEquals("sss",result);
}
@Test
public void test3() throws Exception {
Method m = classToTest.getDeclaredMethod("m2", Object.class, Object.class);
Object result = m.invoke(instanceOfClass, 1,2);
assertEquals(2,result);
}
@Test
public void test4() throws Exception {
Method m = classToTest.getDeclaredMethod("m2", Object.class, Object.class);
Object result = m.invoke(instanceOfClass, 1,"xxx");
assertEquals("xxx",result);
}
}

View File

@ -40,5 +40,19 @@ public class WhileTest {
Integer result = (Integer) m.invoke(instanceOfClass, 0);
assertEquals(2, result);
}
@Test
public void testDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m", Double.class);
Double result = (Double) m.invoke(instanceOfClass, 0.0);
assertEquals(2.0, result);
}
@Test
public void testLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m", Long.class);
Long result = (Long) m.invoke(instanceOfClass, 0l);
assertEquals(2l, result);
}
}

View File

@ -1,4 +1,6 @@
import java.lang.Integer;
import java.lang.Double;
public class BinaryInMeth {
m(a){

View File

@ -1,6 +1,5 @@
import java.lang.Integer;
import java.lang.Long;
import java.lang.Double;
//import java.lang.Double;
public class Fac {

View File

@ -1,4 +1,5 @@
import java.lang.Integer;
import java.lang.String;
public class Plus {

View File

@ -1,11 +1,9 @@
import java.lang.Integer;
// wenn nur ein Import da steht,wird die Type von
// dem Literal 2 Number berechnet => Deswegen kann
// nicht auf den Stack geladen.
import java.lang.Long;
import java.lang.Double;
public class While {
Integer m(x) {
m(x) {
while(x < 2) {
x = x+1;
}