Skip to content

Commit

Permalink
JACOBIN-592 Cleaning up last few of the new unit tests that refer to …
Browse files Browse the repository at this point in the history
…runFrame()
  • Loading branch information
platypusguy committed Nov 7, 2024
1 parent 7d7ffef commit 4ed3aca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/config/buildno.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

package config

var BuildNo = 3195
var BuildNo = 3196
14 changes: 9 additions & 5 deletions src/jvm/interpreter_A-E_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package jvm

import (
"io"
"jacobin/classloader"
"jacobin/frames"
"jacobin/globals"
Expand Down Expand Up @@ -404,27 +405,30 @@ func TestNewCheckcastOfInvalidReference(t *testing.T) {

// redirect stderr to avoid printing error message to console
normalStderr := os.Stderr
_, w, _ := os.Pipe()
r, w, _ := os.Pipe()
os.Stderr = w

f := newFrame(opcodes.CHECKCAST)
push(&f, float64(42.0)) // this should cause the error

fs := frames.CreateFrameStack()
fs.PushFront(&f) // push the new frame
err := runFrame(fs)
interpret(fs)

_ = w.Close()
msg, _ := io.ReadAll(r)
os.Stderr = normalStderr

os.Stderr = normalStderr // restore stderr
errMsg := string(msg)

if err == nil {
if errMsg == "" {
t.Errorf("CHECKCAST: Expected an error, but did not get one")
}

if f.TOS != 0 {
t.Errorf("CHECKCAST: Expected TOS to be 0, got %d", f.TOS)
}

errMsg := err.Error()
if !strings.Contains(errMsg, "CHECKCAST: Invalid class reference") {
t.Errorf("CHECKCAST: Expected different error message. Got: %s", errMsg)
}
Expand Down
29 changes: 17 additions & 12 deletions src/jvm/interpreter_II-LD_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package jvm

import (
"io"
"jacobin/classloader"
"jacobin/frames"
"jacobin/globals"
Expand Down Expand Up @@ -940,7 +941,7 @@ func TestNewLdcInvalidDouble(t *testing.T) {

// hide the error message to stderr
normalStderr := os.Stderr
_, w, _ := os.Pipe()
r, w, _ := os.Pipe()
os.Stderr = w

f := newFrame(opcodes.LDC)
Expand All @@ -964,15 +965,17 @@ func TestNewLdcInvalidDouble(t *testing.T) {

fs := frames.CreateFrameStack()
fs.PushFront(&f) // push the new frame
ret := runFrame(fs)
interpret(fs)

// restore stderr
_ = w.Close()
msg, _ := io.ReadAll(r)
os.Stderr = normalStderr

if ret != nil {
if !strings.Contains(ret.Error(), "LDC: Invalid type") {
t.Errorf("Did not get expected error from LDC with double value, got: %s", ret.Error())
errMsg := string(msg)

if errMsg != "" {
if !strings.Contains(errMsg, "LDC: Invalid type") {
t.Errorf("Did not get expected error from LDC with double value, got: %s", errMsg)
}
} else {
t.Errorf("Did not get expected error from LDC with double value")
Expand Down Expand Up @@ -1123,7 +1126,7 @@ func TestNewLdc2wInvalidForString(t *testing.T) {

// hide the error message to stderr
normalStderr := os.Stderr
_, w, _ := os.Pipe()
r, w, _ := os.Pipe()
os.Stderr = w

f := newFrame(opcodes.LDC2_W)
Expand All @@ -1148,15 +1151,17 @@ func TestNewLdc2wInvalidForString(t *testing.T) {

fs := frames.CreateFrameStack()
fs.PushFront(&f) // push the new frame
ret := runFrame(fs)
interpret(fs)

// restore stderr
_ = w.Close()
msg, _ := io.ReadAll(r)
os.Stderr = normalStderr

if ret != nil {
if !strings.Contains(ret.Error(), "LDC2_W: Invalid type") {
t.Errorf("Did not get expected error from LDC with double value, got: %s", ret.Error())
errMsg := string(msg)

if errMsg != "" {
if !strings.Contains(errMsg, "LDC2_W: Invalid type") {
t.Errorf("Did not get expected error from LDC with double value, got: %s", errMsg)
}
} else {
t.Errorf("Did not get expected error message in TestLdc2wInvalidForString()")
Expand Down
17 changes: 14 additions & 3 deletions src/jvm/interpreter_LL-end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ func TestNewMonitorExit(t *testing.T) {

// NEW: Instantiate object -- here with an error
func TestNewNewWithError(t *testing.T) {
globals.InitGlobals("test")

normalStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

f := newFrame(opcodes.NEW)
f.Meth = append(f.Meth, 0x00)
f.Meth = append(f.Meth, 0x01) // Go to slot 0x0001 in the CP
Expand All @@ -521,13 +527,18 @@ func TestNewNewWithError(t *testing.T) {

fs := frames.CreateFrameStack()
fs.PushFront(&f) // push the new frame
err := runFrame(fs)
interpret(fs)

if err == nil {
_ = w.Close()
msg, _ := io.ReadAll(r)
os.Stderr = normalStderr

errMsg := string(msg)

if errMsg == "" {
t.Errorf("NEW: Expected error message, but got none")
}

errMsg := err.Error()
if !strings.Contains(errMsg, "Invalid type for new object") {
t.Errorf("NEW: got unexpected error message: %s", errMsg)
}
Expand Down

0 comments on commit 4ed3aca

Please sign in to comment.