From fd264cb06f07659236e231aaea9c9661177d32a8 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 12 Sep 2019 16:54:22 +0300 Subject: [PATCH] vm: make REVERSE work with structs Also add more tests. Extend `Array` method of `Element` to work with `StructItem`. --- pkg/vm/stack.go | 2 ++ pkg/vm/vm_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pkg/vm/stack.go b/pkg/vm/stack.go index 740aa835e..e840bccb7 100644 --- a/pkg/vm/stack.go +++ b/pkg/vm/stack.go @@ -121,6 +121,8 @@ func (e *Element) Array() []StackItem { switch t := e.value.(type) { case *ArrayItem: return t.value + case *StructItem: + return t.value default: panic("element is not an array") } diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 87ac78092..02b82b3a0 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -1331,6 +1331,35 @@ func TestREVERSEGoodOneElem(t *testing.T) { assert.Equal(t, int64(elements[0]), e.Int64()) } +func TestREVERSEGoodStruct(t *testing.T) { + eodd := []int{22, 34, 42, 55, 81} + even := []int{22, 34, 42, 55, 81, 99} + eall := [][]int{eodd, even} + + for _, elements := range eall { + prog := makeProgram(REVERSE) + vm := load(prog) + vm.estack.PushVal(1) + + arr := make([]StackItem, len(elements)) + for i := range elements { + arr[i] = makeStackItem(elements[i]) + } + vm.estack.Push(&Element{value: &StructItem{arr}}) + + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 2, vm.estack.Len()) + a := vm.estack.Peek(0).Array() + assert.Equal(t, len(elements), len(a)) + for k, v := range elements { + e := a[len(a)-1-k].Value().(*big.Int) + assert.Equal(t, int64(v), e.Int64()) + } + assert.Equal(t, int64(1), vm.estack.Peek(1).BigInt().Int64()) + } +} + func TestREVERSEGood(t *testing.T) { eodd := []int{22, 34, 42, 55, 81} even := []int{22, 34, 42, 55, 81, 99}