test update

This commit is contained in:
KingJulian 2024-06-19 15:26:41 +02:00
parent e70580ac86
commit eaac9898f4
6 changed files with 134 additions and 26 deletions

View File

@ -14,7 +14,7 @@ public class testAll {
}
@Test
public void TestAstGeneration()
public void TestEmptyClass()
{
System.out.println("Current working directory: " + new File(".").getAbsolutePath());
Program testEmptyClassAST = emptyClassAST.getEmptyProgramm();

View File

@ -5,33 +5,49 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileClassLoader extends ClassLoader {
private final String classFilePath;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public FileClassLoader(String classFilePath) {
this.classFilePath = classFilePath;
public class FileClassLoader extends ClassLoader {
private final String jarFilePath;
public FileClassLoader(String jarFilePath) {
this.jarFilePath = jarFilePath;
System.out.println("Jar File Path: " + jarFilePath);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String classFileName = name.replace('.', '/') + ".class";
try {
byte[] classData = loadClassData();
byte[] classData = loadClassDataFromJar(classFileName);
return defineClass(name, classData, 0, classData.length);
} catch (IOException e) {
throw new ClassNotFoundException("Class not found", e);
}
}
private byte[] loadClassData() throws IOException {
File file = new File(classFilePath);
try (FileInputStream fis = new FileInputStream(file);
private byte[] loadClassDataFromJar(String classFileName) throws IOException {
try (JarFile jarFile = new JarFile(new File(jarFilePath))) {
JarEntry entry = jarFile.getJarEntry(classFileName);
if (entry == null) {
throw new IOException("Class file " + classFileName + " not found in jar file.");
}
try (InputStream is = jarFile.getInputStream(entry);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
while ((bytesRead = is.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
}
}
}

View File

@ -1,19 +1,99 @@
package ByteCode;
import abstractSyntaxTree.Program;
import astGenerator.ASTGenerator;
import gen.DecafLexer;
import gen.DecafParser;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import ASTs.emptyClassAST;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class TestAll {
CompareByteCodeBehaviour byteCodeBehaviourComparer;
private final String codeGenOutputLocation = "output.jar";
public TestAll(){
byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
}
public void testByteCode(String classPath1, String classPath2, String className){
public void testByteCodeFromAst(String classPath, Program abstractSyntaxTree, String className) {
try {
FileClassLoader classLoader1 = new FileClassLoader(classPath1);
FileClassLoader classLoader2 = new FileClassLoader(classPath2);
abstractSyntaxTree.codeGen();
} catch (Exception e){
System.out.println("Le Exception in le codegen");
}
try {
FileClassLoader classLoader1 = new FileClassLoader(classPath);
FileClassLoader classLoader2 = new FileClassLoader(codeGenOutputLocation);
Class<?> class1 = classLoader1.findClass(className);
Class<?> class2 = classLoader2.findClass(className);
assertTrue(CompareByteCodeSyntax.haveSameBehavior(class1, class2));
assertTrue(byteCodeBehaviourComparer.compareMethodBehaviour(class1, class2));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void testByteCodeFromScratch(String correctClassFilePath, String fileToComparePath, String className) {
String content = "";
try {
System.out.println("Classpath: " + Path.of(fileToComparePath));
content = Files.readString(Path.of(fileToComparePath));
} catch (IOException e) {
System.out.println("File not found!");
fail();
}
CharStream codeCharStream = CharStreams.fromString(content);
DecafLexer lexer = new DecafLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
DecafParser parser = new DecafParser(tokens);
ParseTree tree = parser.program();
ASTGenerator generator = new ASTGenerator();
Program abstractSyntaxTree = (Program) generator.visit(tree);
try {
abstractSyntaxTree.typeCheck();
} catch (Exception e){
System.out.println("Le Exception in le type-check");
}
try {
abstractSyntaxTree.codeGen();
} catch (Exception e){
System.out.println("Le Exception in le codegen");
}
try {
FileClassLoader classLoader1 = new FileClassLoader(correctClassFilePath);
FileClassLoader classLoader2 = new FileClassLoader(codeGenOutputLocation);
Class<?> class1 = classLoader1.findClass(className);
Class<?> class2 = classLoader2.findClass(className);
@ -27,12 +107,13 @@ public class TestAll {
}
@Test
public void testEmptyClass() throws Exception {
String classPath1 = "src/test/resources/basicClasses/emptyClass.class";
String classPath2 = "src/test/resources/basicClasses/emptyClass.class";
public void testEmptyClass() {
String classPath = "src/test/resources/basicClasses/emptyClass.jar";
Program ast = emptyClassAST.getEmptyProgramm();
String className = "emptyClass";
testByteCode(classPath1, classPath2, "emptyClass");
String javacode = "src/test/resources/basicClasses/emptyClass.java";
//testByteCodeFromAst(classPath, ast ,className);
testByteCodeFromScratch(classPath, javacode, className);
}

View File

@ -5,7 +5,7 @@ import org.junit.Test;
public class TestAll {
JavaLexerTest testLexer;
public TestAll(){
testLexer = new JavaLexerTest("test/resources");
testLexer = new JavaLexerTest("./src/test/resources");
}
private static void main(String[] args){

View File

@ -1,5 +1,16 @@
package Typecheck;
import ASTs.emptyClassAST;
import abstractSyntaxTree.Program;
import org.junit.Test;
import java.lang.reflect.Type;
public class TestAll {
@Test
public void TestEmptyClass() throws Exception {
Program emptyAst = emptyClassAST.getEmptyProgramm();
TypeChecker.assertTypeCheckResult(emptyAst, true);
}
}

View File

@ -16,7 +16,7 @@ import static org.junit.Assert.assertEquals;
public class TypeChecker {
// Method to test if the TypeCheck returns the expected Result
public void assertTypeCheckResult(Program programmToBeTested, boolean expectedResult) throws Exception {
static public void assertTypeCheckResult(Program programmToBeTested, boolean expectedResult) throws Exception {
boolean actualResult;
try {
TypeCheckResult typeCheckResult = programmToBeTested.typeCheck();