go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

vm_test.go (3529B)


      1 package vm
      2 
      3 import (
      4 	"bytes"
      5 	"testing"
      6 )
      7 
      8 func TestParseOp(t *testing.T) {
      9 	op, b, err := ParseOp([]byte{0x00, LOAD, 0x03, 0x66, 0x6f, 0x6f})
     10 	if err != nil {
     11 		t.Fatal(err)
     12 	}
     13 	if (op != LOAD) {
     14 		t.Fatalf("expected %d, got %d", LOAD, op)
     15 	}
     16 	x := append([]byte{0x03}, []byte("foo")...)
     17 	if !bytes.Equal(b, x) {
     18 		t.Fatalf("expected %x, got %x", x, b)
     19 	}
     20 }
     21 
     22 func TestParseNoArg(t *testing.T) {
     23 	b := NewLine(nil, HALT, nil, nil, nil)
     24 	_, b, _ = opSplit(b)
     25 	b, err := ParseHalt(b)
     26 	if err != nil {
     27 		t.Fatal(err)
     28 	}
     29 	if len(b) > 0 {
     30 		t.Fatalf("expected empty code")
     31 	}
     32 }
     33 
     34 func TestParseSym(t *testing.T) {
     35 	b := NewLine(nil, MAP, []string{"baz"}, nil, nil)
     36 	_, b, _ = opSplit(b)
     37 	sym, b, err := ParseMap(b)
     38 	if err != nil {
     39 		t.Fatal(err)
     40 	}
     41 	if sym != "baz" {
     42 		t.Fatalf("expected sym baz, got %v", sym)
     43 	}
     44 	if len(b) > 0 {
     45 		t.Fatalf("expected empty code")
     46 	}
     47 
     48 	b = NewLine(nil, RELOAD, []string{"xyzzy"}, nil, nil)
     49 	_, b, _ = opSplit(b)
     50 	sym, b, err = ParseReload(b)
     51 	if err != nil {
     52 		t.Fatal(err)
     53 	}
     54 	if sym != "xyzzy" {
     55 		t.Fatalf("expected sym xyzzy, got %v", sym)
     56 	}
     57 	if len(b) > 0 {
     58 		t.Fatalf("expected empty code")
     59 	}
     60 
     61 	b = NewLine(nil, MOVE, []string{"plugh"}, nil, nil)
     62 	_, b, _ = opSplit(b)
     63 	sym, b, err = ParseMove(b)
     64 	if err != nil {
     65 		t.Fatal(err)
     66 	}
     67 	if sym != "plugh" {
     68 		t.Fatalf("expected sym plugh, got %v", sym)
     69 	}
     70 	if len(b) > 0 {
     71 		t.Fatalf("expected empty code")
     72 	}
     73 }
     74 
     75 func TestParseTwoSym(t *testing.T) {
     76 	b := NewLine(nil, INCMP, []string{"foo", "bar"}, nil, nil)
     77 	_, b, _ = opSplit(b)
     78 	one, two, b, err := ParseInCmp(b)
     79 	if err != nil {
     80 		t.Fatal(err)
     81 	}
     82 	if one != "foo" {
     83 		t.Fatalf("expected symone foo, got %v", one)
     84 	}
     85 	if two != "bar" {
     86 		t.Fatalf("expected symtwo bar, got %v", two)
     87 	}
     88 	if len(b) > 0 {
     89 		t.Fatalf("expected empty code")
     90 	}
     91 }
     92 
     93 func TestParseSig(t *testing.T) {
     94 	b := NewLine(nil, CROAK, nil, []byte{0x0b, 0x13}, []uint8{0x04})
     95 	_, b, _ = opSplit(b)
     96 	n, m, b, err := ParseCroak(b)
     97 	if err != nil {
     98 		t.Fatal(err)
     99 	}
    100 	if n != 2835 {
    101 		t.Fatalf("expected n 13, got %v", n)
    102 	}
    103 	if !m {
    104 		t.Fatalf("expected m true")
    105 	}
    106 	if len(b) > 0 {
    107 		t.Fatalf("expected empty code")
    108 	}
    109 }
    110 
    111 func TestParseSymSig(t *testing.T) {
    112 	b := NewLine(nil, CATCH, []string{"baz"}, []byte{0x0a, 0x13}, []uint8{0x01})
    113 	_, b, _ = opSplit(b)
    114 	sym, n, m, b, err := ParseCatch(b)
    115 	if err != nil {
    116 		t.Fatal(err)
    117 	}
    118 	if sym != "baz" {
    119 		t.Fatalf("expected sym baz, got %v", sym)
    120 	}
    121 	if n != 2579 {
    122 		t.Fatalf("expected n 13, got %v", n)
    123 	}
    124 	if !m {
    125 		t.Fatalf("expected m true")
    126 	}
    127 	if len(b) > 0 {
    128 		t.Fatalf("expected empty code")
    129 	}
    130 }
    131 
    132 func TestParseSymAndLen(t *testing.T) {
    133 	b := NewLine(nil, LOAD, []string{"foo"}, []byte{0x2a}, nil)
    134 	_, b, _ = opSplit(b)
    135 	sym, n, b, err := ParseLoad(b)
    136 	if err != nil {
    137 		t.Fatal(err)
    138 	}
    139 	if sym != "foo" {
    140 		t.Fatalf("expected sym foo, got %v", sym)
    141 	}
    142 	if n != 42 {
    143 		t.Fatalf("expected n 42, got %v", n)
    144 	}
    145 
    146 	b = NewLine(nil, LOAD, []string{"bar"}, []byte{0x02, 0x9a}, nil)
    147 	_, b, _ = opSplit(b)
    148 	sym, n, b, err = ParseLoad(b)
    149 	if err != nil {
    150 		t.Fatal(err)
    151 	}
    152 	if sym != "bar" {
    153 		t.Fatalf("expected sym foo, got %v", sym)
    154 	}
    155 	if n != 666 {
    156 		t.Fatalf("expected n 666, got %v", n)
    157 	}
    158 	if len(b) > 0 {
    159 		t.Fatalf("expected empty code")
    160 	}
    161 
    162 	b = NewLine(nil, LOAD, []string{"baz"}, []byte{0x0}, nil)
    163 	_, b, _ = opSplit(b)
    164 	sym, n, b, err = ParseLoad(b)
    165 	if err != nil {
    166 		t.Fatal(err)
    167 	}
    168 	if sym != "baz" {
    169 		t.Fatalf("expected sym foo, got %v", sym)
    170 	}
    171 	if n != 0 {
    172 		t.Fatalf("expected n 666, got %v", n)
    173 	}
    174 	if len(b) > 0 {
    175 		t.Fatalf("expected empty code")
    176 	}
    177 }