/
vm_basic_test.go
374 lines (339 loc) · 7.6 KB
/
vm_basic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package stackvm_test
import (
"fmt"
"testing"
. "github.com/jcorbin/stackvm/x"
"github.com/stretchr/testify/assert"
)
// These tests are essentially "unit" tests operations and/or features of the
// vm.
// So far my testing strategy has been to write end-to-end or "integration"
// tests since it's been a decent trade-off of time to outcome, and it forced
// building tracing to debug failures. Going forward tho, I'd like to start
// writing more targeted/smaller "unit" tests that exercise one op or vm feature.
func TestAssembler(t *testing.T) {
for _, tc := range []struct {
name string
prog []interface{}
code []byte
err string
}{
{
name: "undefined jump label",
prog: []interface{}{
":nope", "jump",
},
err: `undefined labels: ["nope"]`,
},
} {
t.Run(tc.name, func(t *testing.T) {
code, err := Assemble(tc.prog...)
if tc.err != "" {
assert.EqualError(t, err, tc.err, "expected error")
return
}
if assert.NoError(t, err, "unexpected error") {
assert.Equal(t, tc.code, code, "expected machine code")
}
})
}
}
func TestMach_stack_ops(t *testing.T) {
// TODO: more basic coverage
TestCases{
{
Name: "push",
Prog: []interface{}{"push", 1, "hnz", "halt"},
},
{
Name: "0 push",
Prog: []interface{}{0, "push", 1, "hnz", "halt"},
},
{
Name: "1 push",
Prog: []interface{}{1, "push", 1, "hz", "halt"},
},
}.Run(t)
}
func TestMach_misc_ops(t *testing.T) {
TestCases{
{
Name: "nuthin' doin'",
Prog: []interface{}{
"nop", "nop", "nop", "nop",
"halt",
},
},
}.Run(t)
}
func TestMach_basic_math(t *testing.T) {
TestCases{
{
Name: "33addeq5 should fail",
Err: "HALT(1)",
Prog: []interface{}{
3, "push", 3, "push", "add",
5, "push", "eq",
1, "hz", "halt",
},
Result: Result{
Err: "HALT(1)",
},
},
{
Name: "23addeq5 should succeed",
Prog: []interface{}{
2, "push", 3, "push", "add",
5, "push", "eq",
1, "hz", "halt",
},
Result: Result{},
},
}.Run(t)
}
func TestMach_operational_errors(t *testing.T) {
TestCases{
{
Name: "invalid op code",
Err: "invalid op UNDEFINED<0x70>",
Prog: []byte{
0x00, // end-of-options
0x70, // undefined op code
},
Result: Result{Err: "invalid op UNDEFINED<0x70>"},
},
{
Name: "crash: explicit",
Err: "crashed",
Prog: []byte{
0x00, // end-of-options
0x00, // opCodeCrash=0
},
Result: Result{Err: "crashed"},
},
{
Name: "crash: implicit",
Err: "crashed",
Prog: []byte{
0x00, // end-of-options
// empty program, 0 by default
},
Result: Result{Err: "crashed"},
},
{
Name: "crash: jump out of program",
Err: "crashed",
Prog: []interface{}{
96, "jump", "halt",
},
Result: Result{Err: "crashed"},
},
{
Name: "crash: implicit assembled",
Err: "crashed",
Prog: []interface{}{
1, "push",
2, "add",
// and then?...
},
Result: Result{Err: "crashed"},
},
{
Name: "maxops stops an infinite loop",
Err: "op count limit exceeded",
Prog: []interface{}{
".maxOps", 100,
1, "push",
"loop:",
1, "add",
":loop", "jump",
0, "halt",
},
Result: Result{Err: "op count limit exceeded"},
},
{
Name: "maxcopies stops an infinite copy loop",
Prog: []interface{}{
".maxCopies", 100,
"foo:", ":bar", "fork", 1, "halt",
"bar:", ":foo", "fork", 2, "halt",
3, "halt",
},
Result: Result{
Err: "max copies(100) exceeded",
}.WithExpectedHaltCodes(1, 2),
},
}.Run(t)
}
func TestMach_data_refs(t *testing.T) {
TestCase{
Name: "mod-10 check",
Prog: []interface{}{
".data",
"d:", 4, 2, 7, 9, 8,
".text",
".entry", "main:",
":d", "fetch", // d[0] :
4 * 1, ":d", "push", "fetch", // d[0] d[1] :
4 * 2, ":d", "push", "fetch", // d[0] d[1] d[2] :
4 * 3, ":d", "push", "fetch", // d[0] d[1] d[2] d[3] :
4 * 4, ":d", "push", "fetch", // d[0] d[1] d[2] d[3] d[4] :
"add", "add", "add", "add", // s=d[0]+d[1]+d[2]+d[3]+d[4] :
10, "mod", // s%10 :
1, "hnz", // : -- error halt if non-zero
"halt", // : normal halt
},
}.Run(t)
}
func TestMach_bitwise_ops(t *testing.T) {
TestCases{
{
Name: "masking",
Prog: []interface{}{
0xdead, "push", 16, "shiftl",
0xbeef, "bitor",
"dup", 0xdeadbeef, "eq", 1, "hz",
"dup", 0xffff, "bitand",
0xbeef, "eq", 1, "hz",
"dup",
0xffff, "push", "bitnot", "bitand",
16, "shiftr",
0xdead, "eq", 1, "hz",
"halt",
},
},
{
Name: "bitand",
Prog: []interface{}{
0xff, "push", 0x12, "push", "bitand",
0x12, "eq", 1, "hz",
0x0f, "push", 0x12, "bitand",
0x02, "eq", 1, "hz",
"halt",
},
},
{
Name: "bitor",
Prog: []interface{}{
1, "push", 2, "push", "bitor",
3, "eq", 1, "hz",
3, "push", 6, "bitor",
7, "eq", 1, "hz",
"halt",
},
},
{
Name: "bitxor",
Prog: []interface{}{
0x42, "push",
0x99, "push", "bitxor",
0xed, "bitxor",
"dup", 0x42 ^ 0x99 ^ 0xed, "eq", 1, "hz",
"dup",
0x99, "bitxor",
0xed, "bitxor",
0x42, "eq", 1, "hz",
"dup",
0xed, "bitxor",
0x42, "bitxor",
0x99, "eq", 1, "hz",
"dup",
0x42, "bitxor",
0x99, "bitxor",
0xed, "eq", 1, "hz",
"halt",
},
},
{
Name: "bit set & test & clear",
Prog: []interface{}{
// set some bits
40, "push", ":vec", "bitset",
42, "push", ":vec", "push", "bitset",
99, "push", ":vec", "bitset",
// test for them, and some near misses
39, "push", ":vec", "bitest", 1, "hnz",
40, "push", ":vec", "bitest", 1, "hz",
41, "push", ":vec", "bitest", 1, "hnz",
42, "push", ":vec", "push", "bitest", 1, "hz",
43, "push", ":vec", "push", "bitest", 1, "hnz",
98, "push", ":vec", "bitest", 1, "hnz",
99, "push", ":vec", "bitest", 1, "hz",
100, "push", ":vec", "bitest", 1, "hnz",
// clear some bits
42, "push", ":vec", "bitost",
99, "push", ":vec", "push", "bitost",
// test that they're now cleared
42, "push", ":vec", "push", "bitest", 1, "hnz",
99, "push", ":vec", "bitest", 1, "hnz",
// atomic sets
43, "push", ":vec", "push", "bitseta", 1, "hz",
43, "push", ":vec", "push", "bitseta", 1, "hnz",
44, "push", ":vec", "bitseta", 1, "hz",
44, "push", ":vec", "bitseta", 1, "hnz",
// atomic clears
43, "push", ":vec", "push", "bitosta", 1, "hz",
43, "push", ":vec", "push", "bitosta", 1, "hnz",
44, "push", ":vec", "bitosta", 1, "hz",
44, "push", ":vec", "bitosta", 1, "hnz",
"halt",
// 4 * 32 = 128 bits
"vec:", ".data", ".alloc", 4,
},
},
}.Run(t)
}
func TestMach_queueSize(t *testing.T) {
TestCases{
{
Name: "exceeded",
Prog: []interface{}{
".queueSize", 1,
":lol", "fork",
":wut", "fork",
0, "halt",
"lol:", 1, "halt",
"wut:", 2, "halt",
"halt",
},
Result: Result{
Err: "run queue full",
}.WithExpectedHaltCodes(1, 2),
},
{
Name: "sufficient",
Prog: []interface{}{
".queueSize", 2,
":lol", "fork",
":wut", "fork",
0, "halt",
"lol:", 1, "halt",
"wut:", 2, "halt",
"halt",
},
Result: NoResult.WithExpectedHaltCodes(1, 2),
},
}.Run(t)
}
func TestMach_inNout(t *testing.T) {
prog := MustAssemble(
".data",
".in", "N:", 0,
".out", "M:", 0,
".entry", "main:",
":N", "fetch", // N :
"dup", "mul", // N*N :
":M", "storeTo", // : -- M=N*N
"halt",
)
var tcs TestCases
for n := uint32(0); n < 10; n++ {
tcs = append(tcs, TestCase{
Name: fmt.Sprintf("square(%d)", n),
Prog: prog,
Input: map[string][]uint32{"N": {n}},
Result: Result{Values: map[string][]uint32{"M": {n * n}}},
})
}
tcs.Run(t)
}