vise/vm/vm_test.go

163 lines
3.1 KiB
Go
Raw Permalink Normal View History

2023-03-31 11:52:04 +02:00
package vm
import (
"testing"
)
2023-04-02 13:12:06 +02:00
func TestParseNoArg(t *testing.T) {
b := NewLine(nil, HALT, nil, nil, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
b, err := ParseHalt(b)
2023-03-31 11:52:04 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
2023-03-31 11:52:04 +02:00
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
2023-03-31 11:52:04 +02:00
}
2023-03-31 11:59:55 +02:00
2023-04-02 13:12:06 +02:00
func TestParseSym(t *testing.T) {
b := NewLine(nil, MAP, []string{"baz"}, nil, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
sym, b, err := ParseMap(b)
2023-03-31 11:59:55 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
2023-03-31 11:59:55 +02:00
}
2023-04-02 13:12:06 +02:00
if sym != "baz" {
t.Fatalf("expected sym baz, got %v", sym)
2023-03-31 11:59:55 +02:00
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
2023-03-31 11:59:55 +02:00
2023-04-02 13:12:06 +02:00
b = NewLine(nil, RELOAD, []string{"xyzzy"}, nil, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
sym, b, err = ParseReload(b)
2023-03-31 11:59:55 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
2023-03-31 11:59:55 +02:00
}
2023-04-02 13:12:06 +02:00
if sym != "xyzzy" {
t.Fatalf("expected sym xyzzy, got %v", sym)
2023-03-31 11:59:55 +02:00
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
2023-03-31 16:24:29 +02:00
2023-04-02 13:12:06 +02:00
b = NewLine(nil, MOVE, []string{"plugh"}, nil, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
sym, b, err = ParseMove(b)
2023-03-31 16:24:29 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
}
if sym != "plugh" {
t.Fatalf("expected sym plugh, got %v", sym)
2023-03-31 16:24:29 +02:00
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
2023-03-31 16:24:29 +02:00
}
2023-04-02 13:12:06 +02:00
func TestParseTwoSym(t *testing.T) {
b := NewLine(nil, INCMP, []string{"foo", "bar"}, nil, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
one, two, b, err := ParseInCmp(b)
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
}
2023-04-02 13:12:06 +02:00
if one != "foo" {
t.Fatalf("expected symone foo, got %v", one)
}
2023-04-02 13:12:06 +02:00
if two != "bar" {
t.Fatalf("expected symtwo bar, got %v", two)
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
}
func TestParseSig(t *testing.T) {
b := NewLine(nil, CROAK, nil, []byte{0x0b, 0x13}, []uint8{0x04})
_, b, _ = opSplit(b)
n, m, b, err := ParseCroak(b)
if err != nil {
t.Fatal(err)
}
if n != 2835 {
t.Fatalf("expected n 13, got %v", n)
}
if !m {
t.Fatalf("expected m true")
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
}
2023-04-02 13:12:06 +02:00
func TestParseSymSig(t *testing.T) {
b := NewLine(nil, CATCH, []string{"baz"}, []byte{0x0a, 0x13}, []uint8{0x01})
_, b, _ = opSplit(b)
sym, n, m, b, err := ParseCatch(b)
2023-03-31 20:24:30 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
}
2023-04-02 13:12:06 +02:00
if sym != "baz" {
t.Fatalf("expected sym baz, got %v", sym)
}
if n != 2579 {
2023-04-02 13:12:06 +02:00
t.Fatalf("expected n 13, got %v", n)
}
if !m {
t.Fatalf("expected m true")
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
}
2023-03-31 20:24:30 +02:00
2023-04-02 13:12:06 +02:00
func TestParseSymAndLen(t *testing.T) {
b := NewLine(nil, LOAD, []string{"foo"}, []byte{0x2a}, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
sym, n, b, err := ParseLoad(b)
2023-03-31 20:24:30 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
2023-03-31 20:24:30 +02:00
}
2023-04-02 13:12:06 +02:00
if sym != "foo" {
t.Fatalf("expected sym foo, got %v", sym)
2023-03-31 20:24:30 +02:00
}
2023-04-02 13:12:06 +02:00
if n != 42 {
t.Fatalf("expected n 42, got %v", n)
2023-03-31 20:24:30 +02:00
}
2023-04-02 13:12:06 +02:00
b = NewLine(nil, LOAD, []string{"bar"}, []byte{0x02, 0x9a}, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
sym, n, b, err = ParseLoad(b)
2023-04-01 22:25:20 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
2023-04-01 22:25:20 +02:00
}
2023-04-02 13:12:06 +02:00
if sym != "bar" {
t.Fatalf("expected sym foo, got %v", sym)
}
if n != 666 {
t.Fatalf("expected n 666, got %v", n)
2023-04-01 22:25:20 +02:00
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
2023-04-01 22:25:20 +02:00
2023-04-02 13:12:06 +02:00
b = NewLine(nil, LOAD, []string{"baz"}, []byte{0x0}, nil)
_, b, _ = opSplit(b)
2023-04-02 13:12:06 +02:00
sym, n, b, err = ParseLoad(b)
2023-04-01 22:25:20 +02:00
if err != nil {
2023-04-02 13:12:06 +02:00
t.Fatal(err)
2023-04-01 22:25:20 +02:00
}
2023-04-02 13:12:06 +02:00
if sym != "baz" {
t.Fatalf("expected sym foo, got %v", sym)
2023-04-01 22:25:20 +02:00
}
2023-04-02 13:12:06 +02:00
if n != 0 {
t.Fatalf("expected n 666, got %v", n)
2023-04-01 22:25:20 +02:00
}
if len(b) > 0 {
t.Fatalf("expected empty code")
}
2023-04-01 22:25:20 +02:00
}