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 @Test
public void TestAstGeneration() public void TestEmptyClass()
{ {
System.out.println("Current working directory: " + new File(".").getAbsolutePath()); System.out.println("Current working directory: " + new File(".").getAbsolutePath());
Program testEmptyClassAST = emptyClassAST.getEmptyProgramm(); Program testEmptyClassAST = emptyClassAST.getEmptyProgramm();

View File

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

View File

@ -1,19 +1,99 @@
package ByteCode; 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 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 { public class TestAll {
CompareByteCodeBehaviour byteCodeBehaviourComparer; CompareByteCodeBehaviour byteCodeBehaviourComparer;
private final String codeGenOutputLocation = "output.jar";
public TestAll(){ public TestAll(){
byteCodeBehaviourComparer = new CompareByteCodeBehaviour(); byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
} }
public void testByteCode(String classPath1, String classPath2, String className){ public void testByteCodeFromAst(String classPath, Program abstractSyntaxTree, String className) {
try { try {
FileClassLoader classLoader1 = new FileClassLoader(classPath1); abstractSyntaxTree.codeGen();
FileClassLoader classLoader2 = new FileClassLoader(classPath2); } 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<?> class1 = classLoader1.findClass(className);
Class<?> class2 = classLoader2.findClass(className); Class<?> class2 = classLoader2.findClass(className);
@ -27,12 +107,13 @@ public class TestAll {
} }
@Test @Test
public void testEmptyClass() throws Exception { public void testEmptyClass() {
String classPath1 = "src/test/resources/basicClasses/emptyClass.class"; String classPath = "src/test/resources/basicClasses/emptyClass.jar";
String classPath2 = "src/test/resources/basicClasses/emptyClass.class"; Program ast = emptyClassAST.getEmptyProgramm();
String className = "emptyClass"; String className = "emptyClass";
String javacode = "src/test/resources/basicClasses/emptyClass.java";
testByteCode(classPath1, classPath2, "emptyClass"); //testByteCodeFromAst(classPath, ast ,className);
testByteCodeFromScratch(classPath, javacode, className);
} }

View File

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

View File

@ -1,5 +1,16 @@
package Typecheck; package Typecheck;
import ASTs.emptyClassAST;
import abstractSyntaxTree.Program;
import org.junit.Test;
import java.lang.reflect.Type;
public class TestAll { 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 { public class TypeChecker {
// Method to test if the TypeCheck returns the expected Result // 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; boolean actualResult;
try { try {
TypeCheckResult typeCheckResult = programmToBeTested.typeCheck(); TypeCheckResult typeCheckResult = programmToBeTested.typeCheck();