Skip to content

Commit

Permalink
Merge with andrew mods
Browse files Browse the repository at this point in the history
  • Loading branch information
texadactyl committed Dec 26, 2023
2 parents a239f31 + 4a68564 commit 1ed9341
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
60 changes: 50 additions & 10 deletions src/classloader/classes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func TestInsertValid(t *testing.T) {
}
}

// TODO: This test does not appear to test what it contends. Further note:
// the coverage of the missing main() method is tested below and is the test
// responsible for code coverage of the missing main() method, not this one.
func TestInvalidLookupOfMethod_Test0(t *testing.T) {
// Testing the changes made as a result of JACOBIN-103
globals.InitGlobals("test")
Expand Down Expand Up @@ -133,7 +136,7 @@ func TestInvalidLookupOfMethod_Test1(t *testing.T) {
os.Stdout = wout

MethArea = &sync.Map{}
currLen := MethAreaSize()

k := Klass{
Status: 0,
Loader: "",
Expand All @@ -149,12 +152,6 @@ func TestInvalidLookupOfMethod_Test1(t *testing.T) {
// in the MethArea. It's only a placeholder
MethAreaInsert("java/lang/Object", &k)

newLen := MethAreaSize()
if newLen != currLen+2 {
t.Errorf("Expected post-insertion MethArea[] to have length of %d, got: %d",
currLen+1, newLen)
}

_, err := FetchMethodAndCP("TestEntry", "main", "([L)V")
if err == nil {
t.Errorf("Expecting an err msg for invalid MethAreaFetch of main() in MTable, but got none")
Expand Down Expand Up @@ -258,17 +255,17 @@ func TestFetchUTF8stringFromCPEntryNumber(t *testing.T) {

s := FetchUTF8stringFromCPEntryNumber(&cp, 0) // invalid CP entry
if s != "" {
t.Error("Unexpected result in call toFetchUTF8stringFromCPEntryNumber()")
t.Error("Unexpected result in call to FetchUTF8stringFromCPEntryNumber()")
}

s = FetchUTF8stringFromCPEntryNumber(&cp, 1)
if s != "Exceptions" {
t.Error("Unexpected result in call toFetchUTF8stringFromCPEntryNumber()")
t.Error("Unexpected result in call to FetchUTF8stringFromCPEntryNumber()")
}

s = FetchUTF8stringFromCPEntryNumber(&cp, 2) // not UTF8, so should be an error
if s != "" {
t.Error("Unexpected result in call toFetchUTF8stringFromCPEntryNumber()")
t.Error("Unexpected result in call to FetchUTF8stringFromCPEntryNumber()")
}

_ = w.Close()
Expand All @@ -277,3 +274,46 @@ func TestFetchUTF8stringFromCPEntryNumber(t *testing.T) {
_ = wout.Close()
os.Stdout = normalStdout
}

func TestInvalMainMethod(t *testing.T) {
// Testing the changes made as a result of JACOBIN-103
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.FINE)

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
_, w, _ := os.Pipe()
os.Stderr = w

MethArea = &sync.Map{}
k := Klass{
Status: 0,
Loader: "",
Data: &ClData{},
}
k.Data.Name = "testClass"
k.Data.Superclass = "java/lang/Object"
k.Loader = "testloader"
k.Status = 'F'
MethAreaInsert("TestEntry", &k)

// we need a java/lang/Object instance, so just duplicate the entry
// in the MethArea. It's only a placeholder
MethAreaInsert("java/lang/Object", &k)

// fetch a non-existent main() method
_, err := FetchMethodAndCP("java/lan/Object", "main", "([LString;)V")
if err == nil {
t.Errorf("Expecting an err msg for invalid MethAreaFetch of main(), but got none")
}

msg := err.Error()
if !strings.Contains(msg, "main() method not found") {
t.Errorf("TestInvalidLookupOfMethod: Expecting error of 'main() method not found', got %s", err.Error())
}

// restore stderr and stdout to what they were before
_ = w.Close()
os.Stderr = normalStderr
}
33 changes: 33 additions & 0 deletions src/gfunction/javaLangThrowable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Jacobin VM - A Java virtual machine
* Copyright (c) 2023 by the Jacobin Authors. All rights reserved.
* Licensed under Mozilla Public License 2.0 (MPL 2.0) Consult jacobin.org.
*/

package gfunction

import (
"jacobin/statics"
"testing"
)

func TestJavaLangThrowableClinit(t *testing.T) {
statics.Statics = make(map[string]statics.Static)

throwableClinit(nil)
_, ok := statics.Statics["Throwable.UNASSIGNED_STACK"]
if !ok {
t.Error("JavaLangThrowableClinit: Throwable.UNASSIGNED_STACK not found")
}

_, ok = statics.Statics["Throwable.SUPPRESSED_SENTINEL"]
if !ok {
t.Error("JavaLangThrowableClinit: Throwable.SUPPRESSED_SENTINEL not found")
}

_, ok = statics.Statics["Throwable.EMPTY_THROWABLE_ARRAY"]
if !ok {
t.Error("Throwable.EMPTY_THROWABLE_ARRAY not found")
}

}
2 changes: 0 additions & 2 deletions src/statics/statics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
// Statics are placed into this map only when they are first referenced and resolved.
var Statics = make(map[string]Static)

// var StaticsArray []Static

// Static contains all the various items needed for a static variable or function.
type Static struct {
Type string // see the possible returns in types/javatypes.go
Expand Down

0 comments on commit 1ed9341

Please sign in to comment.