debug_test.go (3928B)
1 package vm 2 3 import ( 4 "testing" 5 ) 6 7 8 func TestToString(t *testing.T) { 9 var b []byte 10 var r string 11 var expect string 12 var err error 13 14 b = NewLine(nil, CATCH, []string{"xyzzy"}, []byte{0x0d}, []uint8{1}) 15 r, err = ToString(b) 16 if err != nil { 17 t.Fatal(err) 18 } 19 expect = "CATCH xyzzy 13 1\n" 20 if r != expect { 21 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 22 } 23 24 b = NewLine(nil, CROAK, nil, []byte{0x0d}, []uint8{1}) 25 r, err = ToString(b) 26 if err != nil { 27 t.Fatal(err) 28 } 29 expect = "CROAK 13 1\n" 30 if r != expect { 31 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 32 } 33 34 b = NewLine(nil, LOAD, []string{"foo"}, []byte{0x0a}, nil) 35 r, err = ToString(b) 36 if err != nil { 37 t.Fatal(err) 38 } 39 expect = "LOAD foo 10\n" 40 if r != expect { 41 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 42 } 43 44 b = NewLine(nil, RELOAD, []string{"bar"}, nil, nil) 45 r, err = ToString(b) 46 if err != nil { 47 t.Fatal(err) 48 } 49 expect = "RELOAD bar\n" 50 if r != expect { 51 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 52 } 53 54 b = NewLine(nil, MAP, []string{"inky_pinky"}, nil, nil) 55 r, err = ToString(b) 56 if err != nil { 57 t.Fatal(err) 58 } 59 expect = "MAP inky_pinky\n" 60 if r != expect { 61 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 62 } 63 64 b = NewLine(nil, MOVE, []string{"blinky_clyde"}, nil, nil) 65 r, err = ToString(b) 66 if err != nil { 67 t.Fatal(err) 68 } 69 expect = "MOVE blinky_clyde\n" 70 if r != expect { 71 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 72 } 73 74 b = NewLine(nil, HALT, nil, nil, nil) 75 r, err = ToString(b) 76 if err != nil { 77 t.Fatal(err) 78 } 79 expect = "HALT\n" 80 if r != expect { 81 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 82 } 83 84 b = NewLine(nil, INCMP, []string{"13", "baz"}, nil, nil) 85 r, err = ToString(b) 86 if err != nil { 87 t.Fatal(err) 88 } 89 expect = "INCMP 13 baz\n" 90 if r != expect { 91 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 92 } 93 94 b = NewLine(nil, MNEXT, []string{"11", "nextmenu"}, nil, nil) 95 r, err = ToString(b) 96 if err != nil { 97 t.Fatal(err) 98 } 99 //expect = "MNEXT 11 \"nextmenu\"\n" 100 expect = "MNEXT 11 nextmenu\n" 101 if r != expect { 102 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 103 } 104 105 b = NewLine(nil, MPREV, []string{"222", "previous menu item"}, nil, nil) 106 r, err = ToString(b) 107 if err != nil { 108 t.Fatal(err) 109 } 110 //expect = "MPREV 222 \"previous menu item\"\n" 111 expect = "MPREV 222 previous menu item\n" 112 if r != expect { 113 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 114 } 115 116 b = NewLine(nil, MOUT, []string{"1", "foo"}, nil, nil) 117 r, err = ToString(b) 118 if err != nil { 119 t.Fatal(err) 120 } 121 //expect = "MOUT 1 \"foo\"\n" 122 expect = "MOUT 1 foo\n" 123 if r != expect { 124 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 125 } 126 127 b = NewLine(nil, MSINK, nil, nil, nil) //[]uint8{0x42, 0x2a}) 128 r, err = ToString(b) 129 if err != nil { 130 t.Fatal(err) 131 } 132 //expect = "MSIZE 66 42\n" 133 expect = "MSINK\n" 134 if r != expect { 135 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 136 } 137 } 138 139 func TestToStringMultiple(t *testing.T) { 140 b := NewLine(nil, INCMP, []string{"1", "foo"}, nil, nil) 141 b = NewLine(b, INCMP, []string{"2", "bar"}, nil, nil) 142 b = NewLine(b, CATCH, []string{"aiee"}, []byte{0x02, 0x9a}, []uint8{0}) 143 b = NewLine(b, LOAD, []string{"inky"}, []byte{0x2a}, nil) 144 b = NewLine(b, HALT, nil, nil, nil) 145 r, err := ToString(b) 146 if err != nil { 147 t.Fatal(err) 148 } 149 expect := `INCMP 1 foo 150 INCMP 2 bar 151 CATCH aiee 666 0 152 LOAD inky 42 153 HALT 154 ` 155 if r != expect { 156 t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) 157 } 158 } 159 160 func TestVerifyMultiple(t *testing.T) { 161 b := NewLine(nil, INCMP, []string{"1", "foo"}, nil, nil) 162 b = NewLine(b, INCMP, []string{"2", "bar"}, nil, nil) 163 b = NewLine(b, CATCH, []string{"aiee"}, []byte{0x02, 0x9a}, []uint8{0}) 164 b = NewLine(b, LOAD, []string{"inky"}, []byte{0x2a}, nil) 165 b = NewLine(b, HALT, nil, nil, nil) 166 n, err := ParseAll(b, nil) 167 if err != nil { 168 t.Fatal(err) 169 } 170 if n != 0 { 171 t.Fatalf("expected write count to be 0, was %v (how is that possible)", n) 172 } 173 }