vm: fix XTUCK implementation, add tests
The code that we have actually implements XTUCK and not TUCK. And it's a bit broken, so fix it and add some tests. The most interesting one (that required to touch stack code) is the one when we have 1 element on the stack and are trying to tell XTUCK to push 2 elements deep.
This commit is contained in:
parent
5f0f9e1638
commit
85831e3e92
3 changed files with 55 additions and 5 deletions
|
@ -141,6 +141,8 @@ func (s *Stack) InsertBefore(e, mark *Element) *Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertAt will insert the given item (n) deep on the stack.
|
// InsertAt will insert the given item (n) deep on the stack.
|
||||||
|
// Be very careful using it and _always_ check both e and n before invocation
|
||||||
|
// as it will silently do wrong things otherwise.
|
||||||
func (s *Stack) InsertAt(e *Element, n int) *Element {
|
func (s *Stack) InsertAt(e *Element, n int) *Element {
|
||||||
before := s.Peek(n)
|
before := s.Peek(n)
|
||||||
if before == nil {
|
if before == nil {
|
||||||
|
|
16
pkg/vm/vm.go
16
pkg/vm/vm.go
|
@ -306,13 +306,19 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
b.value = aval
|
b.value = aval
|
||||||
}
|
}
|
||||||
|
|
||||||
case TUCK:
|
case XTUCK:
|
||||||
n := int(v.estack.Pop().BigInt().Int64())
|
n := int(v.estack.Pop().BigInt().Int64())
|
||||||
if n <= 0 {
|
if n < 0 {
|
||||||
panic("OTUCK: invalid length")
|
panic("XTUCK: invalid length")
|
||||||
}
|
}
|
||||||
|
a := v.estack.Dup(0)
|
||||||
v.estack.InsertAt(v.estack.Peek(0), n)
|
if a == nil {
|
||||||
|
panic("no top-level element found")
|
||||||
|
}
|
||||||
|
if n > v.estack.Len() {
|
||||||
|
panic("can't push to the position specified")
|
||||||
|
}
|
||||||
|
v.estack.InsertAt(a, n)
|
||||||
|
|
||||||
case ROT:
|
case ROT:
|
||||||
c := v.estack.Pop()
|
c := v.estack.Pop()
|
||||||
|
|
|
@ -296,6 +296,48 @@ func TestPICKgood(t *testing.T) {
|
||||||
assert.Equal(t, int64(result), vm.estack.Pop().BigInt().Int64())
|
assert.Equal(t, int64(result), vm.estack.Pop().BigInt().Int64())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestXTUCKbadNoitem(t *testing.T) {
|
||||||
|
prog := makeProgram(XTUCK)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestXTUCKbadNoN(t *testing.T) {
|
||||||
|
prog := makeProgram(XTUCK)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestXTUCKbadNegative(t *testing.T) {
|
||||||
|
prog := makeProgram(XTUCK)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(-1)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestXTUCKgood(t *testing.T) {
|
||||||
|
prog := makeProgram(XTUCK)
|
||||||
|
topelement := 5
|
||||||
|
xtuckdepth := 3
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(0)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.estack.PushVal(3)
|
||||||
|
vm.estack.PushVal(4)
|
||||||
|
vm.estack.PushVal(topelement)
|
||||||
|
vm.estack.PushVal(xtuckdepth)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
||||||
|
assert.Equal(t, int64(topelement), vm.estack.Peek(0).BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(topelement), vm.estack.Peek(xtuckdepth).BigInt().Int64())
|
||||||
|
}
|
||||||
|
|
||||||
func makeProgram(opcodes ...Instruction) []byte {
|
func makeProgram(opcodes ...Instruction) []byte {
|
||||||
prog := make([]byte, len(opcodes)+1) // RET
|
prog := make([]byte, len(opcodes)+1) // RET
|
||||||
|
|
Loading…
Reference in a new issue