github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/interop/iterator/interop_test.go (about)

     1  package iterator
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/core/interop"
     8  	"github.com/nspcc-dev/neo-go/pkg/vm"
     9  	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  type testIter struct {
    14  	index int
    15  	arr   []int
    16  }
    17  
    18  func (t *testIter) Next() bool {
    19  	if t.index < len(t.arr) {
    20  		t.index++
    21  	}
    22  	return t.index < len(t.arr)
    23  }
    24  
    25  func (t testIter) Value() stackitem.Item {
    26  	return stackitem.NewBigInteger(big.NewInt(int64(t.arr[t.index])))
    27  }
    28  
    29  // Iterator is thoroughly tested in VM package, these are smoke tests.
    30  func TestIterator(t *testing.T) {
    31  	ic := &interop.Context{VM: vm.New()}
    32  	full := []int{4, 8, 15}
    33  	ic.VM.Estack().PushVal(stackitem.NewInterop(&testIter{index: -1, arr: full}))
    34  
    35  	res := ic.VM.Estack().Pop().Item()
    36  	for i := range full {
    37  		ic.VM.Estack().PushVal(res)
    38  		require.NoError(t, Next(ic))
    39  		require.True(t, ic.VM.Estack().Pop().Bool())
    40  
    41  		ic.VM.Estack().PushVal(res)
    42  		require.NoError(t, Value(ic))
    43  
    44  		value := ic.VM.Estack().Pop().Item().Value()
    45  		require.Equal(t, big.NewInt(int64(full[i])), value)
    46  	}
    47  
    48  	ic.VM.Estack().PushVal(res)
    49  	require.NoError(t, Next(ic))
    50  	require.False(t, false, ic.VM.Estack().Pop().Bool())
    51  }