Skip to content

Commit

Permalink
JACOBIN-552 Adding unit tests for cpUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
platypusguy committed Aug 16, 2024
1 parent 681f0e1 commit 3091ab4
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/classloader/cpUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package classloader

import (
"jacobin/frames"
"jacobin/globals"
"jacobin/log"
"jacobin/types"
"os"
"testing"
)

Expand Down Expand Up @@ -42,3 +45,59 @@ func TestMeInfoFromMethRefInvalid(t *testing.T) {
t.Errorf("Did not get expected result for pointing to CPentry that's not a MethodRef")
}
}

func TestMeInfoFromMethRefValid(t *testing.T) {
globals.InitGlobals("test")

// redirect stderr so as not to pollute the test output with the expected error message
normalStderr := os.Stderr
_, w, _ := os.Pipe()
os.Stderr = w

// Initialize classloaders and method area
err := Init()
if err != nil {
t.Errorf("Failure to load classes in TestMeInfoFromMethRefValid")
}
LoadBaseClasses() // must follow classloader.Init()

f := frames.CreateFrame(4)
f.Meth = append(f.Meth, 0x00)
f.Meth = append(f.Meth, 0x01) // Go to slot 0x0001 in the CP

CP := CPool{}
CP.CpIndex = make([]CpEntry, 10)
CP.CpIndex[0] = CpEntry{Type: 0, Slot: 0}
CP.CpIndex[1] = CpEntry{Type: MethodRef, Slot: 0}

CP.MethodRefs = make([]MethodRefEntry, 1)
CP.MethodRefs[0] = MethodRefEntry{ClassIndex: 2, NameAndType: 3}

CP.CpIndex[2] = CpEntry{Type: ClassRef, Slot: 0}
CP.ClassRefs = make([]uint32, 4)
CP.ClassRefs[0] = types.ObjectPoolStringIndex

CP.CpIndex[3] = CpEntry{Type: NameAndType, Slot: 0}
CP.NameAndTypes = make([]NameAndTypeEntry, 4)
CP.NameAndTypes[0] = NameAndTypeEntry{
NameIndex: 4,
DescIndex: 5,
}
CP.CpIndex[4] = CpEntry{Type: UTF8, Slot: 0} // method name
CP.Utf8Refs = make([]string, 4)
CP.Utf8Refs[0] = "<init>"

CP.CpIndex[5] = CpEntry{Type: UTF8, Slot: 1} // method name
CP.Utf8Refs[1] = "()V"

f.CP = &CP

_, s2, s3 := GetMethInfoFromCPmethref(&CP, 1)
if s2 != "<init>" && s3 != "()V" {
t.Errorf("Expect to get a method: <init>()V, got %s%s", s2, s2)
}

// restore stderr
_ = w.Close()
os.Stderr = normalStderr
}

0 comments on commit 3091ab4

Please sign in to comment.