JavaPatternMatching/test/bytecode/MatrixTest.java

109 lines
3.6 KiB
Java
Raw Normal View History

package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.BeforeClass;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
public class MatrixTest {
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_m1;
private static Object instanceOfClass_m2;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Matrix.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("Matrix");
// instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Matrix.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("Matrix");
}
@Test
public void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IOException, InstantiationException {
// Vector<Vector<Integer>> m1 = new Vector<>();
// Vector<Integer> r1 = new Vector<>();
// r1.addElement(1);
// r1.addElement(0);
// m1.addElement(r1);
// Vector<Integer> r2 = new Vector<>();
// r2.addElement(0);
// r2.addElement(1);
// m1.add(r2);
//
// Vector<Vector<Integer>> m2 = new Vector<>();
// Vector<Integer> mr1 = new Vector<>();
// mr1.addElement(1);
// mr1.addElement(2);
// m2.add(mr1);
// Vector<Integer> mr2 = new Vector<>();
// mr2.addElement(3);
// mr2.addElement(4);
// m2.add(mr2);
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
Vector<Integer> v1 = new Vector<Integer> ();
v1.addElement(2);
v1.addElement(2);
Vector<Integer> v2 = new Vector<Integer> ();
v2.addElement(3);
v2.addElement(3);
//Matrix m1 = new Matrix();
//m1.addElement(v1);
//m1.addElement(v2);
vv.addElement(v1);
vv.addElement(v2);
instanceOfClass_m1 = classToTest.getDeclaredConstructor().newInstance(vv); //Matrix m1 = new Matrix(vv);
Vector<Vector<Integer>> vv1 = new Vector<Vector<Integer>>();
Vector<Integer> v3 = new Vector<Integer> ();
v3.addElement(2);
v3.addElement(2);
Vector<Integer> v4 = new Vector<Integer> ();
v4.addElement(3);
v4.addElement(3);
//Matrix m2 = new Matrix();
//m2.addElement(v3);
//m2.addElement(v4);
vv1.addElement(v3);
vv1.addElement(v4);
instanceOfClass_m2 = classToTest.getDeclaredConstructor().newInstance(vv1);//Matrix m2 = new Matrix(vv1);
//Matrix m3 = m1.mul(vv1);
Method m = classToTest.getDeclaredMethod("mul", Double.class);
Object result = m.invoke(instanceOfClass_m1, instanceOfClass_m2);
System.out.println(instanceOfClass_m1.toString() + " * " + instanceOfClass_m1.toString() + " = " + result.toString());
}
}