commit 0d23e0dbb57fe63b6626527fddc86649cfc20f8f
parent 550a674b0f4d674905d251847ec3c03e725ab9cc
Author: lash <dev@holbrook.no>
Date: Thu, 12 Sep 2024 00:18:17 +0100
Add test code to hit lateral labels in vm runner
Diffstat:
3 files changed, 37 insertions(+), 23 deletions(-)
diff --git a/vm/runner_test.go b/vm/runner_test.go
@@ -812,3 +812,25 @@ func TestMatchFlag(t *testing.T) {
t.Fatalf("expected no terminate")
}
}
+
+func TestBatchRun(t *testing.T) {
+ var err error
+ ctx := context.Background()
+ st := state.NewState(0)
+ st.Down("root")
+ st.Down("one")
+ st.Down("two")
+ ca := cache.NewCache()
+ rs := newTestResource(st)
+ rs.Lock()
+ b := NewLine(nil, MNEXT, []string{"fwd", "0"}, nil, nil)
+ b = NewLine(b, MPREV, []string{"back", "11"}, nil, nil)
+ b = NewLine(b, MSINK, nil, nil, nil)
+ b = NewLine(b, HALT, nil, nil, nil)
+ vm := NewVm(st, rs, ca, nil)
+
+ b, err = vm.Run(ctx, b)
+ if err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/vm/vm.go b/vm/vm.go
@@ -176,15 +176,6 @@ func parseSig(b []byte) (uint32, bool, []byte, error) {
return sig, matchmode, b, nil
}
-
-// split bytecode into head and b using length-prefixed bitfield
-func byteSplit(b []byte) ([]byte, []byte, error) {
- bitFieldSize := b[0]
- bitField := b[1:1+bitFieldSize]
- b = b[1+bitFieldSize:]
- return bitField, b, nil
-}
-
// split bytecode into head and b using length-prefixed integer
func intSplit(b []byte) (uint32, []byte, error) {
l := uint8(b[0])
@@ -224,20 +215,6 @@ func instructionSplit(b []byte) (string, []byte, error) {
return r, b[1+sz:], nil
}
-// check if the start of the given bytecode contains a valid opcode, extract and return it
-func opCheck(b []byte, opIn Opcode) ([]byte, error) {
- var bb []byte
- op, bb, err := opSplit(b)
- if err != nil {
- return b, err
- }
- b = bb
- if op != opIn {
- return b, fmt.Errorf("not a %v instruction", op)
- }
- return b, nil
-}
-
// split bytecode into head and b using opcode
func opSplit(b []byte) (Opcode, []byte, error) {
l := len(b)
diff --git a/vm/vm_test.go b/vm/vm_test.go
@@ -1,9 +1,24 @@
package vm
import (
+ "bytes"
"testing"
)
+func TestParseOp(t *testing.T) {
+ op, b, err := ParseOp([]byte{0x00, LOAD, 0x03, 0x66, 0x6f, 0x6f})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if (op != LOAD) {
+ t.Fatalf("expected %d, got %d", LOAD, op)
+ }
+ x := append([]byte{0x03}, []byte("foo")...)
+ if !bytes.Equal(b, x) {
+ t.Fatalf("expected %x, got %x", x, b)
+ }
+}
+
func TestParseNoArg(t *testing.T) {
b := NewLine(nil, HALT, nil, nil, nil)
_, b, _ = opSplit(b)