109 lines
1.8 KiB
Go
109 lines
1.8 KiB
Go
|
package gmachine_test
|
||
|
|
||
|
import (
|
||
|
"gmachine"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestNew(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
g := gmachine.New()
|
||
|
wantMemSize := gmachine.DefaultMemSize
|
||
|
gotMemSize := len(g.Memory)
|
||
|
if wantMemSize != gotMemSize {
|
||
|
t.Errorf("want %d words of memory, got %d", wantMemSize, gotMemSize)
|
||
|
}
|
||
|
var wantP uint64 = 0
|
||
|
if wantP != g.P {
|
||
|
t.Errorf("want initial P value %d, got %d", wantP, g.P)
|
||
|
}
|
||
|
var wantMemValue uint64 = 0
|
||
|
gotMemValue := g.Memory[gmachine.DefaultMemSize-1]
|
||
|
if wantMemValue != gotMemValue {
|
||
|
t.Errorf("want last memory location to contain %d, got %d", wantMemValue, gotMemValue)
|
||
|
}
|
||
|
var wantRegisterA uint64 = 0
|
||
|
gotRegisterA := g.A
|
||
|
if wantRegisterA != gotRegisterA {
|
||
|
t.Errorf("want registerA with data %d, got %d", wantRegisterA, gotRegisterA)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestHalt(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
var wants uint64 = 1
|
||
|
|
||
|
g := gmachine.New()
|
||
|
g.Run()
|
||
|
got := g.P
|
||
|
|
||
|
if wants != got {
|
||
|
t.Errorf("want P == %d, got %d", wants, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestNOOP(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
var wants uint64 = 2
|
||
|
|
||
|
g := gmachine.New()
|
||
|
g.NewProgram([]uint64{
|
||
|
gmachine.OpNOOP,
|
||
|
gmachine.OpHALT,
|
||
|
})
|
||
|
got := g.P
|
||
|
|
||
|
if wants != got {
|
||
|
t.Errorf("want P == %d, got %d", wants, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestINCA(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
var wants uint64 = 1
|
||
|
|
||
|
g := gmachine.New()
|
||
|
g.Memory[0] = gmachine.OpINCA
|
||
|
g.Run()
|
||
|
got := g.A
|
||
|
|
||
|
if wants != got {
|
||
|
t.Errorf("want A == %d, got %d", wants, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestDECA(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
var wants uint64 = 1
|
||
|
|
||
|
g := gmachine.New()
|
||
|
g.A = 2
|
||
|
g.Memory[0] = gmachine.OpDECA
|
||
|
g.Run()
|
||
|
got := g.A
|
||
|
|
||
|
if wants != got {
|
||
|
t.Errorf("want A == %d, got %d", wants, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSETA(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
var wants uint64 = 3
|
||
|
|
||
|
g := gmachine.New()
|
||
|
g.Memory[0] = gmachine.OpSETA
|
||
|
g.Memory[1] = wants
|
||
|
g.Run()
|
||
|
got := g.A
|
||
|
|
||
|
if wants != got {
|
||
|
t.Errorf("want A == %d, got %d", wants, got)
|
||
|
}
|
||
|
}
|