core: cover enumerator/iterator interops

This commit is contained in:
Evgenii Stratonikov 2020-12-02 12:53:03 +03:00
parent e4ee7cd407
commit b203c23515
2 changed files with 83 additions and 0 deletions

View file

@ -0,0 +1,35 @@
package enumerator
import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/stretchr/testify/require"
)
// Enumerator is thoroughly tested in VM package, these are smoke tests.
func TestEnumerator(t *testing.T) {
ic := &interop.Context{VM: vm.New()}
full := []byte{4, 8, 15}
ic.VM.Estack().PushVal(full[2:])
require.NoError(t, Create(ic))
ic.VM.Estack().PushVal(full[:2])
require.NoError(t, Create(ic))
require.NoError(t, Concat(ic))
res := ic.VM.Estack().Pop().Item()
for i := range full {
ic.VM.Estack().PushVal(res)
require.NoError(t, Next(ic))
require.True(t, ic.VM.Estack().Pop().Bool())
ic.VM.Estack().PushVal(res)
require.NoError(t, Value(ic))
require.Equal(t, big.NewInt(int64(full[i])), ic.VM.Estack().Pop().BigInt())
}
ic.VM.Estack().PushVal(res)
require.NoError(t, Next(ic))
require.False(t, ic.VM.Estack().Pop().Bool())
}

View file

@ -0,0 +1,48 @@
package iterator
import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/stretchr/testify/require"
)
// Iterator is thoroughly tested in VM package, these are smoke tests.
func TestIterator(t *testing.T) {
ic := &interop.Context{VM: vm.New()}
full := []byte{4, 8, 15}
ic.VM.Estack().PushVal(full[2:])
require.NoError(t, Create(ic))
ic.VM.Estack().PushVal(full[:2])
require.NoError(t, Create(ic))
require.NoError(t, Concat(ic))
res := ic.VM.Estack().Pop().Item()
ic.VM.Estack().PushVal(res)
require.NoError(t, vm.EnumeratorNext(ic.VM))
require.True(t, ic.VM.Estack().Pop().Bool())
ic.VM.Estack().PushVal(res)
require.NoError(t, Key(ic))
require.Equal(t, big.NewInt(0), ic.VM.Estack().Pop().BigInt())
ic.VM.Estack().PushVal(res)
require.NoError(t, vm.EnumeratorValue(ic.VM))
require.Equal(t, big.NewInt(int64(full[0])), ic.VM.Estack().Pop().BigInt())
ic.VM.Estack().PushVal(res)
require.NoError(t, vm.EnumeratorNext(ic.VM))
require.True(t, ic.VM.Estack().Pop().Bool())
ic.VM.Estack().PushVal(res)
require.NoError(t, Keys(ic))
require.NoError(t, vm.EnumeratorValue(ic.VM))
require.Equal(t, big.NewInt(1), ic.VM.Estack().Pop().BigInt())
ic.VM.Estack().PushVal(res)
require.NoError(t, Values(ic))
require.NoError(t, vm.EnumeratorValue(ic.VM))
require.Equal(t, big.NewInt(int64(full[1])), ic.VM.Estack().Pop().BigInt())
}