2020-12-02 09:53:03 +00:00
|
|
|
package iterator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2021-04-29 14:19:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-12-02 09:53:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-04-29 14:19:30 +00:00
|
|
|
type testIter struct {
|
|
|
|
index int
|
|
|
|
arr []int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testIter) Next() bool {
|
|
|
|
if t.index < len(t.arr) {
|
|
|
|
t.index++
|
|
|
|
}
|
|
|
|
return t.index < len(t.arr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t testIter) Value() stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(int64(t.arr[t.index])))
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:53:03 +00:00
|
|
|
// Iterator is thoroughly tested in VM package, these are smoke tests.
|
|
|
|
func TestIterator(t *testing.T) {
|
|
|
|
ic := &interop.Context{VM: vm.New()}
|
2021-04-29 14:19:30 +00:00
|
|
|
full := []int{4, 8, 15}
|
|
|
|
ic.VM.Estack().PushVal(stackitem.NewInterop(&testIter{index: -1, arr: full}))
|
2020-12-02 09:53:03 +00:00
|
|
|
|
|
|
|
res := ic.VM.Estack().Pop().Item()
|
2021-01-12 09:30:21 +00:00
|
|
|
for i := range full {
|
|
|
|
ic.VM.Estack().PushVal(res)
|
|
|
|
require.NoError(t, Next(ic))
|
|
|
|
require.True(t, ic.VM.Estack().Pop().Bool())
|
2020-12-02 09:53:03 +00:00
|
|
|
|
2021-01-12 09:30:21 +00:00
|
|
|
ic.VM.Estack().PushVal(res)
|
|
|
|
require.NoError(t, Value(ic))
|
2020-12-02 09:53:03 +00:00
|
|
|
|
2021-01-12 09:30:21 +00:00
|
|
|
value := ic.VM.Estack().Pop().Item().Value()
|
|
|
|
require.Equal(t, big.NewInt(int64(full[i])), value)
|
|
|
|
}
|
2020-12-02 09:53:03 +00:00
|
|
|
|
|
|
|
ic.VM.Estack().PushVal(res)
|
2021-01-12 09:30:21 +00:00
|
|
|
require.NoError(t, Next(ic))
|
|
|
|
require.False(t, false, ic.VM.Estack().Pop().Bool())
|
2020-12-02 09:53:03 +00:00
|
|
|
}
|