Add multi-instruction test stub
This commit is contained in:
parent
ee55f3e5fe
commit
13bb7bed9c
@ -24,11 +24,13 @@ func NewState(bitSize uint64) State {
|
||||
bitSize += (8 - n)
|
||||
}
|
||||
|
||||
return State{
|
||||
st := State{
|
||||
Flags: make([]byte, bitSize / 8),
|
||||
CacheSize: 0,
|
||||
CacheUseSize: 0,
|
||||
}
|
||||
st.Down("")
|
||||
return st
|
||||
}
|
||||
|
||||
func(st State) WithCacheSize(cacheSize uint32) State {
|
||||
@ -40,6 +42,7 @@ func(st *State) Down(input string) {
|
||||
m := make(map[string]string)
|
||||
st.Cache = append(st.Cache, m)
|
||||
st.CacheMap = make(map[string]string)
|
||||
st.ExecPath = append(st.ExecPath, input)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
st.Cache = st.Cache[:l]
|
||||
st.ExecPath = st.ExecPath[:l]
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -69,6 +69,10 @@ func TestStateDownUp(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
err = st.Up()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = st.Up()
|
||||
if err == nil {
|
||||
t.Errorf("expected out of top frame error")
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"git.defalsify.org/festive/state"
|
||||
"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) {
|
||||
var err error
|
||||
for len(instruction) > 0 {
|
||||
log.Printf("instruction is now %v", instruction)
|
||||
op := binary.BigEndian.Uint16(instruction[:2])
|
||||
if 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 {
|
||||
case CATCH:
|
||||
st, instruction, err = RunCatch(instruction[2:], st, rs, ctx)
|
||||
break
|
||||
case CROAK:
|
||||
st, instruction, err = RunCroak(instruction[2:], st, rs, ctx)
|
||||
break
|
||||
case LOAD:
|
||||
st, instruction, err = RunLoad(instruction[2:], st, rs, ctx)
|
||||
break
|
||||
case RELOAD:
|
||||
st, instruction, err = RunReload(instruction[2:], st, rs, ctx)
|
||||
break
|
||||
case MAP:
|
||||
st, instruction, err = RunMap(instruction[2:], st, rs, ctx)
|
||||
break
|
||||
case SINK:
|
||||
st, instruction, err = RunSink(instruction[2:], st, rs, ctx)
|
||||
break
|
||||
default:
|
||||
err = fmt.Errorf("Unhandled state: %v", op)
|
||||
}
|
||||
|
@ -128,3 +128,17 @@ func TestRunLoad(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user