native/std: add memorySearch method

This commit is contained in:
Evgeniy Stratonikov 2021-04-30 15:33:07 +03:00
parent 978f4dfbc5
commit dadfe2b9ab
4 changed files with 188 additions and 1 deletions

View file

@ -389,3 +389,95 @@ func TestMemoryCompare(t *testing.T) {
func() { s.memoryCompare(ic, []stackitem.Item{s2, s1}) })
})
}
func TestMemorySearch(t *testing.T) {
s := newStd()
ic := &interop.Context{VM: vm.New()}
check := func(t *testing.T, result int64, args ...interface{}) {
items := make([]stackitem.Item, len(args))
for i := range args {
items[i] = stackitem.Make(args[i])
}
var actual stackitem.Item
switch len(items) {
case 2:
actual = s.memorySearch2(ic, items)
case 3:
actual = s.memorySearch3(ic, items)
case 4:
actual = s.memorySearch4(ic, items)
default:
panic("invalid args length")
}
require.Equal(t, big.NewInt(result), actual.Value())
}
t.Run("C# compatibility", func(t *testing.T) {
// These tests are taken from C# node.
check(t, 2, "abc", "c", 0)
check(t, 2, "abc", "c", 1)
check(t, 2, "abc", "c", 2)
check(t, -1, "abc", "c", 3)
check(t, -1, "abc", "d", 0)
check(t, 2, "abc", "c", 0, false)
check(t, 2, "abc", "c", 1, false)
check(t, 2, "abc", "c", 2, false)
check(t, -1, "abc", "c", 3, false)
check(t, -1, "abc", "d", 0, false)
check(t, -1, "abc", "c", 0, true)
check(t, -1, "abc", "c", 1, true)
check(t, -1, "abc", "c", 2, true)
check(t, 2, "abc", "c", 3, true)
check(t, -1, "abc", "d", 0, true)
})
t.Run("boundary indices", func(t *testing.T) {
arg := stackitem.Make("aaa")
require.Panics(t, func() {
s.memorySearch3(ic, []stackitem.Item{arg, arg, stackitem.Make(-1)})
})
require.Panics(t, func() {
s.memorySearch3(ic, []stackitem.Item{arg, arg, stackitem.Make(4)})
})
t.Run("still in capacity", func(t *testing.T) {
require.Panics(t, func() {
arr := stackitem.NewByteArray(make([]byte, 5, 10))
s.memorySearch3(ic, []stackitem.Item{arr, arg, stackitem.Make(7)})
})
require.Panics(t, func() {
arr := stackitem.NewByteArray(make([]byte, 5, 10))
s.memorySearch4(ic, []stackitem.Item{arr, arg,
stackitem.Make(7), stackitem.Make(true)})
})
})
})
t.Run("big arguments", func(t *testing.T) {
s1 := stackitem.Make(strings.Repeat("x", stdMaxInputLength+1))
s2 := stackitem.Make("xxx")
start := stackitem.Make(1)
b := stackitem.Make(true)
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.memorySearch2(ic, []stackitem.Item{s1, s2}) })
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.memorySearch2(ic, []stackitem.Item{s2, s1}) })
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.memorySearch3(ic, []stackitem.Item{s1, s2, start}) })
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.memorySearch3(ic, []stackitem.Item{s2, s1, start}) })
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.memorySearch4(ic, []stackitem.Item{s1, s2, start, b}) })
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.memorySearch4(ic, []stackitem.Item{s2, s1, start, b}) })
})
}