go-vise

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

asm_test.go (11019B)


      1 package asm
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/hex"
      6 	"log"
      7 	"testing"
      8 
      9 	"git.defalsify.org/vise.git/vm"
     10 )
     11 
     12 func TestParserRoute(t *testing.T) {
     13 	b := bytes.NewBuffer(nil)
     14 	s := "HALT\n"
     15 	Parse(s, b)
     16 	expect := vm.NewLine(nil, vm.HALT, nil, nil, nil)
     17 	if !bytes.Equal(b.Bytes(), expect) {
     18 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)	
     19 	}
     20 
     21 	b = bytes.NewBuffer(nil)
     22 	s = "MSINK\n"
     23 	Parse(s, b)
     24 	expect = vm.NewLine(nil, vm.MSINK, nil, nil, nil)
     25 	if !bytes.Equal(b.Bytes(), expect) {
     26 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     27 	}
     28 
     29 	b = bytes.NewBuffer(nil)
     30 	s = "MAP tinkywinky\n"
     31 	Parse(s, b)
     32 	expect = vm.NewLine(nil, vm.MAP, []string{"tinkywinky"}, nil, nil)
     33 	if !bytes.Equal(b.Bytes(), expect) {
     34 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)	
     35 	}
     36 
     37 	b = bytes.NewBuffer(nil)
     38 	s = "MOVE dipsy\n"
     39 	Parse(s, b)
     40 	expect = vm.NewLine(nil, vm.MOVE, []string{"dipsy"}, nil, nil)
     41 	if !bytes.Equal(b.Bytes(), expect) {
     42 			log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     43 	}
     44 
     45 	b = bytes.NewBuffer(nil)
     46 	s = "RELOAD lalapu\n"
     47 	Parse(s, b)
     48 	expect = vm.NewLine(nil, vm.RELOAD, []string{"lalapu"}, nil, nil)
     49 	if !bytes.Equal(b.Bytes(), expect) {
     50 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     51 	}
     52 
     53 	b = bytes.NewBuffer(nil)
     54 	s = "LOAD foo 42\n"
     55 	Parse(s, b)
     56 	expect = vm.NewLine(nil, vm.LOAD, []string{"foo"}, []byte{0x2a}, nil)
     57 	if !bytes.Equal(b.Bytes(), expect) {
     58 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     59 	}
     60 
     61 	b = bytes.NewBuffer(nil)
     62 	s = "MOUT foo bar\n"
     63 	Parse(s, b)
     64 	expect = vm.NewLine(nil, vm.MOUT, []string{"foo", "bar"}, nil, nil)
     65 	if !bytes.Equal(b.Bytes(), expect) {
     66 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     67 	}
     68 
     69 	b = bytes.NewBuffer(nil)
     70 	s = "MOUT baz 42\n"
     71 	Parse(s, b)
     72 	expect = vm.NewLine(nil, vm.MOUT, []string{"baz", "42"}, nil, nil)
     73 	if !bytes.Equal(b.Bytes(), expect) {
     74 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     75 	}
     76 
     77 	b = bytes.NewBuffer(nil)
     78 	s = "MNEXT inky 12\n"
     79 	Parse(s, b)
     80 	expect = vm.NewLine(nil, vm.MNEXT, []string{"inky", "12"}, nil, nil)
     81 	if !bytes.Equal(b.Bytes(), expect) {
     82 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     83 	}
     84 
     85 	b = bytes.NewBuffer(nil)
     86 	s = "MPREV pinky 34\n"
     87 	Parse(s, b)
     88 	expect = vm.NewLine(nil, vm.MPREV, []string{"pinky", "34"}, nil, nil)
     89 	if !bytes.Equal(b.Bytes(), expect) {
     90 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     91 	}
     92 
     93 	b = bytes.NewBuffer(nil)
     94 	s = "INCMP foo bar\n"
     95 	Parse(s, b)
     96 	expect = vm.NewLine(nil, vm.INCMP, []string{"foo", "bar"}, nil, nil)
     97 	if !bytes.Equal(b.Bytes(), expect) {
     98 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
     99 	}
    100 
    101 	b = bytes.NewBuffer(nil)
    102 	s = "INCMP baz 42\n"
    103 	Parse(s, b)
    104 	expect = vm.NewLine(nil, vm.INCMP, []string{"baz", "42"}, nil, nil)
    105 	if !bytes.Equal(b.Bytes(), expect) {
    106 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
    107 	}
    108 
    109 	b = bytes.NewBuffer(nil)
    110 	s = "INCMP xyzzy *\n"
    111 	Parse(s, b)
    112 	expect = vm.NewLine(nil, vm.INCMP, []string{"xyzzy", "*"}, nil, nil)
    113 	if !bytes.Equal(b.Bytes(), expect) {
    114 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
    115 	}
    116 
    117 	b = bytes.NewBuffer(nil)
    118 	s = "DOWN foo 2 bar\n"
    119 	Parse(s, b)
    120 	expect = vm.NewLine(nil, vm.MOUT, []string{"bar", "2"}, nil, nil)
    121 	expect = vm.NewLine(expect, vm.HALT, nil, nil, nil)
    122 	expect = vm.NewLine(expect, vm.INCMP, []string{"foo", "2"}, nil, nil)
    123 	if !bytes.Equal(b.Bytes(), expect) {
    124 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
    125 	}
    126 
    127 	b = bytes.NewBuffer(nil)
    128 	s = "UP 3 bar\n"
    129 	Parse(s, b)
    130 	expect = vm.NewLine(nil, vm.MOUT, []string{"bar", "3"}, nil, nil)
    131 	expect = vm.NewLine(expect, vm.HALT, nil, nil, nil)
    132 	expect = vm.NewLine(expect, vm.INCMP, []string{"_", "3"}, nil, nil)
    133 	if !bytes.Equal(b.Bytes(), expect) {
    134 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
    135 	}
    136 
    137 	b = bytes.NewBuffer(nil)
    138 	s = "NEXT 4 baz\n"
    139 	Parse(s, b)
    140 	expect = vm.NewLine(nil, vm.MNEXT, []string{"baz", "4"}, nil, nil)
    141 	expect = vm.NewLine(expect, vm.HALT, nil, nil, nil)
    142 	expect = vm.NewLine(expect, vm.INCMP, []string{">", "4"}, nil, nil)
    143 	if !bytes.Equal(b.Bytes(), expect) {
    144 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
    145 	}
    146 
    147 	b = bytes.NewBuffer(nil)
    148 	s = "PREVIOUS 5 xyzzy\n"
    149 	Parse(s, b)
    150 	expect = vm.NewLine(nil, vm.MPREV, []string{"xyzzy", "5"}, nil, nil)
    151 	expect = vm.NewLine(expect, vm.HALT, nil, nil, nil)
    152 	expect = vm.NewLine(expect, vm.INCMP, []string{"<", "5"}, nil, nil)
    153 	if !bytes.Equal(b.Bytes(), expect) {
    154 		log.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, b)
    155 	}
    156 
    157 }
    158 
    159 func TestParserInit(t *testing.T) {
    160 	var b []byte
    161 	b = vm.NewLine(b, vm.HALT, nil, nil, nil)
    162 	b = vm.NewLine(b, vm.CATCH, []string{"xyzzy"}, []byte{0x02, 0x9a}, []uint8{1})
    163 	b = vm.NewLine(b, vm.INCMP, []string{"pinky", "inky"}, nil, nil)
    164 	b = vm.NewLine(b, vm.LOAD, []string{"foo"}, []byte{42}, nil)
    165 	b = vm.NewLine(b, vm.MOUT, []string{"bar", "barbarbaz"}, nil, nil)
    166 	s, err := vm.ToString(b)
    167 	log.Printf("parsing:\n%s\n", s)
    168 
    169 	n, err := Parse(s, nil)
    170 	if err != nil {
    171 		t.Fatal(err)
    172 	}
    173 	if n != 0 {
    174 		t.Fatalf("expected 0 byte write count, got %v", n)
    175 	}
    176 }
    177 
    178 func TestParserSized(t *testing.T) {
    179 	var b []byte
    180 	b = vm.NewLine(b, vm.LOAD, []string{"foo"}, []byte{42}, nil)
    181 	s, err := vm.ToString(b)
    182 	log.Printf("parsing:\n%s\n", s)
    183 
    184 	r := bytes.NewBuffer(nil)
    185 	n, err := Parse(s, r)
    186 	if err != nil {
    187 		t.Fatal(err)
    188 	}
    189 	if n != 8 {
    190 		t.Fatalf("expected 8 byte write count, got %v", n)
    191 	}
    192 	rb := r.Bytes()
    193 	if !bytes.Equal(rb, []byte{0x00, vm.LOAD, 0x03, 0x66, 0x6f, 0x6f, 0x01, 0x2a}) {
    194 		t.Fatalf("expected 0x00%x012a, got %v", vm.LOAD, rb)
    195 	}
    196 }
    197 
    198 func TestParseDisplay(t *testing.T) {
    199 	var b []byte
    200 	b = vm.NewLine(b, vm.MOUT, []string{"foo", "baz_ba_zbaz"}, nil, nil)
    201 	s, err := vm.ToString(b)
    202 	log.Printf("parsing:\n%s\n", s)
    203 
    204 	r := bytes.NewBuffer(nil)
    205 	n, err := Parse(s, r)
    206 	if err != nil {
    207 		t.Fatal(err)
    208 	}
    209 	if n != 18 {
    210 		t.Fatalf("expected 18 byte write count, got %v", n)
    211 	}
    212 	rb := r.Bytes()
    213 	expect := []byte{0x00, vm.MOUT, 0x03, 0x66, 0x6f, 0x6f, 0x0b, 0x62, 0x61, 0x7a, 0x5f, 0x62, 0x61, 0x5f, 0x7a, 0x62, 0x61, 0x7a}
    214 	if !bytes.Equal(rb, expect) {
    215 		t.Fatalf("expected:\n\t%x\ngot:\n\t%x", expect, rb)
    216 	}
    217 }
    218 
    219 func TestParseDouble(t *testing.T) {
    220 	t.Skip("foo")
    221 	var b []byte
    222 	b = vm.NewLine(b, vm.INCMP, []string{"foo", "bar"}, nil, nil)
    223 	s, err := vm.ToString(b)
    224 	log.Printf("parsing:\n%s\n", s)
    225 
    226 	r := bytes.NewBuffer(nil)
    227 	n, err := Parse(s, r)
    228 	if err != nil {
    229 		t.Fatal(err)
    230 	}
    231 	if n != 10 {
    232 		t.Fatalf("expected 18 byte write count, got %v", n)
    233 	}
    234 	rb := r.Bytes()
    235 	expect := []byte{0x00, vm.INCMP, 0x03, 0x62, 0x61, 0x72, 0x03, 0x66, 0x6f, 0x6f}
    236 	if !bytes.Equal(rb, expect) {
    237 		t.Fatalf("expected %x, got %x", expect, rb)
    238 	}
    239 }
    240 
    241 func TestParseMenu(t *testing.T) {
    242 	s := `DOWN foobar 00 inky_pinky
    243 UP s1 tinkywinky
    244 UP 2 dipsy
    245 `
    246 	r := bytes.NewBuffer(nil)
    247 	n, err := Parse(s, r)
    248 	if err != nil {
    249 		t.Fatal(err)
    250 	}
    251 	log.Printf("wrote %v bytes", n)
    252 
    253 	s = `MOUT inky_pinky 00
    254 MOUT tinkywinky s1
    255 MOUT dipsy 2
    256 HALT
    257 INCMP foobar 00
    258 INCMP _ s1
    259 INCMP _ 2
    260 `
    261 	r_check := bytes.NewBuffer(nil)
    262 	n, err = Parse(s, r_check)
    263 	if err != nil {
    264 		t.Fatal(err)
    265 	}
    266 	log.Printf("wrote %v bytes", n)
    267 
    268 	if !bytes.Equal(r_check.Bytes(), r.Bytes()) {
    269 		t.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", r_check, r)
    270 	}
    271 
    272 }
    273 
    274 func TestParseSingle(t *testing.T) {
    275 	var b []byte
    276 	b = vm.NewLine(b, vm.MAP, []string{"xyzzy"}, nil, nil)
    277 	s, err := vm.ToString(b)
    278 	log.Printf("parsing:\n%s\n", s)
    279 
    280 	r := bytes.NewBuffer(nil)
    281 	n, err := Parse(s, r)
    282 	if err != nil {
    283 		t.Fatal(err)
    284 	}
    285 	if n != 8 {
    286 		t.Fatalf("expected 8 byte write count, got %v", n)
    287 	}
    288 	rb := r.Bytes()
    289 	expect := []byte{0x00, vm.MAP, 0x05, 0x78, 0x79, 0x7a, 0x7a, 0x79}
    290 	if !bytes.Equal(rb, expect) {
    291 		t.Fatalf("expected %x, got %x", expect, rb)
    292 	}
    293 }
    294 
    295 func TestParseSig(t *testing.T) {
    296 	b := vm.NewLine(nil, vm.CATCH, []string{"plugh"}, []byte{0x02, 0x9a}, []uint8{0x2a})
    297 	s, err := vm.ToString(b)
    298 	log.Printf("parsing:\n%s\n", s)
    299 
    300 	r := bytes.NewBuffer(nil)
    301 	n, err := Parse(s, r)
    302 	if err != nil {
    303 		t.Fatal(err)
    304 	}
    305 	if n != 12 {
    306 		t.Fatalf("expected 12 byte write count, got %v", n)
    307 	}
    308 	rb := r.Bytes()
    309 	expect_hex := "000105706c75676802029a01"
    310 	expect, err := hex.DecodeString(expect_hex)
    311 	if err != nil {
    312 		t.Fatal(err)
    313 	}
    314 	if !bytes.Equal(rb, expect) {
    315 		t.Fatalf("expected %v, got %x", expect_hex, rb)
    316 	}
    317 
    318 	b = vm.NewLine(nil, vm.CATCH, []string{"plugh"}, []byte{0x01}, []uint8{0x0})
    319 	s, err = vm.ToString(b)
    320 	log.Printf("parsing:\n%s\n", s)
    321 
    322 	r = bytes.NewBuffer(nil)
    323 	n, err = Parse(s, r)
    324 	if err != nil {
    325 		t.Fatal(err)
    326 	}
    327 	if n != 11 {
    328 		t.Fatalf("expected 11 byte write count, got %v", n)
    329 	}
    330 	rb = r.Bytes()
    331 	expect_hex = "000105706c756768010100"
    332 	expect, err = hex.DecodeString(expect_hex)
    333 	if err != nil {
    334 		t.Fatal(err)
    335 	}
    336 	if !bytes.Equal(rb, expect) {
    337 		t.Fatalf("expected %v, got %x", expect_hex, rb)
    338 	}
    339 }
    340 
    341 func TestParseCroak(t *testing.T) {
    342 	b := bytes.NewBuffer(nil)
    343 	s := "CROAK 2 1\n"
    344 	Parse(s, b)
    345 	expect := vm.NewLine(nil, vm.CROAK, nil, []byte{0x02}, []uint8{0x1})
    346 	if !bytes.Equal(b.Bytes(), expect) {
    347 		t.Fatalf("expected %x, got %x", expect, b)
    348 	}
    349 }
    350 
    351 func TestParseNoarg(t *testing.T) {
    352 	var b []byte
    353 	b = vm.NewLine(b, vm.HALT, nil, nil, nil)
    354 	s, err := vm.ToString(b)
    355 	log.Printf("parsing:\n%s\n", s)
    356 
    357 	r := bytes.NewBuffer(nil)
    358 	n, err := Parse(s, r)
    359 	if err != nil {
    360 		t.Fatal(err)
    361 	}
    362 	if n != 2 {
    363 		t.Fatalf("expected 8 byte write count, got %v", n)
    364 	}
    365 	rb := r.Bytes()
    366 	expect := []byte{0x00, vm.HALT}
    367 	if !bytes.Equal(rb, expect) {
    368 		t.Fatalf("expected %x, got %x", expect, rb)
    369 	}
    370 }
    371 
    372 func TestParserWriteMultiple(t *testing.T) {
    373 	var b []byte
    374 	b = vm.NewLine(b, vm.HALT, nil, nil, nil)
    375 	b = vm.NewLine(b, vm.CATCH, []string{"xyzzy"}, []byte{0x02, 0x9a}, []uint8{1})
    376 	b = vm.NewLine(b, vm.INCMP, []string{"pinky", "inky"}, nil, nil)
    377 	b = vm.NewLine(b, vm.LOAD, []string{"foo"}, []byte{42}, nil)
    378 	b = vm.NewLine(b, vm.MOUT, []string{"bar", "bar_barb_az"}, nil, nil)
    379 	s, err := vm.ToString(b)
    380 	log.Printf("parsing:\n%s\n", s)
    381 
    382 	r := bytes.NewBuffer(nil)
    383 	n, err := Parse(s, r)
    384 	if err != nil {
    385 		t.Fatal(err)
    386 	}
    387 	log.Printf("result %x", r.Bytes())
    388 
    389 	r_expect_hex := "000700010578797a7a7902029a0100080570696e6b7904696e6b79000303666f6f012a000a036261720b6261725f626172625f617a"
    390 	r_expect, err := hex.DecodeString(r_expect_hex)
    391 	if err != nil {
    392 		t.Fatal(err)
    393 	}
    394 	n_expect := len(r_expect)
    395 	if n != n_expect {
    396 		t.Fatalf("expected total %v bytes output, got %v", n_expect, n)
    397 	}
    398 	
    399 	rb := r.Bytes()
    400 	if !bytes.Equal(rb, r_expect) {
    401 		t.Fatalf("expected result:\n\t%v, got:\n\t%x", r_expect_hex, rb)
    402 	}
    403 
    404 	_, err = vm.ParseAll(rb, nil)
    405 	if err != nil {
    406 		t.Fatal(err)
    407 	}
    408 }
    409 
    410 func TestParserCapQuote(t *testing.T) {
    411 	t.Skip("please fix mysterious ignore of initial cap in display sym match")
    412 	b := vm.NewLine(nil, vm.MOUT, []string{"a", "foo"}, nil, nil) 
    413 	b = vm.NewLine(b, vm.MOUT, []string{"b", "Bar"}, nil, nil) 
    414 	b = vm.NewLine(b, vm.MOUT, []string{"c", "baz"}, nil, nil) 
    415 	b = vm.NewLine(b, vm.MSINK, nil, nil, nil)
    416 	s, err := vm.ToString(b)
    417 	log.Printf("parsing:\n%s", s)
    418 
    419 	r := bytes.NewBuffer(nil)
    420 	n, err := Parse(s, r)
    421 	if err != nil {
    422 		t.Fatal(err)
    423 	}
    424 	_ = n
    425 }