MiniJavaCompiler/Test/TestByteCodeGenerator.hs

116 lines
3.6 KiB
Haskell
Raw Normal View History

2024-05-07 12:27:20 +00:00
module TestByteCodeGenerator where
import Test.HUnit
import ByteCode.ClassFile.Generator
import ByteCode.ClassFile
import ByteCode.Constants
import Ast
nakedClass = Class "Testklasse" [] []
expectedClass = ClassFile {
constantPool = [
ClassInfo 4,
MethodRefInfo 1 3,
NameAndTypeInfo 5 6,
Utf8Info "java/lang/Object",
Utf8Info "<init>",
Utf8Info "()V",
2024-05-07 14:32:36 +00:00
Utf8Info "Code",
ClassInfo 9,
2024-05-07 12:27:20 +00:00
Utf8Info "Testklasse"
],
accessFlags = accessPublic,
2024-05-07 14:32:36 +00:00
thisClass = 8,
2024-05-07 12:27:20 +00:00
superClass = 1,
fields = [],
methods = [],
attributes = []
}
classWithFields = Class "Testklasse" [] [VariableDeclaration "int" "testvariable" Nothing]
expectedClassWithFields = ClassFile {
constantPool = [
ClassInfo 4,
MethodRefInfo 1 3,
NameAndTypeInfo 5 6,
Utf8Info "java/lang/Object",
Utf8Info "<init>",
Utf8Info "()V",
2024-05-07 14:32:36 +00:00
Utf8Info "Code",
ClassInfo 9,
2024-05-07 12:27:20 +00:00
Utf8Info "Testklasse",
2024-05-07 14:32:36 +00:00
FieldRefInfo 8 11,
NameAndTypeInfo 12 13,
2024-05-07 12:27:20 +00:00
Utf8Info "testvariable",
Utf8Info "I"
],
accessFlags = accessPublic,
2024-05-07 14:32:36 +00:00
thisClass = 8,
2024-05-07 12:27:20 +00:00
superClass = 1,
fields = [
MemberInfo {
memberAccessFlags = accessPublic,
2024-05-07 14:32:36 +00:00
memberNameIndex = 12,
memberDescriptorIndex = 13,
2024-05-07 12:27:20 +00:00
memberAttributes = []
}
],
methods = [],
attributes = []
}
2024-05-07 14:32:36 +00:00
method = MethodDeclaration "int" "add_two_numbers" [
ParameterDeclaration "int" "a",
ParameterDeclaration "int" "b" ]
(Block [Return (Just (BinaryOperation Addition (Reference "a") (Reference "b")))])
classWithMethod = Class "Testklasse" [method] []
expectedClassWithMethod = ClassFile {
constantPool = [
ClassInfo 4,
MethodRefInfo 1 3,
NameAndTypeInfo 5 6,
Utf8Info "java/lang/Object",
Utf8Info "<init>",
Utf8Info "()V",
Utf8Info "Code",
ClassInfo 9,
Utf8Info "Testklasse",
FieldRefInfo 8 11,
NameAndTypeInfo 12 13,
Utf8Info "add_two_numbers",
Utf8Info "(II)I"
],
accessFlags = accessPublic,
thisClass = 8,
superClass = 1,
fields = [],
methods = [
MemberInfo {
memberAccessFlags = accessPublic,
memberNameIndex = 12,
memberDescriptorIndex = 13,
memberAttributes = [
CodeAttribute {
attributeMaxStack = 420,
attributeMaxLocals = 420,
attributeCode = [Opiadd]
}
]
}
],
attributes = []
}
2024-05-07 12:27:20 +00:00
testBasicConstantPool = TestCase $ assertEqual "basic constant pool" expectedClass $ classBuilder nakedClass emptyClassFile
testFields = TestCase $ assertEqual "fields in constant pool" expectedClassWithFields $ classBuilder classWithFields emptyClassFile
2024-05-07 14:32:36 +00:00
testMethodDescriptor = TestCase $ assertEqual "method descriptor" "(II)I" (methodDescriptor method)
testMethodAssembly = TestCase $ assertEqual "method assembly" expectedClassWithMethod (classBuilder classWithMethod emptyClassFile)
2024-05-07 12:27:20 +00:00
tests = TestList [
TestLabel "Basic constant pool" testBasicConstantPool,
2024-05-07 14:32:36 +00:00
TestLabel "Fields constant pool" testFields,
TestLabel "Method descriptor building" testMethodDescriptor,
TestLabel "Method assembly" testMethodAssembly
2024-05-07 12:27:20 +00:00
]