Add multi-instruction test stub

This commit is contained in:
lash 2023-03-31 15:24:29 +01:00
parent ee55f3e5fe
commit 13bb7bed9c
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 31 additions and 1 deletions

View File

@ -24,11 +24,13 @@ func NewState(bitSize uint64) State {
bitSize += (8 - n) bitSize += (8 - n)
} }
return State{ st := State{
Flags: make([]byte, bitSize / 8), Flags: make([]byte, bitSize / 8),
CacheSize: 0, CacheSize: 0,
CacheUseSize: 0, CacheUseSize: 0,
} }
st.Down("")
return st
} }
func(st State) WithCacheSize(cacheSize uint32) State { func(st State) WithCacheSize(cacheSize uint32) State {
@ -40,6 +42,7 @@ func(st *State) Down(input string) {
m := make(map[string]string) m := make(map[string]string)
st.Cache = append(st.Cache, m) st.Cache = append(st.Cache, m)
st.CacheMap = make(map[string]string) st.CacheMap = make(map[string]string)
st.ExecPath = append(st.ExecPath, input)
} }
func(st *State) Add(key string, value string, sizeHint uint32) error { func(st *State) Add(key string, value string, sizeHint uint32) error {
@ -110,6 +113,7 @@ func(st *State) Up() error {
log.Printf("free frame %v key %v value size %v", l, k, sz) log.Printf("free frame %v key %v value size %v", l, k, sz)
} }
st.Cache = st.Cache[:l] st.Cache = st.Cache[:l]
st.ExecPath = st.ExecPath[:l]
return nil return nil
} }

View File

@ -69,6 +69,10 @@ func TestStateDownUp(t *testing.T) {
t.Error(err) t.Error(err)
} }
err = st.Up() err = st.Up()
if err != nil {
t.Error(err)
}
err = st.Up()
if err == nil { if err == nil {
t.Errorf("expected out of top frame error") t.Errorf("expected out of top frame error")
} }

View File

@ -4,6 +4,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"context" "context"
"log"
"git.defalsify.org/festive/state" "git.defalsify.org/festive/state"
"git.defalsify.org/festive/resource" "git.defalsify.org/festive/resource"
@ -14,6 +15,7 @@ type Runner func(instruction []byte, st state.State, rs resource.Fetcher, ctx co
func Run(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, []byte, error) { func Run(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, []byte, error) {
var err error var err error
for len(instruction) > 0 { for len(instruction) > 0 {
log.Printf("instruction is now %v", instruction)
op := binary.BigEndian.Uint16(instruction[:2]) op := binary.BigEndian.Uint16(instruction[:2])
if op > _MAX { if op > _MAX {
return st, instruction, fmt.Errorf("opcode value %v out of range (%v)", op, _MAX) return st, instruction, fmt.Errorf("opcode value %v out of range (%v)", op, _MAX)
@ -21,16 +23,22 @@ func Run(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Co
switch op { switch op {
case CATCH: case CATCH:
st, instruction, err = RunCatch(instruction[2:], st, rs, ctx) st, instruction, err = RunCatch(instruction[2:], st, rs, ctx)
break
case CROAK: case CROAK:
st, instruction, err = RunCroak(instruction[2:], st, rs, ctx) st, instruction, err = RunCroak(instruction[2:], st, rs, ctx)
break
case LOAD: case LOAD:
st, instruction, err = RunLoad(instruction[2:], st, rs, ctx) st, instruction, err = RunLoad(instruction[2:], st, rs, ctx)
break
case RELOAD: case RELOAD:
st, instruction, err = RunReload(instruction[2:], st, rs, ctx) st, instruction, err = RunReload(instruction[2:], st, rs, ctx)
break
case MAP: case MAP:
st, instruction, err = RunMap(instruction[2:], st, rs, ctx) st, instruction, err = RunMap(instruction[2:], st, rs, ctx)
break
case SINK: case SINK:
st, instruction, err = RunSink(instruction[2:], st, rs, ctx) st, instruction, err = RunSink(instruction[2:], st, rs, ctx)
break
default: default:
err = fmt.Errorf("Unhandled state: %v", op) err = fmt.Errorf("Unhandled state: %v", op)
} }

View File

@ -128,3 +128,17 @@ func TestRunLoad(t *testing.T) {
t.Errorf("Expected %v, got %v", expect, r) t.Errorf("Expected %v, got %v", expect, r)
} }
} }
func TestRunMultiple(t *testing.T) {
st := state.NewState(5)
rs := TestResource{}
b := []byte{0x00, LOAD, 0x03}
b = append(b, []byte("one")...)
b = append(b, []byte{0x00, 0x00, LOAD, 0x03}...)
b = append(b, []byte("two")...)
b = append(b, 0x00)
st, _, err := Run(b, st, &rs, context.TODO())
if err != nil {
t.Error(err)
}
}